|
1 | 1 | from django.test import TestCase |
| 2 | +from django.urls import reverse |
| 3 | +from rest_framework.test import APITestCase |
| 4 | +from rest_framework import status |
| 5 | +from django.contrib.auth.models import User |
| 6 | +from apps.lessons.models import Assignment |
2 | 7 |
|
3 | 8 | # Create your tests here. |
| 9 | +class AssignmentAPITests(APITestCase): |
| 10 | + def setUp(self): |
| 11 | + # Create a superuser for testing |
| 12 | + self.user = User.objects.create_superuser( |
| 13 | + username='testadmin', password='testpassword', email='test@test.com' |
| 14 | + ) |
| 15 | + |
| 16 | + # Get access token |
| 17 | + response = self.client.post( |
| 18 | + reverse('token_obtain_pair'), # Use the actual name of the URL if configured |
| 19 | + {'username': 'testadmin', 'password': 'testpassword'}, |
| 20 | + ) |
| 21 | + self.access_token = response.data['access'] |
| 22 | + |
| 23 | + # Set authorization header |
| 24 | + self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {self.access_token}') |
| 25 | + |
| 26 | + # Create an initial assignment for testing |
| 27 | + self.assignment = Assignment.objects.create( |
| 28 | + title="Initial Assignment", |
| 29 | + description="Initial assignment description.", |
| 30 | + assignment_type="HW", |
| 31 | + deadline="2025-12-01T12:00:00Z", |
| 32 | + ) |
| 33 | + |
| 34 | + # Define URLs |
| 35 | + self.detail_url = reverse('assignment-detail', kwargs={'pk': self.assignment.pk}) |
| 36 | + self.create_url = reverse('assignment-create') |
| 37 | + |
| 38 | + # hardcode? |
| 39 | + # self.list_url = '/lessons/assignments/' |
| 40 | + #self.detail_url = f'/lessons/assignments/{self.assignment.pk}/' |
| 41 | + #self.create_url = '/lessons/assignments/create/' |
| 42 | + |
| 43 | + # Test GET single assignment |
| 44 | + def test_get_assignment(self): |
| 45 | + response = self.client.get(self.detail_url) |
| 46 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 47 | + self.assertEqual(response.data['data']['title'], 'Initial Assignment') |
| 48 | + |
| 49 | + # Test POST create new assignment |
| 50 | + def test_create_assignment(self): |
| 51 | + data = { |
| 52 | + "title": "New Assignment", |
| 53 | + "description": "This is a new assignment.", |
| 54 | + "assignment_type": "HW", |
| 55 | + "deadline": "2025-12-01T12:00:00Z" |
| 56 | + } |
| 57 | + response = self.client.post(self.create_url, data, format='json') |
| 58 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 59 | + self.assertEqual(response.data['data']['title'], 'New Assignment') |
| 60 | + |
| 61 | + # Test PATCH update assignment |
| 62 | + def test_update_assignment(self): |
| 63 | + data = {"title": "Updated Title"} |
| 64 | + response = self.client.patch(self.detail_url, data, format='json') |
| 65 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 66 | + self.assertEqual(response.data['data']['title'], 'Updated Title') |
| 67 | + |
| 68 | + # Test DELETE assignment |
| 69 | + def test_delete_assignment(self): |
| 70 | + response = self.client.delete(self.detail_url) |
| 71 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 72 | + self.assertEqual(response.data['status'], 'success') |
| 73 | + |
| 74 | + # Verify it is deleted |
| 75 | + response = self.client.get(self.detail_url) |
| 76 | + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
0 commit comments