else:
parameters["archive_fallback"] = False
- def _validate_parameters_edit_pdf(self, parameters):
+ def _validate_parameters_edit_pdf(self, parameters, document_id):
if "operations" not in parameters:
raise serializers.ValidationError("operations not specified")
if not isinstance(parameters["operations"], list):
"update_document only allowed with a single output document",
)
+ doc = Document.objects.get(id=document_id)
+ # doc existence is already validated
+ if doc.page_count:
+ for op in parameters["operations"]:
+ if op["page"] < 1 or op["page"] > doc.page_count:
+ raise serializers.ValidationError(
+ f"Page {op['page']} is out of bounds for document with {doc.page_count} pages.",
+ )
+
def validate(self, attrs):
method = attrs["method"]
parameters = attrs["parameters"]
raise serializers.ValidationError(
"Edit PDF method only supports one document",
)
- self._validate_parameters_edit_pdf(parameters)
+ self._validate_parameters_edit_pdf(parameters, attrs["documents"][0])
return attrs
title="B",
correspondent=self.c1,
document_type=self.dt1,
+ page_count=5,
)
self.doc3 = Document.objects.create(
checksum="C",
response.content,
)
+ @mock.patch("documents.serialisers.bulk_edit.edit_pdf")
+ def test_edit_pdf_page_out_of_bounds(self, m):
+ """
+ GIVEN:
+ - API data for editing PDF is called
+ - The page number is out of bounds
+ WHEN:
+ - API is called
+ THEN:
+ - The API fails with a correct error code
+ """
+ self.setup_mock(m, "edit_pdf")
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc2.id],
+ "method": "edit_pdf",
+ "parameters": {"operations": [{"page": 99}]},
+ },
+ ),
+ content_type="application/json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn(b"out of bounds", response.content)
+
@override_settings(AUDIT_LOG_ENABLED=True)
def test_bulk_edit_audit_log_enabled_simple_field(self):
"""