Compare commits

...

4 Commits

Author SHA1 Message Date
Leonhard Strohmidel
724c26c926 #64 TDD: Tests for draft view 2022-09-25 16:14:32 +02:00
Leonhard Strohmidel
06d68380c9 Testing explorer views 2022-09-25 16:14:11 +02:00
Leonhard Strohmidel
f06b6bdae9 Fixing PlaceImage tests 2022-09-25 16:13:19 +02:00
Leonhard Strohmidel
7a7c06882a #64 Testing place modes 2022-09-25 16:12:14 +02:00
7 changed files with 326 additions and 4 deletions

View File

@ -14,7 +14,7 @@
<div class="LP-PlaceTeaser__Meta"> <div class="LP-PlaceTeaser__Meta">
<div class="LP-PlaceTeaser__Info"> <div class="LP-PlaceTeaser__Info">
<span class="LP-PlaceTeaser__Title"> <span class="LP-PlaceTeaser__Title">
<h1 class="LP-Headline LP-Headline--teaser">{{place.name|truncatechars:19}}</h1> <h1 class="LP-Headline LP-Headline--teaser">{{place.name}}</h1>
</span> </span>
<span class="LP-PlaceTeaser__Detail"> <span class="LP-PlaceTeaser__Detail">
<p class="LP-Paragraph">{{place.location|truncatechars:25}}</p> <p class="LP-Paragraph">{{place.location|truncatechars:25}}</p>

View File

@ -42,21 +42,25 @@ class PlaceImageTestCase(ModelTestCase):
if not os.path.isdir(settings.MEDIA_ROOT): if not os.path.isdir(settings.MEDIA_ROOT):
os.mkdir(settings.MEDIA_ROOT) os.mkdir(settings.MEDIA_ROOT)
images_dir = os.path.join(
settings.MEDIA_ROOT,
settings.RELATIVE_THUMBNAIL_PATH,
)
current_dir = os.path.dirname(os.path.abspath(__file__)) current_dir = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')): if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')):
shutil.copyfile( shutil.copyfile(
os.path.join(current_dir, 'im_a_image.jpeg'), os.path.join(current_dir, 'im_a_image.jpeg'),
os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg') os.path.join(images_dir, 'im_a_image_copy.jpeg')
) )
shutil.copyfile( shutil.copyfile(
os.path.join(current_dir, 'im_a_image.jpeg'), os.path.join(current_dir, 'im_a_image.jpeg'),
os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg') os.path.join(images_dir, 'im_a_image_changed.jpeg')
) )
PlaceImage.objects.create( PlaceImage.objects.create(
description='Im a description', description='Im a description',
filename=os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg'), filename=os.path.join(images_dir, 'im_a_image_copy.jpeg'),
place=place, place=place,
submitted_when=timezone.now(), submitted_when=timezone.now(),
submitted_by=user.explorer submitted_by=user.explorer

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

View File

@ -46,6 +46,10 @@ class TestIsAuthenticated(ViewTestCase):
self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/'])) self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/']))
self.assertTrue(len(messages) > 0) self.assertTrue(len(messages) > 0)
self.assertTrue(
_('Please login to proceed') in response.content.decode(),
msg='Expecting a message to tell the user to login'
)
class TestIsPlaceSubmitterMixin(TestCase): class TestIsPlaceSubmitterMixin(TestCase):

View File

