Skip to content

Fix: authtoken.TokenProxy cannot be proxy when not installed #7442 #7571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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: 2 additions & 1 deletion rest_framework/authtoken/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ def pk(self):
return self.user.pk

class Meta:
proxy = True
proxy = 'rest_framework.authtoken' in settings.INSTALLED_APPS
abstract = 'rest_framework.authtoken' not in settings.INSTALLED_APPS
verbose_name = "token"
11 changes: 10 additions & 1 deletion tests/test_authtoken.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import importlib
from io import StringIO

import pytest
from django.contrib.admin import site
from django.contrib.auth.models import User
from django.core.management import CommandError, call_command
from django.test import TestCase
from django.test import TestCase, modify_settings

from rest_framework.authtoken.admin import TokenAdmin
from rest_framework.authtoken.management.commands.drf_create_token import \
Expand All @@ -21,6 +22,14 @@ def setUp(self):
self.user = User.objects.create_user(username='test_user')
self.token = Token.objects.create(key='test token', user=self.user)

def test_authtoken_can_be_imported_when_not_included_in_installed_apps(self):
import rest_framework.authtoken.models
with modify_settings(INSTALLED_APPS={'remove': 'rest_framework.authtoken'}):
importlib.reload(rest_framework.authtoken.models)
# Set the proxy and abstract properties back to the version,
# where authtoken is among INSTALLED_APPS.
importlib.reload(rest_framework.authtoken.models)
Copy link
Contributor

@terencehonles terencehonles Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the solution that's picked, you may want to edit this to check that the two models do exist and that they are indeed abstract instead of just checking that the import works. That way if Django's implementation changes we'd be able to notice the difference and either remove the test or special case it depending on the supported version.

Either way, nice to have a test! I started searching for issues before writing a test and this will help me too 🙂

Comment on lines +29 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably want to put the final reload in a finally: block, that way the imports aren't messed up for a subsequent test. I ran into this issue while adding tests to my PR.


def test_model_admin_displayed_fields(self):
mock_request = object()
token_admin = TokenAdmin(self.token, self.site)
Expand Down