mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-04-28 02:49:26 +00:00
fix: Return HTTP 400 instead of HTTP 500 on DELETE /api/documents/{id}/notes/ with missing or invalid note id (#12582)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user