Testing explorer views

This commit is contained in:
Leonhard Strohmidel 2022-09-25 16:14:11 +02:00
parent f06b6bdae9
commit 06d68380c9
2 changed files with 169 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

View File

@ -0,0 +1,169 @@
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'
)