1- from datetime import datetime as dt
2-
31import pytest
42from django .contrib .auth import get_user_model
53from django .core .exceptions import ImproperlyConfigured , ValidationError
86from django .utils import timezone
97
108from oauth2_provider .models import (
11- clear_expired ,
12- get_access_token_model ,
13- get_application_model ,
14- get_grant_model ,
15- get_refresh_token_model ,
16- get_id_token_model ,
9+ clear_expired , get_access_token_model , get_application_model ,
10+ get_grant_model , get_refresh_token_model
1711)
1812from oauth2_provider .settings import oauth2_settings
1913
20- from .models import SampleRefreshToken
2114
2215Application = get_application_model ()
2316Grant = get_grant_model ()
2417AccessToken = get_access_token_model ()
2518RefreshToken = get_refresh_token_model ()
2619UserModel = get_user_model ()
27- IDToken = get_id_token_model ()
2820
2921
3022class TestModels (TestCase ):
23+
3124 def setUp (self ):
32- self .user = UserModel .objects .create_user (
33- "test_user" ,
"[email protected] " ,
"123456" 34- )
25+ self .
user = UserModel .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
3526
3627 def test_allow_scopes (self ):
3728 self .client .login (username = "test_user" , password = "123456" )
@@ -44,7 +35,11 @@ def test_allow_scopes(self):
4435 )
4536
4637 access_token = AccessToken (
47- user = self .user , scope = "read write" , expires = 0 , token = "" , application = app
38+ user = self .user ,
39+ scope = "read write" ,
40+ expires = 0 ,
41+ token = "" ,
42+ application = app
4843 )
4944
5045 self .assertTrue (access_token .allow_scopes (["read" , "write" ]))
@@ -99,30 +94,35 @@ def test_scopes_property(self):
9994 )
10095
10196 access_token = AccessToken (
102- user = self .user , scope = "read write" , expires = 0 , token = "" , application = app
97+ user = self .user ,
98+ scope = "read write" ,
99+ expires = 0 ,
100+ token = "" ,
101+ application = app
103102 )
104103
105104 access_token2 = AccessToken (
106- user = self .user , scope = "write" , expires = 0 , token = "" , application = app
105+ user = self .user ,
106+ scope = "write" ,
107+ expires = 0 ,
108+ token = "" ,
109+ application = app
107110 )
108111
109- self .assertEqual (
110- access_token .scopes , {"read" : "Reading scope" , "write" : "Writing scope" }
111- )
112+ self .assertEqual (access_token .scopes , {"read" : "Reading scope" , "write" : "Writing scope" })
112113 self .assertEqual (access_token2 .scopes , {"write" : "Writing scope" })
113114
114115
115116@override_settings (
116117 OAUTH2_PROVIDER_APPLICATION_MODEL = "tests.SampleApplication" ,
117118 OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL = "tests.SampleAccessToken" ,
118119 OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = "tests.SampleRefreshToken" ,
119- OAUTH2_PROVIDER_GRANT_MODEL = "tests.SampleGrant" ,
120+ OAUTH2_PROVIDER_GRANT_MODEL = "tests.SampleGrant"
120121)
121122class TestCustomModels (TestCase ):
123+
122124 def setUp (self ):
123- self .user = UserModel .objects .create_user (
124- "test_user" ,
"[email protected] " ,
"123456" 125- )
125+ self .
user = UserModel .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
126126
127127 def test_custom_application_model (self ):
128128 """
@@ -132,8 +132,7 @@ def test_custom_application_model(self):
132132 See issue #90 (https://github.com/jazzband/django-oauth-toolkit/issues/90)
133133 """
134134 related_object_names = [
135- f .name
136- for f in UserModel ._meta .get_fields ()
135+ f .name for f in UserModel ._meta .get_fields ()
137136 if (f .one_to_many or f .one_to_one ) and f .auto_created and not f .concrete
138137 ]
139138 self .assertNotIn ("oauth2_provider:application" , related_object_names )
@@ -164,8 +163,7 @@ def test_custom_access_token_model(self):
164163 """
165164 # Django internals caches the related objects.
166165 related_object_names = [
167- f .name
168- for f in UserModel ._meta .get_fields ()
166+ f .name for f in UserModel ._meta .get_fields ()
169167 if (f .one_to_many or f .one_to_one ) and f .auto_created and not f .concrete
170168 ]
171169 self .assertNotIn ("oauth2_provider:access_token" , related_object_names )
@@ -196,8 +194,7 @@ def test_custom_refresh_token_model(self):
196194 """
197195 # Django internals caches the related objects.
198196 related_object_names = [
199- f .name
200- for f in UserModel ._meta .get_fields ()
197+ f .name for f in UserModel ._meta .get_fields ()
201198 if (f .one_to_many or f .one_to_one ) and f .auto_created and not f .concrete
202199 ]
203200 self .assertNotIn ("oauth2_provider:refresh_token" , related_object_names )
@@ -228,8 +225,7 @@ def test_custom_grant_model(self):
228225 """
229226 # Django internals caches the related objects.
230227 related_object_names = [
231- f .name
232- for f in UserModel ._meta .get_fields ()
228+ f .name for f in UserModel ._meta .get_fields ()
233229 if (f .one_to_many or f .one_to_one ) and f .auto_created and not f .concrete
234230 ]
235231 self .assertNotIn ("oauth2_provider:grant" , related_object_names )
@@ -255,6 +251,7 @@ def test_custom_grant_model_not_installed(self):
255251
256252
257253class TestGrantModel (TestCase ):
254+
258255 def test_str (self ):
259256 grant = Grant (code = "test_code" )
260257 self .assertEqual ("%s" % grant , grant .code )
@@ -266,10 +263,9 @@ def test_expires_can_be_none(self):
266263
267264
268265class TestAccessTokenModel (TestCase ):
266+
269267 def setUp (self ):
270- self .user = UserModel .objects .create_user (
271- "test_user" ,
"[email protected] " ,
"123456" 272- )
268+ self .
user = UserModel .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
273269
274270 def test_str (self ):
275271 access_token = AccessToken (token = "test_token" )
@@ -283,9 +279,7 @@ def test_user_can_be_none(self):
283279 client_type = Application .CLIENT_CONFIDENTIAL ,
284280 authorization_grant_type = Application .GRANT_AUTHORIZATION_CODE ,
285281 )
286- access_token = AccessToken .objects .create (
287- token = "test_token" , application = app , expires = timezone .now ()
288- )
282+ access_token = AccessToken .objects .create (token = "test_token" , application = app , expires = timezone .now ())
289283 self .assertIsNone (access_token .user )
290284
291285 def test_expires_can_be_none (self ):
@@ -295,58 +289,16 @@ def test_expires_can_be_none(self):
295289
296290
297291class TestRefreshTokenModel (TestCase ):
292+
298293 def test_str (self ):
299294 refresh_token = RefreshToken (token = "test_token" )
300295 self .assertEqual ("%s" % refresh_token , refresh_token .token )
301296
302297
303298class TestClearExpired (TestCase ):
299+
304300 def setUp (self ):
305- self .user = UserModel .objects .create_user (
306- "test_user" ,
"[email protected] " ,
"123456" 307- )
308- app1 = Application .objects .create (
309- name = "Test Application" ,
310- redirect_uris = (
311- "http://localhost http://example.com http://example.org custom-scheme://example.com"
312- ),
313- user = self .user ,
314- client_type = Application .CLIENT_CONFIDENTIAL ,
315- authorization_grant_type = Application .GRANT_AUTHORIZATION_CODE ,
316- )
317- app2 = Application .objects .create (
318- name = "Test Application" ,
319- redirect_uris = (
320- "http://localhost http://example.com http://example.org custom-scheme://example.com"
321- ),
322- user = self .user ,
323- client_type = Application .CLIENT_CONFIDENTIAL ,
324- authorization_grant_type = Application .GRANT_AUTHORIZATION_CODE ,
325- )
326- id1 = IDToken .objects .create (
327- token = "666" ,
328- expires = dt .now (),
329- scope = 2 ,
330- application = app1 ,
331- user = self .user ,
332- created = dt .now (),
333- updated = dt .now (),
334- )
335- id2 = IDToken .objects .create (
336- token = "999" ,
337- expires = dt .now (),
338- scope = 2 ,
339- application = app2 ,
340- user = self .user ,
341- created = dt .now (),
342- updated = dt .now (),
343- )
344- refresh_token1 = SampleRefreshToken .objects .create (
345- token = "test_token" , application = app1 , user = self .user ,
346- )
347- refresh_token2 = SampleRefreshToken .objects .create (
348- token = "test_token2" , application = app2 , user = self .user ,
349- )
301+ self .
user = UserModel .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
350302 # Insert two tokens on database.
351303 app = Application .objects .create (
352304 name = "test_app" ,
@@ -359,24 +311,20 @@ def setUp(self):
359311 token = "555" ,
360312 expires = timezone .now (),
361313 scope = 2 ,
362- application = app1 ,
363- id_token = id1 ,
314+ application = app ,
364315 user = self .user ,
365- created = dt .now (),
366- updated = dt .now (),
367- refresh_token = refresh_token1 ,
368- )
316+ created = timezone .now (),
317+ updated = timezone .now (),
318+ )
369319 AccessToken .objects .create (
370320 token = "666" ,
371321 expires = timezone .now (),
372322 scope = 2 ,
373- application = app2 ,
323+ application = app ,
374324 user = self .user ,
375- id_token = id2 ,
376- created = dt .now (),
377- updated = dt .now (),
378- refresh_token = refresh_token2 ,
379- )
325+ created = timezone .now (),
326+ updated = timezone .now (),
327+ )
380328
381329 def test_clear_expired_tokens (self ):
382330 oauth2_settings .REFRESH_TOKEN_EXPIRE_SECONDS = 60
0 commit comments