From 6a8fc5207f588214bc76b818dea0ead55b1758a7 Mon Sep 17 00:00:00 2001 From: Matt Layman Date: Sun, 28 Aug 2016 14:33:25 -0400 Subject: [PATCH] Move all urls into tests/urls.py. --- tests/test_authentication.py | 39 +------------------------------- tests/test_views.py | 18 +++------------ tests/urls.py | 44 ++++++++++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 57 deletions(-) diff --git a/tests/test_authentication.py b/tests/test_authentication.py index f0ff8f2a..6ad6d27b 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -1,17 +1,7 @@ import unittest -from django.http import HttpResponse from django.test import TestCase -from django.conf.urls import url - -from rest_framework import permissions, status -try: - from rest_framework_oauth.authentication import OAuth2Authentication -except ImportError: - try: - from rest_framework.authentication import OAuth2Authentication - except ImportError: - OAuth2Authentication = None +from rest_framework import status try: try: from rest_framework_oauth.compat import oauth2_provider @@ -30,12 +20,10 @@ oauth2_provider = None from rest_framework.test import APIRequestFactory, APIClient -from rest_framework.views import APIView from rest_framework_jwt import utils from rest_framework_jwt.compat import get_user_model from rest_framework_jwt.settings import api_settings, DEFAULTS -from rest_framework_jwt.authentication import JSONWebTokenAuthentication User = get_user_model() @@ -44,33 +32,8 @@ factory = APIRequestFactory() -class MockView(APIView): - permission_classes = (permissions.IsAuthenticated,) - - def get(self, request): - return HttpResponse({'a': 1, 'b': 2, 'c': 3}) - - def post(self, request): - return HttpResponse({'a': 1, 'b': 2, 'c': 3}) - - -urlpatterns = [ - url(r'^jwt/$', MockView.as_view( - authentication_classes=[JSONWebTokenAuthentication])), - - url(r'^jwt-oauth2/$', MockView.as_view( - authentication_classes=[ - JSONWebTokenAuthentication, OAuth2Authentication])), - - url(r'^oauth2-jwt/$', MockView.as_view( - authentication_classes=[ - OAuth2Authentication, JSONWebTokenAuthentication])), -] - - class JSONWebTokenAuthenticationTests(TestCase): """JSON Web Token Authentication""" - urls = 'tests.test_authentication' def setUp(self): self.csrf_client = APIClient(enforce_csrf_checks=True) diff --git a/tests/test_views.py b/tests/test_views.py index 5ee25110..83b712b5 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -3,37 +3,28 @@ from datetime import datetime, timedelta import time +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric import rsa from django import get_version from django.test import TestCase from django.test.utils import override_settings -from django.conf.urls import url from rest_framework import status from rest_framework.test import APIClient -from rest_framework_jwt import utils, views +from rest_framework_jwt import utils from rest_framework_jwt.compat import get_user_model from rest_framework_jwt.settings import api_settings, DEFAULTS -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives.asymmetric import rsa - from . import utils as test_utils User = get_user_model() NO_CUSTOM_USER_MODEL = 'Custom User Model only supported after Django 1.5' -urlpatterns = [ - url(r'^auth-token/$', views.obtain_jwt_token), - url(r'^auth-token-refresh/$', views.refresh_jwt_token), - url(r'^auth-token-verify/$', views.verify_jwt_token), -] - orig_datetime = datetime class BaseTestCase(TestCase): - urls = 'tests.test_views' def setUp(self): self.email = 'jpueblo@example.com' @@ -67,7 +58,6 @@ def test_jwt_login_custom_response_json(self): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(decoded_payload['username'], self.username) - self.assertEqual(response.data['user'], self.username) def tearDown(self): api_settings.JWT_RESPONSE_PAYLOAD_HANDLER =\ @@ -164,7 +154,6 @@ def test_jwt_login_using_zero(self): @override_settings(AUTH_USER_MODEL='tests.CustomUser') class CustomUserObtainJSONWebTokenTests(TestCase): """JSON Web Token Authentication""" - urls = 'tests.test_views' def setUp(self): from .models import CustomUser @@ -209,7 +198,6 @@ def test_jwt_login_json_bad_creds(self): @override_settings(AUTH_USER_MODEL='tests.CustomUserUUID') class CustomUserUUIDObtainJSONWebTokenTests(TestCase): """JSON Web Token Authentication""" - urls = 'tests.test_views' def setUp(self): from .models import CustomUserUUID diff --git a/tests/urls.py b/tests/urls.py index f5a26177..2d5e6c32 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -1,4 +1,40 @@ -""" -Blank URLConf just to keep the test suite happy -""" -urlpatterns = [] +from django.conf.urls import url +from django.http import HttpResponse +from rest_framework import permissions +from rest_framework.views import APIView +try: + from rest_framework_oauth.authentication import OAuth2Authentication +except ImportError: + try: + from rest_framework.authentication import OAuth2Authentication + except ImportError: + OAuth2Authentication = None + +from rest_framework_jwt import views +from rest_framework_jwt.authentication import JSONWebTokenAuthentication + + +class MockView(APIView): + permission_classes = (permissions.IsAuthenticated,) + + def get(self, request): + return HttpResponse({'a': 1, 'b': 2, 'c': 3}) + + def post(self, request): + return HttpResponse({'a': 1, 'b': 2, 'c': 3}) + + +urlpatterns = [ + url(r'^auth-token/$', views.obtain_jwt_token), + url(r'^auth-token-refresh/$', views.refresh_jwt_token), + url(r'^auth-token-verify/$', views.verify_jwt_token), + + url(r'^jwt/$', MockView.as_view( + authentication_classes=[JSONWebTokenAuthentication])), + url(r'^jwt-oauth2/$', MockView.as_view( + authentication_classes=[ + JSONWebTokenAuthentication, OAuth2Authentication])), + url(r'^oauth2-jwt/$', MockView.as_view( + authentication_classes=[ + OAuth2Authentication, JSONWebTokenAuthentication])), +]