Refactor exam image creation and enhance supervisor management tests
This commit is contained in:
@@ -4,13 +4,12 @@ from re import A
|
|||||||
import time
|
import time
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import tempfile
|
|
||||||
|
|
||||||
# from django.contrib.auth.models import User
|
# from django.contrib.auth.models import User
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
@@ -59,11 +58,11 @@ def create_examination(db):
|
|||||||
|
|
||||||
|
|
||||||
def create_rapid_image(question):
|
def create_rapid_image(question):
|
||||||
image = tempfile.NamedTemporaryFile(
|
image = SimpleUploadedFile(
|
||||||
dir=settings.MEDIA_ROOT, suffix=".jpg", delete=False
|
"test_rapid_image.jpg",
|
||||||
|
b"test-rapid-image",
|
||||||
|
content_type="image/jpeg",
|
||||||
)
|
)
|
||||||
image.flush()
|
|
||||||
image._committed = True
|
|
||||||
|
|
||||||
rapid_image = RapidImage(image=image, rapid=question)
|
rapid_image = RapidImage(image=image, rapid=question)
|
||||||
|
|
||||||
@@ -334,25 +333,17 @@ def test_exams(db, client):
|
|||||||
|
|
||||||
cid_scores_soup = BeautifulSoup(cid_scores_res.content, "html.parser")
|
cid_scores_soup = BeautifulSoup(cid_scores_res.content, "html.parser")
|
||||||
|
|
||||||
|
if testing_cid_user:
|
||||||
search_exam = (
|
search_exam = (
|
||||||
cid_scores_soup.find("div", {"id": "exam-assigned"})
|
cid_scores_soup.find("div", {"id": "exam-assigned"})
|
||||||
.find("ul", {"class": exam.app_name})
|
|
||||||
.find("li", attrs={"data-exam-id": exam.pk})
|
.find("li", attrs={"data-exam-id": exam.pk})
|
||||||
)
|
)
|
||||||
assert search_exam
|
assert search_exam
|
||||||
assert str(exam) in str(search_exam)
|
assert str(exam) in str(search_exam)
|
||||||
assert len(search_exam.find("button", {"class": "start-button"})) > 0
|
assert search_exam.find("a", string="Start")
|
||||||
# assert "Active" in assigned_exams # check it is active
|
|
||||||
|
|
||||||
invalid_exam = cid_scores_soup.find_all(
|
|
||||||
"li", attrs={"data-exam-id": exam.pk + 5}
|
|
||||||
)
|
|
||||||
assert not invalid_exam # Check we can't find an invalid exam
|
|
||||||
|
|
||||||
# Check that we have a results link
|
|
||||||
results_exam = (
|
results_exam = (
|
||||||
cid_scores_soup.find("div", {"id": "exam-results"})
|
cid_scores_soup.find("div", {"id": "exam-results"})
|
||||||
.find("ul", {"class": exam.app_name})
|
|
||||||
.find_all("li", attrs={"data-exam-id": exam.pk})
|
.find_all("li", attrs={"data-exam-id": exam.pk})
|
||||||
)
|
)
|
||||||
assert results_exam
|
assert results_exam
|
||||||
@@ -361,6 +352,26 @@ def test_exams(db, client):
|
|||||||
assert "Results Published" not in str(
|
assert "Results Published" not in str(
|
||||||
results_exam
|
results_exam
|
||||||
) # It should not be published yet
|
) # 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(
|
||||||
|
"li", attrs={"data-exam-id": exam.pk + 5}
|
||||||
|
)
|
||||||
|
assert not invalid_exam # Check we can't find an invalid exam
|
||||||
|
|
||||||
if testing_cid_user:
|
if testing_cid_user:
|
||||||
AssertNotFound(
|
AssertNotFound(
|
||||||
@@ -530,10 +541,10 @@ def test_exams_with_dates(db, client):
|
|||||||
exam.active = False
|
exam.active = False
|
||||||
exam.save()
|
exam.save()
|
||||||
|
|
||||||
with pytest.raises(Http404):
|
with pytest.raises(Exam.InactiveException):
|
||||||
exam.check_user_can_take(None, None, user1)
|
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)
|
exam.check_user_can_take(cid_user_1000.cid, cid_user_1000.passcode)
|
||||||
|
|
||||||
with pytest.raises(ValidationError):
|
with pytest.raises(ValidationError):
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from physics.views import GenericExamViews as PhysicExamViews
|
|||||||
from anatomy.views import GenericExamViews as AnatomyExamViews
|
from anatomy.views import GenericExamViews as AnatomyExamViews
|
||||||
|
|
||||||
from generic.models import Supervisor
|
from generic.models import Supervisor
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
APP_NAMES = ("rapids", "anatomy", "longs", "physics", "sbas")
|
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()
|
client.logout()
|
||||||
|
|
||||||
@@ -277,7 +284,7 @@ def test_cid_management(client, create_cid_manager, django_user_model, create_ba
|
|||||||
|
|
||||||
|
|
||||||
def create_supervisor(db, faker):
|
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 ):
|
def test_supervisor_management(db, faker, client, create_cid_manager, django_user_model, create_basic_user ):
|
||||||
s1 = create_supervisor(db, faker)
|
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
|
assert response.status_code == 200
|
||||||
|
|
||||||
soup = BeautifulSoup(response.content, "html.parser")
|
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}))
|
response = client.get(reverse(f"generic:supervisor_detail", kwargs={"pk": s1.pk}))
|
||||||
soup = BeautifulSoup(response.content, "html.parser")
|
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
|
assert soup.find(id="email").text == s1.email
|
||||||
|
|
||||||
new_email = "new@email.com"
|
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")
|
soup = BeautifulSoup(response.content, "html.parser")
|
||||||
assert response.status_code == 302
|
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
|
assert response.status_code == 200
|
||||||
|
|
||||||
soup = BeautifulSoup(response.content, "html.parser")
|
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
|
#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
|
assert response.status_code == 302
|
||||||
|
|
||||||
response = client.get(reverse(f"generic:supervisor"))
|
response = client.get(reverse(f"generic:supervisor"))
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
soup = BeautifulSoup(response.content, "html.parser")
|
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):
|
def test_privacy(db, client):
|
||||||
|
|||||||
Reference in New Issue
Block a user