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):
model_name = '<Class>'
related_name = None
nullable = False

View File

@ -1,18 +1,13 @@
from django.test import Client
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.
'''
class ViewTestCase:
view = None
def setUp(self):
self.view_name = self.view.__name__
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,
msg='Expecting the context of %s to have an attribute \'%s\'' % (
self.view_name,
@ -20,8 +15,8 @@ class ViewTestCaseMixin:
)
)
def assertHasForm(self, response, context_key, form_class):
self.assertHasContextKey(response, context_key)
def _test_form(self, response, context_key, form_class):
self._test_has_context_key(response, context_key)
self.assertEqual(
type(response.context[context_key]),
form_class,
@ -30,48 +25,4 @@ class ViewTestCaseMixin:
context_key,
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,39 +1,55 @@
import datetime
from django.test import TestCase, RequestFactory, Client
from django.test import TestCase, Client
from django.urls import reverse_lazy
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.messages.storage.fallback import FallbackStorage
from django.contrib.auth.models import User
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
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
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):
request = RequestFactory().get('/')
request.user = User.objects.get(id=1)
response = IsAuthenticatedMixin.as_view()(request)
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1}))
self.assertEqual(response.status_code, 200)
def test_not_logged_in(self):
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']))
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
)
self.assertTrue(response.context['messages'])
self.assertTrue(len(response.context['messages']) > 0)

View File

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