import random from django.forms import model_to_dict from django.shortcuts import resolve_url from django.test import TestCase from foerderbarometer.constants import * from input.models import Library, ProjectCategory, WikimediaProject from input.utils.testing import create_superuser, login, request from input.views import PROJECT_FUNDING, TYPES, ApplicationView PATHS = {TYPES[path].code: path for path in TYPES} PATHS[TYPE_PROJ] = PROJECT_FUNDING[0].path CODES = list(PATHS) class AnonymousViewTestCase(TestCase): def test_index(self): response = request(self, 'index') self.assertContains(response, 'Sourcecode') 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 = [ (TYPE_BIB, 'Bibliotheksausweis'), (TYPE_ELIT, 'Online-Ressource'), (TYPE_MAIL, 'Mailadresse beantragen'), (TYPE_IFG, 'gewonnenen Informationen'), (TYPE_LIT, 'Literatur verwenden'), (TYPE_LIST, 'Mailingliste beantragen'), (TYPE_TRAV, 'Transportmittel'), (TYPE_SOFT, 'Lizenz'), (TYPE_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(TYPE_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(TYPE_LIT, 'Literatur verwenden', { 'cost': 20, 'info': 'Test', 'source': 'Test', 'notes': '', 'selfbuy': 'FALSE', 'selfbuy_data': 'Test', 'selfbuy_give_data': True, 'check': True, 'terms_accepted': True, }) def test_extern_lit_without_consent_fails(self): response = self.helper_extern_base(TYPE_LIT, 'Literatur verwenden', { 'cost': 20, 'info': 'Test', 'source': 'Test', 'notes': '', 'selfbuy': 'TRUE', 'selfbuy_data': '', 'selfbuy_give_data': False, '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_proj(self): category = ProjectCategory.objects.order_by('?').first() wikimedia_project = WikimediaProject.objects.order_by('?').first() self.helper_extern(TYPE_PROJ, 'Projektförderung', { 'name': 'Test', 'description': 'Test', 'categories': [category.id, 0], 'categories_other': 'Test', 'wikimedia_projects': [wikimedia_project.id, 0], 'wikimedia_projects_other': 'Test', 'start': '2025-01-01', 'end': '2025-01-02', 'participants_estimated': 1, 'cost': 20, }) 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')