diff --git a/api/actions/views.py b/api/actions/views.py index 5a9f8803781..f28ba99c435 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 (ValueError, DjangoValidationError) as exc: + raise ValidationError(str(exc)) from exc # overrides ListFilterMixin def get_default_queryset(self): 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