authentik.blueprints.tests.test_packaged

test packaged blueprints

 1"""test packaged blueprints"""
 2
 3from collections.abc import Callable
 4from pathlib import Path
 5
 6from django.test import TransactionTestCase
 7
 8from authentik.blueprints.models import BlueprintInstance
 9from authentik.blueprints.tests import apply_blueprint
10from authentik.blueprints.v1.importer import Importer
11from authentik.brands.models import Brand
12
13
14class TestPackaged(TransactionTestCase):
15    """Empty class, test methods are added dynamically"""
16
17    @apply_blueprint("default/default-brand.yaml")
18    def test_decorator_static(self):
19        """Test @apply_blueprint decorator"""
20        self.assertTrue(Brand.objects.filter(domain="authentik-default").exists())
21
22
23def blueprint_tester(file_name: Path) -> Callable:
24    """This is used instead of subTest for better visibility"""
25
26    def tester(self: TestPackaged):
27        base = Path("blueprints/")
28        rel_path = Path(file_name).relative_to(base)
29        importer = Importer.from_string(BlueprintInstance(path=str(rel_path)).retrieve())
30        validation, logs = importer.validate()
31        self.assertTrue(validation, logs)
32        self.assertTrue(importer.apply())
33
34    return tester
35
36
37for blueprint_file in Path("blueprints/").glob("**/*.yaml"):
38    if "local" in str(blueprint_file) or "testing" in str(blueprint_file):
39        continue
40    setattr(TestPackaged, f"test_blueprint_{blueprint_file}", blueprint_tester(blueprint_file))
class TestPackaged(django.test.testcases.TransactionTestCase):
15class TestPackaged(TransactionTestCase):
16    """Empty class, test methods are added dynamically"""
17
18    @apply_blueprint("default/default-brand.yaml")
19    def test_decorator_static(self):
20        """Test @apply_blueprint decorator"""
21        self.assertTrue(Brand.objects.filter(domain="authentik-default").exists())

Empty class, test methods are added dynamically

@apply_blueprint('default/default-brand.yaml')
def test_decorator_static(self):
18    @apply_blueprint("default/default-brand.yaml")
19    def test_decorator_static(self):
20        """Test @apply_blueprint decorator"""
21        self.assertTrue(Brand.objects.filter(domain="authentik-default").exists())

Test @apply_blueprint decorator

def blueprint_tester(file_name: pathlib.Path) -> Callable:
24def blueprint_tester(file_name: Path) -> Callable:
25    """This is used instead of subTest for better visibility"""
26
27    def tester(self: TestPackaged):
28        base = Path("blueprints/")
29        rel_path = Path(file_name).relative_to(base)
30        importer = Importer.from_string(BlueprintInstance(path=str(rel_path)).retrieve())
31        validation, logs = importer.validate()
32        self.assertTrue(validation, logs)
33        self.assertTrue(importer.apply())
34
35    return tester

This is used instead of subTest for better visibility