lostplaces-backend/lostplaces/lostplaces_app/tests/views/test_base_views.py

94 lines
3.1 KiB
Python
Raw Normal View History

2020-09-12 08:39:06 +02:00
import datetime
2020-09-04 21:48:05 +02:00
from django.test import TestCase, Client
from django.urls import reverse_lazy
2020-09-12 08:39:06 +02:00
from django.contrib.auth.models import User
2020-09-04 21:48:05 +02:00
from lostplaces_app.models import Place
from django.contrib.auth.models import User
2020-09-04 21:48:05 +02:00
class TestIsAuthenticated(TestCase):
2020-09-11 23:07:19 +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.objects.get(username='testpeter').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-04 21:48:05 +02:00
def setUp(self):
self. client = Client()
def test_logged_in(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1}))
self.assertEqual(response.status_code, 200)
def test_not_logged_in(self):
url = reverse_lazy('place_detail', kwargs={'pk': 1})
response = self.client.get(url, follow=True)
self.assertRedirects(
response=response,
expected_url='?'.join([str(reverse_lazy('login')), 'next=/place/1/']),
2020-09-04 21:48:05 +02:00
status_code=302,
target_status_code=200,
msg_prefix='''Accesing an IsAuthenticated view while not logged should
redirect to login page with redirect params
''',
fetch_redirect_response=True
)
self.assertTrue(response.context['messages'])
self.assertTrue(len(response.context['messages']) > 0)
class TestIsPlaceSubmitter(TestCase):
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-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):
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)