2020-09-12 11:02:23 +02:00
|
|
|
from django.test import Client
|
|
|
|
|
|
|
|
class ViewTestCase:
|
|
|
|
view = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.view_name = self.view.__name__
|
|
|
|
self. client = Client()
|
|
|
|
|
|
|
|
def _test_has_context_key(self, response, context_key):
|
|
|
|
self.assertTrue( context_key in response.context,
|
|
|
|
msg='Expecting the context of %s to have an attribute \'%s\'' % (
|
|
|
|
self.view_name,
|
|
|
|
context_key
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-13 12:39:02 +02:00
|
|
|
def assertHasForm(self, response, context_key, form_class):
|
2020-09-12 11:02:23 +02:00
|
|
|
self._test_has_context_key(response, context_key)
|
|
|
|
self.assertEqual(
|
|
|
|
type(response.context[context_key]),
|
|
|
|
form_class,
|
|
|
|
msg='Expecting %s\'s context.%s to be of the type %s' % (
|
|
|
|
self.view_name,
|
|
|
|
context_key,
|
|
|
|
form_class.__name__
|
|
|
|
)
|
2020-09-13 12:39:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def assertHttpCode(self, response, code):
|
|
|
|
self.assertEqual(
|
|
|
|
response.status_code, code,
|
|
|
|
"Expected an HTTP %s response, but got HTTP %s" % (
|
|
|
|
code,
|
|
|
|
response.status_code
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertHttpOK(self, response):
|
|
|
|
self.assertHttpCode(response, 200)
|
|
|
|
|
|
|
|
def assertHttpCreated(self, response):
|
|
|
|
self.assertHttpCode(response, 201)
|
|
|
|
|
|
|
|
def assertHttpRedirect(self, response, redirect_to):
|
|
|
|
'''
|
|
|
|
Assert that we had any redirect status code.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
300 <= response.status_code < 400,
|
|
|
|
'Expected an HTTP 3XX (redirect) response, but got HTTP %s' %
|
|
|
|
response.status_code
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(response['Location'], redirect_to)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|