Compare commits
4 Commits
9852646fff
...
05481fc0c8
Author | SHA1 | Date | |
---|---|---|---|
05481fc0c8 | |||
6b00452830 | |||
7e4c5dcf24 | |||
c0f30e56f7 |
@ -152,7 +152,6 @@ class ModelTestCase:
|
||||
|
||||
|
||||
class SubmittableTestCase(ModelTestCase):
|
||||
model_name = '<Class>'
|
||||
related_name = None
|
||||
nullable = False
|
||||
|
||||
|
@ -1,13 +1,18 @@
|
||||
from django.test import Client
|
||||
|
||||
class ViewTestCase:
|
||||
class ViewTestCaseMixin:
|
||||
'''
|
||||
This is a mixni for testing views. It provides functionality to
|
||||
test the context, forms and HTTP Response of responses.
|
||||
Also works with django's ReqeustFactory.
|
||||
'''
|
||||
view = None
|
||||
|
||||
def setUp(self):
|
||||
self.view_name = self.view.__name__
|
||||
self. client = Client()
|
||||
|
||||
def _test_has_context_key(self, response, context_key):
|
||||
def assertHasContextKey(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,
|
||||
@ -15,8 +20,8 @@ class ViewTestCase:
|
||||
)
|
||||
)
|
||||
|
||||
def _test_form(self, response, context_key, form_class):
|
||||
self._test_has_context_key(response, context_key)
|
||||
def assertHasForm(self, response, context_key, form_class):
|
||||
self.assertHasContextKey(response, context_key)
|
||||
self.assertEqual(
|
||||
type(response.context[context_key]),
|
||||
form_class,
|
||||
@ -26,3 +31,47 @@ class ViewTestCase:
|
||||
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
|
||||
|
||||
from django.test import TestCase, Client
|
||||
from django.test import TestCase, RequestFactory, Client
|
||||
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.views import IsAuthenticatedMixin
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class TestIsAuthenticatedMixin(TestCase):
|
||||
class TestIsAuthenticatedMixin(RedirectTestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
@ -17,39 +17,23 @@ class TestIsAuthenticatedMixin(TestCase):
|
||||
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):
|
||||
self.client.login(username='testpeter', password='Develop123')
|
||||
response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1}))
|
||||
request = RequestFactory().get('/')
|
||||
request.user = User.objects.get(id=1)
|
||||
|
||||
response = IsAuthenticatedMixin.as_view()(request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_not_logged_in(self):
|
||||
url = reverse_lazy('place_detail', kwargs={'pk': 1})
|
||||
response = self.client.get(url, follow=True)
|
||||
self.assertRedirects(
|
||||
response=response,
|
||||
expected_url='?'.join([str(reverse_lazy('login')), 'next=/place/1/']),
|
||||
status_code=302,
|
||||
target_status_code=200,
|
||||
msg_prefix='''Accesing an IsAuthenticatedMixin view while not logged should
|
||||
redirect to login page with redirect params
|
||||
''',
|
||||
fetch_redirect_response=True
|
||||
)
|
||||
request = RequestFactory().get('/someurl1234')
|
||||
request.user = AnonymousUser()
|
||||
request.session = 'session'
|
||||
messages = FallbackStorage(request)
|
||||
request._messages = messages
|
||||
|
||||
response = IsAuthenticatedMixin.as_view()(request)
|
||||
self.assertRedirectsTo(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
|
||||
|
||||
self.assertTrue(response.context['messages'])
|
||||
self.assertTrue(len(response.context['messages']) > 0)
|
||||
|
||||
|
@ -10,9 +10,9 @@ from lostplaces_app.views import (
|
||||
PlaceListView
|
||||
)
|
||||
from lostplaces_app.forms import PlaceImageCreateForm, PlaceForm
|
||||
from lostplaces_app.tests.views import ViewTestCase
|
||||
from lostplaces_app.tests.views import ViewTestCaseMixin
|
||||
|
||||
class TestPlaceCreateView(ViewTestCase, TestCase):
|
||||
class TestPlaceCreateView(ViewTestCaseMixin, TestCase):
|
||||
|
||||
view = PlaceCreateView
|
||||
|
||||
@ -39,10 +39,10 @@ class TestPlaceCreateView(ViewTestCase, TestCase):
|
||||
self.client.login(username='testpeter', password='Develop123')
|
||||
response = self.client.get(reverse_lazy('place_create'))
|
||||
|
||||
self._test_form(response, 'place_image_form', PlaceImageCreateForm)
|
||||
self._test_form(response, 'place_form', PlaceForm)
|
||||
self.assertHasForm(response, 'place_image_form', PlaceImageCreateForm)
|
||||
self.assertHasForm(response, 'place_form', PlaceForm)
|
||||
|
||||
class TestPlaceListView(ViewTestCase, TestCase):
|
||||
class TestPlaceListView(ViewTestCaseMixin, TestCase):
|
||||
view = PlaceListView
|
||||
|
||||
@classmethod
|
||||
@ -68,6 +68,6 @@ class TestPlaceListView(ViewTestCase, TestCase):
|
||||
self.client.login(username='testpeter', password='Develop123')
|
||||
response = self.client.get(reverse_lazy('place_list'))
|
||||
|
||||
self._test_has_context_key(response, 'map_config')
|
||||
self.assertHasContextKey(response, 'map_config')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user