lostplaces-backend/django_lostplaces/lostplaces/tests/views/test_place_views.py

214 lines
6.9 KiB
Python
Raw Normal View History

2020-09-19 22:50:07 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2020-09-12 08:39:06 +02:00
2021-06-12 15:07:14 +02:00
import re
from os import path
2020-09-13 19:43:47 +02:00
from django.test import TestCase, Client
from django.urls import reverse
2020-09-12 08:39:06 +02:00
from django.contrib.auth.models import User
from django.utils import timezone
2021-06-12 15:07:14 +02:00
from django.conf import settings
2020-09-04 21:48:05 +02:00
from lostplaces.models import Place
from lostplaces.views import (
2020-09-12 11:02:39 +02:00
PlaceCreateView,
PlaceListView,
PlaceDetailView
2020-09-12 11:02:39 +02:00
)
from lostplaces.forms import PlaceImageForm, PlaceForm
from lostplaces.tests.views import (
ViewTestCase,
TaggableViewTestCaseMixin,
2021-05-15 23:27:25 +02:00
MapableViewTestCaseMixin,
GlobalTemplateTestCaseMixin
)
2020-09-04 21:48:05 +02:00
2021-05-15 23:27:25 +02:00
class TestPlaceListView(GlobalTemplateTestCaseMixin, ViewTestCase):
2021-06-12 15:07:14 +02:00
"""
Tests the view listing all placs
"""
2021-05-15 23:27:25 +02:00
view = PlaceListView
2020-09-12 08:39:06 +02:00
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
2021-06-12 15:07:14 +02:00
for i in range(12):
place = Place.objects.create(
name='Im a place %d' % i,
submitted_when=timezone.now(),
submitted_by=user.explorer,
location='Test %d town' % i,
latitude=50.5 + i/10,
longitude=7.0 - i/10,
description='This is just a test, do not worry'
)
2020-09-12 08:39:06 +02:00
place.tags.add('I a tag', 'testlocation')
place.save()
2020-09-13 19:43:47 +02:00
def setUp(self):
self.client = Client()
2021-05-15 23:27:25 +02:00
def test_list_view(self):
2020-09-04 21:48:05 +02:00
self.client.login(username='testpeter', password='Develop123')
2021-05-15 23:27:25 +02:00
response = self.client.get(reverse('place_list'))
2021-05-15 23:27:25 +02:00
self.assertContext(response, 'mapping_config')
self.assertGlobal(response)
2021-06-12 15:07:14 +02:00
def test_pagination(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_list'))
2021-06-12 15:07:14 +02:00
self.assertNotEqual(
None,
re.search(
"""<ul.*pagination.*>\s*(<li.*>.*</li>\s*){6,}</ul>""",
response.content.decode().replace('\n', '').lower()
),
msg='Expecting the place list to be paginated like [first] [previous] [item] at least 2 times [next] [last]'
)
2021-05-15 23:27:25 +02:00
class TestPlaceCreateView(ViewTestCase):
view = PlaceCreateView
2020-09-12 11:02:39 +02:00
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
2020-09-04 21:48:05 +02:00
)
2020-09-13 19:43:47 +02:00
def setUp(self):
self.client = Client()
2021-06-12 15:07:14 +02:00
2021-05-15 23:27:25 +02:00
def test_has_forms(self):
2020-09-12 11:02:39 +02:00
self.client.login(username='testpeter', password='Develop123')
2021-05-15 23:27:25 +02:00
response = self.client.get(reverse('place_create'))
2021-05-15 23:27:25 +02:00
self.assertHasForm(response, 'place_image_form', PlaceImageForm)
self.assertHasForm(response, 'place_form', PlaceForm)
2021-06-12 15:07:14 +02:00
def test_positive_no_image(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.post(
reverse('place_create'),
{
'name': 'test place 486',
'location': 'test location',
'latitude': 45.804192,
'longitude': 1.860222,
'description': """
Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt.
"""
}
)
self.assertHttpRedirect(response)
place = Place.objects.get(name='test place 486')
self.assertNotEqual(
None,
place,
msg='Submitted place not found in database / model'
),
self.assertNotEqual(
None,
re.search(
""".*%s""" % reverse(
'place_detail', kwargs={'pk': place.id}
),
response.url
)
)
def test_positive_image(self):
self.client.login(username='testpeter', password='Develop123')
image = open(
path.join(
settings.BASE_DIR,
'testdata',
'test_image.jpg'
)
)
response = self.client.post(
reverse('place_create'),
{
'name': 'test place 894',
'location': 'test location',
'latitude': 45.804192,
'longitude': 1.860222,
'description': """
Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt.
""",
'images': [image]
}
)
self.assertHttpRedirect(response)
place = Place.objects.get(name='test place 894')
self.assertNotEqual(
None,
place,
msg='Submitted place not found in database / model'
),
self.assertNotEqual(
None,
re.search(
""".*%s""" % reverse(
'place_detail', kwargs={'pk': place.id}
),
response.url
)
)
self.assertEqual(
len(place.placeimages.all()),
1,
msg='Expecting the place to have exactly 1 place image'
)
2020-09-14 15:18:21 +02:00
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=timezone.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)
)
)
2020-09-14 15:18:21 +02:00
self.assertMapableContext(response.context['mapping_config'])