Skip to content

Commit 34364f4

Browse files
authored
blueprints: fix mismatched API schema and implementation (#22087)
align blueprint import schema with 200 result response
1 parent ea61e1c commit 34364f4

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

authentik/blueprints/api.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ def apply(self, request: Request, *args, **kwargs) -> Response:
217217

218218
@extend_schema(
219219
request={"multipart/form-data": BlueprintUploadSerializer},
220-
responses={
221-
204: BlueprintImportResultSerializer,
222-
400: BlueprintImportResultSerializer,
223-
},
220+
responses={200: BlueprintImportResultSerializer},
224221
)
225222
@action(url_path="import", detail=False, methods=["POST"], parser_classes=(MultiPartParser,))
226223
@validate(
@@ -247,21 +244,13 @@ def import_(self, request: Request, body: BlueprintUploadSerializer) -> Response
247244

248245
import_response = self.BlueprintImportResultSerializer(
249246
data={
250-
"logs": [],
251-
"success": False,
247+
"logs": [LogEventSerializer(log).data for log in logs],
248+
"success": valid,
252249
}
253250
)
254251
import_response.is_valid(raise_exception=True)
255252

256-
import_response.initial_data["logs"] = [LogEventSerializer(log).data for log in logs]
257-
import_response.initial_data["success"] = valid
258-
import_response.is_valid()
259-
if not valid:
260-
return Response(data=import_response.initial_data, status=200)
261-
262-
successful = importer.apply()
263-
import_response.initial_data["success"] = successful
264-
import_response.is_valid()
265-
if not successful:
266-
return Response(data=import_response.initial_data, status=200)
253+
if valid:
254+
import_response.initial_data["success"] = importer.apply()
255+
import_response.is_valid()
267256
return Response(data=import_response.initial_data, status=200)

authentik/blueprints/tests/test_v1_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from json import dumps, loads
44
from tempfile import NamedTemporaryFile, mkdtemp
55

6+
from django.core.files.uploadedfile import SimpleUploadedFile
67
from django.urls import reverse
78
from rest_framework.test import APITestCase
89
from yaml import dump
@@ -141,6 +142,20 @@ def test_api_import_blank_path(self):
141142
)
142143
self.assertEqual(res.status_code, 200)
143144

145+
def test_api_import_invalid_blueprint_returns_result_payload(self):
146+
"""Invalid blueprint content returns a result payload instead of a 400 response."""
147+
file = SimpleUploadedFile("invalid-blueprint.yaml", b'{"version": 3}')
148+
149+
res = self.client.post(
150+
reverse("authentik_api:blueprintinstance-import-"),
151+
data={"file": file},
152+
format="multipart",
153+
)
154+
155+
self.assertEqual(res.status_code, 200)
156+
self.assertFalse(res.json()["success"])
157+
self.assertGreater(len(res.json()["logs"]), 0)
158+
144159
def test_api_import_unknown_path(self):
145160
"""Path not in available blueprints is rejected (covers api.py:56)."""
146161
res = self.client.post(

schema.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9678,18 +9678,14 @@ paths:
96789678
security:
96799679
- authentik: []
96809680
responses:
9681-
'204':
9681+
'200':
96829682
content:
96839683
application/json:
96849684
schema:
96859685
$ref: '#/components/schemas/BlueprintImportResult'
96869686
description: ''
96879687
'400':
9688-
content:
9689-
application/json:
9690-
schema:
9691-
$ref: '#/components/schemas/BlueprintImportResult'
9692-
description: ''
9688+
$ref: '#/components/responses/ValidationErrorResponse'
96939689
'403':
96949690
$ref: '#/components/responses/GenericErrorResponse'
96959691
/oauth2/access_tokens/:

0 commit comments

Comments
 (0)