Skip to content

Commit 481ae69

Browse files
committed
Add migration for CustomToken test model.
Move authentication tests to sub-app to enable this.
1 parent cb4cbb6 commit 481ae69

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

tests/authentication/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.conf import settings
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='CustomToken',
19+
fields=[
20+
('key', models.CharField(max_length=40, primary_key=True, serialize=False)),
21+
('user', models.OneToOneField(on_delete=models.CASCADE, to=settings.AUTH_USER_MODEL)),
22+
],
23+
),
24+
]

tests/authentication/migrations/__init__.py

Whitespace-only changes.

tests/authentication/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding: utf-8
2+
from __future__ import unicode_literals
3+
4+
from django.conf import settings
5+
from django.db import models
6+
7+
8+
class CustomToken(models.Model):
9+
key = models.CharField(max_length=40, primary_key=True)
10+
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

tests/test_authentication.py renamed to tests/authentication/test_authentication.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from django.conf import settings
99
from django.conf.urls import include, url
1010
from django.contrib.auth.models import User
11-
from django.db import models
1211
from django.http import HttpResponse
1312
from django.test import TestCase, override_settings
1413
from django.utils import six
@@ -26,12 +25,9 @@
2625
from rest_framework.test import APIClient, APIRequestFactory
2726
from rest_framework.views import APIView
2827

29-
factory = APIRequestFactory()
30-
28+
from .models import CustomToken
3129

32-
class CustomToken(models.Model):
33-
key = models.CharField(max_length=40, primary_key=True)
34-
user = models.OneToOneField(User, on_delete=models.CASCADE)
30+
factory = APIRequestFactory()
3531

3632

3733
class CustomTokenAuthentication(TokenAuthentication):
@@ -87,7 +83,7 @@ def put(self, request):
8783
]
8884

8985

90-
@override_settings(ROOT_URLCONF='tests.test_authentication')
86+
@override_settings(ROOT_URLCONF=__name__)
9187
class BasicAuthTests(TestCase):
9288
"""Basic authentication"""
9389
def setUp(self):
@@ -169,7 +165,7 @@ def test_fail_post_if_credentials_contain_spaces(self):
169165
assert response.status_code == status.HTTP_401_UNAUTHORIZED
170166

171167

172-
@override_settings(ROOT_URLCONF='tests.test_authentication')
168+
@override_settings(ROOT_URLCONF=__name__)
173169
class SessionAuthTests(TestCase):
174170
"""User session authentication"""
175171
def setUp(self):
@@ -370,7 +366,7 @@ def test_post_json_failing_token_auth(self):
370366
assert response.status_code == status.HTTP_401_UNAUTHORIZED
371367

372368

373-
@override_settings(ROOT_URLCONF='tests.test_authentication')
369+
@override_settings(ROOT_URLCONF=__name__)
374370
class TokenAuthTests(BaseTokenAuthTests, TestCase):
375371
model = Token
376372
path = '/token/'
@@ -429,13 +425,13 @@ def test_token_login_form(self):
429425
assert response.data['token'] == self.key
430426

431427

432-
@override_settings(ROOT_URLCONF='tests.test_authentication')
428+
@override_settings(ROOT_URLCONF=__name__)
433429
class CustomTokenAuthTests(BaseTokenAuthTests, TestCase):
434430
model = CustomToken
435431
path = '/customtoken/'
436432

437433

438-
@override_settings(ROOT_URLCONF='tests.test_authentication')
434+
@override_settings(ROOT_URLCONF=__name__)
439435
class CustomKeywordTokenAuthTests(BaseTokenAuthTests, TestCase):
440436
model = Token
441437
path = '/customkeywordtoken/'
@@ -549,7 +545,7 @@ class MockUser(object):
549545
authentication.authenticate = old_authenticate
550546

551547

552-
@override_settings(ROOT_URLCONF='tests.test_authentication',
548+
@override_settings(ROOT_URLCONF=__name__,
553549
AUTHENTICATION_BACKENDS=('django.contrib.auth.backends.RemoteUserBackend',))
554550
class RemoteUserAuthenticationUnitTests(TestCase):
555551
def setUp(self):

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def pytest_configure(config):
5656
'django.contrib.staticfiles',
5757
'rest_framework',
5858
'rest_framework.authtoken',
59+
'tests.authentication',
5960
'tests.importable',
6061
'tests',
6162
),

0 commit comments

Comments
 (0)