From 481ae69df3b59d628689b03bbf18c6002d11a073 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Thu, 14 Feb 2019 09:30:53 +0100 Subject: [PATCH] Add migration for CustomToken test model. Move authentication tests to sub-app to enable this. --- tests/authentication/__init__.py | 0 .../authentication/migrations/0001_initial.py | 24 +++++++++++++++++++ tests/authentication/migrations/__init__.py | 0 tests/authentication/models.py | 10 ++++++++ .../test_authentication.py | 20 +++++++--------- tests/conftest.py | 1 + 6 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 tests/authentication/__init__.py create mode 100644 tests/authentication/migrations/0001_initial.py create mode 100644 tests/authentication/migrations/__init__.py create mode 100644 tests/authentication/models.py rename tests/{ => authentication}/test_authentication.py (97%) diff --git a/tests/authentication/__init__.py b/tests/authentication/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/authentication/migrations/0001_initial.py b/tests/authentication/migrations/0001_initial.py new file mode 100644 index 000000000..cfc887240 --- /dev/null +++ b/tests/authentication/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='CustomToken', + fields=[ + ('key', models.CharField(max_length=40, primary_key=True, serialize=False)), + ('user', models.OneToOneField(on_delete=models.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/tests/authentication/migrations/__init__.py b/tests/authentication/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/authentication/models.py b/tests/authentication/models.py new file mode 100644 index 000000000..b8d1fd5a6 --- /dev/null +++ b/tests/authentication/models.py @@ -0,0 +1,10 @@ +# coding: utf-8 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import models + + +class CustomToken(models.Model): + key = models.CharField(max_length=40, primary_key=True) + user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) diff --git a/tests/test_authentication.py b/tests/authentication/test_authentication.py similarity index 97% rename from tests/test_authentication.py rename to tests/authentication/test_authentication.py index f2714acb5..793773542 100644 --- a/tests/test_authentication.py +++ b/tests/authentication/test_authentication.py @@ -8,7 +8,6 @@ import pytest from django.conf import settings from django.conf.urls import include, url from django.contrib.auth.models import User -from django.db import models from django.http import HttpResponse from django.test import TestCase, override_settings from django.utils import six @@ -26,14 +25,11 @@ from rest_framework.response import Response from rest_framework.test import APIClient, APIRequestFactory from rest_framework.views import APIView +from .models import CustomToken + factory = APIRequestFactory() -class CustomToken(models.Model): - key = models.CharField(max_length=40, primary_key=True) - user = models.OneToOneField(User, on_delete=models.CASCADE) - - class CustomTokenAuthentication(TokenAuthentication): model = CustomToken @@ -87,7 +83,7 @@ urlpatterns = [ ] -@override_settings(ROOT_URLCONF='tests.test_authentication') +@override_settings(ROOT_URLCONF=__name__) class BasicAuthTests(TestCase): """Basic authentication""" def setUp(self): @@ -169,7 +165,7 @@ class BasicAuthTests(TestCase): assert response.status_code == status.HTTP_401_UNAUTHORIZED -@override_settings(ROOT_URLCONF='tests.test_authentication') +@override_settings(ROOT_URLCONF=__name__) class SessionAuthTests(TestCase): """User session authentication""" def setUp(self): @@ -370,7 +366,7 @@ class BaseTokenAuthTests(object): assert response.status_code == status.HTTP_401_UNAUTHORIZED -@override_settings(ROOT_URLCONF='tests.test_authentication') +@override_settings(ROOT_URLCONF=__name__) class TokenAuthTests(BaseTokenAuthTests, TestCase): model = Token path = '/token/' @@ -429,13 +425,13 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase): assert response.data['token'] == self.key -@override_settings(ROOT_URLCONF='tests.test_authentication') +@override_settings(ROOT_URLCONF=__name__) class CustomTokenAuthTests(BaseTokenAuthTests, TestCase): model = CustomToken path = '/customtoken/' -@override_settings(ROOT_URLCONF='tests.test_authentication') +@override_settings(ROOT_URLCONF=__name__) class CustomKeywordTokenAuthTests(BaseTokenAuthTests, TestCase): model = Token path = '/customkeywordtoken/' @@ -549,7 +545,7 @@ class BasicAuthenticationUnitTests(TestCase): authentication.authenticate = old_authenticate -@override_settings(ROOT_URLCONF='tests.test_authentication', +@override_settings(ROOT_URLCONF=__name__, AUTHENTICATION_BACKENDS=('django.contrib.auth.backends.RemoteUserBackend',)) class RemoteUserAuthenticationUnitTests(TestCase): def setUp(self): diff --git a/tests/conftest.py b/tests/conftest.py index 27558c02b..1c0c6dda7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -56,6 +56,7 @@ def pytest_configure(config): 'django.contrib.staticfiles', 'rest_framework', 'rest_framework.authtoken', + 'tests.authentication', 'tests.importable', 'tests', ),