400 Response when submitting invalid place data and testing more edge cases

This commit is contained in:
reverend 2021-06-12 15:52:52 +02:00
parent b0bf40aa0b
commit 4f465b3162
2 changed files with 124 additions and 42 deletions

View File

@ -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

View File

@ -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):