From 2911fb3aed4f0d5555e5219a9dbff45fefca3d06 Mon Sep 17 00:00:00 2001 From: Anton Krytskyi Date: Mon, 16 Jun 2025 16:52:16 +0300 Subject: [PATCH 1/2] handle django core validation error in drf view --- api/actions/views.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/actions/views.py b/api/actions/views.py index 5a9f8803781..f137487ebc6 100644 --- a/api/actions/views.py +++ b/api/actions/views.py @@ -1,7 +1,8 @@ +from django.core.exceptions import ValidationError as DjangoValidationError from guardian.shortcuts import get_objects_for_user from rest_framework import generics from rest_framework import permissions -from rest_framework.exceptions import NotFound, PermissionDenied +from rest_framework.exceptions import NotFound, PermissionDenied, ValidationError from api.actions.permissions import ReviewActionPermission from api.actions.serializers import NodeRequestActionSerializer, ReviewActionSerializer, PreprintRequestActionSerializer @@ -186,7 +187,10 @@ def perform_create(self, serializer): ), ) - serializer.save(user=self.request.user) + try: + serializer.save(user=self.request.user) + except DjangoValidationError as exc: + raise ValidationError(str(exc)) from exc # overrides ListFilterMixin def get_default_queryset(self): From b11c15c62312d2a1f4f181f3aa50df81054a9204 Mon Sep 17 00:00:00 2001 From: Anton Krytskyi Date: Wed, 18 Jun 2025 18:17:59 +0300 Subject: [PATCH 2/2] add unit tests --- api/actions/views.py | 2 +- api_tests/actions/views/test_action_list.py | 35 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/api/actions/views.py b/api/actions/views.py index f137487ebc6..f28ba99c435 100644 --- a/api/actions/views.py +++ b/api/actions/views.py @@ -189,7 +189,7 @@ def perform_create(self, serializer): try: serializer.save(user=self.request.user) - except DjangoValidationError as exc: + except (ValueError, DjangoValidationError) as exc: raise ValidationError(str(exc)) from exc # overrides ListFilterMixin diff --git a/api_tests/actions/views/test_action_list.py b/api_tests/actions/views/test_action_list.py index c33048656f1..b9254bb3337 100644 --- a/api_tests/actions/views/test_action_list.py +++ b/api_tests/actions/views/test_action_list.py @@ -366,3 +366,38 @@ def test_invalid_target_id(self, app, moderator): expect_errors=True ) assert res.status_code == 404 + + def test_submit_preprint_without_files_returns_400(self, app, url, preprint, node_admin): + # Ensure preprint has no files + preprint.primary_file = None + preprint.save() + + submit_payload = self.create_payload( + preprint._id, + trigger='submit' + ) + + res = app.post_json_api( + url, + submit_payload, + auth=node_admin.auth, + expect_errors=True + ) + assert res.status_code == 400 + + def test_provider_not_reviewed_returns_409(self, app, url, preprint, node_admin): + preprint.provider = PreprintProviderFactory(reviews_workflow=None) + preprint.save() + + submit_payload = self.create_payload( + preprint._id, + trigger='submit' + ) + + res = app.post_json_api( + url, + submit_payload, + auth=node_admin.auth, + expect_errors=True + ) + assert res.status_code == 409