authentik.blueprints.tests.test_v1_conditions

Test blueprints v1

 1"""Test blueprints v1"""
 2
 3from unittest.mock import patch
 4
 5from django.test import TransactionTestCase
 6
 7from authentik.blueprints.v1.importer import Importer
 8from authentik.enterprise.license import LicenseKey
 9from authentik.flows.models import Flow
10from authentik.lib.generators import generate_id
11from authentik.lib.tests.utils import load_fixture
12
13
14class TestBlueprintsV1Conditions(TransactionTestCase):
15    """Test Blueprints conditions attribute"""
16
17    def test_conditions_fulfilled(self):
18        """Test conditions fulfilled"""
19        flow_slug1 = generate_id()
20        flow_slug2 = generate_id()
21        import_yaml = load_fixture(
22            "fixtures/conditions_fulfilled.yaml", id1=flow_slug1, id2=flow_slug2
23        )
24
25        importer = Importer.from_string(import_yaml)
26        self.assertTrue(importer.validate()[0])
27        self.assertTrue(importer.apply())
28        # Ensure objects exist
29        flow: Flow = Flow.objects.filter(slug=flow_slug1).first()
30        self.assertEqual(flow.slug, flow_slug1)
31        flow: Flow = Flow.objects.filter(slug=flow_slug2).first()
32        self.assertEqual(flow.slug, flow_slug2)
33
34    def test_conditions_not_fulfilled(self):
35        """Test conditions not fulfilled"""
36        flow_slug1 = generate_id()
37        flow_slug2 = generate_id()
38        import_yaml = load_fixture(
39            "fixtures/conditions_not_fulfilled.yaml", id1=flow_slug1, id2=flow_slug2
40        )
41
42        importer = Importer.from_string(import_yaml)
43        self.assertTrue(importer.validate()[0])
44        self.assertTrue(importer.apply())
45        # Ensure objects do not exist
46        self.assertFalse(Flow.objects.filter(slug=flow_slug1))
47        self.assertFalse(Flow.objects.filter(slug=flow_slug2))
48
49    def test_enterprise_license_context_unlicensed(self):
50        """Test enterprise license context defaults to a false boolean when unlicensed."""
51        license_key = LicenseKey("test", 0, "Test license", 0, 0)
52
53        with patch("authentik.enterprise.license.LicenseKey.get_total", return_value=license_key):
54            importer = Importer.from_string("""
55version: 1
56entries:
57    - identifiers:
58          name: enterprise-test
59          slug: enterprise-test
60      model: authentik_flows.flow
61      conditions:
62          - !Context goauthentik.io/enterprise/licensed
63      attrs:
64          designation: stage_configuration
65          title: foo
66""")
67
68        self.assertIs(importer.blueprint.context["goauthentik.io/enterprise/licensed"], False)
69
70    def test_enterprise_license_context_licensed(self):
71        """Test enterprise license context defaults to a true boolean when licensed."""
72        license_key = LicenseKey("test", 253402300799, "Test license", 1000, 1000)
73
74        with patch("authentik.enterprise.license.LicenseKey.get_total", return_value=license_key):
75            importer = Importer.from_string("""
76version: 1
77entries:
78    - identifiers:
79          name: enterprise-test
80          slug: enterprise-test
81      model: authentik_flows.flow
82      conditions:
83          - !Context goauthentik.io/enterprise/licensed
84      attrs:
85          designation: stage_configuration
86          title: foo
87""")
88
89        self.assertIs(importer.blueprint.context["goauthentik.io/enterprise/licensed"], True)
class TestBlueprintsV1Conditions(django.test.testcases.TransactionTestCase):
15class TestBlueprintsV1Conditions(TransactionTestCase):
16    """Test Blueprints conditions attribute"""
17
18    def test_conditions_fulfilled(self):
19        """Test conditions fulfilled"""
20        flow_slug1 = generate_id()
21        flow_slug2 = generate_id()
22        import_yaml = load_fixture(
23            "fixtures/conditions_fulfilled.yaml", id1=flow_slug1, id2=flow_slug2
24        )
25
26        importer = Importer.from_string(import_yaml)
27        self.assertTrue(importer.validate()[0])
28        self.assertTrue(importer.apply())
29        # Ensure objects exist
30        flow: Flow = Flow.objects.filter(slug=flow_slug1).first()
31        self.assertEqual(flow.slug, flow_slug1)
32        flow: Flow = Flow.objects.filter(slug=flow_slug2).first()
33        self.assertEqual(flow.slug, flow_slug2)
34
35    def test_conditions_not_fulfilled(self):
36        """Test conditions not fulfilled"""
37        flow_slug1 = generate_id()
38        flow_slug2 = generate_id()
39        import_yaml = load_fixture(
40            "fixtures/conditions_not_fulfilled.yaml", id1=flow_slug1, id2=flow_slug2
41        )
42
43        importer = Importer.from_string(import_yaml)
44        self.assertTrue(importer.validate()[0])
45        self.assertTrue(importer.apply())
46        # Ensure objects do not exist
47        self.assertFalse(Flow.objects.filter(slug=flow_slug1))
48        self.assertFalse(Flow.objects.filter(slug=flow_slug2))
49
50    def test_enterprise_license_context_unlicensed(self):
51        """Test enterprise license context defaults to a false boolean when unlicensed."""
52        license_key = LicenseKey("test", 0, "Test license", 0, 0)
53
54        with patch("authentik.enterprise.license.LicenseKey.get_total", return_value=license_key):
55            importer = Importer.from_string("""
56version: 1
57entries:
58    - identifiers:
59          name: enterprise-test
60          slug: enterprise-test
61      model: authentik_flows.flow
62      conditions:
63          - !Context goauthentik.io/enterprise/licensed
64      attrs:
65          designation: stage_configuration
66          title: foo
67""")
68
69        self.assertIs(importer.blueprint.context["goauthentik.io/enterprise/licensed"], False)
70
71    def test_enterprise_license_context_licensed(self):
72        """Test enterprise license context defaults to a true boolean when licensed."""
73        license_key = LicenseKey("test", 253402300799, "Test license", 1000, 1000)
74
75        with patch("authentik.enterprise.license.LicenseKey.get_total", return_value=license_key):
76            importer = Importer.from_string("""
77version: 1
78entries:
79    - identifiers:
80          name: enterprise-test
81          slug: enterprise-test
82      model: authentik_flows.flow
83      conditions:
84          - !Context goauthentik.io/enterprise/licensed
85      attrs:
86          designation: stage_configuration
87          title: foo
88""")
89
90        self.assertIs(importer.blueprint.context["goauthentik.io/enterprise/licensed"], True)

