2020-09-12 08:39:06 +02:00
|
|
|
import datetime
|
|
|
|
|
2020-09-13 12:39:02 +02:00
|
|
|
from django.test import TestCase, RequestFactory, Client
|
2020-09-04 21:48:05 +02:00
|
|
|
from django.urls import reverse_lazy
|
2020-09-13 12:39:02 +02:00
|
|
|
from django.contrib.auth.models import User, AnonymousUser
|
|
|
|
from django.contrib.messages.storage.fallback import FallbackStorage
|
2020-09-04 21:48:05 +02:00
|
|
|
|
2020-09-14 17:26:17 +02:00
|
|
|
from lostplaces.models import Place
|
|
|
|
from lostplaces.views import IsAuthenticatedMixin
|
|
|
|
from lostplaces.tests.views import ViewTestCase
|
2020-09-13 13:28:22 +02:00
|
|
|
|
2020-09-13 19:43:47 +02:00
|
|
|
class TestIsAuthenticated(ViewTestCase):
|
2020-09-13 13:28:22 +02:00
|
|
|
view = IsAuthenticatedMixin
|
2020-09-04 21:48:05 +02:00
|
|
|
|
2020-09-12 08:39:06 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
user = User.objects.create_user(
|
|
|
|
username='testpeter',
|
|
|
|
password='Develop123'
|
|
|
|
)
|
2020-09-13 19:43:47 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = Client()
|
2020-09-04 21:48:05 +02:00
|
|
|
|
|
|
|
def test_logged_in(self):
|
2020-09-13 12:39:02 +02:00
|
|
|
request = RequestFactory().get('/')
|
|
|
|
request.user = User.objects.get(id=1)
|
|
|
|
|
|
|
|
response = IsAuthenticatedMixin.as_view()(request)
|
2020-09-13 13:28:22 +02:00
|
|
|
# Expecting a 405 because IsAuthenticatedMixin has no 'get' method
|
|
|
|
self.assertHttpMethodNotAllowed(response)
|
2020-09-04 21:48:05 +02:00
|
|
|
|
|
|
|
def test_not_logged_in(self):
|
2020-09-13 12:39:02 +02:00
|
|
|
request = RequestFactory().get('/someurl1234')
|
|
|
|
request.user = AnonymousUser()
|
|
|
|
request.session = 'session'
|
|
|
|
messages = FallbackStorage(request)
|
|
|
|
request._messages = messages
|
|
|
|
|
|
|
|
response = IsAuthenticatedMixin.as_view()(request)
|
2020-09-13 13:28:22 +02:00
|
|
|
self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
|
|
|
|
|
|
|
|
response = self.client.get(response['Location'])
|
|
|
|
self.assertTrue(len(messages) > 0)
|
2020-09-04 21:48:05 +02:00
|
|
|
|
2020-09-13 10:57:53 +02:00
|
|
|
class TestIsPlaceSubmitterMixin(TestCase):
|
2020-09-04 21:48:05 +02:00
|
|
|
|
2020-09-12 08:39:06 +02:00
|
|
|
@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()
|
2020-09-13 19:43:47 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = Client()
|
2020-09-12 08:39:06 +02:00
|
|
|
|
2020-09-04 21:48:05 +02:00
|
|
|
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):
|
2020-09-10 22:30:29 +02:00
|
|
|
User.objects.create_user(
|
2020-09-04 21:48:05 +02:00
|
|
|
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)
|