mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Merge branch 'master' into update-page-count-after-antivirus-scan
This commit is contained in:
@@ -33,11 +33,14 @@ from app.models import (
|
||||
Notification,
|
||||
NOTIFICATION_CREATED,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_PENDING_VIRUS_CHECK,
|
||||
NOTIFICATION_PERMANENT_FAILURE,
|
||||
NOTIFICATION_SENDING,
|
||||
NOTIFICATION_TECHNICAL_FAILURE,
|
||||
NOTIFICATION_VALIDATION_FAILED,
|
||||
NOTIFICATION_VIRUS_SCAN_FAILED,
|
||||
)
|
||||
|
||||
from tests.app.db import create_notification
|
||||
|
||||
from tests.conftest import set_config_values
|
||||
@@ -338,7 +341,7 @@ def test_letter_in_created_state_fails_if_notification_doesnt_exist(sample_notif
|
||||
|
||||
@freeze_time('2018-01-01 18:00')
|
||||
@mock_s3
|
||||
@pytest.mark.parametrize('key_type, noti_status,bucket_config_name,destination_folder', [
|
||||
@pytest.mark.parametrize('key_type,noti_status,bucket_config_name,destination_folder', [
|
||||
(KEY_TYPE_NORMAL, NOTIFICATION_CREATED, 'LETTERS_PDF_BUCKET_NAME', '2018-01-02/'),
|
||||
(KEY_TYPE_TEST, NOTIFICATION_DELIVERED, 'TEST_LETTERS_BUCKET_NAME', '')
|
||||
])
|
||||
@@ -361,23 +364,61 @@ def test_process_letter_task_check_virus_scan_passed(
|
||||
|
||||
mocker.patch('app.celery.letters_pdf_tasks.pdf_page_count', return_value=1)
|
||||
mock_s3upload = mocker.patch('app.celery.letters_pdf_tasks.s3upload')
|
||||
mock_sanitise = mocker.patch('app.celery.letters_pdf_tasks._sanitise_precomiled_pdf')
|
||||
mock_sanitise = mocker.patch('app.celery.letters_pdf_tasks._sanitise_precomiled_pdf', return_value="pdf_content")
|
||||
|
||||
process_virus_scan_passed(filename)
|
||||
|
||||
assert letter_notification.status == noti_status
|
||||
assert letter_notification.billable_units == 1
|
||||
mock_s3upload.assert_called_once_with(
|
||||
filedata=b'pdf_content',
|
||||
region='eu-west-1',
|
||||
bucket_name=target_bucket_name,
|
||||
file_location=destination_folder + filename
|
||||
)
|
||||
mock_sanitise.assert_called_once_with(
|
||||
ANY,
|
||||
letter_notification,
|
||||
b'pdf_content'
|
||||
)
|
||||
mock_s3upload.assert_called_once_with(
|
||||
bucket_name=target_bucket_name,
|
||||
filedata=b'pdf_content',
|
||||
file_location=destination_folder + filename,
|
||||
region='eu-west-1',
|
||||
)
|
||||
|
||||
|
||||
@freeze_time('2018-01-01 18:00')
|
||||
@mock_s3
|
||||
@pytest.mark.parametrize('key_type,is_test_letter', [
|
||||
(KEY_TYPE_NORMAL, False), (KEY_TYPE_TEST, True)
|
||||
])
|
||||
def test_process_letter_task_check_virus_scan_passed_when_sanitise_fails(
|
||||
sample_letter_notification, mocker, key_type, is_test_letter
|
||||
):
|
||||
filename = 'NOTIFY.{}'.format(sample_letter_notification.reference)
|
||||
source_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||
target_bucket_name = current_app.config['INVALID_PDF_BUCKET_NAME']
|
||||
|
||||
conn = boto3.resource('s3', region_name='eu-west-1')
|
||||
conn.create_bucket(Bucket=source_bucket_name)
|
||||
conn.create_bucket(Bucket=target_bucket_name)
|
||||
|
||||
s3 = boto3.client('s3', region_name='eu-west-1')
|
||||
s3.put_object(Bucket=source_bucket_name, Key=filename, Body=b'pdf_content')
|
||||
|
||||
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_precomiled_pdf', return_value=None)
|
||||
|
||||
process_virus_scan_passed(filename)
|
||||
|
||||
assert sample_letter_notification.status == NOTIFICATION_VALIDATION_FAILED
|
||||
mock_sanitise.assert_called_once_with(
|
||||
ANY,
|
||||
sample_letter_notification,
|
||||
b'pdf_content'
|
||||
)
|
||||
mock_move_s3.assert_called_once_with(
|
||||
source_bucket_name, filename,
|
||||
target_bucket_name, filename
|
||||
)
|
||||
|
||||
|
||||
def test_get_page_count_set_notification_to_permanent_failure_when_not_pdf(
|
||||
@@ -391,7 +432,7 @@ def test_get_page_count_set_notification_to_permanent_failure_when_not_pdf(
|
||||
|
||||
def test_process_letter_task_check_virus_scan_failed(sample_letter_notification, mocker):
|
||||
filename = 'NOTIFY.{}'.format(sample_letter_notification.reference)
|
||||
sample_letter_notification.status = 'pending-virus-check'
|
||||
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
mock_move_failed_pdf = mocker.patch('app.celery.letters_pdf_tasks.move_failed_pdf')
|
||||
|
||||
with pytest.raises(VirusScanError) as e:
|
||||
@@ -404,7 +445,7 @@ def test_process_letter_task_check_virus_scan_failed(sample_letter_notification,
|
||||
|
||||
def test_process_letter_task_check_virus_scan_error(sample_letter_notification, mocker):
|
||||
filename = 'NOTIFY.{}'.format(sample_letter_notification.reference)
|
||||
sample_letter_notification.status = 'pending-virus-check'
|
||||
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
mock_move_failed_pdf = mocker.patch('app.celery.letters_pdf_tasks.move_failed_pdf')
|
||||
|
||||
with pytest.raises(VirusScanError) as e:
|
||||
@@ -436,6 +477,7 @@ def test_replay_letters_in_error_for_one_file(notify_api, mocker):
|
||||
|
||||
|
||||
def test_sanitise_precompiled_pdf_returns_data_from_template_preview(rmock, sample_letter_notification):
|
||||
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
rmock.post('http://localhost:9999/precompiled/sanitise', content=b'new_pdf', status_code=200)
|
||||
mock_celery = Mock(**{'retry.side_effect': Retry})
|
||||
|
||||
@@ -446,6 +488,7 @@ def test_sanitise_precompiled_pdf_returns_data_from_template_preview(rmock, samp
|
||||
|
||||
|
||||
def test_sanitise_precompiled_pdf_returns_none_on_validation_error(rmock, sample_letter_notification):
|
||||
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
rmock.post('http://localhost:9999/precompiled/sanitise', content=b'new_pdf', status_code=400)
|
||||
mock_celery = Mock(**{'retry.side_effect': Retry})
|
||||
|
||||
@@ -455,6 +498,7 @@ def test_sanitise_precompiled_pdf_returns_none_on_validation_error(rmock, sample
|
||||
|
||||
|
||||
def test_sanitise_precompiled_pdf_retries_on_http_error(rmock, sample_letter_notification):
|
||||
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
rmock.post('http://localhost:9999/precompiled/sanitise', content=b'new_pdf', status_code=500)
|
||||
mock_celery = Mock(**{'retry.side_effect': Retry})
|
||||
|
||||
@@ -466,6 +510,7 @@ def test_sanitise_precompiled_pdf_sets_notification_to_technical_failure_after_t
|
||||
rmock,
|
||||
sample_letter_notification
|
||||
):
|
||||
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
rmock.post('http://localhost:9999/precompiled/sanitise', content=b'new_pdf', status_code=500)
|
||||
mock_celery = Mock(**{'retry.side_effect': MaxRetriesExceededError})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user