Compare commits

..

No commits in common. "05481fc0c87e5b46834b1feb0854d623f78f5656" and "9852646fff14ccbbee306cba5e05b3db98e130de" have entirely different histories.

4 changed files with 47 additions and 79 deletions

View File

@ -152,6 +152,7 @@ class ModelTestCase:
class SubmittableTestCase(ModelTestCase): class SubmittableTestCase(ModelTestCase):
model_name = '<Class>'
related_name = None related_name = None
nullable = False nullable = False

View File

@ -1,18 +1,13 @@
from django.test import Client from django.test import Client
class ViewTestCaseMixin: class ViewTestCase:
'''
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 view = None
def setUp(self): def setUp(self):
self.view_name = self.view.__name__ self.view_name = self.view.__name__
self. client = Client() self. client = Client()
def assertHasContextKey(self, response, context_key): def _test_has_context_key(self, response, context_key):
self.assertTrue( context_key in response.context, self.assertTrue( context_key in response.context,
msg='Expecting the context of %s to have an attribute \'%s\'' % ( msg='Expecting the context of %s to have an attribute \'%s\'' % (
self.view_name, self.view_name,
@ -20,8 +15,8 @@ class ViewTestCaseMixin:
) )
) )
def assertHasForm(self, response, context_key, form_class): def _test_form(self, response, context_key, form_class):
self.assertHasContextKey(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]),
form_class, form_class,
@ -31,47 +26,3 @@ class ViewTestCaseMixin:
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)

View File

@ -1,14 +1,14 @@
import datetime import datetime
from django.test import TestCase, RequestFactory, Client from django.test import TestCase, Client
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.contrib.auth.models import User, AnonymousUser from django.contrib.auth.models import User
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
class TestIsAuthenticatedMixin(RedirectTestCase): from django.contrib.auth.models import User
class TestIsAuthenticatedMixin(TestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
@ -17,23 +17,39 @@ class TestIsAuthenticatedMixin(RedirectTestCase):
password='Develop123' password='Develop123'
) )
def test_logged_in(self): place = Place.objects.create(
request = RequestFactory().get('/') name='Im a place',
request.user = User.objects.get(id=1) 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()
response = IsAuthenticatedMixin.as_view()(request) 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}))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_not_logged_in(self): def test_not_logged_in(self):
request = RequestFactory().get('/someurl1234') url = reverse_lazy('place_detail', kwargs={'pk': 1})
request.user = AnonymousUser() response = self.client.get(url, follow=True)
request.session = 'session' self.assertRedirects(
messages = FallbackStorage(request) response=response,
request._messages = messages expected_url='?'.join([str(reverse_lazy('login')), 'next=/place/1/']),
status_code=302,
response = IsAuthenticatedMixin.as_view()(request) target_status_code=200,
self.assertRedirectsTo(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234'])) msg_prefix='''Accesing an IsAuthenticatedMixin view while not logged should
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)

View File

@ -10,9 +10,9 @@ from lostplaces_app.views import (
PlaceListView PlaceListView
) )
from lostplaces_app.forms import PlaceImageCreateForm, PlaceForm from lostplaces_app.forms import PlaceImageCreateForm, PlaceForm
from lostplaces_app.tests.views import ViewTestCaseMixin from lostplaces_app.tests.views import ViewTestCase
class TestPlaceCreateView(ViewTestCaseMixin, TestCase): class TestPlaceCreateView(ViewTestCase, TestCase):
view = PlaceCreateView view = PlaceCreateView
@ -39,10 +39,10 @@ class TestPlaceCreateView(ViewTestCaseMixin, 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.assertHasForm(response, 'place_image_form', PlaceImageCreateForm) self._test_form(response, 'place_image_form', PlaceImageCreateForm)
self.assertHasForm(response, 'place_form', PlaceForm) self._test_form(response, 'place_form', PlaceForm)
class TestPlaceListView(ViewTestCaseMixin, TestCase): class TestPlaceListView(ViewTestCase, TestCase):
view = PlaceListView view = PlaceListView
@classmethod @classmethod
@ -68,6 +68,6 @@ class TestPlaceListView(ViewTestCaseMixin, TestCase):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse_lazy('place_list')) response = self.client.get(reverse_lazy('place_list'))
self.assertHasContextKey(response, 'map_config') self._test_has_context_key(response, 'map_config')