22
22
UserModel = get_user_model ()
23
23
24
24
25
- class TestModels (TestCase ):
25
+ class BaseTestModels (TestCase ):
26
26
def setUp (self ):
27
27
self .
user = UserModel .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
28
28
29
+ def tearDown (self ):
30
+ self .user .delete ()
31
+
32
+
33
+ class TestModels (BaseTestModels ):
29
34
def test_allow_scopes (self ):
30
35
self .client .login (username = "test_user" , password = "123456" )
31
36
app = Application .objects .create (
@@ -103,10 +108,7 @@ def test_scopes_property(self):
103
108
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = "tests.SampleRefreshToken" ,
104
109
OAUTH2_PROVIDER_GRANT_MODEL = "tests.SampleGrant" ,
105
110
)
106
- class TestCustomModels (TestCase ):
107
- def setUp (self ):
108
- self .
user = UserModel .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
109
-
111
+ class TestCustomModels (BaseTestModels ):
110
112
def test_custom_application_model (self ):
111
113
"""
112
114
If a custom application model is installed, it should be present in
@@ -237,7 +239,21 @@ def test_custom_grant_model_not_installed(self):
237
239
oauth2_settings .GRANT_MODEL = "oauth2_provider.Grant"
238
240
239
241
240
- class TestGrantModel (TestCase ):
242
+ class TestGrantModel (BaseTestModels ):
243
+ def setUp (self ):
244
+ super ().setUp ()
245
+ self .application = Application .objects .create (
246
+ name = "Test Application" ,
247
+ redirect_uris = "" ,
248
+ user = self .user ,
249
+ client_type = Application .CLIENT_CONFIDENTIAL ,
250
+ authorization_grant_type = Application .GRANT_AUTHORIZATION_CODE ,
251
+ )
252
+
253
+ def tearDown (self ):
254
+ self .application .delete ()
255
+ super ().tearDown ()
256
+
241
257
def test_str (self ):
242
258
grant = Grant (code = "test_code" )
243
259
self .assertEqual ("%s" % grant , grant .code )
@@ -247,11 +263,26 @@ def test_expires_can_be_none(self):
247
263
self .assertIsNone (grant .expires )
248
264
self .assertTrue (grant .is_expired ())
249
265
266
+ def test_redirect_uri_can_be_longer_than_255_chars (self ):
267
+ long_redirect_uri = "http://example.com/{}" .format ("authorized/" * 25 )
268
+ self .assertTrue (len (long_redirect_uri ) > 255 )
269
+ grant = Grant .objects .create (
270
+ user = self .user ,
271
+ code = "test_code" ,
272
+ application = self .application ,
273
+ expires = timezone .now (),
274
+ redirect_uri = long_redirect_uri ,
275
+ scope = "" ,
276
+ )
277
+ grant .refresh_from_db ()
278
+
279
+ # It would be necessary to run test using another DB engine than sqlite
280
+ # that transform varchar(255) into text data type.
281
+ # https://sqlite.org/datatype3.html#affinity_name_examples
282
+ self .assertEqual (grant .redirect_uri , long_redirect_uri )
250
283
251
- class TestAccessTokenModel (TestCase ):
252
- def setUp (self ):
253
- self .
user = UserModel .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
254
284
285
+ class TestAccessTokenModel (BaseTestModels ):
255
286
def test_str (self ):
256
287
access_token = AccessToken (token = "test_token" )
257
288
self .assertEqual ("%s" % access_token , access_token .token )
@@ -273,15 +304,15 @@ def test_expires_can_be_none(self):
273
304
self .assertTrue (access_token .is_expired ())
274
305
275
306
276
- class TestRefreshTokenModel (TestCase ):
307
+ class TestRefreshTokenModel (BaseTestModels ):
277
308
def test_str (self ):
278
309
refresh_token = RefreshToken (token = "test_token" )
279
310
self .assertEqual ("%s" % refresh_token , refresh_token .token )
280
311
281
312
282
- class TestClearExpired (TestCase ):
313
+ class TestClearExpired (BaseTestModels ):
283
314
def setUp (self ):
284
- self . user = UserModel . objects . create_user ( "test_user" , "[email protected] " , "123456" )
315
+ super (). setUp ( )
285
316
# Insert two tokens on database.
286
317
app = Application .objects .create (
287
318
name = "test_app" ,
0 commit comments