lostplaces-backend/lostplaces/lostplaces_app/tests/views/__init__.py

108 lines
3.3 KiB
Python
Raw Normal View History

2020-09-13 19:43:47 +02:00
from django.test import TestCase
2020-09-12 11:02:23 +02:00
2020-09-13 19:43:47 +02:00
class ViewTestCase(TestCase):
2020-09-13 12:41:31 +02:00
'''
This is a mixni for testing views. It provides functionality to
test the context, forms and HTTP Response of responses.
2020-09-13 19:43:47 +02:00
All methods take responses, so this base class can be used
with django's RequestFactory and Test-Client
2020-09-13 12:41:31 +02:00
'''
2020-09-12 11:02:23 +02:00
view = None
2020-09-13 12:49:26 +02:00
def assertContext(self, response, key, value=None):
'''
Checks weather the response's context has the given key
and, if passed, checks the value
'''
self.assertTrue(
key in response.context,
2020-09-12 11:02:23 +02:00
msg='Expecting the context of %s to have an attribute \'%s\'' % (
2020-09-13 19:43:47 +02:00
self.view.__name__,
2020-09-13 12:49:26 +02:00
key
2020-09-12 11:02:23 +02:00
)
)
2020-09-13 12:49:26 +02:00
if value:
self.assertEqual(
value,
response.context[key],
msg='Expecting the context of %s to have %s set to \'%s\'' % (
2020-09-13 19:43:47 +02:00
self.view.__name__,
2020-09-13 12:49:26 +02:00
key,
str(value)
)
)
2020-09-12 11:02:23 +02:00
2020-09-13 19:43:47 +02:00
def assertHasForm(self, response, key, form_class):
2020-09-13 12:49:26 +02:00
'''
Checks if response has a form under the given key and if
the forms class matches.
'''
2020-09-13 19:43:47 +02:00
self.assertContext(response, key)
2020-09-12 11:02:23 +02:00
self.assertEqual(
2020-09-13 19:43:47 +02:00
type(response.context[key]),
2020-09-12 11:02:23 +02:00
form_class,
msg='Expecting %s\'s context.%s to be of the type %s' % (
2020-09-13 19:43:47 +02:00
self.view.__name__,
key,
2020-09-12 11:02:23 +02:00
form_class.__name__
)
2020-09-13 12:39:02 +02:00
)
def assertHttpCode(self, response, code):
2020-09-13 12:49:26 +02:00
'''
Checks if the response has the given status code
'''
2020-09-13 12:39:02 +02:00
self.assertEqual(
response.status_code, code,
"Expected an HTTP %s response, but got HTTP %s" % (
code,
response.status_code
)
)
2020-09-13 19:43:47 +02:00
2020-09-13 12:49:26 +02:00
def assertHttpRedirect(self, response, redirect_to=None):
2020-09-13 12:39:02 +02:00
'''
2020-09-13 12:49:26 +02:00
Checks weather the response redirected, and if passed,
if it redirected to the expected loaction
2020-09-13 12:39:02 +02:00
'''
2020-09-13 19:43:47 +02:00
2020-09-13 12:39:02 +02:00
self.assertTrue(
300 <= response.status_code < 400,
'Expected an HTTP 3XX (redirect) response, but got HTTP %s' %
response.status_code
)
2020-09-13 19:43:47 +02:00
self.assertTrue(
'location' in response,
msg='Expecting a redirect to have an location, got none'
)
2020-09-13 12:49:26 +02:00
if redirect_to:
self.assertEqual(
2020-09-13 19:43:47 +02:00
response['location'],
2020-09-13 12:49:26 +02:00
redirect_to,
msg='Expecing the response to redirect to %s, where redirected to %s instea' % (
str(redirect_to),
2020-09-13 19:43:47 +02:00
str(response['location'])
2020-09-13 12:49:26 +02:00
)
)
2020-09-13 12:39:02 +02:00
2020-09-13 19:43:47 +02:00
def assertHttpOK(self, response):
self.assertHttpCode(response, 200)
def assertHttpCreated(self, response):
self.assertHttpCode(response, 201)
2020-09-13 12:39:02 +02:00
def assertHttpBadRequest(self, response):
self.assertHttpCode(response, 400)
def assertHttpUnauthorized(self, response):
self.assertHttpCode(response, 401)
def assertHttpForbidden(self, response):
self.assertHttpCode(response, 403)
def assertHttpNotFound(self, response):
self.assertHttpCode(response, 404)
def assertHttpMethodNotAllowed(self, response):
self.assertHttpCode(response, 405)