29
29
AccessToken = get_access_token_model ()
30
30
UserModel = get_user_model ()
31
31
32
- exp = datetime .datetime .now () + datetime .timedelta (days = 1 )
32
+ default_exp = datetime .datetime .now () + datetime .timedelta (days = 1 )
33
33
34
34
35
35
class ScopeResourceView (ScopedProtectedResourceView ):
@@ -42,27 +42,28 @@ def post(self, request, *args, **kwargs):
42
42
return HttpResponse ("This is a protected resource" , 200 )
43
43
44
44
45
+ class MockResponse :
46
+ def __init__ (self , json_data , status_code ):
47
+ self .json_data = json_data
48
+ self .status_code = status_code
49
+
50
+ def json (self ):
51
+ return self .json_data
52
+
53
+
45
54
def mocked_requests_post (url , data , * args , ** kwargs ):
46
55
"""
47
56
Mock the response from the authentication server
48
57
"""
49
58
50
- class MockResponse :
51
- def __init__ (self , json_data , status_code ):
52
- self .json_data = json_data
53
- self .status_code = status_code
54
-
55
- def json (self ):
56
- return self .json_data
57
-
58
59
if "token" in data and data ["token" ] and data ["token" ] != "12345678900" :
59
60
return MockResponse (
60
61
{
61
62
"active" : True ,
62
63
"scope" : "read write dolphin" ,
63
64
"client_id" : "client_id_{}" .format (data ["token" ]),
64
65
"username" : "{}_user" .format (data ["token" ]),
65
- "exp" : int (calendar .timegm (exp .timetuple ())),
66
+ "exp" : int (calendar .timegm (default_exp .timetuple ())),
66
67
},
67
68
200 ,
68
69
)
@@ -75,6 +76,21 @@ def json(self):
75
76
)
76
77
77
78
79
+ def mocked_introspect_request_short_living_token (url , data , * args , ** kwargs ):
80
+ exp = datetime .datetime .now () + datetime .timedelta (minutes = 30 )
81
+
82
+ return MockResponse (
83
+ {
84
+ "active" : True ,
85
+ "scope" : "read write dolphin" ,
86
+ "client_id" : "client_id_{}" .format (data ["token" ]),
87
+ "username" : "{}_user" .format (data ["token" ]),
88
+ "exp" : int (calendar .timegm (exp .timetuple ())),
89
+ },
90
+ 200 ,
91
+ )
92
+
93
+
78
94
urlpatterns = [
79
95
path ("oauth2/" , include ("oauth2_provider.urls" )),
80
96
path ("oauth2-test-resource/" , ScopeResourceView .as_view ()),
@@ -152,24 +168,76 @@ def test_get_token_from_authentication_server_existing_token(self, mock_get):
152
168
self .assertEqual (token .user .username , "foo_user" )
153
169
self .assertEqual (token .scope , "read write dolphin" )
154
170
155
- @mock .patch ("requests.post" , side_effect = mocked_requests_post )
156
- def test_get_token_from_authentication_server_expires_timezone (self , mock_get ):
171
+ @mock .patch ("requests.post" , side_effect = mocked_introspect_request_short_living_token )
172
+ def test_get_token_from_authentication_server_expires_no_timezone (self , mock_get ):
157
173
"""
158
174
Test method _get_token_from_authentication_server for projects with USE_TZ False
159
175
"""
160
176
settings_use_tz_backup = settings .USE_TZ
161
177
settings .USE_TZ = False
162
178
try :
163
- self .validator ._get_token_from_authentication_server (
179
+ access_token = self .validator ._get_token_from_authentication_server (
180
+ "foo" ,
181
+ oauth2_settings .RESOURCE_SERVER_INTROSPECTION_URL ,
182
+ oauth2_settings .RESOURCE_SERVER_AUTH_TOKEN ,
183
+ oauth2_settings .RESOURCE_SERVER_INTROSPECTION_CREDENTIALS ,
184
+ )
185
+
186
+ self .assertFalse (access_token .is_expired ())
187
+ except ValueError as exception :
188
+ self .fail (str (exception ))
189
+ finally :
190
+ settings .USE_TZ = settings_use_tz_backup
191
+
192
+ @mock .patch ("requests.post" , side_effect = mocked_introspect_request_short_living_token )
193
+ def test_get_token_from_authentication_server_expires_utc_timezone (self , mock_get ):
194
+ """
195
+ Test method _get_token_from_authentication_server for projects with USE_TZ True and a UTC Timezone
196
+ """
197
+ settings_use_tz_backup = settings .USE_TZ
198
+ settings_time_zone_backup = settings .TIME_ZONE
199
+ settings .USE_TZ = True
200
+ settings .TIME_ZONE = "UTC"
201
+ try :
202
+ access_token = self .validator ._get_token_from_authentication_server (
164
203
"foo" ,
165
204
oauth2_settings .RESOURCE_SERVER_INTROSPECTION_URL ,
166
205
oauth2_settings .RESOURCE_SERVER_AUTH_TOKEN ,
167
206
oauth2_settings .RESOURCE_SERVER_INTROSPECTION_CREDENTIALS ,
168
207
)
208
+
209
+ self .assertFalse (access_token .is_expired ())
210
+ except ValueError as exception :
211
+ self .fail (str (exception ))
212
+ finally :
213
+ settings .USE_TZ = settings_use_tz_backup
214
+ settings .TIME_ZONE = settings_time_zone_backup
215
+
216
+ @mock .patch ("requests.post" , side_effect = mocked_introspect_request_short_living_token )
217
+ def test_get_token_from_authentication_server_expires_non_utc_timezone (self , mock_get ):
218
+ """
219
+ Test method _get_token_from_authentication_server for projects with USE_TZ True and a non UTC Timezone
220
+
221
+ This test is important to check if the UTC Exp. date gets converted correctly
222
+ """
223
+ settings_use_tz_backup = settings .USE_TZ
224
+ settings_time_zone_backup = settings .TIME_ZONE
225
+ settings .USE_TZ = True
226
+ settings .TIME_ZONE = "Europe/Amsterdam"
227
+ try :
228
+ access_token = self .validator ._get_token_from_authentication_server (
229
+ "foo" ,
230
+ oauth2_settings .RESOURCE_SERVER_INTROSPECTION_URL ,
231
+ oauth2_settings .RESOURCE_SERVER_AUTH_TOKEN ,
232
+ oauth2_settings .RESOURCE_SERVER_INTROSPECTION_CREDENTIALS ,
233
+ )
234
+
235
+ self .assertFalse (access_token .is_expired ())
169
236
except ValueError as exception :
170
237
self .fail (str (exception ))
171
238
finally :
172
239
settings .USE_TZ = settings_use_tz_backup
240
+ settings .TIME_ZONE = settings_time_zone_backup
173
241
174
242
@mock .patch ("requests.post" , side_effect = mocked_requests_post )
175
243
def test_validate_bearer_token (self , mock_get ):
0 commit comments