Mixin Test does not rely on actual place view

This commit is contained in:
reverend 2021-05-15 23:26:27 +02:00
parent f5f5980f9b
commit 29d067002e

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python 23238#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.test import TestCase, RequestFactory, Client from django.test import TestCase, RequestFactory, Client
@ -6,6 +6,7 @@ from django.urls import reverse_lazy
from django.contrib.auth.models import User, AnonymousUser from django.contrib.auth.models import User, AnonymousUser
from django.contrib.messages.storage.fallback import FallbackStorage from django.contrib.messages.storage.fallback import FallbackStorage
from django.utils import timezone from django.utils import timezone
from django.shortcuts import render
from lostplaces.models import Place from lostplaces.models import Place
from lostplaces.views import IsAuthenticatedMixin from lostplaces.views import IsAuthenticatedMixin
@ -33,14 +34,14 @@ class TestIsAuthenticated(ViewTestCase):
self.assertHttpMethodNotAllowed(response) self.assertHttpMethodNotAllowed(response)
def test_not_logged_in(self): def test_not_logged_in(self):
request = RequestFactory().get('/someurl1234') request = RequestFactory().get('/')
request.user = AnonymousUser() request.user = AnonymousUser()
request.session = 'session' request.session = 'session'
messages = FallbackStorage(request) messages = FallbackStorage(request)
request._messages = messages request._messages = messages
response = IsAuthenticatedMixin.as_view()(request) response = IsAuthenticatedMixin.as_view()(request)
self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234'])) self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/']))
response = self.client.get(response['Location']) response = self.client.get(response['Location'])
self.assertTrue(len(messages) > 0) self.assertTrue(len(messages) > 0)
@ -86,4 +87,5 @@ class TestIsPlaceSubmitterMixin(TestCase):
response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1})) response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1}))
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assertTrue(response.context['messages']) self.assertTrue(response.context['messages'])
self.assertTrue(len(response.context['messages']) > 0) self.assertTrue(len(response.context['messages']) > 0)