From 374be7a03ff0f0387aeb92672b1fde6693edc6ac Mon Sep 17 00:00:00 2001 From: winkidney Date: Fri, 17 Jul 2020 21:49:17 +0800 Subject: [PATCH] Fix: Fix test failure cased by new django-test client response + Also fix the old middleware + Fix MIDDLEWARE name error --- core/tests/api.py | 20 ++++++++++---------- core/tests/views.py | 5 ++--- django_images/tests.py | 3 ++- pinry/middleware.py | 6 ++---- pinry/settings/base.py | 2 +- users/middleware.py | 8 +++----- users/tests.py | 2 +- users/urls.py | 2 ++ 8 files changed, 23 insertions(+), 25 deletions(-) diff --git a/core/tests/api.py b/core/tests/api.py index 28703ab..6076e9b 100644 --- a/core/tests/api.py +++ b/core/tests/api.py @@ -55,16 +55,16 @@ class BoardPrivacyTests(APITestCase): def test_should_non_owner_and_anonymous_user_has_no_permission_to_list_private_board(self): resp = self.client.get(self.boards_url) - self.assertEqual(len(resp.data), 0, resp.data) + self.assertEqual(len(resp.json()), 0, resp.json()) self.client.login(username=self.non_owner.username, password='password') resp = self.client.get(self.boards_url) - self.assertEqual(len(resp.data), 0, resp.data) + self.assertEqual(len(resp.json()), 0, resp.content) def test_should_owner_has_permission_to_list_private_board(self): self.client.login(username=self.non_owner.username, password='password') resp = self.client.get(self.boards_url) - self.assertEqual(len(resp.data), 0, resp.data) + self.assertEqual(len(resp.json()), 0, resp.content) def test_should_non_owner_and_anonymous_user_has_no_permission_to_view_private_board(self): resp = self.client.get(self.board_url) @@ -106,35 +106,35 @@ class PinPrivacyTests(APITestCase): def test_should_non_owner_and_anonymous_user_has_no_permission_to_list_private_pin(self): resp = self.client.get(reverse("pin-list")) - self.assertEqual(len(resp.data['results']), 0, resp.data) + self.assertEqual(len(resp.json()['results']), 0, resp.content) self.client.login(username=self.non_owner.username, password='password') resp = self.client.get(reverse("pin-list")) - self.assertEqual(len(resp.data['results']), 0, resp.data) + self.assertEqual(len(resp.json()['results']), 0, resp.content) def test_should_non_owner_and_anonymous_user_has_no_permission_to_list_private_pin_in_board(self): resp = self.client.get(self.board_url) - self.assertEqual(len(resp.data['pins_detail']), 0, resp.data) + self.assertEqual(len(resp.json()['pins_detail']), 0, resp.content) self.client.login(username=self.non_owner.username, password='password') resp = self.client.get(self.board_url) - self.assertEqual(len(resp.data['pins_detail']), 0, resp.data) + self.assertEqual(len(resp.json()['pins_detail']), 0, resp.content) def test_should_owner_user_has_permission_to_list_private_pin_in_board(self): self.client.login(username=self.owner.username, password='password') resp = self.client.get(self.board_url) - self.assertEqual(len(resp.data['pins_detail']), 1, resp.data) + self.assertEqual(len(resp.json()['pins_detail']), 1, resp.content) def test_should_owner_user_has_permission_to_list_private_pin(self): self.client.login(username=self.owner.username, password='password') resp = self.client.get(reverse("pin-list")) - self.assertEqual(len(resp.data['results']), 1, resp.data) + self.assertEqual(len(resp.json()['results']), 1, resp.content) def test_should_owner_has_permission_to_view_private_pin(self): self.client.login(username=self.owner.username, password='password') resp = self.client.get(self.private_pin_url) self.assertEqual(resp.status_code, 200) - self.assertEqual(resp.data['id'], self.private_pin.id) + self.assertEqual(resp.json()['id'], self.private_pin.id) def test_should_anonymous_user_has_no_permission_to_view_private_pin(self): resp = self.client.get(self.private_pin_url) diff --git a/core/tests/views.py b/core/tests/views.py index 3489c56..2607423 100644 --- a/core/tests/views.py +++ b/core/tests/views.py @@ -1,8 +1,7 @@ -from django.core.urlresolvers import reverse from django.test import TestCase from core.models import Image -from core.tests import create_user +from core.tests import create_user, reverse from users.models import User @@ -25,7 +24,7 @@ class CreateImageTest(TestCase): self.assertEqual(response.json()['id'], image.pk) def test_post_error(self): - response = self.client.post(reverse('image-list'), {'image': None}) + response = self.client.post(reverse('image-list'), {'image': ''}) self.assertEqual( response.json(), { diff --git a/django_images/tests.py b/django_images/tests.py index 8452577..4f4cbe6 100644 --- a/django_images/tests.py +++ b/django_images/tests.py @@ -1,9 +1,10 @@ +from io import BytesIO + import mock import qrcode from django.test import TestCase from django.core.files.images import ImageFile from django.conf import settings -from django.utils.six import BytesIO from django_images.models import Image, Thumbnail from django_images.templatetags.images import at_size from django_images.utils import scale_and_crop_single diff --git a/pinry/middleware.py b/pinry/middleware.py index 05e640d..4b23336 100644 --- a/pinry/middleware.py +++ b/pinry/middleware.py @@ -1,10 +1,8 @@ from django.middleware.csrf import get_token +from django.utils.deprecation import MiddlewareMixin -class ForceCSRFCookieMiddleware: - - def __init__(self, get_response): - self.get_response = get_response +class ForceCSRFCookieMiddleware(MiddlewareMixin): def process_request(self, request): if "CSRF_TOKEN" not in request.META: diff --git a/pinry/settings/base.py b/pinry/settings/base.py index 1d70c1b..700f6ce 100644 --- a/pinry/settings/base.py +++ b/pinry/settings/base.py @@ -106,7 +106,7 @@ USE_TZ = True STATIC_URL = '/static/' -MEDIA_URL = '/static/media/' +MEDIA_URL = '/media/' # Set to False to disable people from creating new accounts. ALLOW_NEW_REGISTRATIONS = True diff --git a/users/middleware.py b/users/middleware.py index d3eab24..f78388a 100644 --- a/users/middleware.py +++ b/users/middleware.py @@ -1,18 +1,16 @@ from django.conf import settings from django.http import HttpResponseForbidden +from django.utils.deprecation import MiddlewareMixin -class Public: +class Public(MiddlewareMixin): acceptable_paths = ( "/api/v2/profile/", ) - def __init__(self, get_response): - self.get_response = get_response - def process_request(self, request): - if settings.PUBLIC is False and not request.user.is_authenticated(): + if settings.PUBLIC is False and not request.user.is_authenticated: for path in self.acceptable_paths: if not request.path.startswith(path): return HttpResponseForbidden() diff --git a/users/tests.py b/users/tests.py index 3986fee..7149cb3 100644 --- a/users/tests.py +++ b/users/tests.py @@ -1,8 +1,8 @@ -from django.core.urlresolvers import reverse from django.test import TestCase from django.test.utils import override_settings import mock +from django.urls import reverse from .auth.backends import CombinedAuthBackend from .models import User diff --git a/users/urls.py b/users/urls.py index 5ec6619..515879b 100644 --- a/users/urls.py +++ b/users/urls.py @@ -3,6 +3,8 @@ from django.conf.urls import url, include from users.views import login_user from . import views + +app_name = "users" urlpatterns = [ url(r'', include(views.drf_router.urls)), url(r'^login/$', login_user, name='login'),