|
1 | | -from django.test import TestCase |
| 1 | +import logging |
| 2 | + |
| 3 | +from django.test import TestCase, Client |
| 4 | +from django.urls import reverse |
| 5 | +from django.utils import timezone |
| 6 | +from datetime import timedelta |
| 7 | + |
| 8 | +from courses.models import ( |
| 9 | + User, |
| 10 | + Course, |
| 11 | + Project, |
| 12 | + ProjectSubmission, |
| 13 | + ProjectState, |
| 14 | + Enrollment, |
| 15 | + Homework, |
| 16 | + HomeworkState, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +credentials = dict( |
| 24 | + username="test@test.com", |
| 25 | + email="test@test.com", |
| 26 | + password="12345", |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class CadminViewTests(TestCase): |
| 31 | + def setUp(self): |
| 32 | + self.client = Client() |
| 33 | + |
| 34 | + self.user = User.objects.create_user(**credentials) |
| 35 | + |
| 36 | + # Create admin user |
| 37 | + self.admin_user = User.objects.create_user( |
| 38 | + username="admin@test.com", |
| 39 | + email="admin@test.com", |
| 40 | + password="admin123", |
| 41 | + is_staff=True, |
| 42 | + ) |
| 43 | + |
| 44 | + self.course = Course.objects.create( |
| 45 | + slug="test-course", |
| 46 | + title="Test Course", |
| 47 | + description="Test Course Description", |
| 48 | + ) |
| 49 | + |
| 50 | + self.homework = Homework.objects.create( |
| 51 | + course=self.course, |
| 52 | + slug="test-homework", |
| 53 | + title="Test Homework", |
| 54 | + due_date=timezone.now() + timedelta(days=7), |
| 55 | + state=HomeworkState.OPEN.value, |
| 56 | + ) |
| 57 | + |
| 58 | + self.project = Project.objects.create( |
| 59 | + course=self.course, |
| 60 | + slug="test-project", |
| 61 | + title="Test Project", |
| 62 | + submission_due_date=timezone.now() + timedelta(days=7), |
| 63 | + peer_review_due_date=timezone.now() + timedelta(days=14), |
| 64 | + state=ProjectState.COLLECTING_SUBMISSIONS.value, |
| 65 | + ) |
| 66 | + |
| 67 | + def test_course_list_unauthenticated_redirects(self): |
| 68 | + """Test that unauthenticated users are redirected from course list""" |
| 69 | + url = reverse("cadmin_course_list") |
| 70 | + response = self.client.get(url) |
| 71 | + self.assertEqual(response.status_code, 302) |
| 72 | + self.assertIn("/accounts/login/", response.url) |
| 73 | + |
| 74 | + def test_course_list_non_staff_denied(self): |
| 75 | + """Test that non-staff users cannot access course list""" |
| 76 | + self.client.login(username="test@test.com", password="12345") |
| 77 | + url = reverse("cadmin_course_list") |
| 78 | + response = self.client.get(url) |
| 79 | + self.assertEqual(response.status_code, 302) |
| 80 | + |
| 81 | + def test_course_list_staff_allowed(self): |
| 82 | + """Test that staff users can access course list""" |
| 83 | + self.client.login(username="admin@test.com", password="admin123") |
| 84 | + url = reverse("cadmin_course_list") |
| 85 | + response = self.client.get(url) |
| 86 | + self.assertEqual(response.status_code, 200) |
| 87 | + self.assertContains(response, "Course Administration") |
| 88 | + |
| 89 | + def test_course_admin_staff_allowed(self): |
| 90 | + """Test that staff users can access course admin page""" |
| 91 | + self.client.login(username="admin@test.com", password="admin123") |
| 92 | + url = reverse("cadmin_course", kwargs={"course_slug": self.course.slug}) |
| 93 | + response = self.client.get(url) |
| 94 | + self.assertEqual(response.status_code, 200) |
| 95 | + self.assertContains(response, self.course.title) |
| 96 | + self.assertContains(response, "Admin Panel") |
| 97 | + |
| 98 | + def test_homework_submissions_redirect_from_courses(self): |
| 99 | + """Test that homework submissions view redirects to cadmin""" |
| 100 | + url = reverse( |
| 101 | + "homework_submissions", |
| 102 | + kwargs={ |
| 103 | + "course_slug": self.course.slug, |
| 104 | + "homework_slug": self.homework.slug, |
| 105 | + } |
| 106 | + ) |
| 107 | + response = self.client.get(url) |
| 108 | + self.assertEqual(response.status_code, 302) |
| 109 | + self.assertIn("cadmin", response.url) |
| 110 | + |
| 111 | + def test_project_submissions_redirect_from_courses(self): |
| 112 | + """Test that project submissions view redirects to cadmin""" |
| 113 | + url = reverse( |
| 114 | + "project_submissions", |
| 115 | + kwargs={ |
| 116 | + "course_slug": self.course.slug, |
| 117 | + "project_slug": self.project.slug, |
| 118 | + } |
| 119 | + ) |
| 120 | + response = self.client.get(url) |
| 121 | + self.assertEqual(response.status_code, 302) |
| 122 | + self.assertIn("cadmin", response.url) |
| 123 | + |
| 124 | + def test_cadmin_homework_submissions_staff_allowed(self): |
| 125 | + """Test that staff users can view homework submissions in cadmin""" |
| 126 | + self.client.login(username="admin@test.com", password="admin123") |
| 127 | + url = reverse( |
| 128 | + "cadmin_homework_submissions", |
| 129 | + kwargs={ |
| 130 | + "course_slug": self.course.slug, |
| 131 | + "homework_slug": self.homework.slug, |
| 132 | + } |
| 133 | + ) |
| 134 | + response = self.client.get(url) |
| 135 | + self.assertEqual(response.status_code, 200) |
| 136 | + self.assertContains(response, self.homework.title) |
| 137 | + |
| 138 | + def test_cadmin_project_submissions_staff_allowed(self): |
| 139 | + """Test that staff users can view project submissions in cadmin""" |
| 140 | + self.client.login(username="admin@test.com", password="admin123") |
| 141 | + url = reverse( |
| 142 | + "cadmin_project_submissions", |
| 143 | + kwargs={ |
| 144 | + "course_slug": self.course.slug, |
| 145 | + "project_slug": self.project.slug, |
| 146 | + } |
| 147 | + ) |
| 148 | + response = self.client.get(url) |
| 149 | + self.assertEqual(response.status_code, 200) |
| 150 | + self.assertContains(response, self.project.title) |
2 | 151 |
|
3 | | -# Create your tests here. |
|
0 commit comments