test_server.py - Tests using the web2py server

These tests start the web2py server then submit requests to it. All the fixtures are auto-imported by pytest from conftest.py.

Imports

These are listed in the order prescribed by PEP 8.

Standard library

from textwrap import dedent
import json
from threading import Thread
import datetime
import re
import sys
import time
 

Third-party imports

import pytest
import six
 

Local imports

from .utils import web2py_controller_import
 
 

Debugging notes

Invoke the debugger.

##import pdb; pdb.set_trace()

Put this in web2py code, then use the web-based debugger.

##from gluon.debug import dbg; dbg.set_trace()
 

Tests

Use for easy manual testing of the server, by setting up a user and class automatically. Comment out the line below to enable it.

@pytest.mark.skip(reason="Only needed for manual testing.")
def test_manual(runestone_db_tools, test_user):

Modify this as desired to create courses, users, etc. for manual testing.

    course_1 = runestone_db_tools.create_course()
    test_user("bob", "bob", course_1)
 

Pause in the debugger until manual testing is done.

    import pdb

    pdb.set_trace()


def test_killer(test_assignment, test_client, test_user_1, runestone_db_tools):

This test ensures that we have the routing set up for testing properly. This test will fail if routes.py is set up as follows. routes_onerror = [

System Message: ERROR/3 (/home/docs/checkouts/readthedocs.org/user_builds/runestoneserver/checkouts/v6.2.1/tests/test_server.py, line 60)

Unexpected indentation.

(‘runestone/static/404’, ‘/runestone/static/fail.html’), (‘runestone/500’, ‘/runestone/default/reportabug.html’), ]

System Message: WARNING/2 (/home/docs/checkouts/readthedocs.org/user_builds/runestoneserver/checkouts/v6.2.1/tests/test_server.py, line 63)

Block quote ends without a blank line; unexpected unindent.

for testing purposes we don’t want web2py to capture 500 errors.

    with pytest.raises(Exception) as excinfo:
        test_client.post("admin/killer")
        assert test_client.text == ""
    print(excinfo.value)
    assert "ticket" in str(excinfo.value) or "INTERNAL" in str(excinfo.value)
 
 

Validate the HTML produced by various web2py pages. NOTE – this is the start of a really really long decorator for test_1

@pytest.mark.parametrize(
    "url, requires_login, expected_string, expected_errors",
    [

Admin

FIXME: Flashed messages don’t seem to work. (‘admin/index’, False, ‘You must be registered for a course to access this page’, 1), (‘admin/index’, True, ‘You must be an instructor to access this page’, 1),

        ("admin/doc", True, "Runestone Help and Documentation", 1),

Assignments

        ("assignments/chooseAssignment", True, "Assignments", 1),
        ("assignments/doAssignment", True, "Bad Assignment ID", 1),
        (
            "assignments/practice",
            True,
            "Practice tool is not set up for this course yet.",
            1,
        ),
        ("assignments/practiceNotStartedYet", True, "test_course_1", 1),