.
This commit is contained in:
+109
-11
@@ -1,24 +1,122 @@
|
||||
import json
|
||||
import pytest
|
||||
#from django.contrib.auth.models import User
|
||||
|
||||
# from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
|
||||
from rich.pretty import pprint
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from rapids.views import GenericExamViews as RapidExamViews
|
||||
from sbas.views import GenericExamViews as SbaExamViews
|
||||
from longs.views import GenericExamViews as LongExamViews
|
||||
from physics.views import GenericExamViews as PhysicExamViews
|
||||
from anatomy.views import GenericExamViews as AnatomyExamViews
|
||||
|
||||
APP_NAMES = ("rapids", "anatomy", "longs", "physics", "sbas")
|
||||
|
||||
JSON_APPS = ("rapids", "anatomy", "longs")
|
||||
|
||||
EXAM_VIEWS = (
|
||||
RapidExamViews,
|
||||
SbaExamViews,
|
||||
LongExamViews,
|
||||
PhysicExamViews,
|
||||
AnatomyExamViews,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_superuser(db, django_user_model):
|
||||
return django_user_model.objects.create_superuser("admin", "ross@xkjq.uk", "adminpassword")
|
||||
return django_user_model.objects.create_superuser(
|
||||
"admin", "ross@xkjq.uk", "adminpassword"
|
||||
)
|
||||
|
||||
#@pytest.fixture
|
||||
#def create_exam(db):
|
||||
|
||||
# @pytest.fixture
|
||||
# def create_exam(db):
|
||||
# return Exam.objects.create(name="test exam", exam_mode=True)
|
||||
|
||||
# Very basic tests, make sure we can hit key urls without error
|
||||
|
||||
@pytest.mark.parametrize("app_name", APP_NAMES)
|
||||
def test_index(client, create_superuser, app_name):
|
||||
|
||||
@pytest.mark.parametrize("exam_views", EXAM_VIEWS)
|
||||
def test_index(client, create_superuser, exam_views, django_user_model):
|
||||
|
||||
basic_user = django_user_model.objects.create_user(
|
||||
"user1", "user@user.net", "pass2"
|
||||
)
|
||||
|
||||
e1 = exam_views.Exam.objects.create(name="test exam", exam_mode=True)
|
||||
e = exam_views.Exam.objects.create(name="test exam 2", exam_mode=True)
|
||||
e.author.add(basic_user)
|
||||
|
||||
client.login(username="admin", password="adminpassword")
|
||||
response = client.get(reverse(f'{app_name}:index'))
|
||||
print(reverse(f'{app_name}:index'))
|
||||
print(response.status_code)
|
||||
print(response.content)
|
||||
assert response.status_code == 200
|
||||
response = client.get(reverse(f"{exam_views.app_name}:index"))
|
||||
assert response.status_code == 200
|
||||
|
||||
assert (
|
||||
BeautifulSoup(response.content, "html.parser")
|
||||
.find("h4", {"class": "exam-number-title"})
|
||||
.getText()
|
||||
== "2 exams found."
|
||||
)
|
||||
|
||||
if exam_views.app_name in JSON_APPS:
|
||||
response = client.get(reverse(f"{exam_views.app_name}:active_exams"))
|
||||
|
||||
json_response = json.loads(response.content)["exams"]
|
||||
|
||||
assert len(json_response) == 2
|
||||
|
||||
for json_exam in json_response:
|
||||
eid_app_type, eid = json_exam["eid"].split("/")
|
||||
# hack for long / longs mismathc
|
||||
assert (
|
||||
eid_app_type == exam_views.app_name
|
||||
or eid_app_type == exam_views.app_name[:-1]
|
||||
)
|
||||
|
||||
assert exam_views.Exam.objects.get(pk=int(eid)).name == json_exam["name"]
|
||||
|
||||
add_user_response = client.post(
|
||||
reverse(f"{exam_views.app_name}:exam_json_edit", args=(e1.pk,)),
|
||||
{"edit_user_user": basic_user.pk, "add": "true"},
|
||||
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
|
||||
)
|
||||
|
||||
print(f"{e1.valid_user_users.all()=}")
|
||||
|
||||
assert json.loads(add_user_response.content)["status"] == "success"
|
||||
|
||||
client.logout()
|
||||
|
||||
client.force_login(basic_user)
|
||||
|
||||
response = client.get(reverse(f"{exam_views.app_name}:index"))
|
||||
assert response.status_code == 200
|
||||
assert (
|
||||
BeautifulSoup(response.content, "html.parser")
|
||||
.find("h4", {"class": "exam-number-title"})
|
||||
.getText()
|
||||
== "1 exams found."
|
||||
)
|
||||
|
||||
if exam_views.app_name in JSON_APPS:
|
||||
response = client.get(reverse(f"{exam_views.app_name}:active_exams"))
|
||||
|
||||
json_response = json.loads(response.content)["exams"]
|
||||
|
||||
assert len(json_response) == 1
|
||||
|
||||
e1.active = True
|
||||
e1.save()
|
||||
|
||||
response = client.get(reverse(f"{exam_views.app_name}:active_exams"))
|
||||
|
||||
json_response = json.loads(response.content)["exams"]
|
||||
|
||||
print(json_response)
|
||||
|
||||
assert len(json_response) == 2
|
||||
|
||||
Reference in New Issue
Block a user