foerderbarometer/input/tests/views.py

179 lines
5.0 KiB
Python
Raw Normal View History

import random
2025-11-10 16:52:06 +00:00
from django.forms import model_to_dict
2025-10-14 14:48:39 +00:00
from django.shortcuts import resolve_url
2025-08-18 14:42:09 +00:00
from django.test import TestCase
2025-10-16 10:26:52 +00:00
from input.models import Library, TYPE_PROJ
2025-08-21 08:42:55 +00:00
from input.utils.testing import create_superuser, login, request
2025-11-10 16:52:06 +00:00
from input.views import TYPES, ApplicationView
2025-08-18 14:42:09 +00:00
PATHS = {TYPES[path].code: path for path in TYPES}
CODES = list(PATHS)
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')
def test_extern_post(self):
code = random.choice(CODES)
url = self.helper_url(code)
request(self, 'extern', expected_url=url, data={'url': url})
2025-11-10 16:53:10 +00:00
def test_extern_invalid_url(self):
request(self, 'extern', data={'url': 'https://domain.not/allowed/to/be/redirected/'})
2025-10-14 14:48:39 +00:00
@classmethod
def get_step_data(cls, choice, **data):
return {
'realname': 'Test',
'email': 'test@example.com',
'choice': choice,
'check': True,
**data,
}
2025-08-19 12:52:18 +00:00
@staticmethod
2025-10-14 14:48:39 +00:00
def helper_url(code):
return resolve_url('extern', type=PATHS[code])
2025-08-19 12:52:18 +00:00
2025-10-14 14:59:36 +00:00
def helper_extern_base(self, choice, text, data):
2025-10-14 14:48:39 +00:00
url = self.helper_url(choice)
2025-08-19 12:52:18 +00:00
2025-10-14 14:48:39 +00:00
response = request(self, url)
2025-08-18 14:42:09 +00:00
2025-08-19 12:52:18 +00:00
self.assertContains(response, text)
2025-10-14 14:48:39 +00:00
data = self.get_step_data(choice, **data)
2025-08-19 12:52:18 +00:00
2025-10-14 14:59:36 +00:00
return request(self, url, data=data)
def helper_extern(self, choice, text, data):
response = self.helper_extern_base(choice, text, data)
2025-08-19 12:52:18 +00:00
self.assertContains(response, 'Deine Anfrage wurde gesendet.')
2025-08-18 14:42:09 +00:00
def test_extern_types(self):
2025-08-19 12:52:18 +00:00
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-10-16 10:26:52 +00:00
(TYPE_PROJ, 'Projektförderung'),
2025-08-19 12:52:18 +00:00
]
2025-08-18 14:42:09 +00:00
2025-10-14 14:48:39 +00:00
for code, text in types:
with self.subTest(type=code):
url = self.helper_url(code)
response = request(self, url)
2025-08-19 12:52:18 +00:00
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,
'terms_accepted': True,
2025-08-19 12:52:18 +00:00
})
2025-08-18 14:42:09 +00:00
2025-10-14 14:59:36 +00:00
def test_extern_lit_without_consent_fails(self):
response = self.helper_extern_base('LIT', 'Literatur verwenden', {
'cost': 20,
'info': 'Test',
'source': 'Test',
'notes': '',
'selfbuy': 'TRUE',
'selfbuy_data': 'NONE',
'selfbuy_give_data': True,
'check': False,
})
self.assertContains(response, 'Dieses Feld ist zwingend erforderlich.')
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
2025-10-14 14:48:39 +00:00
def test_extern_invalid_code(self):
request(self, 'extern', args=['invalid'], status_code=404)
2025-11-10 16:52:06 +00:00
def test_unknown_name(self):
obj = Library(type=Library.TYPE)
data = model_to_dict(obj)
name = ApplicationView.get_recipient_name(obj, data)
self.assertEqual(name, 'Unbekannt')
2025-10-14 14:48:39 +00:00
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')