Squashed commit of the following:

commit 97b044cafb7f17f23b3b1beedcf70af209a60ddc
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:25:40 2020 +0200

    Updating gitignore

commit 4891d80486e1f95db8ae66385c7c97426a3ca1a4
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:25:20 2020 +0200

    Updating Readme

commit f05c43abbdc7eb30896ad6d10fe80fd6483338d9
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:23:30 2020 +0200

    Renaming Module

commit fd5ad2ee9f8cbacd565da45b257928192ffc651c
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:23:16 2020 +0200

    Renaming module references

commit 828a0dd5dd73723b84b77908497903ed26b6966b
Author: reverend <reverend@reverend2048.de>
Date:   Mon Sep 14 17:21:20 2020 +0200

    Renaming Project
This commit is contained in:
2020-09-14 17:26:17 +02:00
parent 0765f6606f
commit 547179b0ca
97 changed files with 53 additions and 50 deletions

View File

@@ -0,0 +1,224 @@
from django.test import TestCase
from lostplaces.models import Taggable, Mapable
from taggit.models import Tag
class ViewTestCase(TestCase):
'''
This is a mixni for testing views. It provides functionality to
test the context, forms and HTTP Response of responses.
All methods take responses, so this base class can be used
with django's RequestFactory and Test-Client
'''
view = None
def assertContext(self, response, key, value=None):
'''
Checks weather the response's context has the given key
and, if passed, checks the value
'''
self.assertTrue(
key in response.context,
msg='Expecting the context of %s to have an attribute \'%s\'' % (
self.view.__name__,
key
)
)
if value:
self.assertEqual(
value,
response.context[key],
msg='Expecting the context of %s to have %s set to \'%s\'' % (
self.view.__name__,
key,
str(value)
)
)
def assertHasForm(self, response, key, form_class):
'''
Checks if response has a form under the given key and if
the forms class matches.
'''
self.assertContext(response, key)
self.assertEqual(
type(response.context[key]),
form_class,
msg='Expecting %s\'s context.%s to be of the type %s' % (
self.view.__name__,
key,
form_class.__name__
)
)
def assertHttpCode(self, response, code):
'''
Checks if the response has the given status code
'''
self.assertEqual(
response.status_code, code,
msg='Expecting an HTTP %s response, but got HTTP %s' % (
code,
response.status_code
)
)
def assertHttpRedirect(self, response, redirect_to=None):
'''
Checks weather the response redirected, and if passed,
if it redirected to the expected loaction
'''
self.assertTrue(
300 <= response.status_code < 400,
'Expected an HTTP 3XX (redirect) response, but got HTTP %s' %
response.status_code
)
self.assertTrue(
'location' in response,
msg='Expecting a redirect to have an location, got none'
)
if redirect_to:
self.assertEqual(
response['location'],
redirect_to,
msg='Expecing the response to redirect to %s, where redirected to %s instea' % (
str(redirect_to),
str(response['location'])
)
)
def assertHttpOK(self, response):
self.assertHttpCode(response, 200)
def assertHttpCreated(self, response):
self.assertHttpCode(response, 201)
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)
class TaggableViewTestCaseMixin:
def assertTaggableContext(self, context):
self.assertTrue(
'all_tags' in context,
msg='Expecting the context for taggable to contain an \'all_tags\' attribute'
)
for tag in context['all_tags']:
self.assertTrue(
isinstance(tag, Tag),
msg='Expecting all entries to be an instance of %s, got %s' % (
str(Tag),
str(type(tag))
)
)
self.assertTrue(
'submit_form' in context,
msg='Expecting the context for taggable to contain \'submit_form\' attribute'
)
self.assertTrue(
'tagged_item' in context,
msg='Expecting the context for taggable to contain \'tagged_item\' attribute'
)
self.assertTrue(
isinstance(context['tagged_item'], Taggable),
msg='Expecting the tagged_item to be an instance of %s' % (
str(Taggable)
)
)
self.assertTrue(
'submit_url_name' in context,
msg='Expecting the context for taggable to contain \'submit_url_name\' attribute'
)
self.assertTrue(
type(context['submit_url_name']) == str,
msg='Expecting submit_url_name to be of type string'
)
self.assertTrue(
'delete_url_name' in context,
msg='Expecting the context for taggable to contain \'delete_url_name\' attribute'
)
self.assertTrue(
type(context['delete_url_name']) == str,
msg='Expecting delete_url_name to be of type string'
)
class MapableViewTestCaseMixin:
def assertMapableContext(self, context):
self.assertTrue(
'all_points' in context,
msg='Expecting the context for mapable point to contain \'all_points\' attribute'
)
for point in context['all_points']:
self.assertTrue(
isinstance(point, Mapable),
msg='Expecting all entries to be an instance of %s, got %s' % (
str(Mapable),
str(type(point))
)
)
self.assertTrue(
'map_center' in context,
msg='Expecting the context for mapable point to contain \'map_center\' attribute'
)
self.assertTrue(
'latitude' in context['map_center'],
msg='Expecting the map center to contain an \'latitude\' attribute'
)
self.assertTrue(
isinstance(context['map_center']['latitude'], float) or isinstance(context['map_center']['latitude'], int),
msg='Expecting the latitude of the map center to be numeric, type %s given' % (
str(type(context['map_center']['latitude']))
)
)
self.assertTrue(
-90 <= context['map_center']['latitude'] <= 90,
msg='Expecting the latitude of map center to be in the range of -90 and 90'
)
self.assertTrue(
'longitude' in context['map_center'],
msg='Expecting the map center to contain an \'longitude\' attribute'
)
self.assertTrue(
isinstance(context['map_center']['longitude'], float) or isinstance(context['map_center']['longitude'], int),
msg='Expecting the longitude of the map center to be numeric, type %s given' % (
str(type(context['map_center']['longitude']))
)
)
self.assertTrue(
-180 <= context['map_center']['longitude'] <= 180,
msg='Expecting the longitude of map center to be in the range of -180 and 180'
)

