diff --git a/input/tests/__init__.py b/input/tests/__init__.py index e44ead7..89a8481 100644 --- a/input/tests/__init__.py +++ b/input/tests/__init__.py @@ -1,2 +1,3 @@ +from .admin import AdminTestCase from .models import ModelTestCase from .views import AuthenticatedViewTestCase, AnonymousViewTestCase diff --git a/input/tests/admin.py b/input/tests/admin.py new file mode 100644 index 0000000..5116180 --- /dev/null +++ b/input/tests/admin.py @@ -0,0 +1,115 @@ +import datetime + +from django.core import mail +from django.forms import model_to_dict +from django.test import TestCase + +from input.models import ( + Project, + ProjectRequest, + ProjectDeclined, + Library, + ELiterature, + Email, + IFG, + Literature, + List, + Travel, + Software, + BusinessCard, ProjectCategory, WikimediaProject, Account, +) +from input.utils.admin import admin_url +from input.utils.testing import request, create_superuser, login + + +class AdminTestCase(TestCase): + + @classmethod + def setUpTestData(cls): + cls.user = user = create_superuser('admin', first_name='Max', last_name='Mustermann') + + cls.data = data = { + 'realname': f'{user.first_name} {user.last_name}', + 'email': user.email, + } + + cls.objs = [ + ProjectCategory.objects.order_by('?').first(), + WikimediaProject.objects.order_by('?').first(), + Project.objects.create(**data, granted=True), + ProjectRequest.objects.create(**data, granted=None), + ProjectDeclined.objects.create(**data, granted=False), + Library.objects.create(**data, library='Test'), + ELiterature.objects.create(**data, library='Test'), + Software.objects.create(**data, library='Test'), + Email.objects.create(**data), + IFG.objects.create(**data), + Literature.objects.create(**data, selfbuy_give_data=False), + List.objects.create(**data), + Travel.objects.create(**data), + BusinessCard.objects.create(**data), + ] + + def setUp(self): + login(self) + + def test_changelists(self): + for obj in self.objs: + model = type(obj) + url = admin_url(model, 'changelist') + + with self.subTest(model=model): + request(self, url) + + def test_change_views(self): + for obj in self.objs: + model = type(obj) + url = admin_url(model, 'change', obj.id) + + with self.subTest(model=model): + request(self, url) + + def test_display_values(self): + for obj in self.objs: + model = type(obj) + + with self.subTest(model=model): + self.assertTrue(f'{obj}') + + def test_grant_project_request(self): + account = Account.objects.create(code='test') + category = ProjectCategory.objects.first() + wikimedia = WikimediaProject.objects.first() + obj = ProjectRequest.objects.create( + **self.data, + name='Test', + description='Test', + otrs='https://example.com', + granted=None, + start=datetime.date(2025, 1, 1), + end=datetime.date(2026, 1, 1), + ) + + obj.categories.add(category) + obj.wikimedia_projects.add(wikimedia) + + url = admin_url(ProjectRequest, 'change', obj.id) + expected_url = admin_url(ProjectRequest, 'changelist') + data = { + **model_to_dict(obj), + 'granted': True, + 'granted_date': obj.start, + 'granted_from': self.user.username, + 'account': account.code, + 'categories': [category.id], + 'wikimedia_projects': [wikimedia.id], + } + + for key in list(data): + if data[key] is None: + data.pop(key) + + with self.captureOnCommitCallbacks(execute=True): + request(self, url, expected_url=expected_url, data=data) + + self.assertEqual(len(mail.outbox), 2) diff --git a/input/utils/admin.py b/input/utils/admin.py new file mode 100644 index 0000000..1efb627 --- /dev/null +++ b/input/utils/admin.py @@ -0,0 +1,18 @@ +from django.contrib import admin +from django.db.models import Model +from django.urls import reverse + + +def admin_url(model: type[Model], view: str, *args, site=None, **kwargs) -> str: + return reverse(admin_url_name(model, view, site=site), args=args, kwargs=kwargs) + + +def admin_url_name(model: type[Model], view: str, *, site=None) -> str: + namespace = (site or admin.site).name + view_name = admin_view_name(model, view) + + return f'{namespace}:{view_name}' + + +def admin_view_name(model: type[Model], view: str) -> str: + return f'{model._meta.app_label}_{model._meta.model_name}_{view}'