mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-29 11:09:27 -05:00
Coverage for the filtering errors
This commit is contained in:
parent
a8eb460c3a
commit
aa6aad1a23
@ -778,8 +778,9 @@ class DocumentsOrderingFilter(OrderingFilter):
|
||||
param = request.query_params.get("ordering")
|
||||
if param and self.prefix in param:
|
||||
custom_field_id = int(param.split(self.prefix)[1])
|
||||
try:
|
||||
field = CustomField.objects.get(pk=custom_field_id)
|
||||
if not field:
|
||||
except CustomField.DoesNotExist:
|
||||
raise ValueError("Custom field not found")
|
||||
|
||||
annotation = None
|
||||
@ -877,6 +878,7 @@ class DocumentsOrderingFilter(OrderingFilter):
|
||||
)
|
||||
|
||||
if not annotation:
|
||||
# Only happens if a new data type is added and not handled here
|
||||
raise ValueError("Invalid custom field data type")
|
||||
|
||||
queryset = (
|
||||
|
@ -2909,3 +2909,40 @@ class TestDocumentApiCustomFieldsSorting(DirectoriesMixin, APITestCase):
|
||||
[results[0]["id"], results[1]["id"], results[2]["id"]],
|
||||
[self.doc1.id, self.doc3.id, self.doc2.id],
|
||||
)
|
||||
|
||||
def test_document_custom_fields_sorting_invalid(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Documents with custom fields
|
||||
WHEN:
|
||||
- API request for document filtering with invalid custom field sorting
|
||||
THEN:
|
||||
- 400 is returned
|
||||
"""
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
response = self.client.get(
|
||||
"/api/documents/?ordering=custom_field_999",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_document_custom_fields_sorting_invalid_data_type(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Documents with custom fields
|
||||
WHEN:
|
||||
- API request for document filtering with a custom field sorting with a new (unhandled) data type
|
||||
THEN:
|
||||
- 400 is returned
|
||||
"""
|
||||
|
||||
custom_field = CustomField.objects.create(
|
||||
name="custom field",
|
||||
data_type="foo",
|
||||
)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
response = self.client.get(
|
||||
f"/api/documents/?ordering=custom_field_{custom_field.pk}",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
Loading…
x
Reference in New Issue
Block a user