Skip to content

Commit 090a43a

Browse files
committed
fixup!: add tests
1 parent 9ddfaf7 commit 090a43a

5 files changed

Lines changed: 98 additions & 11 deletions

File tree

openedx/core/djangoapps/agreements/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class ProctoringPIISignature(TimeStampedModel):
7171
class Meta:
7272
app_label = 'agreements'
7373

74+
7475
class UserAgreementRecord(models.Model):
7576
"""
7677
This model stores the agreements a user has accepted or acknowledged.

openedx/core/djangoapps/agreements/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Meta:
3232
model = LTIPIISignature
3333
fields = ('username', 'course_id', 'lti_tools', 'created_at')
3434

35+
3536
class UserAgreementsSerializer(serializers.Serializer):
3637
"""
3738
Serializer for UserAgreementRecord model

openedx/core/djangoapps/agreements/tests/test_api.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
Tests for the Agreements API
33
"""
44
import logging
5+
from datetime import datetime, timedelta
56

7+
from django.test import TestCase
8+
from opaque_keys.edx.keys import CourseKey
69
from testfixtures import LogCapture
710

811
from common.djangoapps.student.tests.factories import UserFactory
@@ -12,15 +15,17 @@
1215
get_integrity_signatures_for_course,
1316
get_pii_receiving_lti_tools,
1417
create_lti_pii_signature,
15-
get_lti_pii_signature
18+
get_lti_pii_signature,
19+
create_user_agreement_record,
20+
get_user_agreements,
21+
get_user_agreement_record,
1622
)
1723
from openedx.core.djangolib.testing.utils import skip_unless_lms
18-
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order
19-
from xmodule.modulestore.tests.factories import CourseFactory # lint-amnesty, pylint: disable=wrong-import-order
24+
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
25+
from xmodule.modulestore.tests.factories import CourseFactory
2026
from ..models import (
2127
LTIPIITool,
2228
)
23-
from opaque_keys.edx.keys import CourseKey
2429

2530
LOGGER_NAME = "openedx.core.djangoapps.agreements.api"
2631

@@ -186,3 +191,37 @@ def _assert_ltitools(self, lti_list):
186191
Helper function to assert the returned list has the correct tools
187192
"""
188193
self.assertEqual(self.lti_tools, lti_list)
194+
195+
196+
@skip_unless_lms
197+
class UserAgreementsTests(TestCase):
198+
"""
199+
Tests for the python APIs related to user agreements.
200+
"""
201+
def setUp(self):
202+
self.user = UserFactory()
203+
204+
def test_get_user_agreements(self, ):
205+
result = list(get_user_agreements(self.user))
206+
assert len(result) == 0
207+
208+
record = create_user_agreement_record(self.user, 'test_type')
209+
result = list(get_user_agreements(self.user))
210+
211+
assert len(result) == 1
212+
assert result[0].agreement_type == 'test_type'
213+
assert result[0].username == self.user.username
214+
assert result[0].accepted_at == record.accepted_at
215+
216+
def test_get_user_agreement_record(self):
217+
record = create_user_agreement_record(self.user, 'test_type')
218+
result = get_user_agreement_record(self.user, 'test_type')
219+
220+
assert result == record
221+
222+
result = get_user_agreement_record(self.user, 'test_type', datetime.now() + timedelta(days=1))
223+
224+
assert result is None
225+
226+
def tearDown(self):
227+
self.user.delete()

openedx/core/djangoapps/agreements/tests/test_views.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22
Tests for agreements views
33
"""
44

5+
import json
56
from datetime import datetime, timedelta
67
from unittest.mock import patch
78

89
from django.conf import settings
910
from django.urls import reverse
10-
from rest_framework.test import APITestCase
11-
from rest_framework import status
1211
from freezegun import freeze_time
13-
import json
12+
from rest_framework import status
13+
from rest_framework.test import APITestCase
1414

15-
from common.djangoapps.student.tests.factories import UserFactory, AdminFactory
1615
from common.djangoapps.student.roles import CourseStaffRole
16+
from common.djangoapps.student.tests.factories import UserFactory, AdminFactory
1717
from openedx.core.djangoapps.agreements.api import (
1818
create_integrity_signature,
1919
get_integrity_signatures_for_course,
20-
get_lti_pii_signature
20+
get_lti_pii_signature, create_user_agreement_record
2121
)
2222
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
2425
from xmodule.modulestore.tests.factories import CourseFactory # lint-amnesty, pylint: disable=wrong-import-order
2526

2627

@@ -289,3 +290,49 @@ def test_post_lti_pii_signature(self):
289290
signature = get_lti_pii_signature(self.user.username, self.course_id)
290291
self.assertEqual(signature.user.username, self.user.username)
291292
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

openedx/core/djangoapps/agreements/urls.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from django.conf import settings
66
from django.urls import re_path, path
7-
from django.urls.conf import include
87

98
from .views import IntegritySignatureView, LTIPIISignatureView, UserAgreementsView
109

0 commit comments

Comments
 (0)