|
1 | 1 | from django.test import override_settings |
| 2 | +from django.contrib.auth.models import Permission |
2 | 3 | from rest_framework.test import APIClient |
3 | 4 |
|
4 | 5 | from course.models import CourseInstance |
@@ -144,3 +145,85 @@ def test_put_course(self): |
144 | 145 | t = map(lambda x: x.user.username, course.teachers) |
145 | 146 | self.assertIn('staff', t) |
146 | 147 | self.assertIn('newteacher', t) |
| 148 | + |
| 149 | + def test_get_current_teachers(self): |
| 150 | + self.user.email = 'teacher1@example.com' |
| 151 | + self.user.save(update_fields=['email']) |
| 152 | + self.user1.email = 'teacher2@example.com' |
| 153 | + self.user1.save(update_fields=['email']) |
| 154 | + self.user2.email = 'pastteacher@example.com' |
| 155 | + self.user2.save(update_fields=['email']) |
| 156 | + |
| 157 | + self.current_course_instance.add_teacher(self.user.userprofile) |
| 158 | + self.current_course_instance.add_teacher(self.user1.userprofile) |
| 159 | + self.future_course_instance.add_teacher(self.user1.userprofile) |
| 160 | + self.past_course_instance.add_teacher(self.user2.userprofile) |
| 161 | + |
| 162 | + client = APIClient() |
| 163 | + client.force_authenticate(user=self.superuser) |
| 164 | + response = client.get('/api/v2/courses/current-teachers/') |
| 165 | + |
| 166 | + self.assertEqual(response.status_code, 200) |
| 167 | + self.assertIn('count', response.data) |
| 168 | + self.assertIn('results', response.data) |
| 169 | + |
| 170 | + results = response.data['results'] |
| 171 | + self.assertEqual(response.data['count'], 2) |
| 172 | + self.assertEqual(len(results), 2) |
| 173 | + |
| 174 | + emails = sorted(row['email'] for row in results) |
| 175 | + self.assertEqual(emails, ['teacher1@example.com', 'teacher2@example.com']) |
| 176 | + |
| 177 | + def test_get_current_teachers_requires_permission(self): |
| 178 | + self.current_course_instance.add_teacher(self.user.userprofile) |
| 179 | + |
| 180 | + client = APIClient() |
| 181 | + client.force_authenticate(user=self.user) |
| 182 | + response = client.get('/api/v2/courses/current-teachers/') |
| 183 | + self.assertEqual(response.status_code, 403) |
| 184 | + |
| 185 | + def test_get_current_teachers_with_permission(self): |
| 186 | + self.current_course_instance.add_teacher(self.user1.userprofile) |
| 187 | + self.user1.email = 'teacher2@example.com' |
| 188 | + self.user1.save(update_fields=['email']) |
| 189 | + |
| 190 | + permission = Permission.objects.get( |
| 191 | + content_type__app_label='course', |
| 192 | + codename='view_current_teachers', |
| 193 | + ) |
| 194 | + self.user.user_permissions.add(permission) |
| 195 | + |
| 196 | + client = APIClient() |
| 197 | + client.force_authenticate(user=self.user) |
| 198 | + response = client.get('/api/v2/courses/current-teachers/') |
| 199 | + |
| 200 | + self.assertEqual(response.status_code, 200) |
| 201 | + emails = [row['email'] for row in response.data['results']] |
| 202 | + self.assertIn('teacher2@example.com', emails) |
| 203 | + |
| 204 | + def test_get_current_teachers_ended_within_days(self): |
| 205 | + self.user.email = 'teacher1@example.com' |
| 206 | + self.user.save(update_fields=['email']) |
| 207 | + self.user2.email = 'pastteacher@example.com' |
| 208 | + self.user2.save(update_fields=['email']) |
| 209 | + |
| 210 | + self.current_course_instance.add_teacher(self.user.userprofile) |
| 211 | + self.past_course_instance.add_teacher(self.user2.userprofile) |
| 212 | + |
| 213 | + client = APIClient() |
| 214 | + client.force_authenticate(user=self.superuser) |
| 215 | + response = client.get('/api/v2/courses/current-teachers/?ended_within_days=365') |
| 216 | + |
| 217 | + self.assertEqual(response.status_code, 200) |
| 218 | + emails = sorted(row['email'] for row in response.data['results']) |
| 219 | + self.assertEqual(emails, ['pastteacher@example.com', 'teacher1@example.com']) |
| 220 | + |
| 221 | + def test_get_current_teachers_ended_within_days_invalid(self): |
| 222 | + client = APIClient() |
| 223 | + client.force_authenticate(user=self.superuser) |
| 224 | + |
| 225 | + response = client.get('/api/v2/courses/current-teachers/?ended_within_days=abc') |
| 226 | + self.assertEqual(response.status_code, 400) |
| 227 | + |
| 228 | + response = client.get('/api/v2/courses/current-teachers/?ended_within_days=-1') |
| 229 | + self.assertEqual(response.status_code, 400) |
0 commit comments