5
5
from django .core .files .base import ContentFile
6
6
from django .test .client import BOUNDARY , MULTIPART_CONTENT , encode_multipart
7
7
from django .urls import reverse
8
- from rest_framework .test import APITestCase
8
+ from rest_framework .test import APITransactionTestCase
9
9
10
10
from authentik .core .models import Application
11
11
from authentik .core .tests .utils import create_test_admin_user , create_test_flow
17
17
from authentik .providers .saml .models import SAMLProvider
18
18
19
19
20
- class TestApplicationsAPI (APITestCase ):
20
+ class TestApplicationsAPI (APITransactionTestCase ):
21
21
"""Test applications API"""
22
22
23
23
def setUp (self ) -> None :
@@ -40,6 +40,14 @@ def setUp(self) -> None:
40
40
policy = DummyPolicy .objects .create (name = "deny" , result = False , wait_min = 1 , wait_max = 2 ),
41
41
order = 0 ,
42
42
)
43
+ self .test_files = []
44
+
45
+ def tearDown (self ) -> None :
46
+ # Clean up any test files
47
+ for app in [self .allowed , self .denied ]:
48
+ if app .meta_icon :
49
+ app .meta_icon .delete ()
50
+ super ().tearDown ()
43
51
44
52
def test_formatted_launch_url (self ):
45
53
"""Test formatted launch URL"""
@@ -58,19 +66,22 @@ def test_formatted_launch_url(self):
58
66
)
59
67
60
68
def test_set_icon (self ):
61
- """Test set_icon"""
62
- file = ContentFile (b"text " , "name " )
69
+ """Test set_icon and cleanup """
70
+ file = ContentFile (b"test-content " , "test-icon.png " )
63
71
self .client .force_login (self .user )
72
+
73
+ # Test setting icon
64
74
response = self .client .post (
65
75
reverse (
66
76
"authentik_api:application-set-icon" ,
67
77
kwargs = {"slug" : self .allowed .slug },
68
78
),
69
- data = encode_multipart (data = {"file" : file }, boundary = BOUNDARY ),
79
+ data = encode_multipart (BOUNDARY , {"file" : file }),
70
80
content_type = MULTIPART_CONTENT ,
71
81
)
72
82
self .assertEqual (response .status_code , 200 )
73
83
84
+ # Verify icon was set correctly
74
85
app_raw = self .client .get (
75
86
reverse (
76
87
"authentik_api:application-detail" ,
@@ -80,7 +91,23 @@ def test_set_icon(self):
80
91
app = loads (app_raw .content )
81
92
self .allowed .refresh_from_db ()
82
93
self .assertEqual (self .allowed .get_meta_icon , app ["meta_icon" ])
83
- self .assertEqual (self .allowed .meta_icon .read (), b"text" )
94
+ self .assertEqual (self .allowed .meta_icon .read (), b"test-content" )
95
+
96
+ # Test icon replacement
97
+ new_file = ContentFile (b"new-content" , "new-icon.png" )
98
+ response = self .client .post (
99
+ reverse (
100
+ "authentik_api:application-set-icon" ,
101
+ kwargs = {"slug" : self .allowed .slug },
102
+ ),
103
+ data = encode_multipart (BOUNDARY , {"file" : new_file }),
104
+ content_type = MULTIPART_CONTENT ,
105
+ )
106
+ self .assertEqual (response .status_code , 200 )
107
+
108
+ # Verify new icon was set and old one was cleaned up
109
+ self .allowed .refresh_from_db ()
110
+ self .assertEqual (self .allowed .meta_icon .read (), b"new-content" )
84
111
85
112
def test_check_access (self ):
86
113
"""Test check_access operation"""
0 commit comments