1
1
"""
2
2
Unit test for various Utility functions
3
3
"""
4
+
4
5
import json
6
+ from datetime import date , timedelta
5
7
from unittest .mock import patch
6
8
7
9
import ddt
14
16
from common .djangoapps .student .tests .factories import GlobalStaffFactory , UserFactory
15
17
from lms .djangoapps .courseware .constants import UNEXPECTED_ERROR_IS_ELIGIBLE
16
18
from lms .djangoapps .courseware .tests .factories import FinancialAssistanceConfigurationFactory
19
+ from lms .djangoapps .courseware .access_utils import adjust_start_date
17
20
from lms .djangoapps .courseware .utils import (
18
21
create_financial_assistance_application ,
19
22
get_financial_assistance_application_status ,
20
- is_eligible_for_financial_aid
23
+ is_eligible_for_financial_aid ,
21
24
)
22
25
23
26
27
+ @ddt .ddt
28
+ class TestAccessUtils (TestCase ):
29
+ """Tests for the access_utils functions."""
30
+
31
+ @ddt .data (
32
+ # days_early_for_beta, is_beta_user, expected_date
33
+ (None , True , "2025-01-03" ),
34
+ (2 , True , "2025-01-01" ),
35
+ (timedelta .max .days + 10 , True , "2025-01-03" ),
36
+ (None , False , "2025-01-03" ),
37
+ (2 , False , "2025-01-03" ),
38
+ (timedelta .max .days + 10 , False , "2025-01-03" ),
39
+ )
40
+ @ddt .unpack
41
+ def test_adjust_start_date (self , days_early_for_beta , is_beta_user , expected_date ):
42
+ """Tests adjust_start_date
43
+
44
+ Should only modify the date if the user is beta for the course,
45
+ and `days_early_for_beta` is sensible number."""
46
+ start = date (2025 , 1 , 3 )
47
+ expected = date .fromisoformat (expected_date )
48
+ user = "princessofpower"
49
+ course_key = "edx1+8675"
50
+ with patch ("lms.djangoapps.courseware.access_utils.CourseBetaTesterRole" ) as role_mock :
51
+ instance = role_mock .return_value
52
+ instance .has_user .return_value = is_beta_user
53
+ adjusted_date = adjust_start_date (user , days_early_for_beta , start , course_key )
54
+ self .assertEqual (expected , adjusted_date )
55
+
56
+
24
57
@ddt .ddt
25
58
class TestFinancialAssistanceViews (TestCase ):
26
59
"""
@@ -29,17 +62,17 @@ class TestFinancialAssistanceViews(TestCase):
29
62
30
63
def setUp (self ) -> None :
31
64
super ().setUp ()
32
- self .test_course_id = ' course-v1:edX+Test+1'
65
+ self .test_course_id = " course-v1:edX+Test+1"
33
66
self .user = UserFactory ()
34
67
self .global_staff = GlobalStaffFactory .create ()
35
68
_ = FinancialAssistanceConfigurationFactory (
36
- api_base_url = ' http://financial.assistance.test:1234' ,
69
+ api_base_url = " http://financial.assistance.test:1234" ,
37
70
service_username = self .global_staff .username ,
38
71
fa_backend_enabled_courses_percentage = 100 ,
39
- enabled = True
72
+ enabled = True ,
40
73
)
41
74
_ = Application .objects .create (
42
- name = ' Test Application' ,
75
+ name = " Test Application" ,
43
76
user = self .global_staff ,
44
77
client_type = Application .CLIENT_PUBLIC ,
45
78
authorization_grant_type = Application .GRANT_CLIENT_CREDENTIALS ,
@@ -51,33 +84,31 @@ def _mock_response(self, status_code, content=None):
51
84
"""
52
85
mock_response = Response ()
53
86
mock_response .status_code = status_code
54
- mock_response ._content = json .dumps (content ).encode (' utf-8' ) # pylint: disable=protected-access
87
+ mock_response ._content = json .dumps (content ).encode (" utf-8" ) # pylint: disable=protected-access
55
88
return mock_response
56
89
57
90
@ddt .data (
58
- {' is_eligible' : True , ' reason' : None },
59
- {' is_eligible' : False , ' reason' : ' This course is not eligible for financial aid' }
91
+ {" is_eligible" : True , " reason" : None },
92
+ {" is_eligible" : False , " reason" : " This course is not eligible for financial aid" },
60
93
)
61
94
def test_is_eligible_for_financial_aid (self , response_data ):
62
95
"""
63
96
Tests the functionality of is_eligible_for_financial_aid which calls edx-financial-assistance backend
64
97
to return eligibility status for financial assistance for a given course.
65
98
"""
66
- with patch .object (OAuthAPIClient , ' request' ) as oauth_mock :
99
+ with patch .object (OAuthAPIClient , " request" ) as oauth_mock :
67
100
oauth_mock .return_value = self ._mock_response (status .HTTP_200_OK , response_data )
68
101
is_eligible , reason = is_eligible_for_financial_aid (self .test_course_id )
69
- assert is_eligible is response_data .get (' is_eligible' )
70
- assert reason == response_data .get (' reason' )
102
+ assert is_eligible is response_data .get (" is_eligible" )
103
+ assert reason == response_data .get (" reason" )
71
104
72
105
def test_is_eligible_for_financial_aid_invalid_course_id (self ):
73
106
"""
74
107
Tests the functionality of is_eligible_for_financial_aid for an invalid course id.
75
108
"""
76
109
error_message = f"Invalid course id { self .test_course_id } provided"
77
- with patch .object (OAuthAPIClient , 'request' ) as oauth_mock :
78
- oauth_mock .return_value = self ._mock_response (
79
- status .HTTP_400_BAD_REQUEST , {"message" : error_message }
80
- )
110
+ with patch .object (OAuthAPIClient , "request" ) as oauth_mock :
111
+ oauth_mock .return_value = self ._mock_response (status .HTTP_400_BAD_REQUEST , {"message" : error_message })
81
112
is_eligible , reason = is_eligible_for_financial_aid (self .test_course_id )
82
113
assert is_eligible is False
83
114
assert reason == error_message
@@ -86,9 +117,9 @@ def test_is_eligible_for_financial_aid_invalid_unexpected_error(self):
86
117
"""
87
118
Tests the functionality of is_eligible_for_financial_aid for an unexpected error
88
119
"""
89
- with patch .object (OAuthAPIClient , ' request' ) as oauth_mock :
120
+ with patch .object (OAuthAPIClient , " request" ) as oauth_mock :
90
121
oauth_mock .return_value = self ._mock_response (
91
- status .HTTP_500_INTERNAL_SERVER_ERROR , {' error' : ' unexpected error occurred' }
122
+ status .HTTP_500_INTERNAL_SERVER_ERROR , {" error" : " unexpected error occurred" }
92
123
)
93
124
is_eligible , reason = is_eligible_for_financial_aid (self .test_course_id )
94
125
assert is_eligible is False
@@ -99,45 +130,39 @@ def test_get_financial_assistance_application_status(self):
99
130
Tests the functionality of get_financial_assistance_application_status against a user id and a course id
100
131
edx-financial-assistance backend to return status of a financial assistance application.
101
132
"""
102
- test_response = {'id' : 123 , ' status' : ' ACCEPTED' , ' coupon_code' : ' ABCD..' }
103
- with patch .object (OAuthAPIClient , ' request' ) as oauth_mock :
133
+ test_response = {"id" : 123 , " status" : " ACCEPTED" , " coupon_code" : " ABCD.." }
134
+ with patch .object (OAuthAPIClient , " request" ) as oauth_mock :
104
135
oauth_mock .return_value = self ._mock_response (status .HTTP_200_OK , test_response )
105
136
has_application , reason = get_financial_assistance_application_status (self .user .id , self .test_course_id )
106
137
assert has_application is True
107
138
assert reason == test_response
108
139
109
140
@ddt .data (
110
- {
111
- 'status' : status .HTTP_400_BAD_REQUEST ,
112
- 'content' : {'message' : 'Invalid course id provided' }
113
- },
114
- {
115
- 'status' : status .HTTP_404_NOT_FOUND ,
116
- 'content' : {'message' : 'Application details not found' }
117
- }
141
+ {"status" : status .HTTP_400_BAD_REQUEST , "content" : {"message" : "Invalid course id provided" }},
142
+ {"status" : status .HTTP_404_NOT_FOUND , "content" : {"message" : "Application details not found" }},
118
143
)
119
144
def test_get_financial_assistance_application_status_unsuccessful (self , response_data ):
120
145
"""
121
146
Tests unsuccessful scenarios of get_financial_assistance_application_status
122
147
against a user id and a course id edx-financial-assistance backend.
123
148
"""
124
- with patch .object (OAuthAPIClient , ' request' ) as oauth_mock :
125
- oauth_mock .return_value = self ._mock_response (response_data .get (' status' ), response_data .get (' content' ))
149
+ with patch .object (OAuthAPIClient , " request" ) as oauth_mock :
150
+ oauth_mock .return_value = self ._mock_response (response_data .get (" status" ), response_data .get (" content" ))
126
151
has_application , reason = get_financial_assistance_application_status (self .user .id , self .test_course_id )
127
152
assert has_application is False
128
- assert reason == response_data .get (' content' ).get (' message' )
153
+ assert reason == response_data .get (" content" ).get (" message" )
129
154
130
155
def test_create_financial_assistance_application (self ):
131
156
"""
132
157
Tests the functionality of create_financial_assistance_application which calls edx-financial-assistance backend
133
158
to create a new financial assistance application given a form data.
134
159
"""
135
160
test_form_data = {
136
- ' lms_user_id' : self .user .id ,
137
- ' course_id' : self .test_course_id ,
161
+ " lms_user_id" : self .user .id ,
162
+ " course_id" : self .test_course_id ,
138
163
}
139
- with patch .object (OAuthAPIClient , ' request' ) as oauth_mock :
140
- oauth_mock .return_value = self ._mock_response (status .HTTP_200_OK , {' success' : True })
164
+ with patch .object (OAuthAPIClient , " request" ) as oauth_mock :
165
+ oauth_mock .return_value = self ._mock_response (status .HTTP_200_OK , {" success" : True })
141
166
response = create_financial_assistance_application (form_data = test_form_data )
142
167
assert response .status_code == status .HTTP_204_NO_CONTENT
143
168
@@ -147,12 +172,12 @@ def test_create_financial_assistance_application_bad_request(self):
147
172
to create a new financial assistance application given a form data.
148
173
"""
149
174
test_form_data = {
150
- ' lms_user_id' : self .user .id ,
151
- ' course_id' : ' invalid_course_id' ,
175
+ " lms_user_id" : self .user .id ,
176
+ " course_id" : " invalid_course_id" ,
152
177
}
153
- error_response = {' message' : ' Invalid course id provided' }
154
- with patch .object (OAuthAPIClient , ' request' ) as oauth_mock :
178
+ error_response = {" message" : " Invalid course id provided" }
179
+ with patch .object (OAuthAPIClient , " request" ) as oauth_mock :
155
180
oauth_mock .return_value = self ._mock_response (status .HTTP_400_BAD_REQUEST , error_response )
156
181
response = create_financial_assistance_application (form_data = test_form_data )
157
182
assert response .status_code == status .HTTP_400_BAD_REQUEST
158
- assert json .loads (response .content .decode (' utf-8' )) == error_response
183
+ assert json .loads (response .content .decode (" utf-8" )) == error_response
0 commit comments