Adding more methods to viewtestcase
This commit is contained in:
parent
c0f30e56f7
commit
7e4c5dcf24
@ -15,7 +15,7 @@ class ViewTestCase:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _test_form(self, response, context_key, form_class):
|
def assertHasForm(self, response, context_key, form_class):
|
||||||
self._test_has_context_key(response, context_key)
|
self._test_has_context_key(response, context_key)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
type(response.context[context_key]),
|
type(response.context[context_key]),
|
||||||
@ -26,3 +26,47 @@ class ViewTestCase:
|
|||||||
form_class.__name__
|
form_class.__name__
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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)
|
@ -1,14 +1,14 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from django.test import TestCase, Client
|
from django.test import TestCase, RequestFactory, Client
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User, AnonymousUser
|
||||||
|
from django.contrib.messages.storage.fallback import FallbackStorage
|
||||||
|
|
||||||
from lostplaces_app.models import Place
|
from lostplaces_app.models import Place
|
||||||
|
from lostplaces_app.views import IsAuthenticatedMixin
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
class TestIsAuthenticatedMixin(RedirectTestCase):
|
||||||
|
|
||||||
class TestIsAuthenticatedMixin(TestCase):
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
@ -17,39 +17,23 @@ class TestIsAuthenticatedMixin(TestCase):
|
|||||||
password='Develop123'
|
password='Develop123'
|
||||||
)
|
)
|
||||||
|
|
||||||
place = Place.objects.create(
|
|
||||||
name='Im a place',
|
|
||||||
submitted_when=datetime.datetime.now(),
|
|
||||||
submitted_by=User.objects.get(username='testpeter').explorer,
|
|
||||||
location='Testtown',
|
|
||||||
latitude=50.5,
|
|
||||||
longitude=7.0,
|
|
||||||
description='This is just a test, do not worry'
|
|
||||||
)
|
|
||||||
place.tags.add('I a tag', 'testlocation')
|
|
||||||
place.save()
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self. client = Client()
|
|
||||||
|
|
||||||
def test_logged_in(self):
|
def test_logged_in(self):
|
||||||
self.client.login(username='testpeter', password='Develop123')
|
request = RequestFactory().get('/')
|
||||||
response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1}))
|
request.user = User.objects.get(id=1)
|
||||||
|
|
||||||
|
response = IsAuthenticatedMixin.as_view()(request)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
def test_not_logged_in(self):
|
def test_not_logged_in(self):
|
||||||
url = reverse_lazy('place_detail', kwargs={'pk': 1})
|
request = RequestFactory().get('/someurl1234')
|
||||||
response = self.client.get(url, follow=True)
|
request.user = AnonymousUser()
|
||||||
self.assertRedirects(
|
request.session = 'session'
|
||||||
response=response,
|
messages = FallbackStorage(request)
|
||||||
expected_url='?'.join([str(reverse_lazy('login')), 'next=/place/1/']),
|
request._messages = messages
|
||||||
status_code=302,
|
|
||||||
target_status_code=200,
|
response = IsAuthenticatedMixin.as_view()(request)
|
||||||
msg_prefix='''Accesing an IsAuthenticatedMixin view while not logged should
|
self.assertRedirectsTo(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
|
||||||
redirect to login page with redirect params
|
|
||||||
''',
|
|
||||||
fetch_redirect_response=True
|
|
||||||
)
|
|
||||||
self.assertTrue(response.context['messages'])
|
self.assertTrue(response.context['messages'])
|
||||||
self.assertTrue(len(response.context['messages']) > 0)
|
self.assertTrue(len(response.context['messages']) > 0)
|
||||||
|
|
||||||
|
@ -39,8 +39,8 @@ class TestPlaceCreateView(ViewTestCase, TestCase):
|
|||||||
self.client.login(username='testpeter', password='Develop123')
|
self.client.login(username='testpeter', password='Develop123')
|
||||||
response = self.client.get(reverse_lazy('place_create'))
|
response = self.client.get(reverse_lazy('place_create'))
|
||||||
|
|
||||||
self._test_form(response, 'place_image_form', PlaceImageCreateForm)
|
self.assertHasForm(response, 'place_image_form', PlaceImageCreateForm)
|
||||||
self._test_form(response, 'place_form', PlaceForm)
|
self.assertHasForm(response, 'place_form', PlaceForm)
|
||||||
|
|
||||||
class TestPlaceListView(ViewTestCase, TestCase):
|
class TestPlaceListView(ViewTestCase, TestCase):
|
||||||
view = PlaceListView
|
view = PlaceListView
|
||||||
|
Loading…
Reference in New Issue
Block a user