|
| 1 | +import json |
| 2 | + |
| 3 | +from django.test import TestCase, Client |
| 4 | +from django.utils import timezone |
| 5 | + |
| 6 | +from accounts.models import CustomUser, Token |
| 7 | +from courses.models import Course, Homework, Question |
| 8 | +from courses.models.homework import HomeworkState |
| 9 | + |
| 10 | + |
| 11 | +class HomeworksAPITestCase(TestCase): |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + self.user = CustomUser.objects.create( |
| 15 | + username="testuser", |
| 16 | + email="test@example.com", |
| 17 | + password="password", |
| 18 | + ) |
| 19 | + self.token = Token.objects.create(user=self.user) |
| 20 | + self.client = Client() |
| 21 | + self.client.defaults["HTTP_AUTHORIZATION"] = f"Token {self.token.key}" |
| 22 | + |
| 23 | + self.course = Course.objects.create( |
| 24 | + title="Test Course", |
| 25 | + slug="test-course", |
| 26 | + description="Test", |
| 27 | + ) |
| 28 | + |
| 29 | + def _create_homework(self, slug="hw1", state=HomeworkState.CLOSED.value): |
| 30 | + return Homework.objects.create( |
| 31 | + course=self.course, |
| 32 | + title="Homework 1", |
| 33 | + slug=slug, |
| 34 | + description="Description", |
| 35 | + due_date=timezone.now(), |
| 36 | + state=state, |
| 37 | + ) |
| 38 | + |
| 39 | + def test_list_homeworks(self): |
| 40 | + self._create_homework() |
| 41 | + response = self.client.get(f"/api/courses/{self.course.slug}/homeworks/") |
| 42 | + self.assertEqual(response.status_code, 200) |
| 43 | + data = response.json() |
| 44 | + self.assertEqual(len(data["homeworks"]), 1) |
| 45 | + self.assertEqual(data["homeworks"][0]["slug"], "hw1") |
| 46 | + |
| 47 | + def test_create_homework(self): |
| 48 | + payload = { |
| 49 | + "name": "Homework 2", |
| 50 | + "due_date": "2026-04-01T23:59:59Z", |
| 51 | + "description": "New homework", |
| 52 | + } |
| 53 | + response = self.client.post( |
| 54 | + f"/api/courses/{self.course.slug}/homeworks/", |
| 55 | + json.dumps(payload), |
| 56 | + content_type="application/json", |
| 57 | + ) |
| 58 | + self.assertEqual(response.status_code, 201) |
| 59 | + data = response.json() |
| 60 | + self.assertEqual(len(data["created"]), 1) |
| 61 | + self.assertEqual(data["created"][0]["title"], "Homework 2") |
| 62 | + self.assertEqual(data["created"][0]["state"], "CL") |
| 63 | + |
| 64 | + def test_create_homework_bulk(self): |
| 65 | + payload = [ |
| 66 | + {"name": "HW A", "due_date": "2026-04-01T23:59:59Z"}, |
| 67 | + {"name": "HW B", "due_date": "2026-04-02T23:59:59Z"}, |
| 68 | + ] |
| 69 | + response = self.client.post( |
| 70 | + f"/api/courses/{self.course.slug}/homeworks/", |
| 71 | + json.dumps(payload), |
| 72 | + content_type="application/json", |
| 73 | + ) |
| 74 | + self.assertEqual(response.status_code, 201) |
| 75 | + data = response.json() |
| 76 | + self.assertEqual(len(data["created"]), 2) |
| 77 | + |
| 78 | + def test_create_homework_with_questions(self): |
| 79 | + payload = { |
| 80 | + "name": "HW with Q", |
| 81 | + "due_date": "2026-04-01T23:59:59Z", |
| 82 | + "questions": [ |
| 83 | + { |
| 84 | + "text": "What is 2+2?", |
| 85 | + "question_type": "MC", |
| 86 | + "possible_answers": ["3", "4", "5"], |
| 87 | + "correct_answer": "2", |
| 88 | + } |
| 89 | + ], |
| 90 | + } |
| 91 | + response = self.client.post( |
| 92 | + f"/api/courses/{self.course.slug}/homeworks/", |
| 93 | + json.dumps(payload), |
| 94 | + content_type="application/json", |
| 95 | + ) |
| 96 | + self.assertEqual(response.status_code, 201) |
| 97 | + data = response.json() |
| 98 | + self.assertEqual(data["created"][0]["questions_count"], 1) |
| 99 | + |
| 100 | + def test_create_homework_missing_fields(self): |
| 101 | + payload = {"name": "No date"} |
| 102 | + response = self.client.post( |
| 103 | + f"/api/courses/{self.course.slug}/homeworks/", |
| 104 | + json.dumps(payload), |
| 105 | + content_type="application/json", |
| 106 | + ) |
| 107 | + self.assertEqual(response.status_code, 400) |
| 108 | + |
| 109 | + def test_create_homework_duplicate_slug(self): |
| 110 | + self._create_homework(slug="hw1") |
| 111 | + payload = {"name": "HW 1", "slug": "hw1", "due_date": "2026-04-01T23:59:59Z"} |
| 112 | + response = self.client.post( |
| 113 | + f"/api/courses/{self.course.slug}/homeworks/", |
| 114 | + json.dumps(payload), |
| 115 | + content_type="application/json", |
| 116 | + ) |
| 117 | + self.assertEqual(response.status_code, 400) |
| 118 | + |
| 119 | + def test_patch_homework_state(self): |
| 120 | + hw = self._create_homework() |
| 121 | + response = self.client.patch( |
| 122 | + f"/api/courses/{self.course.slug}/homeworks/{hw.id}/", |
| 123 | + json.dumps({"state": "OP"}), |
| 124 | + content_type="application/json", |
| 125 | + ) |
| 126 | + self.assertEqual(response.status_code, 200) |
| 127 | + data = response.json() |
| 128 | + self.assertEqual(data["state"], "OP") |
| 129 | + hw.refresh_from_db() |
| 130 | + self.assertEqual(hw.state, "OP") |
| 131 | + |
| 132 | + def test_patch_homework_description(self): |
| 133 | + hw = self._create_homework() |
| 134 | + response = self.client.patch( |
| 135 | + f"/api/courses/{self.course.slug}/homeworks/{hw.id}/", |
| 136 | + json.dumps({"description": "Updated"}), |
| 137 | + content_type="application/json", |
| 138 | + ) |
| 139 | + self.assertEqual(response.status_code, 200) |
| 140 | + hw.refresh_from_db() |
| 141 | + self.assertEqual(hw.description, "Updated") |
| 142 | + |
| 143 | + def test_patch_homework_invalid_state(self): |
| 144 | + hw = self._create_homework() |
| 145 | + response = self.client.patch( |
| 146 | + f"/api/courses/{self.course.slug}/homeworks/{hw.id}/", |
| 147 | + json.dumps({"state": "XX"}), |
| 148 | + content_type="application/json", |
| 149 | + ) |
| 150 | + self.assertEqual(response.status_code, 400) |
| 151 | + |
| 152 | + def test_patch_homework_invalid_field(self): |
| 153 | + hw = self._create_homework() |
| 154 | + response = self.client.patch( |
| 155 | + f"/api/courses/{self.course.slug}/homeworks/{hw.id}/", |
| 156 | + json.dumps({"id": 999}), |
| 157 | + content_type="application/json", |
| 158 | + ) |
| 159 | + self.assertEqual(response.status_code, 400) |
| 160 | + |
| 161 | + def test_delete_homework_closed(self): |
| 162 | + hw = self._create_homework(state=HomeworkState.CLOSED.value) |
| 163 | + response = self.client.delete( |
| 164 | + f"/api/courses/{self.course.slug}/homeworks/{hw.id}/" |
| 165 | + ) |
| 166 | + self.assertEqual(response.status_code, 200) |
| 167 | + self.assertFalse(Homework.objects.filter(id=hw.id).exists()) |
| 168 | + |
| 169 | + def test_delete_homework_not_closed(self): |
| 170 | + hw = self._create_homework(state=HomeworkState.OPEN.value) |
| 171 | + response = self.client.delete( |
| 172 | + f"/api/courses/{self.course.slug}/homeworks/{hw.id}/" |
| 173 | + ) |
| 174 | + self.assertEqual(response.status_code, 400) |
| 175 | + self.assertTrue(Homework.objects.filter(id=hw.id).exists()) |
0 commit comments