Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

fix: Return HTTP 400 instead of HTTP 500 on DELETE /api/documents/{id}/notes/ with missing or invalid note id (#12582)

Gaëtan GOUZIbe8658d61ae1
diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py
index 352ce7810..04926cb75 100644
--- a/src/documents/tests/test_api_documents.py
+++ b/src/documents/tests/test_api_documents.py
@@ -2720,6 +2720,77 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
# modified was updated to today
self.assertEqual(doc.modified.day, timezone.now().day)
+ def test_delete_note_missing_id(self) -> None:
+ """
+ GIVEN:
+ - Existing document
+ WHEN:
+ - API DELETE request to notes endpoint without an id query param
+ - API DELETE request to notes endpoint with an empty id query param
+ THEN:
+ - HTTP 400 is returned
+ """
+ doc = Document.objects.create(
+ title="test",
+ mime_type="application/pdf",
+ content="this is a document",
+ )
+
+ response = self.client.delete(
+ f"/api/documents/{doc.pk}/notes/",
+ format="json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+
+ response = self.client.delete(
+ f"/api/documents/{doc.pk}/notes/?id=",
+ format="json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+
+ def test_delete_note_invalid_id(self) -> None:
+ """
+ GIVEN:
+ - Existing document
+ WHEN:
+ - API DELETE request to notes endpoint with a non-integer note id
+ THEN:
+ - HTTP 400 is returned
+ """
+ doc = Document.objects.create(
+ title="test",
+ mime_type="application/pdf",
+ content="this is a document",
+ )
+
+ response = self.client.delete(
+ f"/api/documents/{doc.pk}/notes/?id=notaninteger",
+ format="json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+
+ def test_delete_note_nonexistent_id(self) -> None:
+ """
+ GIVEN:
+ - Existing document, no notes
+ WHEN:
+ - API DELETE request to notes endpoint with a non-existent note id
+ THEN:
+ - HTTP 404 is returned
+ """
+ doc = Document.objects.create(
+ title="test",
+ mime_type="application/pdf",
+ content="this is a document",
+ )
+
+ response = self.client.delete(
+ f"/api/documents/{doc.pk}/notes/?id=99999",
+ format="json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+
def test_get_notes_no_doc(self):
"""
GIVEN:
diff --git a/src/documents/views.py b/src/documents/views.py
index 732fe2232..d08543aea 100644
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -1125,7 +1125,14 @@ class DocumentViewSet(
):
return HttpResponseForbidden("Insufficient permissions to delete notes")
- note = Note.objects.get(id=int(request.GET.get("id")), document=doc)
+ note_id = request.GET.get("id")
+ if not note_id:
+ raise ValidationError({"id": "This field is required."})
+ try:
+ note_id_int = int(note_id)
+ except ValueError:
+ raise ValidationError({"id": "A valid integer is required."})
+ note = get_object_or_404(Note, id=note_id_int, document=doc)
if settings.AUDIT_LOG_ENABLED:
LogEntry.objects.log_create(
instance=doc,