authentik.admin.files.backends.tests.test_static_backend

 1from django.test import TestCase
 2
 3from authentik.admin.files.backends.static import StaticBackend
 4from authentik.admin.files.usage import FileUsage
 5
 6
 7class TestStaticBackend(TestCase):
 8    """Test Static backend functionality"""
 9
10    def setUp(self):
11        """Set up test fixtures"""
12        self.usage = FileUsage.MEDIA
13        self.backend = StaticBackend(self.usage)
14
15    def test_init(self):
16        """Test StaticBackend initialization"""
17        self.assertEqual(self.backend.usage, self.usage)
18
19    def test_allowed_usages(self):
20        """Test that StaticBackend only supports MEDIA usage"""
21        self.assertEqual(self.backend.allowed_usages, [FileUsage.MEDIA])
22
23    def test_supports_file_path_static_prefix(self):
24        """Test supports_file_path returns True for /static prefix"""
25        self.assertTrue(self.backend.supports_file("/static/assets/icons/test.svg"))
26        self.assertTrue(self.backend.supports_file("/static/authentik/sources/icon.png"))
27
28    def test_supports_file_path_not_static(self):
29        """Test supports_file_path returns False for non-static paths"""
30        self.assertFalse(self.backend.supports_file("web/dist/assets/icons/test.svg"))
31        self.assertFalse(self.backend.supports_file("web/dist/assets/images/logo.png"))
32        self.assertFalse(self.backend.supports_file("media/public/test.png"))
33        self.assertFalse(self.backend.supports_file("/media/test.svg"))
34        self.assertFalse(self.backend.supports_file("test.jpg"))
35
36    def test_list_files(self):
37        """Test list_files lists built-in source icons with a variant per theme"""
38        files = list(self.backend.list_files())
39
40        self.assertIn("/static/authentik/sources/github/%(theme)s.svg", files)
41        self.assertIn("/static/authentik/sources/kerberos/%(theme)s.png", files)
42        self.assertNotIn("/static/authentik/sources/github/light.svg", files)
43        self.assertNotIn("/static/authentik/sources/github/dark.svg", files)
44
45    def test_themed_urls_source_icon(self):
46        """Test themed_urls of a built-in source icon picked in the file picker"""
47        self.assertEqual(
48            self.backend.themed_urls("/static/authentik/sources/github/%(theme)s.svg"),
49            {
50                "light": "/static/authentik/sources/github/light.svg",
51                "dark": "/static/authentik/sources/github/dark.svg",
52            },
53        )
54
55    def test_themed_urls_source_icon_variant(self):
56        """Test themed_urls of one variant of a built-in source icon, as used by defaults"""
57        github = {
58            "light": "/static/authentik/sources/github/light.svg",
59            "dark": "/static/authentik/sources/github/dark.svg",
60        }
61        self.assertEqual(
62            self.backend.themed_urls("/static/authentik/sources/github/light.svg"), github
63        )
64        self.assertEqual(
65            self.backend.themed_urls("/static/authentik/sources/github/dark.svg"), github
66        )
67
68    def test_themed_urls_other_static_files(self):
69        """Test themed_urls returns None for other static files"""
70        self.assertIsNone(self.backend.themed_urls("/static/dist/assets/icons/icon.svg"))
71        self.assertIsNone(self.backend.themed_urls("/static/dist/assets/icons/light.svg"))
72        self.assertIsNone(self.backend.themed_urls("/static/authentik/sources/github/logo.svg"))
73        self.assertIsNone(
74            self.backend.themed_urls("/static/authentik/sources/github/../github/light.svg")
75        )
class TestStaticBackend(django.test.testcases.TestCase):
 8class TestStaticBackend(TestCase):
 9    """Test Static backend functionality"""
10
11    def setUp(self):
12        """Set up test fixtures"""
13        self.usage = FileUsage.MEDIA
14        self.backend = StaticBackend(self.usage)
15
16    def test_init(self):
17        """Test StaticBackend initialization"""
18        self.assertEqual(self.backend.usage, self.usage)
19
20    def test_allowed_usages(self):
21        """Test that StaticBackend only supports MEDIA usage"""
22        self.assertEqual(self.backend.allowed_usages, [FileUsage.MEDIA])
23
24    def test_supports_file_path_static_prefix(self):
25        """Test supports_file_path returns True for /static prefix"""
26        self.assertTrue(self.backend.supports_file("/static/assets/icons/test.svg"))
27        self.assertTrue(self.backend.supports_file("/static/authentik/sources/icon.png"))
28
29    def test_supports_file_path_not_static(self):
30        """Test supports_file_path returns False for non-static paths"""
31        self.assertFalse(self.backend.supports_file("web/dist/assets/icons/test.svg"))
32        self.assertFalse(self.backend.supports_file("web/dist/assets/images/logo.png"))
33        self.assertFalse(self.backend.supports_file("media/public/test.png"))
34        self.assertFalse(self.backend.supports_file("/media/test.svg"))
35        self.assertFalse(self.backend.supports_file("test.jpg"))
36
37    def test_list_files(self):
38        """Test list_files lists built-in source icons with a variant per theme"""
39        files = list(self.backend.list_files())
40
41        self.assertIn("/static/authentik/sources/github/%(theme)s.svg", files)
42        self.assertIn("/static/authentik/sources/kerberos/%(theme)s.png", files)
43        self.assertNotIn("/static/authentik/sources/github/light.svg", files)
44        self.assertNotIn("/static/authentik/sources/github/dark.svg", files)
45
46    def test_themed_urls_source_icon(self):
47        """Test themed_urls of a built-in source icon picked in the file picker"""
48        self.assertEqual(
49            self.backend.themed_urls("/static/authentik/sources/github/%(theme)s.svg"),
50            {
51                "light": "/static/authentik/sources/github/light.svg",
52                "dark": "/static/authentik/sources/github/dark.svg",
53            },
54        )
55
56    def test_themed_urls_source_icon_variant(self):
57        """Test themed_urls of one variant of a built-in source icon, as used by defaults"""
58        github = {
59            "light": "/static/authentik/sources/github/light.svg",
60            "dark": "/static/authentik/sources/github/dark.svg",
61        }
62        self.assertEqual(
63            self.backend.themed_urls("/static/authentik/sources/github/light.svg"), github
64        )
65        self.assertEqual(
66            self.backend.themed_urls("/static/authentik/sources/github/dark.svg"), github
67        )
68
69    def test_themed_urls_other_static_files(self):
70        """Test themed_urls returns None for other static files"""
71        self.assertIsNone(self.backend.themed_urls("/static/dist/assets/icons/icon.svg"))
72        self.assertIsNone(self.backend.themed_urls("/static/dist/assets/icons/light.svg"))
73        self.assertIsNone(self.backend.themed_urls("/static/authentik/sources/github/logo.svg"))
74        self.assertIsNone(
75            self.backend.themed_urls("/static/authentik/sources/github/../github/light.svg")
76        )

