|
2 | 2 | Tests for agreements views |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import json |
5 | 6 | from datetime import datetime, timedelta |
6 | 7 | from unittest.mock import patch |
7 | 8 |
|
8 | 9 | from django.conf import settings |
9 | 10 | from django.urls import reverse |
10 | | -from rest_framework.test import APITestCase |
11 | | -from rest_framework import status |
12 | 11 | from freezegun import freeze_time |
13 | | -import json |
| 12 | +from rest_framework import status |
| 13 | +from rest_framework.test import APITestCase |
14 | 14 |
|
15 | | -from common.djangoapps.student.tests.factories import UserFactory, AdminFactory |
16 | 15 | from common.djangoapps.student.roles import CourseStaffRole |
| 16 | +from common.djangoapps.student.tests.factories import UserFactory, AdminFactory |
17 | 17 | from openedx.core.djangoapps.agreements.api import ( |
18 | 18 | create_integrity_signature, |
19 | 19 | get_integrity_signatures_for_course, |
20 | | - get_lti_pii_signature |
| 20 | + get_lti_pii_signature, create_user_agreement_record |
21 | 21 | ) |
22 | 22 | from openedx.core.djangolib.testing.utils import skip_unless_lms |
23 | | -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order |
| 23 | +from xmodule.modulestore.tests.django_utils import \ |
| 24 | + ModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order |
24 | 25 | from xmodule.modulestore.tests.factories import CourseFactory # lint-amnesty, pylint: disable=wrong-import-order |
25 | 26 |
|
26 | 27 |
|
@@ -289,3 +290,49 @@ def test_post_lti_pii_signature(self): |
289 | 290 | signature = get_lti_pii_signature(self.user.username, self.course_id) |
290 | 291 | self.assertEqual(signature.user.username, self.user.username) |
291 | 292 | self.assertEqual(signature.lti_tools, self.lti_tools) |
| 293 | + |
| 294 | + |
| 295 | +@skip_unless_lms |
| 296 | +class UserAgreementsViewTests(APITestCase): |
| 297 | + """ |
| 298 | + Tests for the UserAgreementsView |
| 299 | + """ |
| 300 | + |
| 301 | + def setUp(self): |
| 302 | + self.user = UserFactory(username="testuser", password="password") |
| 303 | + self.client.login(username="testuser", password="password") |
| 304 | + self.url = reverse('user_agreements', kwargs={'agreement_type': 'sample_agreement'}) |
| 305 | + |
| 306 | + def test_get_user_agreement_record_no_data(self): |
| 307 | + response = self.client.get(self.url) |
| 308 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 309 | + |
| 310 | + def test_get_user_agreement_record_invalid_date(self): |
| 311 | + response = self.client.get(self.url, {'after': 'invalid_date'}) |
| 312 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 313 | + |
| 314 | + def test_get_user_agreement_record(self): |
| 315 | + create_user_agreement_record(self.user, 'sample_agreement') |
| 316 | + response = self.client.get(self.url) |
| 317 | + assert response.status_code == status.HTTP_200_OK |
| 318 | + assert 'accepted_at' in response.data |
| 319 | + |
| 320 | + response = self.client.get(self.url, {"after": str(datetime.now() + timedelta(days=1))}) |
| 321 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 322 | + |
| 323 | + def test_post_user_agreement(self): |
| 324 | + with freeze_time("2024-11-21 12:00:00"): |
| 325 | + response = self.client.post(self.url) |
| 326 | + assert response.status_code == status.HTTP_201_CREATED |
| 327 | + |
| 328 | + response = self.client.get(self.url) |
| 329 | + assert response.status_code == status.HTTP_200_OK |
| 330 | + |
| 331 | + response = self.client.get(self.url, {"after": "2024-11-21T13:00:00Z"}) |
| 332 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 333 | + |
| 334 | + response = self.client.post(self.url) |
| 335 | + assert response.status_code == status.HTTP_201_CREATED |
| 336 | + |
| 337 | + response = self.client.get(self.url, {"after": "2024-11-21T13:00:00Z"}) |
| 338 | + assert response.status_code == status.HTTP_200_OK |
0 commit comments