Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions oauth2_provider/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def __str__(self):
class Application(AbstractApplication):
pass

# Add swappable like this to not break django 1.4 compatibility
Application._meta.swappable = 'OAUTH2_PROVIDER_APPLICATION_MODEL'


@python_2_unicode_compatible
class Grant(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion oauth2_provider/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'WRITE_SCOPE': 'write',
'AUTHORIZATION_CODE_EXPIRE_SECONDS': 60,
'ACCESS_TOKEN_EXPIRE_SECONDS': 36000,
'APPLICATION_MODEL': 'oauth2_provider.Application',
'APPLICATION_MODEL': getattr(settings, 'OAUTH2_PROVIDER_APPLICATION_MODEL', 'oauth2_provider.Application'),

# Special settings that will be evaluated at runtime
'_SCOPES': [],
Expand Down
6 changes: 6 additions & 0 deletions oauth2_provider/tests/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.db import models
from oauth2_provider.models import AbstractApplication


class TestApplication(AbstractApplication):
custom_field = models.CharField(max_length=255)
1 change: 1 addition & 0 deletions oauth2_provider/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'django.contrib.admin',

'oauth2_provider',
'oauth2_provider.tests',
)

LOGGING = {
Expand Down
26 changes: 26 additions & 0 deletions oauth2_provider/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from __future__ import unicode_literals

try:
from unittest import skipIf
except ImportError:
from django.utils.unittest.case import skipIf

import django
from django.test import TestCase
from django.test.utils import override_settings
from django.core.exceptions import ValidationError

from ..models import AccessToken, get_application_model
Expand Down Expand Up @@ -60,3 +67,22 @@ def test_grant_implicit_redirect_uris(self):
)

self.assertRaises(ValidationError, app.full_clean)

@skipIf(django.VERSION < (1, 5), "Behavior is broken on 1.4 and there is no solution")
@override_settings(OAUTH2_PROVIDER_APPLICATION_MODEL='tests.TestApplication')
class TestCustomApplicationModel(TestCase):
def setUp(self):
self.user = UserModel.objects.create_user("test_user", "[email protected]", "123456")

def test_related_objects(self):
"""
If a custom application model is installed, it should be present in
the related objects and not the swapped out one.

See issue #90 (https://github.com/evonove/django-oauth-toolkit/issues/90)
"""
# Django internals caches the related objects.
del UserModel._meta._related_objects_cache
related_object_names = [ro.name for ro in UserModel._meta.get_all_related_objects()]
self.assertNotIn('oauth2_provider:application', related_object_names)
self.assertIn('tests:testapplication', related_object_names)