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
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):
'''
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.utils import timezone
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from lostplaces.models import Place
@ -99,7 +100,7 @@ class TestPlaceCreateView(ViewTestCase):
self.client.login(username='testpeter', password='Develop123')
response = self.client.post(
reverse('place_create'),
{
data={
'name': 'test place 486',
'location': 'test location',
'latitude': 45.804192,
@ -107,23 +108,27 @@ class TestPlaceCreateView(ViewTestCase):
'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.
"""
}
},
follow=True
)
self.assertHttpRedirect(response)
self.assertHttpOK(response)
place = Place.objects.get(name='test place 486')
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(
reverse(
'place_detail', kwargs={'pk': place.id}
),
response.redirect_chain[-1][0]
)
self.assertMessage(
response,
_('Successfully created place'),
'success',
msg='Expecting a visible success message'
)
def test_positive_image(self):
@ -138,7 +143,7 @@ class TestPlaceCreateView(ViewTestCase):
)
response = self.client.post(
reverse('place_create'),
{
data={
'name': 'test place 894',
'location': 'test location',
'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.
""",
'filename': [image]
}
},
follow=True
)
self.assertHttpRedirect(response)
self.assertHttpOK(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(
reverse(
'place_detail', kwargs={'pk': place.id}
),
response.redirect_chain[-1][0]
)
self.assertEqual(
len(place.placeimages.all()),
1,
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):
self.client.login(username='testpeter', password='Develop123')
@ -190,6 +199,12 @@ class TestPlaceCreateView(ViewTestCase):
0,
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):
self.client.login(username='testpeter', password='Develop123')
@ -210,6 +225,12 @@ class TestPlaceCreateView(ViewTestCase):
0,
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):
self.client.login(username='testpeter', password='Develop123')
@ -230,6 +251,12 @@ class TestPlaceCreateView(ViewTestCase):
0,
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):
self.client.login(username='testpeter', password='Develop123')
@ -250,6 +277,12 @@ class TestPlaceCreateView(ViewTestCase):
0,
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):
view = PlaceDetailView
@ -273,6 +306,19 @@ class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixi
place.tags.add('I a tag', 'testlocation')
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):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_detail', kwargs={'pk': 1}))