9
9
from django .http import QueryDict
10
10
from django .test import TestCase
11
11
from django .utils .html import escape
12
+ from mock import patch
12
13
13
14
from provider import constants , scope
14
15
from provider .oauth2 .backends import AccessTokenBackend , BasicClientBackend , RequestParamsClientBackend
@@ -166,8 +167,8 @@ def test_token_authorization_redirects_to_correct_uri(self):
166
167
self .assertEqual (url , self .get_client ().redirect_uri )
167
168
self .assertTrue ('access_token' in urlparse .parse_qs (fragment ))
168
169
170
+ @patch ('provider.constants.SINGLE_ACCESS_TOKEN' , True )
169
171
def test_token_ignores_expired_tokens (self ):
170
- constants .SINGLE_ACCESS_TOKEN = True
171
172
AccessToken .objects .create (
172
173
user = self .get_user (),
173
174
client = self .get_client (),
@@ -179,11 +180,9 @@ def test_token_ignores_expired_tokens(self):
179
180
self .client .post (self .auth_url2 (), data = {'authorize' : 'Authorize' })
180
181
181
182
self .assertEqual (AccessToken .objects .count (), 2 )
182
- constants .SINGLE_ACCESS_TOKEN = False
183
183
184
+ @patch ('provider.constants.SINGLE_ACCESS_TOKEN' , True )
184
185
def test_token_doesnt_return_tokens_from_another_client (self ):
185
- constants .SINGLE_ACCESS_TOKEN = True
186
-
187
186
# Different client than we'll be submitting an RPC for.
188
187
AccessToken .objects .create (
189
188
user = self .get_user (),
@@ -195,10 +194,9 @@ def test_token_doesnt_return_tokens_from_another_client(self):
195
194
self .client .post (self .auth_url2 (), data = {'authorize' : 'Authorize' })
196
195
197
196
self .assertEqual (AccessToken .objects .count (), 2 )
198
- constants .SINGLE_ACCESS_TOKEN = False
199
197
198
+ @patch ('provider.constants.SINGLE_ACCESS_TOKEN' , True )
200
199
def test_token_authorization_respects_single_access_token_constant (self ):
201
- constants .SINGLE_ACCESS_TOKEN = True
202
200
self .login ()
203
201
self .client .get (self .auth_url (), data = self .get_auth_params (response_type = "token" ))
204
202
self .client .post (self .auth_url2 (), data = {'authorize' : 'Authorize' })
@@ -210,10 +208,9 @@ def test_token_authorization_respects_single_access_token_constant(self):
210
208
self .client .post (self .auth_url2 (), data = {'authorize' : 'Authorize' })
211
209
212
210
self .assertEqual (AccessToken .objects .count (), 1 )
213
- constants .SINGLE_ACCESS_TOKEN = False
214
211
212
+ @patch ('provider.constants.SINGLE_ACCESS_TOKEN' , False )
215
213
def test_token_authorization_can_do_multi_access_tokens (self ):
216
- constants .SINGLE_ACCESS_TOKEN = False
217
214
self .login ()
218
215
self .client .get (self .auth_url (), data = self .get_auth_params (response_type = "token" ))
219
216
self .client .post (self .auth_url2 (), data = {'authorize' : 'Authorize' })
@@ -226,8 +223,8 @@ def test_token_authorization_can_do_multi_access_tokens(self):
226
223
227
224
self .assertEqual (AccessToken .objects .count (), 2 )
228
225
226
+ @patch ('provider.constants.SINGLE_ACCESS_TOKEN' , False )
229
227
def test_token_authorization_cancellation (self ):
230
- constants .SINGLE_ACCESS_TOKEN = False
231
228
self .login ()
232
229
self .client .get (self .auth_url (), data = self .get_auth_params (response_type = "token" ))
233
230
self .client .post (self .auth_url2 ())
@@ -436,19 +433,14 @@ def test_fetching_access_token_with_invalid_grant_type(self):
436
433
self .assertEqual (400 , response .status_code )
437
434
self .assertEqual ('unsupported_grant_type' , json .loads (response .content )['error' ], response .content )
438
435
436
+ @patch ('provider.constants.SINGLE_ACCESS_TOKEN' , True )
439
437
def test_fetching_single_access_token (self ):
440
- constants .SINGLE_ACCESS_TOKEN = True
441
-
442
438
result1 = self ._login_authorize_get_token ()
443
439
result2 = self ._login_authorize_get_token ()
444
440
445
441
self .assertEqual (result1 ['access_token' ], result2 ['access_token' ])
446
442
447
- constants .SINGLE_ACCESS_TOKEN = False
448
-
449
443
def test_fetching_single_access_token_after_refresh (self ):
450
- constants .SINGLE_ACCESS_TOKEN = True
451
-
452
444
token = self ._login_authorize_get_token ()
453
445
454
446
self .client .post (self .access_token_url (), {
@@ -461,8 +453,6 @@ def test_fetching_single_access_token_after_refresh(self):
461
453
new_token = self ._login_authorize_get_token ()
462
454
self .assertNotEqual (token ['access_token' ], new_token ['access_token' ])
463
455
464
- constants .SINGLE_ACCESS_TOKEN = False
465
-
466
456
def test_fetching_access_token_multiple_times (self ):
467
457
self ._login_authorize_get_token ()
468
458
code = self .get_grant ().code
@@ -534,7 +524,7 @@ def test_password_grant_public(self):
534
524
535
525
def test_password_grant_confidential (self ):
536
526
c = self .get_client ()
537
- c .client_type = 0 # confidential
527
+ c .client_type = constants . CONFIDENTIAL
538
528
c .save ()
539
529
540
530
response = self .client .post (self .access_token_url (), {
0 commit comments