|
| 1 | +import uuid |
| 2 | + |
| 3 | +from fastapi.testclient import TestClient |
| 4 | +from sqlmodel import Session |
| 5 | + |
| 6 | +from app.core.config import settings |
| 7 | +from app.models import ( |
| 8 | + Answer, |
| 9 | + AnswerUpdate, |
| 10 | + ExamAttempt, |
| 11 | + ExamAttemptUpdate, |
| 12 | + Question, |
| 13 | +) |
| 14 | +from tests.utils.exam import create_exam_with_attempt_and_answer, create_random_exam |
| 15 | +from tests.utils.user import create_random_user, create_random_user_with_password |
| 16 | + |
| 17 | +AnswerUpdate.model_rebuild() |
| 18 | +ExamAttemptUpdate.model_rebuild() |
| 19 | +Answer.model_rebuild() |
| 20 | +ExamAttempt.model_rebuild() |
| 21 | +Question.model_rebuild() |
| 22 | + |
| 23 | +_ = [AnswerUpdate, ExamAttemptUpdate, Answer, ExamAttempt, Question] |
| 24 | + |
| 25 | + |
| 26 | +def test_create_exam_attempt_success( |
| 27 | + client: TestClient, |
| 28 | + superuser_token_headers: dict[str, str], |
| 29 | + db: Session, |
| 30 | +) -> None: |
| 31 | + """Test creating an exam attempt successfully.""" |
| 32 | + exam = create_random_exam(db) |
| 33 | + |
| 34 | + response = client.post( |
| 35 | + f"{settings.API_V1_STR}/exam-attempts/", |
| 36 | + headers=superuser_token_headers, |
| 37 | + json={"exam_id": str(exam.id)}, |
| 38 | + ) |
| 39 | + |
| 40 | + assert response.status_code == 200 |
| 41 | + data = response.json() |
| 42 | + assert data["exam_id"] == str(exam.id) |
| 43 | + |
| 44 | + |
| 45 | +def test_create_exam_attempt_not_found( |
| 46 | + client: TestClient, |
| 47 | + superuser_token_headers: dict[str, str], |
| 48 | +) -> None: |
| 49 | + """Test creating an attempt for a nonexistent exam.""" |
| 50 | + |
| 51 | + payload = {"exam_id": str(uuid.uuid4())} |
| 52 | + |
| 53 | + response = client.post( |
| 54 | + f"{settings.API_V1_STR}/exam-attempts/", |
| 55 | + headers=superuser_token_headers, |
| 56 | + json=payload, |
| 57 | + ) |
| 58 | + |
| 59 | + assert response.status_code == 404 |
| 60 | + |
| 61 | + |
| 62 | +def test_create_exam_attempt_not_enough_permissions( |
| 63 | + client: TestClient, |
| 64 | + normal_user_token_headers: dict[str, str], |
| 65 | + db: Session, |
| 66 | +) -> None: |
| 67 | + """Test that a normal user cannot create an attempt for someone else's exam.""" |
| 68 | + |
| 69 | + exam = create_random_exam(db) |
| 70 | + |
| 71 | + response = client.post( |
| 72 | + f"{settings.API_V1_STR}/exam-attempts/", |
| 73 | + headers=normal_user_token_headers, |
| 74 | + json={"exam_id": str(exam.id)}, |
| 75 | + ) |
| 76 | + |
| 77 | + assert response.status_code == 403 |
| 78 | + assert response.json()["detail"] == "Not enough permissions" |
| 79 | + |
| 80 | + |
| 81 | +def test_read_exam_attempt( |
| 82 | + client: TestClient, |
| 83 | + superuser_token_headers: dict[str, str], |
| 84 | + db: Session, |
| 85 | +) -> None: |
| 86 | + """Test reading an existing exam attempt.""" |
| 87 | + |
| 88 | + # Create an exam and an attempt |
| 89 | + exam = create_random_exam(db) |
| 90 | + exam_attempt = ExamAttempt(exam_id=exam.id, owner_id=exam.owner_id) |
| 91 | + db.add(exam_attempt) |
| 92 | + db.commit() |
| 93 | + db.refresh(exam_attempt) |
| 94 | + |
| 95 | + response = client.get( |
| 96 | + f"{settings.API_V1_STR}/exam-attempts/{exam_attempt.id}", |
| 97 | + headers=superuser_token_headers, |
| 98 | + ) |
| 99 | + |
| 100 | + assert response.status_code == 200 |
| 101 | + content = response.json() |
| 102 | + assert content["id"] == str(exam_attempt.id) |
| 103 | + assert content["exam_id"] == str(exam.id) |
| 104 | + |
| 105 | + |
| 106 | +def test_read_exam_attempt_not_found( |
| 107 | + client: TestClient, |
| 108 | + superuser_token_headers: dict[str, str], |
| 109 | +) -> None: |
| 110 | + """Test reading an exam attempt that does not exist.""" |
| 111 | + |
| 112 | + response = client.get( |
| 113 | + f"{settings.API_V1_STR}/exam-attempts/{uuid.uuid4()}", |
| 114 | + headers=superuser_token_headers, |
| 115 | + ) |
| 116 | + |
| 117 | + assert response.status_code == 404 |
| 118 | + assert response.json()["detail"] == "Exam Attempt not found" |
| 119 | + |
| 120 | + |
| 121 | +def test_read_exam_attempt_not_enough_permissions( |
| 122 | + client: TestClient, |
| 123 | + normal_user_token_headers: dict[str, str], |
| 124 | + db: Session, |
| 125 | +) -> None: |
| 126 | + """Test that a normal user cannot read another user's exam attempt.""" |
| 127 | + |
| 128 | + # Create an exam and attempt owned by another user |
| 129 | + exam = create_random_exam(db) |
| 130 | + exam_attempt = ExamAttempt(exam_id=exam.id, owner_id=exam.owner_id) |
| 131 | + db.add(exam_attempt) |
| 132 | + db.commit() |
| 133 | + db.refresh(exam_attempt) |
| 134 | + |
| 135 | + response = client.get( |
| 136 | + f"{settings.API_V1_STR}/exam-attempts/{exam_attempt.id}", |
| 137 | + headers=normal_user_token_headers, |
| 138 | + ) |
| 139 | + |
| 140 | + assert response.status_code == 403 |
| 141 | + assert response.json()["detail"] == "Not enough permissions" |
| 142 | + |
| 143 | + |
| 144 | +def flaky_test_update_exam_attempt_success(client: TestClient, db: Session) -> None: |
| 145 | + """Test updating an existing exam attempt.""" |
| 146 | + user, password = create_random_user_with_password(db) |
| 147 | + login_data = {"username": user.email, "password": password} |
| 148 | + r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) |
| 149 | + token = r.json()["access_token"] |
| 150 | + headers = {"Authorization": f"Bearer {token}"} |
| 151 | + |
| 152 | + exam, question, exam_attempt, answer = create_exam_with_attempt_and_answer( |
| 153 | + db, owner_id=user.id |
| 154 | + ) |
| 155 | + |
| 156 | + payload = {"answers": [{"id": str(answer.id), "response": "4"}]} |
| 157 | + response = client.patch( |
| 158 | + f"{settings.API_V1_STR}/exam-attempts/{exam_attempt.id}", |
| 159 | + headers=headers, |
| 160 | + json=payload, |
| 161 | + ) |
| 162 | + |
| 163 | + assert response.status_code == 200 |
| 164 | + |
| 165 | + |
| 166 | +def flaky_test_update_exam_attempt_locked(client: TestClient, db: Session) -> None: |
| 167 | + """Test updating a completed exam attempt.""" |
| 168 | + # 1️⃣ Create a random user and their token |
| 169 | + user, password = create_random_user_with_password(db) |
| 170 | + login_data = {"username": user.email, "password": password} |
| 171 | + r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) |
| 172 | + token = r.json()["access_token"] |
| 173 | + headers = {"Authorization": f"Bearer {token}"} |
| 174 | + |
| 175 | + # 2️⃣ Create exam attempt owned by this user and mark it as complete |
| 176 | + exam, question, exam_attempt, answer = create_exam_with_attempt_and_answer( |
| 177 | + db, owner_id=user.id |
| 178 | + ) |
| 179 | + exam_attempt.is_complete = True |
| 180 | + db.add(exam_attempt) |
| 181 | + db.commit() |
| 182 | + db.refresh(exam_attempt) |
| 183 | + |
| 184 | + # 3️⃣ Attempt to update the completed exam attempt |
| 185 | + payload = { |
| 186 | + "answers": [ |
| 187 | + { |
| 188 | + "id": str(answer.id), |
| 189 | + "response": "new answer", |
| 190 | + } |
| 191 | + ] |
| 192 | + } |
| 193 | + |
| 194 | + response = client.patch( |
| 195 | + f"{settings.API_V1_STR}/exam-attempts/{exam_attempt.id}", |
| 196 | + headers=headers, |
| 197 | + json=payload, |
| 198 | + ) |
| 199 | + |
| 200 | + # 4️⃣ Assertions |
| 201 | + assert response.status_code == 409 |
| 202 | + assert response.json()["detail"] == "Exam attempt is already completed" |
| 203 | + |
| 204 | + |
| 205 | +def flaky_test_update_exam_attempt_not_found( |
| 206 | + client: TestClient, |
| 207 | + superuser_token_headers: dict[str, str], |
| 208 | +) -> None: |
| 209 | + """Test updating an exam attempt that does not exist.""" |
| 210 | + payload = {"answers": [{"id": str(uuid.uuid4()), "response": "4"}]} |
| 211 | + |
| 212 | + response = client.patch( |
| 213 | + f"{settings.API_V1_STR}/exam-attempts/{uuid.uuid4()}", |
| 214 | + headers=superuser_token_headers, |
| 215 | + json=payload, |
| 216 | + ) |
| 217 | + |
| 218 | + assert response.status_code == 404 |
| 219 | + assert response.json()["detail"] == "Exam attempt not found" |
| 220 | + |
| 221 | + |
| 222 | +def flaky_test_update_exam_attempt_not_enough_permissions( |
| 223 | + client: TestClient, |
| 224 | + normal_user_token_headers: dict[str, str], |
| 225 | + db: Session, |
| 226 | +) -> None: |
| 227 | + """Test that a normal user cannot update another user's exam attempt.""" |
| 228 | + user = create_random_user(db) |
| 229 | + |
| 230 | + exam, question, exam_attempt, answer = create_exam_with_attempt_and_answer( |
| 231 | + db, owner_id=user.id |
| 232 | + ) |
| 233 | + |
| 234 | + payload = {"answers": [{"id": str(uuid.uuid4()), "response": "4"}]} |
| 235 | + |
| 236 | + response = client.patch( |
| 237 | + f"{settings.API_V1_STR}/exam-attempts/{exam_attempt.id}", |
| 238 | + headers=normal_user_token_headers, |
| 239 | + json=payload, |
| 240 | + ) |
| 241 | + |
| 242 | + assert response.status_code == 403 |
| 243 | + assert response.json()["detail"] == "Not allowed" |
0 commit comments