foerderbarometer/input/tests/views.py

179 lines
5.0 KiB
Python

import random
from django.forms import model_to_dict
from django.shortcuts import resolve_url
from django.test import TestCase
from input.models import Library, TYPE_PROJ
from input.utils.testing import create_superuser, login, request
from input.views import TYPES, ApplicationView
PATHS = {TYPES[path].code: path for path in TYPES}
CODES = list(PATHS)
class AnonymousViewTestCase(TestCase):
def test_index(self):
response = request(self, 'index')
self.assertContains(response, '<a href="https://srcsrv.wikimedia.de/beba/foerderbarometer">Sourcecode</a>')
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})
def test_extern_invalid_url(self):
request(self, 'extern', data={'url': 'https://domain.not/allowed/to/be/redirected/'})
@classmethod
def get_step_data(cls, choice, **data):
return {
'realname': 'Test',
'email': 'test@example.com',
'choice': choice,
'check': True,
**data,
}
@staticmethod
def helper_url(code):
return resolve_url('extern', type=PATHS[code])
def helper_extern_base(self, choice, text, data):
url = self.helper_url(choice)
response = request(self, url)
self.assertContains(response, text)
data = self.get_step_data(choice, **data)
return request(self, url, data=data)
def helper_extern(self, choice, text, data):
response = self.helper_extern_base(choice, text, data)
self.assertContains(response, 'Deine Anfrage wurde gesendet.')
def test_extern_types(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'),
(TYPE_PROJ, 'Projektförderung'),
]
for code, text in types:
with self.subTest(type=code):
url = self.helper_url(code)
response = request(self, url)
self.assertContains(response, text)
def test_extern_travel(self):
self.helper_extern('TRAV', 'Transportmittel', {
'project_name': 'Test',
'transport': 'BAHN',
'travelcost': 10,
'checkin': '2025-01-01',
'checkout': '2025-01-02',
'hotel': 'TRUE',
'notes': '',
})
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,
})
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.')
def test_extern_bib(self):
self.helper_extern('BIB', 'Bibliotheksausweis', {
'cost': 20,
'library': 'Test',
'duration': 'Test',
'notes': '',
})
def test_extern_invalid_code(self):
request(self, 'extern', args=['invalid'], status_code=404)
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')
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')