Test Static backend functionality

def setUp(self):
11    def setUp(self):
12        """Set up test fixtures"""
13        self.usage = FileUsage.MEDIA
14        self.backend = StaticBackend(self.usage)

Set up test fixtures

def test_init(self):
16    def test_init(self):
17        """Test StaticBackend initialization"""
18        self.assertEqual(self.backend.usage, self.usage)

Test StaticBackend initialization

def test_allowed_usages(self):
20    def test_allowed_usages(self):
21        """Test that StaticBackend only supports MEDIA usage"""
22        self.assertEqual(self.backend.allowed_usages, [FileUsage.MEDIA])

Test that StaticBackend only supports MEDIA usage

def test_supports_file_path_static_prefix(self):
24    def test_supports_file_path_static_prefix(self):
25        """Test supports_file_path returns True for /static prefix"""
26        self.assertTrue(self.backend.supports_file("/static/assets/icons/test.svg"))
27        self.assertTrue(self.backend.supports_file("/static/authentik/sources/icon.png"))

Test supports_file_path returns True for /static prefix

def test_supports_file_path_not_static(self):
29    def test_supports_file_path_not_static(self):
30        """Test supports_file_path returns False for non-static paths"""
31        self.assertFalse(self.backend.supports_file("web/dist/assets/icons/test.svg"))
32        self.assertFalse(self.backend.supports_file("web/dist/assets/images/logo.png"))
33        self.assertFalse(self.backend.supports_file("media/public/test.png"))
34        self.assertFalse(self.backend.supports_file("/media/test.svg"))
35        self.assertFalse(self.backend.supports_file("test.jpg"))

Test supports_file_path returns False for non-static paths

def test_list_files(self):
37    def test_list_files(self):
38        """Test list_files lists built-in source icons with a variant per theme"""
39        files = list(self.backend.list_files())
40
41        self.assertIn("/static/authentik/sources/github/%(theme)s.svg", files)
42        self.assertIn("/static/authentik/sources/kerberos/%(theme)s.png", files)
43        self.assertNotIn("/static/authentik/sources/github/light.svg", files)
44        self.assertNotIn("/static/authentik/sources/github/dark.svg", files)

Test list_files lists built-in source icons with a variant per theme

def test_themed_urls_source_icon(self):
46    def test_themed_urls_source_icon(self):
47        """Test themed_urls of a built-in source icon picked in the file picker"""
48        self.assertEqual(
49            self.backend.themed_urls("/static/authentik/sources/github/%(theme)s.svg"),
50            {
51                "light": "/static/authentik/sources/github/light.svg",
52                "dark": "/static/authentik/sources/github/dark.svg",
53            },
54        )

Test themed_urls of a built-in source icon picked in the file picker

def test_themed_urls_source_icon_variant(self):
56    def test_themed_urls_source_icon_variant(self):
57        """Test themed_urls of one variant of a built-in source icon, as used by defaults"""
58        github = {
59            "light": "/static/authentik/sources/github/light.svg",
60            "dark": "/static/authentik/sources/github/dark.svg",
61        }
62        self.assertEqual(
63            self.backend.themed_urls("/static/authentik/sources/github/light.svg"), github
64        )
65        self.assertEqual(
66            self.backend.themed_urls("/static/authentik/sources/github/dark.svg"), github
67        )

Test themed_urls of one variant of a built-in source icon, as used by defaults

def test_themed_urls_other_static_files(self):
69    def test_themed_urls_other_static_files(self):
70        """Test themed_urls returns None for other static files"""
71        self.assertIsNone(self.backend.themed_urls("/static/dist/assets/icons/icon.svg"))
72        self.assertIsNone(self.backend.themed_urls("/static/dist/assets/icons/light.svg"))
73        self.assertIsNone(self.backend.themed_urls("/static/authentik/sources/github/logo.svg"))
74        self.assertIsNone(
75            self.backend.themed_urls("/static/authentik/sources/github/../github/light.svg")
76        )

Test themed_urls returns None for other static files