feat: add import exam answers functionality with UI and backend handling

This commit is contained in:
Ross
2026-07-05 22:37:55 +01:00
parent 2744758aa8
commit 89963d6257
6 changed files with 314 additions and 7 deletions
@@ -170,3 +170,65 @@ def test_candidate_submit_success(anonymous_client, exam_data):
data = response.json()
assert data["success"] is True
assert data["question_count"] == 1
@pytest.fixture
def cid_manager_client(db):
from django.contrib.auth.models import Group
user = User.objects.create_user(username="manager1", password="password", email="manager1@test.com")
g, _ = Group.objects.get_or_create(name="cid_user_manager")
user.groups.add(g)
client = Client()
client.force_login(user)
return client
@pytest.mark.django_db
def test_import_exam_answers_unauthorized(anonymous_client):
# Anonymous or non-manager users should be redirected or blocked
url = reverse("generic:import_exam_answers")
response = anonymous_client.get(url)
assert response.status_code == 403
@pytest.mark.django_db
def test_import_exam_answers_get(cid_manager_client):
url = reverse("generic:import_exam_answers")
response = cid_manager_client.get(url)
assert response.status_code == 200
assert b"Import Exam Answers" in response.content
@pytest.mark.django_db
def test_import_exam_answers_post_success(cid_manager_client, exam_data, logged_in_user):
exam, question = exam_data
from io import BytesIO
from anatomy.models import UserAnswer as AnatomyUserAnswer
import_data = {
"eid": f"anatomy/{exam.pk}",
"cid": f"u-{logged_in_user.pk}",
"start_time": timezone.now().timestamp(),
"answers": [
{
"qid": question.pk,
"ans": "Imported Test Answer",
"qidn": "1",
}
]
}
file_content = json.dumps(import_data).encode("utf-8")
answers_file = BytesIO(file_content)
answers_file.name = "backup.json"
url = reverse("generic:import_exam_answers")
response = cid_manager_client.post(url, {"answers_file": answers_file}, format="multipart")
assert response.status_code == 302 # redirects to manage_cids on success
# Verify answers were saved
answers = AnatomyUserAnswer.objects.filter(exam=exam, user=logged_in_user)
assert answers.count() == 1
assert answers.first().answer == "Imported Test Answer"