@ -0,0 +1,272 @@
import os
import shutil
from django.utils.translation import gettext as _
from django.utils import timezone
from django.contrib.auth.models import User
from django.conf import settings
from django.urls import reverse
from lostplaces.tests.views import (
ViewTestCase,
GlobalTemplateTestCaseMixin
)
from lostplaces.views import ExplorerProfileView
from lostplaces.models import(
Place,
PlaceImage,
PhotoAlbum
)
class TestExplorerProfileView(GlobalTemplateTestCaseMixin, ViewTestCase):
view = ExplorerProfileView
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
def test_unauth_profile_access(self):
user = User.objects.get(username='testpeter')
response = self.client.get(
reverse('explorer_profile', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpCode(response, 302)
self.assertFalse(
user.username in response.content.decode(),
msg='Expecting the username to not be visible to unauthorized users'
)
def test_unauth_profile_access_follow_redirect(self):
user = User.objects.get(username='testpeter')
response = self.client.get(
reverse('explorer_profile', kwargs={
'explorer_id': user.explorer.id
}),
follow=True
)
self.assertHttpOK(response)
self.assertTrue(
_('Please login to proceed') in response.content.decode(),
msg='Expecting a message to tell the user to login'
)
def test_explorer_places(self):
user = User.objects.get(username='testpeter')
Place.objects.create(
name='Im the latest place 4369',
submitted_when=timezone.now(),
submitted_by=user.explorer,
location='Test %d town' % 5,
latitude=50.5 + 5/10,
longitude=7.0 - 5/10,
description='This is just a test, do not worry %d' % 5,
level=3
)
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(
reverse('explorer_profile', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpOK(response)
self.assertTrue(
'Im the latest place 4369' in response.content.decode(),
msg='Expecting the latest place to be visible on the submitters profile page'
)
def test_explorer_image(self):
user = User.objects.get(username='testpeter')
place = Place.objects.create(
name='Im a the latest place 4369',
submitted_when=timezone.now(),
submitted_by=user.explorer,
location='Test %d town' % 5,
latitude=50.5 + 5/10,
longitude=7.0 - 5/10,
description='This is just a test, do not worry %d' % 5,
level=3
)
current_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(
settings.MEDIA_ROOT,
settings.RELATIVE_THUMBNAIL_PATH,
'im_a_image_3649.jpeg'
)
shutil.copyfile(
os.path.join(current_dir, 'im_a_image.jpeg'),
file_path
)
PlaceImage.objects.create(
description='Im a description',
filename=file_path,
place=place,
submitted_when=timezone.now(),
submitted_by=user.explorer
)
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(
reverse('explorer_profile', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpOK(response)
self.assertTrue(
os.path.join(settings.RELATIVE_THUMBNAIL_PATH,'im_a_image_3649.jpeg') in response.content.decode(),
msg='Expecting the latest place image to be visible on the submitters profile page'
)
def test_explorer_photoalbum(self):
user = User.objects.get(username='testpeter')
place = Place.objects.create(
name='Im a the latest place 4369',
submitted_when=timezone.now(),
submitted_by=user.explorer,
location='Test %d town' % 5,
latitude=50.5 + 5/10,
longitude=7.0 - 5/10,
description='This is just a test, do not worry %d' % 5,
level=3
)
PhotoAlbum.objects.create(
place=place,
submitted_by=user.explorer,
url='http://example.org/6897134',
label='Im a exmpale link label 6423'
)
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(
reverse('explorer_profile', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpOK(response)
self.assertTrue(
'href="http://example.org/6897134"' in response.content.decode(),
msg='Expecting the latest photoalbum url to be linked on the submitters profile page'
)
self.assertTrue(
'Im a exmpale link label 6423' in response.content.decode(),
msg='Expecting the latest photoalbum label to be on the submitters profile page'
)
class TestExplorerDraftsView(GlobalTemplateTestCaseMixin, ViewTestCase):
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
User.objects.create_user(
username='otheruser',
password='Develop123'
)
superuser = User.objects.create_user(
username='toor',
password='Develop123'
)
superuser.is_superuser = True
superuser.save()
def test_draft_view(self):
user = User.objects.get(username='testpeter')
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(
reverse('explorer_drafts', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpOK(response)
def test_draft_view_unauthorized_user(self):
user = User.objects.get(username='testpeter')
self.client.login(username='otheruser', password='Develop123')
response = self.client.get(
reverse('explorer_drafts', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpForbidden(response)
def test_draft_view_superuser(self):
user = User.objects.get(username='testpeter')
self.client.login(username='toor', password='Develop123')
response = self.client.get(
reverse('explorer_drafts', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpOK(response)
def test_place_in_draft_view(self):
user = User.objects.get(username='testpeter')
Place.objects.create(
name='Im a draft place 3792',
submitted_when=timezone.now(),
submitted_by=user.explorer,
location='Test %d town' % 5,
latitude=50.5 + 5/10,
longitude=7.0 - 5/10,
description='This is just a test, do not worry %d' % 5,
mode='draft',
level=3
)
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(
reverse('explorer_drafts', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertTrue(
'Im a draft place 3792' in response.content.decode(),
msg='Expecting a place draft to be visible in the submitters drafs view'
)
def test_place_not_in_draft_view(self):
user = User.objects.get(username='testpeter')
Place.objects.create(
name='Im a draft place 3819',
submitted_when=timezone.now(),
submitted_by=user.explorer,
location='Test %d town' % 5,
latitude=50.5 + 5/10,
longitude=7.0 - 5/10,
description='This is just a test, do not worry %d' % 5,
level=3
)
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(
reverse('explorer_drafts', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertFalse(
'Im a draft place 3819' in response.content.decode(),
msg='Expecting a live place to not be visible in the submitters drafs view'
)

View File

@ -242,6 +242,11 @@ class TestPlaceCreateView(ViewTestCase):
'success', 'success',
msg='Expecting a visible success message' msg='Expecting a visible success message'
) )
self.assertEqual(
Place.objects.get(name='test place 486').mode,
'live',
msg='Expeting the place to be in \'live\' mode'
)
def test_positive_image(self): def test_positive_image(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
@ -291,6 +296,11 @@ class TestPlaceCreateView(ViewTestCase):
'success', 'success',
msg='Expecting a visible success message' msg='Expecting a visible success message'
) )
self.assertEqual(
Place.objects.get(name='test place 894').mode,
'live',
msg='Expeting the place to be in \'live\' mode'
)
def test_negative_no_name(self): def test_negative_no_name(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
@ -396,6 +406,31 @@ class TestPlaceCreateView(ViewTestCase):
msg='Expecing a visible error message' msg='Expecing a visible error message'
) )
def test_positve_save_as_draft(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.post(
reverse('place_create'),
{
'name': 'test name 6483',
'location': 'wurstwasser',
'latitude': 45.4654,
'longitude': 68.135489,
'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.
""",
'draft': True
},
follow=True
)
self.assertHttpOK(response)
self.assertEqual(
Place.objects.get(name='test name 6483').mode,
'draft',
msg='Expeting the place to be in \'draft\' mode'
)
class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase): class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase):
view = PlaceDetailView view = PlaceDetailView
@ -449,6 +484,7 @@ class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixi
self.client.login(username='blubberbernd', password='Develop123') self.client.login(username='blubberbernd', password='Develop123')
response = self.client.get(reverse('place_detail', kwargs={'pk': 1})) response = self.client.get(reverse('place_detail', kwargs={'pk': 1}))
self.assertHttpForbidden(response)
self.assertFalse( self.assertFalse(
'Im a place' in response.content.decode(), 'Im a place' in response.content.decode(),
msg='Expecting the user to not see the places' msg='Expecting the user to not see the places'
@ -458,6 +494,7 @@ class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixi
self.client.login(username='toor', password='Develop123') self.client.login(username='toor', password='Develop123')
response = self.client.get(reverse('place_detail', kwargs={'pk': 1})) response = self.client.get(reverse('place_detail', kwargs={'pk': 1}))
self.assertHttpOK(response)
self.assertTrue( self.assertTrue(
'Im a place' in response.content.decode(), 'Im a place' in response.content.decode(),
msg='Expecting the superuser to see all places' msg='Expecting the superuser to see all places'
@ -467,6 +504,7 @@ class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixi
self.client.login(username='blubberbernd', password='Develop123') self.client.login(username='blubberbernd', password='Develop123')
response = self.client.get(reverse('place_detail', kwargs={'pk': 2})) response = self.client.get(reverse('place_detail', kwargs={'pk': 2}))
self.assertHttpOK(response)
self.assertTrue( self.assertTrue(
'Im a own place' in response.content.decode(), 'Im a own place' in response.content.decode(),
msg='Expecting the user to see it\'s own places' msg='Expecting the user to see it\'s own places'
@ -476,6 +514,7 @@ class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixi
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_detail', kwargs={'pk': 2})) response = self.client.get(reverse('place_detail', kwargs={'pk': 2}))
self.assertHttpOK(response)
self.assertTrue( self.assertTrue(
'Im a own place' in response.content.decode(), 'Im a own place' in response.content.decode(),
msg='Expecting the user to see places where their level is high enough' msg='Expecting the user to see places where their level is high enough'

View File

@ -113,6 +113,9 @@ class PlaceCreateView(MultiplePlaceImageUploadMixin, IsAuthenticatedMixin, View)
place = place_form.save(commit=False) place = place_form.save(commit=False)
# Save logged in user as "submitted_by" # Save logged in user as "submitted_by"
place.submitted_by = submitter place.submitted_by = submitter
if place_form.cleaned_data['draft']:
place.mode = 'draft';
place.save() place.save()
self.handle_place_images(request, place) self.handle_place_images(request, place)