11import json
2- from apps .test import BaseApiTest
3- from django .core .management import call_command
4- from django .http import HttpRequest
5- from django .urls import reverse
2+ from http import HTTPStatus
3+ from unittest import mock
64
75# from oauth2_provider.compat import parse_qs, urlparse
86from urllib .parse import parse_qs , urlparse
7+
8+ from django .core .management import call_command
9+ from django .http import HttpRequest
10+ from django .urls import reverse
911from oauth2_provider .models import AccessToken , RefreshToken
1012from rest_framework .test import APIClient
1113from waffle .testutils import override_switch
12- from apps . authorization . models import DataAccessGrant , ArchivedDataAccessGrant
13- from apps .dot_ext .models import ArchivedToken , Application
14- from http import HTTPStatus
15- from unittest import mock
14+
15+ from apps .authorization .models import ArchivedDataAccessGrant , DataAccessGrant
16+ from apps . dot_ext . models import Application , ArchivedToken
17+ from apps . test import BaseApiTest
1618
1719
1820class TestBeneficiaryDemographicScopesChanges (BaseApiTest ):
@@ -127,7 +129,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
127129 )
128130
129131 # Assert auth request was successful
130- self .assertEqual (status_code , 200 )
132+ self .assertEqual (status_code , HTTPStatus . OK )
131133
132134 # Assert scope in response content
133135 self .assertEqual (response_scopes , sorted (APPLICATION_SCOPES_FULL ))
@@ -138,7 +140,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
138140 # Assert access to userinfo end point?
139141 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_1 .token )
140142 response = client .get ('/v1/connect/userinfo' )
141- self .assertEqual (response .status_code , 200 )
143+ self .assertEqual (response .status_code , HTTPStatus . OK )
142144
143145 # ------ TEST #2: Test refresh of token_1
144146 refresh_request_data = {
@@ -152,7 +154,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
152154 content = json .loads (response .content .decode ('utf-8' ))
153155
154156 # Assert successful
155- self .assertEqual (response .status_code , 200 )
157+ self .assertEqual (response .status_code , HTTPStatus . OK )
156158
157159 # Assert response scopes
158160 response_scopes = sorted (content ['scope' ].split ())
@@ -166,7 +168,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
166168 # Assert access to userinfo end point?
167169 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token .token )
168170 response = client .get ('/v1/connect/userinfo' )
169- self .assertEqual (response .status_code , 200 )
171+ self .assertEqual (response .status_code , HTTPStatus . OK )
170172
171173 # Verify token counts expected.
172174 self .assertEqual (AccessToken .objects .count (), 1 )
@@ -197,7 +199,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
197199 # Assert NO access to userinfo end point?
198200 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_3 .token )
199201 response = client .get ('/v1/connect/userinfo' )
200- self .assertEqual (response .status_code , 403 )
202+ self .assertEqual (response .status_code , HTTPStatus . FORBIDDEN )
201203
202204 # Verify token counts expected.
203205 self .assertEqual (AccessToken .objects .count (), 1 )
@@ -215,7 +217,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
215217 # Test access to userinfo end point? NO ACCESS!
216218 response = client .get ('/v1/connect/userinfo' )
217219 content = json .loads (response .content )
218- self .assertEqual (response .status_code , 401 )
220+ self .assertEqual (response .status_code , HTTPStatus . UNAUTHORIZED )
219221 self .assertEqual (content .get ('detail' , None ), 'Authentication credentials were not provided.' )
220222
221223 # ------ TEST #5: Test token_1 from TEST #1 token refresh? NO ACCESS!
@@ -241,7 +243,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
241243 )
242244
243245 # Assert auth request was successful
244- self .assertEqual (status_code , 200 )
246+ self .assertEqual (status_code , HTTPStatus . OK )
245247
246248 # Assert scope in response content
247249 self .assertEqual (response_scopes , sorted (APPLICATION_SCOPES_FULL ))
@@ -252,7 +254,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
252254 # Assert access to userinfo end point?
253255 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_6 .token )
254256 response = client .get ('/v1/connect/userinfo' )
255- self .assertEqual (response .status_code , 200 )
257+ self .assertEqual (response .status_code , HTTPStatus . OK )
256258
257259 # ------ TEST #7: Test token_3 from TEST #3 again. It should still have access, but no permission with status=403.
258260
@@ -262,13 +264,13 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
262264 # Test access to userinfo end point?
263265 response = client .get ('/v1/connect/userinfo' )
264266 content = json .loads (response .content )
265- self .assertEqual (response .status_code , 403 )
266- self .assertEqual (content .get ('detail' , None ), 'You do not have permission to perform this action .' )
267+ self .assertEqual (response .status_code , HTTPStatus . UNAUTHORIZED )
268+ self .assertEqual (content .get ('detail' , None ), 'Authentication credentials were not provided .' )
267269
268270 # Verify token counts expected.
269- self .assertEqual (AccessToken .objects .count (), 2 )
270- self .assertEqual (RefreshToken .objects .count (), 2 )
271- self .assertEqual (ArchivedToken .objects .count (), 2 )
271+ self .assertEqual (AccessToken .objects .count (), 1 )
272+ self .assertEqual (RefreshToken .objects .count (), 1 )
273+ self .assertEqual (ArchivedToken .objects .count (), 3 )
272274
273275 # Verify grant counts expected.
274276 self .assertEqual (DataAccessGrant .objects .count (), 1 )
@@ -280,15 +282,15 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
280282
281283 # Perform partial authorization request, with out application getting an access token.
282284 response = self .client .post (reverse ('oauth2_provider:authorize' ), data = payload )
283- self .assertEqual (response .status_code , 302 )
285+ self .assertEqual (response .status_code , HTTPStatus . FOUND )
284286
285287 # Setup token_3 in APIClient from previous step. It should be removed now?
286288 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_3 .token )
287289
288290 # Test access to userinfo end point?
289291 response = client .get ('/v1/connect/userinfo' )
290292 content = json .loads (response .content )
291- self .assertEqual (response .status_code , 401 )
293+ self .assertEqual (response .status_code , HTTPStatus . UNAUTHORIZED )
292294 self .assertEqual (content .get ('detail' , None ), 'Authentication credentials were not provided.' )
293295
294296 # Verify token counts expected.
@@ -309,7 +311,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
309311 )
310312
311313 # Assert auth request was successful
312- self .assertEqual (status_code , 200 )
314+ self .assertEqual (status_code , HTTPStatus . OK )
313315
314316 # Verify token counts expected.
315317 self .assertEqual (AccessToken .objects .count (), 1 )
@@ -323,14 +325,14 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
323325 # Assert access to userinfo end point?
324326 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_9 .token )
325327 response = client .get ('/v1/connect/userinfo' )
326- self .assertEqual (response .status_code , 200 )
328+ self .assertEqual (response .status_code , HTTPStatus . OK )
327329
328330 # Beneficiary chooses the DENY button choice on consent page
329331 payload ['allow' ] = False
330332
331333 # Perform partial authorization request, with out application getting an access token.
332334 response = self .client .post (reverse ('oauth2_provider:authorize' ), data = payload )
333- self .assertEqual (response .status_code , 302 )
335+ self .assertEqual (response .status_code , HTTPStatus . FOUND )
334336
335337 # Verify token counts expected.
336338 self .assertEqual (AccessToken .objects .count (), 1 )
@@ -346,7 +348,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
346348 # when the allow parameter is false
347349 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_9 .token )
348350 response = client .get ('/v1/connect/userinfo' )
349- self .assertEqual (response .status_code , 200 )
351+ self .assertEqual (response .status_code , HTTPStatus . OK )
350352
351353 # BB2-4270: Remove prior active tokens so tests below are not looking for multiple active tokens
352354 # which is an impossible state
@@ -360,12 +362,12 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
360362 payload ['allow' ] = True
361363
362364 # Perform authorization request
363- token_10 , refresh_token_10 , status_code , response_scopes , access_token_scopes = self . _authorize_and_request_token (
364- payload , application
365+ token_10 , refresh_token_10 , status_code , response_scopes , access_token_scopes = (
366+ self . _authorize_and_request_token ( payload , application )
365367 )
366368
367369 # Assert auth request was successful
368- self .assertEqual (status_code , 200 )
370+ self .assertEqual (status_code , HTTPStatus . OK )
369371
370372 # Verify token counts expected.
371373 self .assertEqual (AccessToken .objects .count (), 1 )
@@ -379,15 +381,15 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
379381 # Assert access to userinfo end point?
380382 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_10 .token )
381383 response = client .get ('/v1/connect/userinfo' )
382- self .assertEqual (response .status_code , 200 )
384+ self .assertEqual (response .status_code , HTTPStatus . OK )
383385
384386 # Application changes choice to require demographic scopes
385387 application .require_demographic_scopes = False
386388 application .save ()
387389
388390 # Perform partial authorization request, with out application getting an access token.
389391 response = self .client .post (reverse ('oauth2_provider:authorize' ), data = payload )
390- self .assertEqual (response .status_code , 302 )
392+ self .assertEqual (response .status_code , HTTPStatus . FOUND )
391393
392394 # Verify token counts expected.
393395 self .assertEqual (AccessToken .objects .count (), 0 )
@@ -400,7 +402,7 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
400402
401403 # Perform partial authorization request, with out application getting an access token.
402404 response = self .client .post (reverse ('oauth2_provider:authorize' ), data = payload )
403- self .assertEqual (response .status_code , 302 )
405+ self .assertEqual (response .status_code , HTTPStatus . FOUND )
404406
405407 # Verify token counts expected.
406408 self .assertEqual (AccessToken .objects .count (), 0 )
@@ -414,5 +416,5 @@ def test_bene_demo_scopes_change(self, mock_get_and_update):
414416 # Assert access to userinfo end point?
415417 client .credentials (HTTP_AUTHORIZATION = 'Bearer ' + token_10 .token )
416418 response = client .get ('/v1/connect/userinfo' )
417- self .assertEqual (response .status_code , 401 )
419+ self .assertEqual (response .status_code , HTTPStatus . UNAUTHORIZED )
418420 self .assertEqual (content .get ('detail' , None ), 'Authentication credentials were not provided.' )
0 commit comments