View File

@@ -0,0 +1,87 @@
import datetime
from django.test import TestCase, RequestFactory, Client
from django.urls import reverse_lazy
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.messages.storage.fallback import FallbackStorage
from lostplaces.models import Place
from lostplaces.views import IsAuthenticatedMixin
from lostplaces.tests.views import ViewTestCase
class TestIsAuthenticated(ViewTestCase):
view = IsAuthenticatedMixin
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
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)
# Expecting a 405 because IsAuthenticatedMixin has no 'get' method
self.assertHttpMethodNotAllowed(response)
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.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
response = self.client.get(response['Location'])
self.assertTrue(len(messages) > 0)
class TestIsPlaceSubmitterMixin(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.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 setUp(self):
self. client = Client()
def test_is_submitter(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1}))
self.assertEqual(response.status_code, 200)
def test_is_no_submitter(self):
User.objects.create_user(
username='manfred',
password='Develop123'
)
self.client.login(username='manfred', password='Develop123')
response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1}))
self.assertEqual(response.status_code, 403)
self.assertTrue(response.context['messages'])
self.assertTrue(len(response.context['messages']) > 0)

View File

@@ -0,0 +1,126 @@
import datetime
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User
from lostplaces.models import Place
from lostplaces.views import (
PlaceCreateView,
PlaceListView,
PlaceDetailView
)
from lostplaces.forms import PlaceImageCreateForm, PlaceForm
from lostplaces.tests.views import (
ViewTestCase,
TaggableViewTestCaseMixin,
MapableViewTestCaseMixin
)
class TestPlaceCreateView(ViewTestCase):
view = PlaceCreateView
@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.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_has_forms(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_create'))
self.assertHasForm(response, 'place_image_form', PlaceImageCreateForm)
self.assertHasForm(response, 'place_form', PlaceForm)
class TestPlaceListView(ViewTestCase):
view = PlaceListView
@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.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_list_view(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_list'))
self.assertContext(response, 'mapping_config')
class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase):
view = PlaceDetailView
@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.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 test_context(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_detail', kwargs={'pk': 1}))
self.assertTrue(
'tagging_config' in response.context,
msg='Expecting the context of %s to have an \'tagging_config\'' % (
str(self.view)
)
)
self.assertTaggableContext(response.context['tagging_config'])
self.assertTrue(
'mapping_config' in response.context,
msg='Expecting the context of %s to have an \'mapping_config\'' % (
str(self.view)
)
)
self.assertMapableContext(response.context['mapping_config'])