Testing messgaes

This commit is contained in:
reverend 2021-06-12 17:06:09 +02:00
parent 29d2813fcc
commit 51addd8fbb
2 changed files with 82 additions and 22 deletions

View File

@ -17,6 +17,20 @@ class ViewTestCase(TestCase):
''' '''
view = None view = None
def assertMessage(self, response, message_text, message_type, msg=None):
self.assertNotEqual(
None,
re.search(
"""<div.*message.*%s.*>.*%s.*</div>""" % (
message_type.lower(),
message_text.lower()
),
response.content.decode().replace('\n', '').lower()
),
msg
)
def assertContext(self, response, key, value=None): def assertContext(self, response, key, value=None):
''' '''
Checks weather the response's context has the given key Checks weather the response's context has the given key

View File

@ -9,6 +9,7 @@ from django.urls import reverse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from lostplaces.models import Place from lostplaces.models import Place
@ -99,7 +100,7 @@ class TestPlaceCreateView(ViewTestCase):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
response = self.client.post( response = self.client.post(
reverse('place_create'), reverse('place_create'),
{ data={
'name': 'test place 486', 'name': 'test place 486',
'location': 'test location', 'location': 'test location',
'latitude': 45.804192, 'latitude': 45.804192,
@ -107,23 +108,27 @@ class TestPlaceCreateView(ViewTestCase):
'description': """ '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. 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.
""" """
} },
follow=True
) )
self.assertHttpRedirect(response) self.assertHttpOK(response)
place = Place.objects.get(name='test place 486') place = Place.objects.get(name='test place 486')
self.assertNotEqual( self.assertNotEqual(
None, None,
place, place,
msg='Submitted place not found in database / model' msg='Submitted place not found in database / model'
), ),
self.assertNotEqual( self.assertEqual(
None, reverse(
re.search( 'place_detail', kwargs={'pk': place.id}
""".*%s""" % reverse( ),
'place_detail', kwargs={'pk': place.id} response.redirect_chain[-1][0]
), )
response.url self.assertMessage(
) response,
_('Successfully created place'),
'success',
msg='Expecting a visible success message'
) )
def test_positive_image(self): def test_positive_image(self):
@ -138,7 +143,7 @@ class TestPlaceCreateView(ViewTestCase):
) )
response = self.client.post( response = self.client.post(
reverse('place_create'), reverse('place_create'),
{ data={
'name': 'test place 894', 'name': 'test place 894',
'location': 'test location', 'location': 'test location',
'latitude': 45.804192, 'latitude': 45.804192,
@ -147,29 +152,33 @@ class TestPlaceCreateView(ViewTestCase):
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. 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] 'filename': [image]
} },
follow=True
) )
self.assertHttpRedirect(response) self.assertHttpOK(response)
place = Place.objects.get(name='test place 894') place = Place.objects.get(name='test place 894')
self.assertNotEqual( self.assertNotEqual(
None, None,
place, place,
msg='Submitted place not found in database / model' msg='Submitted place not found in database / model'
), ),
self.assertNotEqual( self.assertEqual(
None, reverse(
re.search( 'place_detail', kwargs={'pk': place.id}
""".*%s""" % reverse( ),
'place_detail', kwargs={'pk': place.id} response.redirect_chain[-1][0]
),
response.url
)
) )
self.assertEqual( self.assertEqual(
len(place.placeimages.all()), len(place.placeimages.all()),
1, 1,
msg='Expecting the place to have exactly 1 place image' msg='Expecting the place to have exactly 1 place image'
) )
self.assertMessage(
response,
_('Successfully created place'),
'success',
msg='Expecting a visible success message'
)
def test_negative_no_name(self): def test_negative_no_name(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
@ -190,6 +199,12 @@ class TestPlaceCreateView(ViewTestCase):
0, 0,
msg='Expecting no place to be created' msg='Expecting no place to be created'
) )
self.assertMessage(
response,
_('Please fill in all required fields.'),
'error',
msg='Expecing a visible error message'
)
def test_negative_no_location(self): def test_negative_no_location(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
@ -210,6 +225,12 @@ class TestPlaceCreateView(ViewTestCase):
0, 0,
msg='Expecting no place to be created' msg='Expecting no place to be created'
) )
self.assertMessage(
response,
_('Please fill in all required fields.'),
'error',
msg='Expecing a visible error message'
)
def test_negative_no_latitude(self): def test_negative_no_latitude(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
@ -230,6 +251,12 @@ class TestPlaceCreateView(ViewTestCase):
0, 0,
msg='Expecting no place to be created' msg='Expecting no place to be created'
) )
self.assertMessage(
response,
_('Please fill in all required fields.'),
'error',
msg='Expecing a visible error message'
)
def test_negative_no_longitude(self): def test_negative_no_longitude(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
@ -250,6 +277,12 @@ class TestPlaceCreateView(ViewTestCase):
0, 0,
msg='Expecting no place to be created' msg='Expecting no place to be created'
) )
self.assertMessage(
response,
_('Please fill in all required fields.'),
'error',
msg='Expecing a visible error message'
)
class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase): class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase):
view = PlaceDetailView view = PlaceDetailView
@ -273,6 +306,19 @@ class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixi
place.tags.add('I a tag', 'testlocation') place.tags.add('I a tag', 'testlocation')
place.save() place.save()
def test_not_authenticated(self):
response = self.client.get(reverse('place_detail', kwargs={'pk': 1}))
self.assertHttpRedirect(response)
self.assertEqual(
'%s?next=%s' % (
reverse('login'),
reverse('place_detail', kwargs={'pk': 1})
),
response.url,
msg='Expecting unauthenticated user to be redirected to login page, using the \'next\' GET-Param to store the places details page'
)
def test_context(self): def test_context(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_detail', kwargs={'pk': 1})) response = self.client.get(reverse('place_detail', kwargs={'pk': 1}))