Test Blueprints conditions attribute

def test_conditions_fulfilled(self):
18    def test_conditions_fulfilled(self):
19        """Test conditions fulfilled"""
20        flow_slug1 = generate_id()
21        flow_slug2 = generate_id()
22        import_yaml = load_fixture(
23            "fixtures/conditions_fulfilled.yaml", id1=flow_slug1, id2=flow_slug2
24        )
25
26        importer = Importer.from_string(import_yaml)
27        self.assertTrue(importer.validate()[0])
28        self.assertTrue(importer.apply())
29        # Ensure objects exist
30        flow: Flow = Flow.objects.filter(slug=flow_slug1).first()
31        self.assertEqual(flow.slug, flow_slug1)
32        flow: Flow = Flow.objects.filter(slug=flow_slug2).first()
33        self.assertEqual(flow.slug, flow_slug2)

Test conditions fulfilled

def test_conditions_not_fulfilled(self):
35    def test_conditions_not_fulfilled(self):
36        """Test conditions not fulfilled"""
37        flow_slug1 = generate_id()
38        flow_slug2 = generate_id()
39        import_yaml = load_fixture(
40            "fixtures/conditions_not_fulfilled.yaml", id1=flow_slug1, id2=flow_slug2
41        )
42
43        importer = Importer.from_string(import_yaml)
44        self.assertTrue(importer.validate()[0])
45        self.assertTrue(importer.apply())
46        # Ensure objects do not exist
47        self.assertFalse(Flow.objects.filter(slug=flow_slug1))
48        self.assertFalse(Flow.objects.filter(slug=flow_slug2))

Test conditions not fulfilled

def test_enterprise_license_context_unlicensed(self):
50    def test_enterprise_license_context_unlicensed(self):
51        """Test enterprise license context defaults to a false boolean when unlicensed."""
52        license_key = LicenseKey("test", 0, "Test license", 0, 0)
53
54        with patch("authentik.enterprise.license.LicenseKey.get_total", return_value=license_key):
55            importer = Importer.from_string("""
56version: 1
57entries:
58    - identifiers:
59          name: enterprise-test
60          slug: enterprise-test
61      model: authentik_flows.flow
62      conditions:
63          - !Context goauthentik.io/enterprise/licensed
64      attrs:
65          designation: stage_configuration
66          title: foo
67""")
68
69        self.assertIs(importer.blueprint.context["goauthentik.io/enterprise/licensed"], False)

Test enterprise license context defaults to a false boolean when unlicensed.

def test_enterprise_license_context_licensed(self):
71    def test_enterprise_license_context_licensed(self):
72        """Test enterprise license context defaults to a true boolean when licensed."""
73        license_key = LicenseKey("test", 253402300799, "Test license", 1000, 1000)
74
75        with patch("authentik.enterprise.license.LicenseKey.get_total", return_value=license_key):
76            importer = Importer.from_string("""
77version: 1
78entries:
79    - identifiers:
80          name: enterprise-test
81          slug: enterprise-test
82      model: authentik_flows.flow
83      conditions:
84          - !Context goauthentik.io/enterprise/licensed
85      attrs:
86          designation: stage_configuration
87          title: foo
88""")
89
90        self.assertIs(importer.blueprint.context["goauthentik.io/enterprise/licensed"], True)

Test enterprise license context defaults to a true boolean when licensed.