From 5d2ae8499038c0d491163e9fbbbd2da5199de25f Mon Sep 17 00:00:00 2001 From: Ross Date: Thu, 4 Aug 2022 18:27:23 +0100 Subject: [PATCH] . --- anatomy/tests/test_exams.py | 89 +++++++++++++++---- generic/templates/generic/cid_group_view.html | 2 +- .../generic/cid_group_view_detail.html | 28 ++++++ generic/urls.py | 1 + generic/views.py | 11 +++ rad/views.py | 20 +++-- 6 files changed, 124 insertions(+), 27 deletions(-) create mode 100755 generic/templates/generic/cid_group_view_detail.html diff --git a/anatomy/tests/test_exams.py b/anatomy/tests/test_exams.py index 466c2378..5da38f73 100644 --- a/anatomy/tests/test_exams.py +++ b/anatomy/tests/test_exams.py @@ -21,9 +21,10 @@ from generic.models import CidUser, CidUserGroup APP_NAME = "anatomy" + def create_cid_user_and_groups(db): group1 = CidUserGroup.objects.create(name="Group1") - group2= CidUserGroup.objects.create(name="Group2") + group2 = CidUserGroup.objects.create(name="Group2") cid_user_1000 = CidUser.objects.create(cid=1000, passcode="ABCD", group=group1) cid_user_1001 = CidUser.objects.create(cid=1001, passcode="EFGH", group=group1) @@ -33,10 +34,10 @@ def create_cid_user_and_groups(db): assert cid_user_1001.cid == 1001 - - def create_exam(db): - exam: Exam = AnatomyExamViews.Exam.objects.create(name="Cid Exam", exam_mode=True, active=True) + exam: Exam = AnatomyExamViews.Exam.objects.create( + name="Cid Exam", exam_mode=True, active=True + ) group1: CidUserGroup = CidUserGroup.objects.get(name="Group1") @@ -50,12 +51,14 @@ def create_exam(db): return exam + def create_structures(db): structure = Structure.objects.create(structure="test structure") structure.save() structure = Structure.objects.create(structure="test structure 2") structure.save() + def create_question_types(db): question_type = QuestionType.objects.create(text="test question type") question_type.save() @@ -66,17 +69,20 @@ def create_question_types(db): def create_question(exam): # Just assign random stuff - image = tempfile.NamedTemporaryFile(dir=settings.MEDIA_ROOT, suffix=".jpg", delete=False) + image = tempfile.NamedTemporaryFile( + dir=settings.MEDIA_ROOT, suffix=".jpg", delete=False + ) - print(image.name) - print(image.file) - print(os.listdir(settings.MEDIA_ROOT)) - - question: Question = Question.objects.create(question_type=QuestionType.objects.first(), structure=Structure.objects.first(), image=image.name) + question: Question = Question.objects.create( + question_type=QuestionType.objects.first(), + structure=Structure.objects.first(), + image=image.name, + ) question.exams.set([exam]) return question + def test_exams(db, client): create_cid_user_and_groups(db) exam = create_exam(db) @@ -96,22 +102,33 @@ def test_exams(db, client): for cid_user in [cid_user_1001]: # Test active exams sections works as intended - active_exams_cid = client.get(reverse(f"active_exams_cid", kwargs={"cid":cid_user.cid, "passcode":cid_user.passcode})) + active_exams_cid = client.get( + reverse( + f"active_exams_cid", + kwargs={"cid": cid_user.cid, "passcode": cid_user.passcode}, + ) + ) assert active_exams_cid.status_code == 200 assert len(json.loads(active_exams_cid.content)["exams"]) == 1 - active_exams_cid = client.get(reverse(f"active_exams_cid", kwargs={"cid":1001, "passcode":"EFGH"})) + active_exams_cid = client.get( + reverse(f"active_exams_cid", kwargs={"cid": 1001, "passcode": "EFGH"}) + ) assert active_exams_cid.status_code == 200 assert len(json.loads(active_exams_cid.content)["exams"]) == 1 - no_active_exams_cid = client.get(reverse(f"active_exams_cid", kwargs={"cid":2001, "passcode":"EGFH"})) + no_active_exams_cid = client.get( + reverse(f"active_exams_cid", kwargs={"cid": 2001, "passcode": "EGFH"}) + ) assert no_active_exams_cid.status_code == 401 assert json.loads(no_active_exams_cid.content)["status"] == "invalid" - invalid_passcode = client.get(reverse(f"active_exams_cid", kwargs={"cid":1001, "passcode":"ABCD"})) + invalid_passcode = client.get( + reverse(f"active_exams_cid", kwargs={"cid": 1001, "passcode": "ABCD"}) + ) assert invalid_passcode.status_code == 401 assert json.loads(invalid_passcode.content)["status"] == "invalid" @@ -124,12 +141,48 @@ def test_exams(db, client): res = client.get(exam_metadata["url"]) assert res.status_code == 200 exam_json = json.loads(res.content) - print("---", exam_json["questions"].values()) - - - assert question1.get_question_json(answers=False) in exam_json["questions"].values() + assert ( + question1.get_question_json(answers=False) + in exam_json["questions"].values() + ) assert len(exam_json["questions"]) == 3 + exam.active = False + exam.save() + res = client.get(exam_metadata["url"]) + assert res.status_code == 404 + + exam.active = True + exam.save() + + post_json = { + "eid": f"anatomy/{exam.pk}", + "cid": cid_user_1001.cid, + "start_time": "1659618141.731", + "answers": [[]] + } + + # Test submission + # Start with 0 answers + res = client.post(reverse("global_exam_answers_submit"), data=post_json) + + print(res.status_code) + json_res = json.loads(res.content) + + assert json_res["success"] == True + assert json_res["question_count"] == 0 + + post_json = { + "eid": f"anatomy/{exam.pk+10}", + "cid": cid_user_1001.cid, + "start_time": "1659618141.731", + "answers": [[]] + } + res = client.post(reverse("global_exam_answers_submit"), data=post_json) + json_res = json.loads(res.content) + assert json_res["success"] == False + + # Test that we ca diff --git a/generic/templates/generic/cid_group_view.html b/generic/templates/generic/cid_group_view.html index d6fc119a..60d5b7ea 100755 --- a/generic/templates/generic/cid_group_view.html +++ b/generic/templates/generic/cid_group_view.html @@ -6,7 +6,7 @@ Create new group

{% for group in groups %} -

{{group.name}} +

{{group.name}}