Skip to content

Commit 33f613d

Browse files
committed
wip
god help me
1 parent a07ce35 commit 33f613d

File tree

5 files changed

+737
-125
lines changed

5 files changed

+737
-125
lines changed

authentik/core/api/applications.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ def list(self, request: Request) -> Response:
281281
serializer = self.get_serializer(allowed_applications, many=True)
282282
return self.get_paginated_response(serializer.data)
283283

284+
@action(
285+
detail=True,
286+
pagination_class=None,
287+
filter_backends=[],
288+
methods=["POST"],
289+
parser_classes=(MultiPartParser,),
290+
)
284291
@permission_required("authentik_core.change_application")
285292
@extend_schema(
286293
request={
@@ -291,18 +298,17 @@ def list(self, request: Request) -> Response:
291298
400: OpenApiResponse(description="Bad request"),
292299
},
293300
)
301+
def set_icon(self, request: Request, slug: str):
302+
"""Set application icon"""
303+
app: Application = self.get_object()
304+
return set_file(request, app, "meta_icon")
305+
294306
@action(
295307
detail=True,
296308
pagination_class=None,
297309
filter_backends=[],
298310
methods=["POST"],
299-
parser_classes=(MultiPartParser,),
300311
)
301-
def set_icon(self, request: Request, slug: str):
302-
"""Set application icon"""
303-
app: Application = self.get_object()
304-
return set_file(request, app, "meta_icon")
305-
306312
@permission_required("authentik_core.change_application")
307313
@extend_schema(
308314
request=FilePathSerializer,
@@ -311,12 +317,6 @@ def set_icon(self, request: Request, slug: str):
311317
400: OpenApiResponse(description="Bad request"),
312318
},
313319
)
314-
@action(
315-
detail=True,
316-
pagination_class=None,
317-
filter_backends=[],
318-
methods=["POST"],
319-
)
320320
def set_icon_url(self, request: Request, slug: str):
321321
"""Set application icon (as URL)"""
322322
app: Application = self.get_object()

authentik/core/tests/test_applications_api.py

+33-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.core.files.base import ContentFile
66
from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart
77
from django.urls import reverse
8-
from rest_framework.test import APITestCase
8+
from rest_framework.test import APITransactionTestCase
99

1010
from authentik.core.models import Application
1111
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
@@ -17,7 +17,7 @@
1717
from authentik.providers.saml.models import SAMLProvider
1818

1919

20-
class TestApplicationsAPI(APITestCase):
20+
class TestApplicationsAPI(APITransactionTestCase):
2121
"""Test applications API"""
2222

2323
def setUp(self) -> None:
@@ -40,6 +40,14 @@ def setUp(self) -> None:
4040
policy=DummyPolicy.objects.create(name="deny", result=False, wait_min=1, wait_max=2),
4141
order=0,
4242
)
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()
4351

4452
def test_formatted_launch_url(self):
4553
"""Test formatted launch URL"""
@@ -58,19 +66,22 @@ def test_formatted_launch_url(self):
5866
)
5967

6068
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")
6371
self.client.force_login(self.user)
72+
73+
# Test setting icon
6474
response = self.client.post(
6575
reverse(
6676
"authentik_api:application-set-icon",
6777
kwargs={"slug": self.allowed.slug},
6878
),
69-
data=encode_multipart(data={"file": file}, boundary=BOUNDARY),
79+
data=encode_multipart(BOUNDARY, {"file": file}),
7080
content_type=MULTIPART_CONTENT,
7181
)
7282
self.assertEqual(response.status_code, 200)
7383

84+
# Verify icon was set correctly
7485
app_raw = self.client.get(
7586
reverse(
7687
"authentik_api:application-detail",
@@ -80,7 +91,23 @@ def test_set_icon(self):
8091
app = loads(app_raw.content)
8192
self.allowed.refresh_from_db()
8293
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")
84111

85112
def test_check_access(self):
86113
"""Test check_access operation"""

authentik/root/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
],
204204
"DEFAULT_PARSER_CLASSES": [
205205
"drf_orjson_renderer.parsers.ORJSONParser",
206+
"rest_framework.parsers.MultiPartParser",
206207
],
207208
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
208209
"TEST_REQUEST_DEFAULT_FORMAT": "json",

0 commit comments

Comments
 (0)