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

301 lines
9.7 KiB
Python
Raw Permalink Normal View History

2022-09-25 16:14:11 +02:00
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'
)
2022-09-25 18:02:59 +02:00
def test_draft_in_explorer_places(self):
user = User.objects.get(username='testpeter')
Place.objects.create(
name='Im the latest place in draft 3671',
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,
mode='draft'
)
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(
reverse('explorer_profile', kwargs={
'explorer_id': user.explorer.id
})
)
self.assertHttpOK(response)
self.assertFalse(
'Im the latest place in draft 3671' in response.content.decode(),
msg='Expecting a place in draft mode to not show up on the submitters profile page'
)
2022-09-25 16:14:11 +02:00
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'
2022-09-25 16:14:32 +02:00
)
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'
)