foerderbarometer/input/tests/views.py

167 lines
4.8 KiB
Python
Raw Normal View History

2025-08-18 14:42:09 +00:00
from django.test import TestCase
2025-08-21 08:42:55 +00:00
from input.models import Library
from input.utils.testing import create_superuser, login, request
2025-08-18 14:42:09 +00:00
2025-08-21 08:42:55 +00:00
class AnonymousViewTestCase(TestCase):
2025-08-18 14:42:09 +00:00
def test_index(self):
2025-08-21 08:08:24 +00:00
response = request(self, 'index')
self.assertContains(response, '<a href="https://srcsrv.wikimedia.de/beba/foerderbarometer">Sourcecode</a>')
2025-08-18 14:42:09 +00:00
def test_extern(self):
request(self, 'extern')
2025-08-19 12:52:18 +00:00
@staticmethod
def get_step_data(step, data):
return {'extern_view-current_step': step, **data}
@classmethod
def get_first_step_data(cls, choice):
return cls.get_step_data(0, {
2025-08-18 14:42:09 +00:00
'0-realname': 'Test',
'0-email': 'test@example.com',
2025-08-19 12:52:18 +00:00
'0-choice': choice,
2025-08-18 14:42:09 +00:00
'0-check': True,
2025-08-19 12:52:18 +00:00
})
def helper_extern(self, choice, text, data):
first_step_data = self.get_first_step_data(choice)
response = request(self, 'extern', data=first_step_data)
2025-08-18 14:42:09 +00:00
2025-08-19 12:52:18 +00:00
self.assertContains(response, text)
second_step_data = self.get_step_data(1, data)
response = request(self, 'extern', data=second_step_data)
self.assertContains(response, 'Deine Anfrage wurde gesendet.')
2025-08-18 14:42:09 +00:00
2025-08-19 12:52:18 +00:00
def test_extern_first_steps(self):
types = [
('BIB', 'Bibliotheksausweis'),
('ELIT', 'Online-Ressource'),
('MAIL', 'Mailadresse beantragen'),
('IFG', 'gewonnenen Informationen'),
('LIT', 'Literatur verwenden'),
('LIST', 'Mailingliste beantragen'),
('TRAV', 'Transportmittel'),
('SOFT', 'Lizenz'),
('VIS', 'DIN 5008'),
]
2025-08-18 14:42:09 +00:00
2025-08-19 12:52:18 +00:00
for choice, text in types:
with self.subTest(type=choice):
self.client.session.clear()
data = self.get_first_step_data(choice)
response = request(self, 'extern', data=data)
self.assertContains(response, text)
def test_extern_travel(self):
self.helper_extern('TRAV', 'Transportmittel', {
2025-08-18 14:42:09 +00:00
'project_name': 'Test',
'transport': 'BAHN',
'travelcost': 10,
'checkin': '2025-01-01',
'checkout': '2025-01-02',
'hotel': 'TRUE',
'notes': '',
2025-08-19 12:52:18 +00:00
})
2025-08-18 14:42:09 +00:00
2025-08-19 12:52:18 +00:00
def test_extern_lit(self):
self.helper_extern('LIT', 'Literatur verwenden', {
'cost': 20,
'info': 'Test',
'source': 'Test',
'notes': '',
'selfbuy': 'TRUE',
'selfbuy_data': 'NONE',
'selfbuy_give_data': True,
'check': True,
})
2025-08-18 14:42:09 +00:00
2025-08-19 12:52:18 +00:00
def test_extern_bib(self):
self.helper_extern('BIB', 'Bibliotheksausweis', {
'cost': 20,
'library': 'Test',
'duration': 'Test',
'notes': '',
})
2025-08-21 08:42:55 +00:00
class AuthenticatedViewTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = create_superuser('staff')
def setUp(self):
login(self)
def test_export(self):
request(self, 'export')
def helper_auth_deny(self, view, expected):
obj = Library.objects.create(library='Test')
request(self, view, args=[obj.type, obj.id])
obj.refresh_from_db(fields=['granted'])
self.assertEqual(obj.granted, expected)
def helper_auth_deny_error(self, view):
response = request(self, view, args=['TEST', 1])
self.assertContains(response, 'ERROR')
def test_authorize(self):
self.helper_auth_deny('authorize', True)
def test_authorize_error(self):
self.helper_auth_deny_error('authorize')
def test_deny(self):
self.helper_auth_deny('deny', False)
def test_deny_error(self):
self.helper_auth_deny_error('deny')
class TermsConsentViewTests(AnonymousViewTestCase):
def test_extern_lit(self):
"""
Negative case: Literature form without terms_accepted must not succeed
and should display a required field error.
"""
# Step 1
resp = request(self, 'extern', data=self.get_first_step_data('LIT'))
self.assertContains(resp, 'Literatur verwenden')
# Step 2: submit without terms_accepted
payload = self.get_step_data(1, {
'cost': 20,
'info': 'Test',
'source': 'Test',
'notes': '',
'selfbuy': 'TRUE',
'selfbuy_data': 'NONE',
'selfbuy_give_data': 'on', # checkbox checked
# terms_accepted intentionally omitted
})
resp = request(self, 'extern', data=payload)
# Expect to remain on step 2 with required field error
self.assertEqual(resp.status_code, 200)
self.assertNotContains(resp, 'Deine Anfrage wurde gesendet.')
self.assertRegex(
resp.content.decode(),
r'(Dieses Feld ist erforderlich|This field is required\.)'
)