*: add missing error codes as swagger annotations

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-04-10 23:55:43 +02:00
parent 32d88c3a49
commit d76db3caba
12 changed files with 66 additions and 37 deletions

View file

@ -52,6 +52,13 @@ class TaskViewSet(ViewSet):
tasks = sorted(TaskInfo.all().values(), key=lambda task: task.task_name) tasks = sorted(TaskInfo.all().values(), key=lambda task: task.task_name)
return Response(TaskSerializer(tasks, many=True).data) return Response(TaskSerializer(tasks, many=True).data)
@swagger_auto_schema(
responses={
204: "Task retried successfully",
404: "Task not found",
500: "Failed to retry task",
}
)
@action(detail=True, methods=["post"]) @action(detail=True, methods=["post"])
# pylint: disable=invalid-name # pylint: disable=invalid-name
def retry(self, request: Request, pk=None) -> Response: def retry(self, request: Request, pk=None) -> Response:
@ -70,12 +77,8 @@ class TaskViewSet(ViewSet):
% {"name": task.task_name} % {"name": task.task_name}
), ),
) )
return Response( return Response(status=204)
{
"successful": True,
}
)
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
# if we get an import error, the module path has probably changed # if we get an import error, the module path has probably changed
task.delete() task.delete()
return Response({"successful": False}) return Response(status=500)

View file

@ -39,9 +39,7 @@ class TestAdminAPI(TestCase):
kwargs={"pk": "clean_expired_models"}, kwargs={"pk": "clean_expired_models"},
) )
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 204)
body = loads(response.content)
self.assertTrue(body["successful"])
def test_tasks_retry_404(self): def test_tasks_retry_404(self):
"""Test Task API (retry, 404)""" """Test Task API (retry, 404)"""

View file

