Put pdf letter validation failed message in S3 metadata

So it can be used to tell the user why the letter has failed validation
This commit is contained in:
Pea Tyczynska
2019-10-11 12:24:53 +01:00
parent 0152dcf254
commit 0a617379c4
3 changed files with 93 additions and 53 deletions

View File

@@ -436,10 +436,9 @@ def test_process_letter_task_check_virus_scan_passed(
json={
"file": base64.b64encode(b"new_pdf").decode("utf-8"),
"validation_passed": True,
"errors": {
"content_outside_of_printable_area": [],
"document_not_a4_size_portrait_orientation": [],
}
"message": "",
"invalid_pages": [],
"page_count": 1
},
status_code=200
)
@@ -479,7 +478,16 @@ def test_process_letter_task_check_virus_scan_passed_when_sanitise_fails(
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
sample_letter_notification.key_type = key_type
mock_move_s3 = mocker.patch('app.letters.utils._move_s3_object')
mock_sanitise = mocker.patch('app.celery.letters_pdf_tasks._sanitise_precompiled_pdf', return_value=None)
sanitise_response = {
"file": base64.b64encode(b"nyan").decode("utf-8"),
"validation_passed": False,
"message": "content-outside-printable-area",
"invalid_pages": [1],
"page_count": 1
}
mock_sanitise = mocker.patch(
'app.celery.letters_pdf_tasks._sanitise_precompiled_pdf', return_value=sanitise_response
)
mock_get_page_count = mocker.patch('app.celery.letters_pdf_tasks.get_page_count', return_value=2)
process_virus_scan_passed(filename)
@@ -492,8 +500,12 @@ def test_process_letter_task_check_virus_scan_passed_when_sanitise_fails(
b'pdf_content'
)
mock_move_s3.assert_called_once_with(
source_bucket_name, filename,
target_bucket_name, filename
source_bucket=source_bucket_name, source_filename=filename,
target_bucket=target_bucket_name, target_filename=filename, metadata={
"validation_failed_message": "content-outside-printable-area",
"invalid_pages": ["1"],
"page_count": "1"
}
)
mock_get_page_count.assert_called_once_with(b'pdf_content')
@@ -533,10 +545,9 @@ def test_process_letter_task_check_virus_scan_passed_when_redaction_fails(
"file": base64.b64encode(b"new_pdf").decode("utf-8"),
"validation_passed": True,
"redaction_failed_message": "No matches for address block during redaction procedure",
"errors": {
"content_outside_of_printable_area": [],
"document_not_a4_size_portrait_orientation": []
}
"message": "",
"invalid_pages": "",
"page_count": 2
},
status_code=200
)
@@ -582,8 +593,8 @@ def test_process_letter_task_check_virus_scan_passed_when_file_cannot_be_opened(
mock_sanitise.assert_not_called()
mock_get_page_count.assert_called_once_with(b'pdf_content')
mock_move_s3.assert_called_once_with(
source_bucket_name, filename,
target_bucket_name, filename
source_bucket=source_bucket_name, source_filename=filename,
target_bucket=target_bucket_name, target_filename=filename, metadata={}
)
assert sample_letter_notification.status == NOTIFICATION_VALIDATION_FAILED
assert sample_letter_notification.billable_units == 0
@@ -626,10 +637,9 @@ def test_process_virus_scan_passed_logs_error_and_sets_tech_failure_if_s3_error_
json={
"file": base64.b64encode(b"new_pdf").decode("utf-8"),
"validation_passed": True,
"errors": {
"content_outside_of_printable_area": [],
"document_not_a4_size_portrait_orientation": [],
}
"message": "",
"invalid_pages": [],
"page_count": 1
},
status_code=200
)
@@ -656,7 +666,11 @@ def test_move_invalid_letter_and_update_status_logs_error_and_sets_tech_failure_
side_effect=ClientError(error_response, 'operation_name'))
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.exception')
_move_invalid_letter_and_update_status(sample_letter_notification, 'filename', mocker.Mock())
_move_invalid_letter_and_update_status(
notification=sample_letter_notification,
filename='filename',
scan_pdf_object=mocker.Mock()
)
assert sample_letter_notification.status == NOTIFICATION_TECHNICAL_FAILURE
mock_logger.assert_called_once_with(
@@ -720,10 +734,9 @@ def test_sanitise_precompiled_pdf_returns_data_from_template_preview(rmock, samp
json={
"file": base64.b64encode(b"new_pdf").decode("utf-8"),
"validation_passed": True,
"errors": {
"content_outside_of_printable_area": [],
"document_not_a4_size_portrait_orientation": [],
}
"message": "",
"invalid_pages": [],
"page_count": 1
},
status_code=200
)
@@ -732,26 +745,26 @@ def test_sanitise_precompiled_pdf_returns_data_from_template_preview(rmock, samp
assert rmock.called
assert rmock.request_history[0].url == endpoint
assert base64.b64decode(response.json()["file"].encode()) == b"new_pdf"
assert base64.b64decode(response["file"].encode()) == b"new_pdf"
assert rmock.last_request.text == 'old_pdf'
def test_sanitise_precompiled_pdf_returns_none_on_validation_error(rmock, sample_letter_notification):
def test_sanitise_precompiled_pdf_return_validation_error(rmock, sample_letter_notification):
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
endpoint = 'http://localhost:9999/precompiled/sanitise'
response_json = {
"file": base64.b64encode(b"nyan").decode("utf-8"),
"validation_passed": False,
"message": "content-outside-printable-area",
"invalid_pages": [1],
"page_count": 1
}
with requests_mock.mock() as rmock:
rmock.request(
"POST",
endpoint,
json={
"file": base64.b64encode(b"nyan").decode("utf-8"),
"validation_passed": False,
"errors": {
"content_outside_of_printable_area": [1],
"document_not_a4_size_portrait_orientation": [],
}
},
json=response_json,
status_code=400
)
mock_celery = Mock(**{'retry.side_effect': Retry})
@@ -759,7 +772,7 @@ def test_sanitise_precompiled_pdf_returns_none_on_validation_error(rmock, sample
assert rmock.called
assert rmock.request_history[0].url == endpoint
assert response is None
assert response == response_json
def test_sanitise_precompiled_pdf_passes_the_service_id_and_notification_id_to_template_preview(