From e6da2a94d1db215aec39f6c1a6d34e5d5148d5ac Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 15 Sep 2025 01:13:26 -0700 Subject: [PATCH] coverage, unify error tests --- src/documents/tests/test_api_documents.py | 47 ++++++++++++++++------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py index 48d52eca0..a30269c8d 100644 --- a/src/documents/tests/test_api_documents.py +++ b/src/documents/tests/test_api_documents.py @@ -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