Browse Source

Fix: Fix test failure cased by new django-test client response

+ Also fix the old middleware
+ Fix MIDDLEWARE name error
pull/218/head
winkidney 4 years ago
parent
commit
374be7a03f
8 changed files with 23 additions and 25 deletions
  1. +10
    -10
      core/tests/api.py
  2. +2
    -3
      core/tests/views.py
  3. +2
    -1
      django_images/tests.py
  4. +2
    -4
      pinry/middleware.py
  5. +1
    -1
      pinry/settings/base.py
  6. +3
    -5
      users/middleware.py
  7. +1
    -1
      users/tests.py
  8. +2
    -0
      users/urls.py

+ 10
- 10
core/tests/api.py View File

@@ -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)


+ 2
- 3
core/tests/views.py View File

@@ -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(),
{


+ 2
- 1
django_images/tests.py View File

@@ -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


+ 2
- 4
pinry/middleware.py View File

@@ -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:


+ 1
- 1
pinry/settings/base.py View File

@@ -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


+ 3
- 5
users/middleware.py View File

@@ -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()

+ 1
- 1
users/tests.py View File

@@ -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


+ 2
- 0
users/urls.py View File

@@ -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'),


Loading…
Cancel
Save