admin: add generic tests
This commit is contained in:
parent
5ba55356a9
commit
db69c3e38d
|
@ -0,0 +1,37 @@
|
||||||
|
"""admin tests"""
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from django.shortcuts import reverse
|
||||||
|
from django.test import Client, TestCase
|
||||||
|
from django.urls.exceptions import NoReverseMatch
|
||||||
|
|
||||||
|
from passbook.admin.urls import urlpatterns
|
||||||
|
from passbook.core.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class TestAdmin(TestCase):
|
||||||
|
"""Generic admin tests"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.user = User.objects.create_superuser(username="test")
|
||||||
|
self.client = Client()
|
||||||
|
self.client.force_login(self.user)
|
||||||
|
|
||||||
|
|
||||||
|
def generic_view_tester(view_name: str) -> Callable:
|
||||||
|
"""This is used instead of subTest for better visibility"""
|
||||||
|
|
||||||
|
def tester(self: TestAdmin):
|
||||||
|
try:
|
||||||
|
full_url = reverse(f"passbook_admin:{view_name}")
|
||||||
|
response = self.client.get(full_url)
|
||||||
|
self.assertTrue(response.status_code < 500)
|
||||||
|
except NoReverseMatch:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return tester
|
||||||
|
|
||||||
|
|
||||||
|
for url in urlpatterns:
|
||||||
|
method_name = url.name.replace("-", "_")
|
||||||
|
setattr(TestAdmin, f"test_{method_name}", generic_view_tester(url.name))
|
|
@ -62,9 +62,10 @@ class PolicyCreateView(
|
||||||
|
|
||||||
def get_form_class(self):
|
def get_form_class(self):
|
||||||
policy_type = self.request.GET.get("type")
|
policy_type = self.request.GET.get("type")
|
||||||
model = next(x for x in all_subclasses(Policy) if x.__name__ == policy_type)
|
try:
|
||||||
if not model:
|
model = next(x for x in all_subclasses(Policy) if x.__name__ == policy_type)
|
||||||
raise Http404
|
except StopIteration as exc:
|
||||||
|
raise Http404 from exc
|
||||||
return path_to_class(model.form)
|
return path_to_class(model.form)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -53,11 +53,14 @@ class PropertyMappingCreateView(
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super().get_context_data(**kwargs)
|
kwargs = super().get_context_data(**kwargs)
|
||||||
property_mapping_type = self.request.GET.get("type")
|
property_mapping_type = self.request.GET.get("type")
|
||||||
model = next(
|
try:
|
||||||
x
|
model = next(
|
||||||
for x in all_subclasses(PropertyMapping)
|
x
|
||||||
if x.__name__ == property_mapping_type
|
for x in all_subclasses(PropertyMapping)
|
||||||
)
|
if x.__name__ == property_mapping_type
|
||||||
|
)
|
||||||
|
except StopIteration as exc:
|
||||||
|
raise Http404 from exc
|
||||||
kwargs["type"] = model._meta.verbose_name
|
kwargs["type"] = model._meta.verbose_name
|
||||||
form_cls = self.get_form_class()
|
form_cls = self.get_form_class()
|
||||||
if hasattr(form_cls, "template_name"):
|
if hasattr(form_cls, "template_name"):
|
||||||
|
@ -66,13 +69,14 @@ class PropertyMappingCreateView(
|
||||||
|
|
||||||
def get_form_class(self):
|
def get_form_class(self):
|
||||||
property_mapping_type = self.request.GET.get("type")
|
property_mapping_type = self.request.GET.get("type")
|
||||||
model = next(
|
try:
|
||||||
x
|
model = next(
|
||||||
for x in all_subclasses(PropertyMapping)
|
x
|
||||||
if x.__name__ == property_mapping_type
|
for x in all_subclasses(PropertyMapping)
|
||||||
)
|
if x.__name__ == property_mapping_type
|
||||||
if not model:
|
)
|
||||||
raise Http404
|
except StopIteration as exc:
|
||||||
|
raise Http404 from exc
|
||||||
return path_to_class(model.form)
|
return path_to_class(model.form)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -52,9 +52,12 @@ class ProviderCreateView(
|
||||||
|
|
||||||
def get_form_class(self):
|
def get_form_class(self):
|
||||||
provider_type = self.request.GET.get("type")
|
provider_type = self.request.GET.get("type")
|
||||||
model = next(x for x in all_subclasses(Provider) if x.__name__ == provider_type)
|
try:
|
||||||
if not model:
|
model = next(
|
||||||
raise Http404
|
x for x in all_subclasses(Provider) if x.__name__ == provider_type
|
||||||
|
)
|
||||||
|
except StopIteration as exc:
|
||||||
|
raise Http404 from exc
|
||||||
return path_to_class(model.form)
|
return path_to_class(model.form)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -52,9 +52,10 @@ class SourceCreateView(
|
||||||
|
|
||||||
def get_form_class(self):
|
def get_form_class(self):
|
||||||
source_type = self.request.GET.get("type")
|
source_type = self.request.GET.get("type")
|
||||||
model = next(x for x in all_subclasses(Source) if x.__name__ == source_type)
|
try:
|
||||||
if not model:
|
model = next(x for x in all_subclasses(Source) if x.__name__ == source_type)
|
||||||
raise Http404
|
except StopIteration as exc:
|
||||||
|
raise Http404 from exc
|
||||||
return path_to_class(model.form)
|
return path_to_class(model.form)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,9 +59,10 @@ class StageCreateView(
|
||||||
|
|
||||||
def get_form_class(self):
|
def get_form_class(self):
|
||||||
stage_type = self.request.GET.get("type")
|
stage_type = self.request.GET.get("type")
|
||||||
model = next(x for x in all_subclasses(Stage) if x.__name__ == stage_type)
|
try:
|
||||||
if not model:
|
model = next(x for x in all_subclasses(Stage) if x.__name__ == stage_type)
|
||||||
raise Http404
|
except StopIteration as exc:
|
||||||
|
raise Http404 from exc
|
||||||
return path_to_class(model.form)
|
return path_to_class(model.form)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
isort -rc passbook
|
isort -rc passbook
|
||||||
pyright
|
pyright
|
||||||
black passbook
|
black passbook
|
||||||
|
./manage.py generate_swagger -o swagger.yaml -f yaml
|
||||||
scripts/coverage.sh
|
scripts/coverage.sh
|
||||||
bandit -r passbook
|
bandit -r passbook
|
||||||
pylint passbook
|
pylint passbook
|
||||||
prospector
|
prospector
|
||||||
./manage.py generate_swagger -o swagger.yaml -f yaml
|
|
||||||
|
|
Reference in New Issue