Skip to content

Commit a9e1ee8

Browse files
Aristobulo Menesesjleclanche
authored andcommitted
Test all custom models
Application, Grant, AccessToken and RefreshToken are now swappable.
1 parent e4035de commit a9e1ee8

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

tests/test_models.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,17 @@ def test_scopes_property(self):
115115
self.assertEqual(access_token2.scopes, {'write': 'Writing scope'})
116116

117117

118-
@override_settings(OAUTH2_PROVIDER_APPLICATION_MODEL="tests.SampleApplication")
119-
class TestCustomApplicationModel(TestCase):
118+
@override_settings(
119+
OAUTH2_PROVIDER_APPLICATION_MODEL="tests.SampleApplication",
120+
OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL="tests.SampleAccessToken",
121+
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL="tests.SampleRefreshToken",
122+
OAUTH2_PROVIDER_GRANT_MODEL="tests.SampleGrant"
123+
)
124+
class TestCustomModels(TestCase):
120125
def setUp(self):
121126
self.user = UserModel.objects.create_user("test_user", "test@user.com", "123456")
122127

123-
def test_related_objects(self):
128+
def test_custom_application_model(self):
124129
"""
125130
If a custom application model is installed, it should be present in
126131
the related objects and not the swapped out one.
@@ -134,6 +139,45 @@ def test_related_objects(self):
134139
self.assertNotIn('oauth2_provider:application', related_object_names)
135140
self.assertIn("tests_sampleapplication", related_object_names)
136141

142+
def test_custom_access_token_model(self):
143+
"""
144+
If a custom access token model is installed, it should be present in
145+
the related objects and not the swapped out one.
146+
"""
147+
# Django internals caches the related objects.
148+
related_object_names = [
149+
f.name for f in UserModel._meta.get_fields()
150+
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
151+
]
152+
self.assertNotIn('oauth2_provider:access_token', related_object_names)
153+
self.assertIn("tests_sampleaccesstoken", related_object_names)
154+
155+
def test_custom_refresh_token_model(self):
156+
"""
157+
If a custom refresh token model is installed, it should be present in
158+
the related objects and not the swapped out one.
159+
"""
160+
# Django internals caches the related objects.
161+
related_object_names = [
162+
f.name for f in UserModel._meta.get_fields()
163+
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
164+
]
165+
self.assertNotIn('oauth2_provider:refresh_token', related_object_names)
166+
self.assertIn("tests_samplerefreshtoken", related_object_names)
167+
168+
def test_custom_grant_model(self):
169+
"""
170+
If a custom grant model is installed, it should be present in
171+
the related objects and not the swapped out one.
172+
"""
173+
# Django internals caches the related objects.
174+
related_object_names = [
175+
f.name for f in UserModel._meta.get_fields()
176+
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
177+
]
178+
self.assertNotIn('oauth2_provider:grant', related_object_names)
179+
self.assertIn("tests_samplegrant", related_object_names)
180+
137181

138182
class TestGrantModel(TestCase):
139183

0 commit comments

Comments
 (0)