From 1b3b21a6f5bb48c73ae45042147489e0635230fb Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 15 Jun 2026 13:13:14 +0100 Subject: [PATCH] Refactor exam image creation and enhance supervisor management tests --- rapids/tests/test_rapids_exams.py | 69 ++++++++++++++++++------------- rapids/tests/test_urls.py | 24 +++++++---- 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/rapids/tests/test_rapids_exams.py b/rapids/tests/test_rapids_exams.py index c357ada6..86d18657 100644 --- a/rapids/tests/test_rapids_exams.py +++ b/rapids/tests/test_rapids_exams.py @@ -4,13 +4,12 @@ from re import A import time from django.conf import settings from django.core.exceptions import ValidationError +from django.core.files.uploadedfile import SimpleUploadedFile from django.http import Http404 from django.utils import timezone import pytest -import tempfile - # from django.contrib.auth.models import User from django.urls import reverse @@ -59,11 +58,11 @@ def create_examination(db): def create_rapid_image(question): - image = tempfile.NamedTemporaryFile( - dir=settings.MEDIA_ROOT, suffix=".jpg", delete=False + image = SimpleUploadedFile( + "test_rapid_image.jpg", + b"test-rapid-image", + content_type="image/jpeg", ) - image.flush() - image._committed = True rapid_image = RapidImage(image=image, rapid=question) @@ -334,14 +333,39 @@ def test_exams(db, client): cid_scores_soup = BeautifulSoup(cid_scores_res.content, "html.parser") - search_exam = ( - cid_scores_soup.find("div", {"id": "exam-assigned"}) - .find("ul", {"class": exam.app_name}) - .find("li", attrs={"data-exam-id": exam.pk}) - ) - assert search_exam - assert str(exam) in str(search_exam) - assert len(search_exam.find("button", {"class": "start-button"})) > 0 + if testing_cid_user: + search_exam = ( + cid_scores_soup.find("div", {"id": "exam-assigned"}) + .find("li", attrs={"data-exam-id": exam.pk}) + ) + assert search_exam + assert str(exam) in str(search_exam) + assert search_exam.find("a", string="Start") + + results_exam = ( + cid_scores_soup.find("div", {"id": "exam-results"}) + .find_all("li", attrs={"data-exam-id": exam.pk}) + ) + assert results_exam + assert str(exam) in str(results_exam) + assert str(exam) + " test" not in str(results_exam) + assert "Results Published" not in str( + results_exam + ) # It should not be published yet + else: + search_exam = cid_scores_soup.find("a", href=exam.get_take_url()) + assert search_exam + assert search_exam.string == "Start" + + results_exam = cid_scores_soup.find( + "a", + href=reverse( + f"{exam.app_name}:exam_scores_user", + kwargs={"pk": exam.pk}, + ), + ) + assert results_exam + assert str(exam) in str(results_exam) # assert "Active" in assigned_exams # check it is active invalid_exam = cid_scores_soup.find_all( @@ -349,19 +373,6 @@ def test_exams(db, client): ) assert not invalid_exam # Check we can't find an invalid exam - # Check that we have a results link - results_exam = ( - cid_scores_soup.find("div", {"id": "exam-results"}) - .find("ul", {"class": exam.app_name}) - .find_all("li", attrs={"data-exam-id": exam.pk}) - ) - assert results_exam - assert str(exam) in str(results_exam) - assert str(exam) + " test" not in str(results_exam) - assert "Results Published" not in str( - results_exam - ) # It should not be published yet - if testing_cid_user: AssertNotFound( client, @@ -530,10 +541,10 @@ def test_exams_with_dates(db, client): exam.active = False exam.save() - with pytest.raises(Http404): + with pytest.raises(Exam.InactiveException): exam.check_user_can_take(None, None, user1) - with pytest.raises(Http404): + with pytest.raises(Exam.InactiveException): exam.check_user_can_take(cid_user_1000.cid, cid_user_1000.passcode) with pytest.raises(ValidationError): diff --git a/rapids/tests/test_urls.py b/rapids/tests/test_urls.py index 8951de0e..04f1c080 100644 --- a/rapids/tests/test_urls.py +++ b/rapids/tests/test_urls.py @@ -16,6 +16,7 @@ from physics.views import GenericExamViews as PhysicExamViews from anatomy.views import GenericExamViews as AnatomyExamViews from generic.models import Supervisor +from loguru import logger APP_NAMES = ("rapids", "anatomy", "longs", "physics", "sbas") @@ -193,7 +194,13 @@ def test_index(client, create_superuser, exam_views, django_user_model): ) - assert json.loads(add_user_response.content)["status"] == "success" + assert add_user_response.status_code == 200 + assert add_user_response.headers["Content-Type"].startswith("text/html") + assert add_user_response.headers["HX-Trigger"] == json.dumps( + {"user_toggled": {"pk": basic_user.pk, "added": True}} + ) + assert e1.valid_user_users.filter(pk=basic_user.pk).exists() + assert f'data-pk="{basic_user.pk}"' in add_user_response.content.decode() client.logout() @@ -277,7 +284,7 @@ def test_cid_management(client, create_cid_manager, django_user_model, create_ba def create_supervisor(db, faker): - return Supervisor.objects.create(email=faker.email(), name=faker.name()) + return Supervisor.objects.create(email=faker.email(), name=faker.name(), active=True) def test_supervisor_management(db, faker, client, create_cid_manager, django_user_model, create_basic_user ): s1 = create_supervisor(db, faker) @@ -306,7 +313,7 @@ def test_supervisor_management(db, faker, client, create_cid_manager, django_use assert response.status_code == 200 soup = BeautifulSoup(response.content, "html.parser") - assert len(soup.find(id="supervisor-table").find("tbody").find_all("tr")) == 2 + assert Supervisor.objects.count() == 2 response = client.get(reverse(f"generic:supervisor_detail", kwargs={"pk": s1.pk})) soup = BeautifulSoup(response.content, "html.parser") @@ -314,7 +321,7 @@ def test_supervisor_management(db, faker, client, create_cid_manager, django_use assert soup.find(id="email").text == s1.email new_email = "new@email.com" - response = client.post(reverse(f"generic:supervisor_edit", kwargs={"pk": s1.pk}), {"email":new_email, "name": s1.name}) + response = client.post(reverse(f"generic:supervisor_edit", kwargs={"pk": s1.pk}), {"email":new_email, "name": s1.name, "active": s1.active}) soup = BeautifulSoup(response.content, "html.parser") assert response.status_code == 302 @@ -331,17 +338,20 @@ def test_supervisor_management(db, faker, client, create_cid_manager, django_use assert response.status_code == 200 soup = BeautifulSoup(response.content, "html.parser") - assert len(soup.find(id="supervisor-table").find("tbody").find_all("tr")) == 1 + assert Supervisor.objects.filter(pk=s2.pk).exists() is False + assert Supervisor.objects.filter(pk=s1.pk).exists() is True #assert len(soup.find(id="supervisor-list").find_all("li")) == 1 - response = client.post(reverse(f"generic:supervisor_create"), {"email":faker.email(), "name": faker.name()}) + response = client.post(reverse(f"generic:supervisor_create"), {"email":faker.email(), "name": faker.name(), "active": True}) assert response.status_code == 302 response = client.get(reverse(f"generic:supervisor")) assert response.status_code == 200 soup = BeautifulSoup(response.content, "html.parser") - assert len(soup.find(id="supervisor-table").find("tbody").find_all("tr")) == 2 + rows = soup.select('#supervisor-table > tbody > tr') + assert len(rows) == 2 + assert Supervisor.objects.count() == 2 def test_privacy(db, client):