From 4f465b3162b740576dd257db95da97a361ef6b8f Mon Sep 17 00:00:00 2001 From: reverend Date: Sat, 12 Jun 2021 15:52:52 +0200 Subject: [PATCH] 400 Response when submitting invalid place data and testing more edge cases --- .../tests/views/test_place_views.py | 163 +++++++++++++----- .../lostplaces/views/place_views.py | 3 +- 2 files changed, 124 insertions(+), 42 deletions(-) diff --git a/django_lostplaces/lostplaces/tests/views/test_place_views.py b/django_lostplaces/lostplaces/tests/views/test_place_views.py index 397f434..24db9e2 100644 --- a/django_lostplaces/lostplaces/tests/views/test_place_views.py +++ b/django_lostplaces/lostplaces/tests/views/test_place_views.py @@ -126,49 +126,130 @@ class TestPlaceCreateView(ViewTestCase): ) ) - def test_positive_image(self): - self.client.login(username='testpeter', password='Develop123') - image = open( - path.join( - settings.BASE_DIR, - 'testdata', - 'test_image.jpg' - ) - ) - response = self.client.post( - reverse('place_create'), - { - 'name': 'test place 894', - 'location': 'test location', - 'latitude': 45.804192, - 'longitude': 1.860222, - 'description': """ - Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt. - """, - 'images': [image] - } - ) - self.assertHttpRedirect(response) - place = Place.objects.get(name='test place 894') - self.assertNotEqual( - None, - place, - msg='Submitted place not found in database / model' + def test_positive_image(self): + self.client.login(username='testpeter', password='Develop123') + image = open( + path.join( + settings.BASE_DIR, + 'testdata', + 'test_image.jpg' ), - self.assertNotEqual( - None, - re.search( - """.*%s""" % reverse( - 'place_detail', kwargs={'pk': place.id} - ), - response.url - ) - ) - self.assertEqual( - len(place.placeimages.all()), - 1, - msg='Expecting the place to have exactly 1 place image' + 'rb' + ) + response = self.client.post( + reverse('place_create'), + { + 'name': 'test place 894', + 'location': 'test location', + 'latitude': 45.804192, + 'longitude': 1.860222, + 'description': """ + Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt. + """, + 'filename': [image] + } + ) + self.assertHttpRedirect(response) + place = Place.objects.get(name='test place 894') + self.assertNotEqual( + None, + place, + msg='Submitted place not found in database / model' + ), + self.assertNotEqual( + None, + re.search( + """.*%s""" % reverse( + 'place_detail', kwargs={'pk': place.id} + ), + response.url ) + ) + self.assertEqual( + len(place.placeimages.all()), + 1, + msg='Expecting the place to have exactly 1 place image' + ) + + def test_negative_no_name(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.post( + reverse('place_create'), + { + 'location': 'test location 456', + 'latitude': 45.804192, + 'longitude': 1.860222, + 'description': """ + Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt. + """ + } + ) + self.assertHttpBadRequest(response) + self.assertEqual( + len(Place.objects.filter(location='test location 456')), + 0, + msg='Expecting no place to be created' + ) + + def test_negative_no_location(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.post( + reverse('place_create'), + { + 'name': 'test name 376', + 'latitude': 45.804192, + 'longitude': 1.860222, + 'description': """ + Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt. + """ + } + ) + self.assertHttpBadRequest(response) + self.assertEqual( + len(Place.objects.filter(name='test name 376')), + 0, + msg='Expecting no place to be created' + ) + + def test_negative_no_latitude(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.post( + reverse('place_create'), + { + 'name': 'test name 417', + 'location': 'location', + 'longitude': 1.860222, + 'description': """ + Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt. + """ + } + ) + self.assertHttpBadRequest(response) + self.assertEqual( + len(Place.objects.filter(name='test name 417')), + 0, + msg='Expecting no place to be created' + ) + + def test_negative_no_longitude(self): + self.client.login(username='testpeter', password='Develop123') + response = self.client.post( + reverse('place_create'), + { + 'name': 'test name 417', + 'location': 'location', + 'latitude': 45.804192, + 'description': """ + Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt. + """ + } + ) + self.assertHttpBadRequest(response) + self.assertEqual( + len(Place.objects.filter(name='test name 417')), + 0, + msg='Expecting no place to be created' + ) class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase): view = PlaceDetailView diff --git a/django_lostplaces/lostplaces/views/place_views.py b/django_lostplaces/lostplaces/views/place_views.py index 29069db..18c10a9 100644 --- a/django_lostplaces/lostplaces/views/place_views.py +++ b/django_lostplaces/lostplaces/views/place_views.py @@ -111,7 +111,8 @@ class PlaceCreateView(MultiplePlaceImageUploadMixin, IsAuthenticatedMixin, View) context={ 'place_form': place_form, 'place_image_form': PlaceImageForm() - } + }, + status=400 ) class PlaceDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, DeleteView):