Testing messages

This commit is contained in:
reverend 2020-09-13 13:28:22 +02:00
parent b77c5d1d7f
commit f1c51ab8a7

View File

@ -7,9 +7,11 @@ from django.contrib.messages.storage.fallback import FallbackStorage
from lostplaces_app.models import Place
from lostplaces_app.views import IsAuthenticatedMixin
from lostplaces_app.tests.views import ViewTestCaseMixin
class TestIsAuthenticated(ViewTestCaseMixin, TestCase):
view = IsAuthenticatedMixin
class TestIsAuthenticatedMixin(RedirectTestCase):
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
@ -22,7 +24,8 @@ class TestIsAuthenticatedMixin(RedirectTestCase):
request.user = User.objects.get(id=1)
response = IsAuthenticatedMixin.as_view()(request)
self.assertEqual(response.status_code, 200)
# Expecting a 405 because IsAuthenticatedMixin has no 'get' method
self.assertHttpMethodNotAllowed(response)
def test_not_logged_in(self):
request = RequestFactory().get('/someurl1234')
@ -32,10 +35,10 @@ class TestIsAuthenticatedMixin(RedirectTestCase):
request._messages = messages
response = IsAuthenticatedMixin.as_view()(request)
self.assertRedirectsTo(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
self.assertTrue(response.context['messages'])
self.assertTrue(len(response.context['messages']) > 0)
self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
response = self.client.get(response['Location'])
self.assertTrue(len(messages) > 0)
class TestIsPlaceSubmitterMixin(TestCase):