mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-09-16 21:55:37 -05:00
coverage, unify error tests
This commit is contained in:
@@ -1548,19 +1548,6 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
|
||||
id=str(uuid.uuid4()),
|
||||
)
|
||||
|
||||
# invalid custom field ids
|
||||
with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/",
|
||||
{
|
||||
"document": f,
|
||||
"custom_fields": json.dumps({"3456": "a string"}),
|
||||
},
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.consume_file_mock.assert_not_called()
|
||||
self.consume_file_mock.reset_mock()
|
||||
|
||||
cf_string = CustomField.objects.create(
|
||||
name="stringfield",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
@@ -1597,6 +1584,40 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
|
||||
{cf_string.id: "a string", cf_int.id: 123},
|
||||
)
|
||||
|
||||
def test_upload_with_custom_fields_errors(self):
|
||||
"""
|
||||
GIVEN: A document with a source file
|
||||
WHEN: Upload the document with invalid custom fields payloads
|
||||
THEN: The upload is rejected
|
||||
"""
|
||||
self.consume_file_mock.return_value = celery.result.AsyncResult(
|
||||
id=str(uuid.uuid4()),
|
||||
)
|
||||
|
||||
error_payloads = [
|
||||
# Non-integer key in mapping
|
||||
{"custom_fields": json.dumps({"abc": "a string"})},
|
||||
# List with non-integer entry
|
||||
{"custom_fields": json.dumps(["abc"])},
|
||||
# Nonexistent id in mapping
|
||||
{"custom_fields": json.dumps({99999999: "a string"})},
|
||||
# Nonexistent id in list
|
||||
{"custom_fields": json.dumps([99999999])},
|
||||
# Invalid type (JSON string, not list/dict/int)
|
||||
{"custom_fields": json.dumps("not-a-supported-structure")},
|
||||
]
|
||||
|
||||
for payload in error_payloads:
|
||||
with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f:
|
||||
data = {"document": f, **payload}
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/",
|
||||
data,
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
self.consume_file_mock.assert_not_called()
|
||||
|
||||
def test_upload_with_webui_source(self):
|
||||
"""
|
||||
GIVEN: A document with a source file
|
||||
|
Reference in New Issue
Block a user