Testing messages

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

View File

@ -7,8 +7,10 @@ from django.contrib.messages.storage.fallback import FallbackStorage
from lostplaces_app.models import Place from lostplaces_app.models import Place
from lostplaces_app.views import IsAuthenticatedMixin from lostplaces_app.views import IsAuthenticatedMixin
from lostplaces_app.tests.views import ViewTestCaseMixin
class TestIsAuthenticatedMixin(RedirectTestCase): class TestIsAuthenticated(ViewTestCaseMixin, TestCase):
view = IsAuthenticatedMixin
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
@ -22,7 +24,8 @@ class TestIsAuthenticatedMixin(RedirectTestCase):
request.user = User.objects.get(id=1) request.user = User.objects.get(id=1)
response = IsAuthenticatedMixin.as_view()(request) 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): def test_not_logged_in(self):
request = RequestFactory().get('/someurl1234') request = RequestFactory().get('/someurl1234')
@ -32,10 +35,10 @@ class TestIsAuthenticatedMixin(RedirectTestCase):
request._messages = messages request._messages = messages
response = IsAuthenticatedMixin.as_view()(request) response = IsAuthenticatedMixin.as_view()(request)
self.assertRedirectsTo(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234'])) self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
self.assertTrue(response.context['messages']) response = self.client.get(response['Location'])
self.assertTrue(len(response.context['messages']) > 0) self.assertTrue(len(messages) > 0)
class TestIsPlaceSubmitterMixin(TestCase): class TestIsPlaceSubmitterMixin(TestCase):