diff --git a/lostplaces/lostplaces_app/tests/views/__init__.py b/lostplaces/lostplaces_app/tests/views/__init__.py index 689e3d6..f958b96 100644 --- a/lostplaces/lostplaces_app/tests/views/__init__.py +++ b/lostplaces/lostplaces_app/tests/views/__init__.py @@ -15,7 +15,7 @@ class ViewTestCase: ) ) - def _test_form(self, response, context_key, form_class): + def assertHasForm(self, response, context_key, form_class): self._test_has_context_key(response, context_key) self.assertEqual( type(response.context[context_key]), @@ -25,4 +25,48 @@ class ViewTestCase: context_key, form_class.__name__ ) - ) \ No newline at end of file + ) + + def assertHttpCode(self, response, code): + self.assertEqual( + response.status_code, code, + "Expected an HTTP %s response, but got HTTP %s" % ( + code, + response.status_code + ) + ) + + def assertHttpOK(self, response): + self.assertHttpCode(response, 200) + + def assertHttpCreated(self, response): + self.assertHttpCode(response, 201) + + def assertHttpRedirect(self, response, redirect_to): + ''' + Assert that we had any redirect status code. + ''' + + self.assertTrue( + 300 <= response.status_code < 400, + 'Expected an HTTP 3XX (redirect) response, but got HTTP %s' % + response.status_code + ) + + self.assertEqual(response['Location'], redirect_to) + + + def assertHttpBadRequest(self, response): + self.assertHttpCode(response, 400) + + def assertHttpUnauthorized(self, response): + self.assertHttpCode(response, 401) + + def assertHttpForbidden(self, response): + self.assertHttpCode(response, 403) + + def assertHttpNotFound(self, response): + self.assertHttpCode(response, 404) + + def assertHttpMethodNotAllowed(self, response): + self.assertHttpCode(response, 405) \ No newline at end of file diff --git a/lostplaces/lostplaces_app/tests/views/test_base_views.py b/lostplaces/lostplaces_app/tests/views/test_base_views.py index 5efa093..b3d8ae2 100644 --- a/lostplaces/lostplaces_app/tests/views/test_base_views.py +++ b/lostplaces/lostplaces_app/tests/views/test_base_views.py @@ -1,55 +1,39 @@ import datetime -from django.test import TestCase, Client +from django.test import TestCase, RequestFactory, Client from django.urls import reverse_lazy -from django.contrib.auth.models import User +from django.contrib.auth.models import User, AnonymousUser +from django.contrib.messages.storage.fallback import FallbackStorage from lostplaces_app.models import Place +from lostplaces_app.views import IsAuthenticatedMixin -from django.contrib.auth.models import User - -class TestIsAuthenticatedMixin(TestCase): - +class TestIsAuthenticatedMixin(RedirectTestCase): + @classmethod def setUpTestData(cls): user = User.objects.create_user( username='testpeter', password='Develop123' ) - - place = Place.objects.create( - name='Im a place', - submitted_when=datetime.datetime.now(), - submitted_by=User.objects.get(username='testpeter').explorer, - location='Testtown', - latitude=50.5, - longitude=7.0, - description='This is just a test, do not worry' - ) - place.tags.add('I a tag', 'testlocation') - place.save() - - def setUp(self): - self. client = Client() def test_logged_in(self): - self.client.login(username='testpeter', password='Develop123') - response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1})) + request = RequestFactory().get('/') + request.user = User.objects.get(id=1) + + response = IsAuthenticatedMixin.as_view()(request) self.assertEqual(response.status_code, 200) def test_not_logged_in(self): - url = reverse_lazy('place_detail', kwargs={'pk': 1}) - response = self.client.get(url, follow=True) - self.assertRedirects( - response=response, - expected_url='?'.join([str(reverse_lazy('login')), 'next=/place/1/']), - status_code=302, - target_status_code=200, - msg_prefix='''Accesing an IsAuthenticatedMixin view while not logged should - redirect to login page with redirect params - ''', - fetch_redirect_response=True - ) + request = RequestFactory().get('/someurl1234') + request.user = AnonymousUser() + request.session = 'session' + messages = FallbackStorage(request) + 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) diff --git a/lostplaces/lostplaces_app/tests/views/test_place_views.py b/lostplaces/lostplaces_app/tests/views/test_place_views.py index a7caf1e..387285c 100644 --- a/lostplaces/lostplaces_app/tests/views/test_place_views.py +++ b/lostplaces/lostplaces_app/tests/views/test_place_views.py @@ -39,8 +39,8 @@ class TestPlaceCreateView(ViewTestCase, TestCase): self.client.login(username='testpeter', password='Develop123') response = self.client.get(reverse_lazy('place_create')) - self._test_form(response, 'place_image_form', PlaceImageCreateForm) - self._test_form(response, 'place_form', PlaceForm) + self.assertHasForm(response, 'place_image_form', PlaceImageCreateForm) + self.assertHasForm(response, 'place_form', PlaceForm) class TestPlaceListView(ViewTestCase, TestCase): view = PlaceListView