@ -124,7 +124,7 @@ class ApplicationViewSet(ModelViewSet):
required=True, required=True,
) )
], ],
responses={200: "Success"}, responses={200: "Success", 400: "Bad request"},
) )
@action( @action(
detail=True, detail=True,

View file

@ -99,7 +99,7 @@ class PropertyMappingViewSet(
@permission_required("authentik_core.view_propertymapping") @permission_required("authentik_core.view_propertymapping")
@swagger_auto_schema( @swagger_auto_schema(
request_body=PolicyTestSerializer(), request_body=PolicyTestSerializer(),
responses={200: PropertyMappingTestResultSerializer}, responses={200: PropertyMappingTestResultSerializer, 400: "Invalid parameters"},
) )
@action(detail=True, pagination_class=None, filter_backends=[], methods=["POST"]) @action(detail=True, pagination_class=None, filter_backends=[], methods=["POST"])
# pylint: disable=unused-argument, invalid-name # pylint: disable=unused-argument, invalid-name

View file

@ -67,7 +67,12 @@ class TokenViewSet(ModelViewSet):
serializer.save(user=self.request.user) serializer.save(user=self.request.user)
@permission_required("authentik_core.view_token_key") @permission_required("authentik_core.view_token_key")
@swagger_auto_schema(responses={200: TokenViewSerializer(many=False)}) @swagger_auto_schema(
responses={
200: TokenViewSerializer(many=False),
404: "Token not found or expired",
}
)
@action(detail=True, pagination_class=None, filter_backends=[]) @action(detail=True, pagination_class=None, filter_backends=[])
# pylint: disable=unused-argument # pylint: disable=unused-argument
def view_key(self, request: Request, identifier: str) -> Response: def view_key(self, request: Request, identifier: str) -> Response:

View file

@ -123,7 +123,7 @@ class CertificateKeyPairViewSet(ModelViewSet):
@permission_required(None, ["authentik_crypto.add_certificatekeypair"]) @permission_required(None, ["authentik_crypto.add_certificatekeypair"])
@swagger_auto_schema( @swagger_auto_schema(
request_body=CertificateGenerationSerializer(), request_body=CertificateGenerationSerializer(),
responses={200: CertificateKeyPairSerializer}, responses={200: CertificateKeyPairSerializer, 400: "Bad request"},
) )
@action(detail=False, methods=["POST"]) @action(detail=False, methods=["POST"])
def generate(self, request: Request) -> Response: def generate(self, request: Request) -> Response:

View file

@ -59,7 +59,10 @@ class NotificationTransportViewSet(ModelViewSet):
@permission_required("authentik_events.change_notificationtransport") @permission_required("authentik_events.change_notificationtransport")
@swagger_auto_schema( @swagger_auto_schema(
responses={200: NotificationTransportTestSerializer(many=False)}, responses={
200: NotificationTransportTestSerializer(many=False),
503: "Failed to test transport",
},
request_body=no_body, request_body=no_body,
) )
@action(detail=True, pagination_class=None, filter_backends=[], methods=["post"]) @action(detail=True, pagination_class=None, filter_backends=[], methods=["post"])

View file

@ -268,7 +268,7 @@ class FlowViewSet(ModelViewSet):
required=True, required=True,
) )
], ],
responses={200: "Success"}, responses={200: "Success", 400: "Bad request"},
) )
@action( @action(
detail=True, detail=True,
@ -289,7 +289,7 @@ class FlowViewSet(ModelViewSet):
return Response({}) return Response({})
@swagger_auto_schema( @swagger_auto_schema(
responses={200: LinkSerializer(many=False)}, responses={200: LinkSerializer(many=False), 400: "Flow not applicable"},
) )
@action(detail=True, pagination_class=None, filter_backends=[]) @action(detail=True, pagination_class=None, filter_backends=[])
# pylint: disable=unused-argument # pylint: disable=unused-argument

View file

@ -138,7 +138,7 @@ class PolicyViewSet(
@permission_required("authentik_policies.view_policy") @permission_required("authentik_policies.view_policy")
@swagger_auto_schema( @swagger_auto_schema(
request_body=PolicyTestSerializer(), request_body=PolicyTestSerializer(),
responses={200: PolicyTestResultSerializer()}, responses={200: PolicyTestResultSerializer(), 400: "Invalid parameters"},
) )
@action(detail=True, pagination_class=None, filter_backends=[], methods=["POST"]) @action(detail=True, pagination_class=None, filter_backends=[], methods=["POST"])
# pylint: disable=unused-argument, invalid-name # pylint: disable=unused-argument, invalid-name

View file

@ -66,7 +66,12 @@ class OAuth2ProviderViewSet(ModelViewSet):
queryset = OAuth2Provider.objects.all() queryset = OAuth2Provider.objects.all()
serializer_class = OAuth2ProviderSerializer serializer_class = OAuth2ProviderSerializer
@swagger_auto_schema(responses={200: OAuth2ProviderSetupURLs(many=False)}) @swagger_auto_schema(
responses={
200: OAuth2ProviderSetupURLs(many=False),
404: "Provider has no application assigned",
}
)
@action(methods=["GET"], detail=True) @action(methods=["GET"], detail=True)
# pylint: disable=invalid-name # pylint: disable=invalid-name
def setup_urls(self, request: Request, pk: int) -> str: def setup_urls(self, request: Request, pk: int) -> str:

View file

@ -79,7 +79,12 @@ class SAMLProviderViewSet(ModelViewSet):
queryset = SAMLProvider.objects.all() queryset = SAMLProvider.objects.all()
serializer_class = SAMLProviderSerializer serializer_class = SAMLProviderSerializer
@swagger_auto_schema(responses={200: SAMLMetadataSerializer(many=False)}) @swagger_auto_schema(
responses={
200: SAMLMetadataSerializer(many=False),
404: "Provider has no application assigned",
}
)
@action(methods=["GET"], detail=True, permission_classes=[AllowAny]) @action(methods=["GET"], detail=True, permission_classes=[AllowAny])
# pylint: disable=invalid-name, unused-argument # pylint: disable=invalid-name, unused-argument
def metadata(self, request: Request, pk: int) -> Response: def metadata(self, request: Request, pk: int) -> Response:

View file

@ -107,17 +107,19 @@ paths:
description: Retry task description: Retry task
parameters: [] parameters: []
responses: responses:
'201': '204':
description: '' description: Task retried successfully
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
'404': '404':
description: Object does not exist or caller has insufficient permissions description: Object does not exist or caller has insufficient permissions
to access it. to access it.
schema: schema:
$ref: '#/definitions/APIException' $ref: '#/definitions/APIException'
'500':
description: Failed to retry task
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
tags: tags:
- admin - admin
parameters: parameters:
@ -1387,6 +1389,8 @@ paths:
responses: responses:
'200': '200':
description: Success description: Success
'400':
description: Bad request
'403': '403':
description: Authentication credentials were invalid, absent or insufficient. description: Authentication credentials were invalid, absent or insufficient.
schema: schema:
@ -1844,15 +1848,15 @@ paths:
description: '' description: ''
schema: schema:
$ref: '#/definitions/TokenView' $ref: '#/definitions/TokenView'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
'404': '404':
description: Object does not exist or caller has insufficient permissions description: Object does not exist or caller has insufficient permissions
to access it. to access it.
schema: schema:
$ref: '#/definitions/APIException' $ref: '#/definitions/APIException'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
tags: tags:
- core - core
parameters: parameters:
@ -3402,6 +3406,8 @@ paths:
description: '' description: ''
schema: schema:
$ref: '#/definitions/NotificationTransportTest' $ref: '#/definitions/NotificationTransportTest'
'503':
description: Failed to test transport
'403': '403':
description: Authentication credentials were invalid, absent or insufficient. description: Authentication credentials were invalid, absent or insufficient.
schema: schema:
@ -4045,6 +4051,8 @@ paths:
description: '' description: ''
schema: schema:
$ref: '#/definitions/Link' $ref: '#/definitions/Link'
'400':
description: Flow not applicable
'403': '403':
description: Authentication credentials were invalid, absent or insufficient. description: Authentication credentials were invalid, absent or insufficient.
schema: schema:
@ -4105,6 +4113,8 @@ paths:
responses: responses:
'200': '200':
description: Success description: Success
'400':
description: Bad request
'403': '403':
description: Authentication credentials were invalid, absent or insufficient. description: Authentication credentials were invalid, absent or insufficient.
schema: schema:
@ -8748,15 +8758,15 @@ paths:
description: '' description: ''
schema: schema:
$ref: '#/definitions/OAuth2ProviderSetupURLs' $ref: '#/definitions/OAuth2ProviderSetupURLs'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
'404': '404':
description: Object does not exist or caller has insufficient permissions description: Object does not exist or caller has insufficient permissions
to access it. to access it.
schema: schema:
$ref: '#/definitions/APIException' $ref: '#/definitions/APIException'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
tags: tags:
- providers - providers
parameters: parameters:
@ -9205,15 +9215,15 @@ paths:
description: '' description: ''
schema: schema:
$ref: '#/definitions/SAMLMetadata' $ref: '#/definitions/SAMLMetadata'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
'404': '404':
description: Object does not exist or caller has insufficient permissions description: Object does not exist or caller has insufficient permissions
to access it. to access it.
schema: schema:
$ref: '#/definitions/APIException' $ref: '#/definitions/APIException'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
tags: tags:
- providers - providers
parameters: parameters: