From 38eb6d45b74cc51af12d19c99b905f5540fb18ff Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 6 Mar 2025 14:46:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(back)=20fix=20ValidationError=20ex?= =?UTF-8?q?ception=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The exception handler for the ValidationError was not testing correctly the existence of some attributes like `message_dict`. --- src/backend/core/api/__init__.py | 7 ++++--- .../tests/documents/test_api_document_invitations.py | 11 +++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/backend/core/api/__init__.py b/src/backend/core/api/__init__.py index bc8d1329..46f2a8b0 100644 --- a/src/backend/core/api/__init__.py +++ b/src/backend/core/api/__init__.py @@ -17,9 +17,10 @@ def exception_handler(exc, context): https://gist.github.com/twidi/9d55486c36b6a51bdcb05ce3a763e79f """ if isinstance(exc, ValidationError): - detail = exc.message_dict - - if hasattr(exc, "message"): + detail = None + if hasattr(exc, "message_dict"): + detail = exc.message_dict + elif hasattr(exc, "message"): detail = exc.message elif hasattr(exc, "messages"): detail = exc.messages diff --git a/src/backend/core/tests/documents/test_api_document_invitations.py b/src/backend/core/tests/documents/test_api_document_invitations.py index 92e17794..9efd2190 100644 --- a/src/backend/core/tests/documents/test_api_document_invitations.py +++ b/src/backend/core/tests/documents/test_api_document_invitations.py @@ -424,7 +424,8 @@ def test_api_document_invitations_create_privileged_members( def test_api_document_invitations_create_email_from_senders_language(): """ - When inviting on a document a user who does not exist yet in our database, the invitation email should be sent in the language of the sending user. + When inviting on a document a user who does not exist yet in our database, + the invitation email should be sent in the language of the sending user. """ user = factories.UserFactory(language="fr-fr") document = factories.DocumentFactory() @@ -559,9 +560,11 @@ def test_api_document_invitations_create_cannot_duplicate_invitation(): ) assert response.status_code == 400 - assert response.json() == [ - "Document invitation with this Email address and Document already exists." - ] + assert response.json() == { + "__all__": [ + "Document invitation with this Email address and Document already exists." + ], + } def test_api_document_invitations_create_cannot_invite_existing_users():