1
+ import importlib
1
2
from io import StringIO
2
3
3
4
import pytest
4
5
from django .contrib .admin import site
5
6
from django .contrib .auth .models import User
7
+ from django .core .exceptions import ImproperlyConfigured
6
8
from django .core .management import CommandError , call_command
7
- from django .test import TestCase
9
+ from django .db import models
10
+ from django .test import TestCase , modify_settings
8
11
9
12
from rest_framework .authtoken .admin import TokenAdmin
10
13
from rest_framework .authtoken .management .commands .drf_create_token import \
@@ -21,6 +24,31 @@ def setUp(self):
21
24
self .user = User .objects .create_user (username = 'test_user' )
22
25
self .token = Token .objects .create (key = 'test token' , user = self .user )
23
26
27
+ def test_authtoken_can_be_imported_when_not_installed (self ):
28
+ try :
29
+ import rest_framework .authtoken .models
30
+ authtoken_models = rest_framework .authtoken .models
31
+ assert issubclass (authtoken_models .Token , models .Model )
32
+ assert issubclass (authtoken_models .TokenProxy , models .Model )
33
+ assert not authtoken_models .Token ._meta .abstract
34
+ assert authtoken_models .TokenProxy ._meta .proxy
35
+
36
+ with modify_settings (INSTALLED_APPS = {
37
+ 'remove' : 'rest_framework.authtoken' }):
38
+ importlib .reload (rest_framework .authtoken .models )
39
+ authtoken_models = rest_framework .authtoken .models
40
+ assert issubclass (authtoken_models .Token , models .Model )
41
+ assert authtoken_models .Token ._meta .abstract
42
+ with pytest .raises (ImproperlyConfigured ):
43
+ authtoken_models .TokenProxy ()
44
+ with pytest .raises (ImproperlyConfigured ):
45
+ authtoken_models .TokenProxy .objects
46
+
47
+ finally :
48
+ # Set the proxy and abstract properties back to the version,
49
+ # where authtoken is among INSTALLED_APPS.
50
+ importlib .reload (rest_framework .authtoken .models )
51
+
24
52
def test_model_admin_displayed_fields (self ):
25
53
mock_request = object ()
26
54
token_admin = TokenAdmin (self .token , self .site )
0 commit comments