Skip to content

Commit 8ca8dea

Browse files
author
Aristobulo Meneses
committed
Test all custom models, as Application, Grant, AccessToken and RefreshToken are now swappable.
1 parent 037ed33 commit 8ca8dea

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

tests/test_models.py

Lines changed: 62 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", "[email protected]", "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.
@@ -140,6 +145,60 @@ def test_related_objects(self):
140145
self.assertNotIn('oauth2_provider:application', related_object_names)
141146
self.assertIn("tests_sampleapplication", related_object_names)
142147

148+
def test_custom_access_token_model(self):
149+
"""
150+
If a custom access token model is installed, it should be present in
151+
the related objects and not the swapped out one.
152+
"""
153+
# Django internals caches the related objects.
154+
if django.VERSION < (1, 8):
155+
del UserModel._meta._related_objects_cache
156+
if django.VERSION < (1, 10):
157+
related_object_names = [ro.name for ro in UserModel._meta.get_all_related_objects()]
158+
else:
159+
related_object_names = [
160+
f.name for f in UserModel._meta.get_fields()
161+
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
162+
]
163+
self.assertNotIn('oauth2_provider:access_token', related_object_names)
164+
self.assertIn("tests_sampleaccesstoken", related_object_names)
165+
166+
def test_custom_refresh_token_model(self):
167+
"""
168+
If a custom refresh token model is installed, it should be present in
169+
the related objects and not the swapped out one.
170+
"""
171+
# Django internals caches the related objects.
172+
if django.VERSION < (1, 8):
173+
del UserModel._meta._related_objects_cache
174+
if django.VERSION < (1, 10):
175+
related_object_names = [ro.name for ro in UserModel._meta.get_all_related_objects()]
176+
else:
177+
related_object_names = [
178+
f.name for f in UserModel._meta.get_fields()
179+
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
180+
]
181+
self.assertNotIn('oauth2_provider:refresh_token', related_object_names)
182+
self.assertIn("tests_samplerefreshtoken", related_object_names)
183+
184+
def test_custom_grant_model(self):
185+
"""
186+
If a custom grant model is installed, it should be present in
187+
the related objects and not the swapped out one.
188+
"""
189+
# Django internals caches the related objects.
190+
if django.VERSION < (1, 8):
191+
del UserModel._meta._related_objects_cache
192+
if django.VERSION < (1, 10):
193+
related_object_names = [ro.name for ro in UserModel._meta.get_all_related_objects()]
194+
else:
195+
related_object_names = [
196+
f.name for f in UserModel._meta.get_fields()
197+
if (f.one_to_many or f.one_to_one) and f.auto_created and not f.concrete
198+
]
199+
self.assertNotIn('oauth2_provider:grant', related_object_names)
200+
self.assertIn("tests_samplegrant", related_object_names)
201+
143202

144203
class TestGrantModel(TestCase):
145204

0 commit comments

Comments
 (0)