From f8cae76f05ea27a7dd59b3a633b092f27a12fba3 Mon Sep 17 00:00:00 2001 From: reverend Date: Fri, 4 Sep 2020 21:48:05 +0200 Subject: [PATCH] testing base views --- .../tests/views/test_base_views.py | 58 +++++++++++++++++++ .../tests/views/test_place_views.py | 35 +++++++++++ 2 files changed, 93 insertions(+) create mode 100644 lostplaces/lostplaces_app/tests/views/test_base_views.py diff --git a/lostplaces/lostplaces_app/tests/views/test_base_views.py b/lostplaces/lostplaces_app/tests/views/test_base_views.py new file mode 100644 index 0000000..6525595 --- /dev/null +++ b/lostplaces/lostplaces_app/tests/views/test_base_views.py @@ -0,0 +1,58 @@ +from django.test import TestCase, Client +from django.urls import reverse_lazy + +from lostplaces_app.models import Place + +from lostplaces_app.models import Explorer +from lostplaces_app.tests.models.test_place_model import mock_place +from lostplaces_app.tests import mock_user + +class TestIsAuthenticated(TestCase): + def setUp(self): + self. client = Client() + mock_place() + mock_user() + + def test_logged_in(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1})) + 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')), 'redirect_to=/place/1/']), + status_code=302, + target_status_code=200, + msg_prefix='''Accesing an IsAuthenticated view while not logged should + redirect to login page with redirect params + ''', + fetch_redirect_response=True + ) + self.assertTrue(response.context['messages']) + self.assertTrue(len(response.context['messages']) > 0) + +class TestIsPlaceSubmitter(TestCase): + + def setUp(self): + self. client = Client() + mock_place() + mock_user() + + def test_is_submitter(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1})) + self.assertEqual(response.status_code, 200) + + def test_is_no_submitter(self): + Explorer.objects.create_user( + username='manfred', + password='Develop123' + ) + self.client.login(username='manfred', password='Develop123') + response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1})) + self.assertEqual(response.status_code, 403) + self.assertTrue(response.context['messages']) + self.assertTrue(len(response.context['messages']) > 0) \ No newline at end of file diff --git a/lostplaces/lostplaces_app/tests/views/test_place_views.py b/lostplaces/lostplaces_app/tests/views/test_place_views.py index e69de29..ff4b46f 100644 --- a/lostplaces/lostplaces_app/tests/views/test_place_views.py +++ b/lostplaces/lostplaces_app/tests/views/test_place_views.py @@ -0,0 +1,35 @@ +from django.test import TestCase, Client +from django.urls import reverse_lazy + +from lostplaces_app.models import Place + +from lostplaces_app.tests.models.test_place_model import mock_place +from lostplaces_app.tests import mock_user + +class TestPlaceCreateView(TestCase): + + def setUp(self): + self. client = Client() + mock_place() + mock_user() + + def test_url_logged_in(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1})) + self.assertEqual(response.status_code, 200) + + def test_url_not_logged_in(self): + url = reverse_lazy('place_detail', kwargs={'pk': 1}) + response = self.client.get(url) + self.assertRedirects( + response=response, + expected_url='?'.join([str(reverse_lazy('login')), 'redirect_to=/place/1/']), + status_code=302, + target_status_code=200, + msg_prefix='''Accesing PlaceDetailView while not logged should + redirect to login page with redirect params + ''', + fetch_redirect_response=True + ) + + \ No newline at end of file