mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 06:21:50 -05:00
Merge branch 'master' of https://github.com/alphagov/notifications-api into vb-report-tasks
This commit is contained in:
@@ -19,8 +19,8 @@ from app.celery.letters_pdf_tasks import (
|
||||
letter_in_created_state,
|
||||
process_virus_scan_passed,
|
||||
process_virus_scan_failed,
|
||||
)
|
||||
from app.letters.utils import get_letter_pdf_filename
|
||||
process_virus_scan_error)
|
||||
from app.letters.utils import get_letter_pdf_filename, ScanErrorType
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEST,
|
||||
@@ -28,8 +28,8 @@ from app.models import (
|
||||
NOTIFICATION_CREATED,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_VIRUS_SCAN_FAILED,
|
||||
NOTIFICATION_SENDING
|
||||
)
|
||||
NOTIFICATION_SENDING,
|
||||
NOTIFICATION_TECHNICAL_FAILURE)
|
||||
|
||||
from tests.conftest import set_config_values
|
||||
|
||||
@@ -341,9 +341,20 @@ def test_process_letter_task_check_virus_scan_passed(
|
||||
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'
|
||||
mock_delete_pdf = mocker.patch('app.celery.letters_pdf_tasks.delete_pdf_from_letters_scan_bucket')
|
||||
mock_move_failed_pdf = mocker.patch('app.celery.letters_pdf_tasks.move_failed_pdf')
|
||||
|
||||
process_virus_scan_failed(filename)
|
||||
|
||||
mock_delete_pdf.assert_called_once_with(filename)
|
||||
mock_move_failed_pdf.assert_called_once_with(filename, ScanErrorType.FAILURE)
|
||||
assert sample_letter_notification.status == NOTIFICATION_VIRUS_SCAN_FAILED
|
||||
|
||||
|
||||
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'
|
||||
mock_move_failed_pdf = mocker.patch('app.celery.letters_pdf_tasks.move_failed_pdf')
|
||||
|
||||
process_virus_scan_error(filename)
|
||||
|
||||
mock_move_failed_pdf.assert_called_once_with(filename, ScanErrorType.ERROR)
|
||||
assert sample_letter_notification.status == NOTIFICATION_TECHNICAL_FAILURE
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
from celery.exceptions import MaxRetriesExceededError
|
||||
from notifications_utils.recipients import InvalidEmailError
|
||||
|
||||
import app
|
||||
from app.celery import provider_tasks
|
||||
from app.celery.provider_tasks import deliver_sms, deliver_email
|
||||
from app.clients.email.aws_ses import AwsSesClientException
|
||||
from app.exceptions import NotificationTechnicalFailureException
|
||||
|
||||
|
||||
@@ -92,6 +94,24 @@ def test_should_technical_error_and_not_retry_if_invalid_email(sample_notificati
|
||||
assert sample_notification.status == 'technical-failure'
|
||||
|
||||
|
||||
def test_should_retry_and_log_exception(sample_notification, mocker):
|
||||
error_response = {
|
||||
'Error': {
|
||||
'Code': 'SomeError',
|
||||
'Message': 'some error message from amazon',
|
||||
'Type': 'Sender'
|
||||
}
|
||||
}
|
||||
ex = ClientError(error_response=error_response, operation_name='opname')
|
||||
mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=AwsSesClientException(str(ex)))
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.retry')
|
||||
|
||||
deliver_email(sample_notification.id)
|
||||
|
||||
assert provider_tasks.deliver_email.retry.called is True
|
||||
assert sample_notification.status == 'created'
|
||||
|
||||
|
||||
def test_send_sms_should_switch_providers_on_provider_failure(sample_notification, mocker):
|
||||
provider_to_use = mocker.patch('app.delivery.send_to_providers.provider_to_use')
|
||||
provider_to_use.return_value.send_sms.side_effect = Exception('Error')
|
||||
|
||||
@@ -11,8 +11,8 @@ from app.letters.utils import (
|
||||
get_letter_pdf_filename,
|
||||
get_letter_pdf,
|
||||
upload_letter_pdf,
|
||||
move_scanned_pdf_to_test_or_live_pdf_bucket
|
||||
)
|
||||
move_scanned_pdf_to_test_or_live_pdf_bucket,
|
||||
ScanErrorType, move_failed_pdf)
|
||||
from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEST, PRECOMPILED_TEMPLATE_NAME
|
||||
from app.variables import Retention
|
||||
|
||||
@@ -163,3 +163,43 @@ def test_move_scanned_letter_pdf_to_processing_bucket(
|
||||
|
||||
assert folder_date_name + filename in [o.key for o in target_bucket.objects.all()]
|
||||
assert filename not in [o.key for o in source_bucket.objects.all()]
|
||||
|
||||
|
||||
@mock_s3
|
||||
@freeze_time(FROZEN_DATE_TIME)
|
||||
def test_move_failed_pdf_error(notify_api):
|
||||
filename = 'test.pdf'
|
||||
source_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||
target_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||
|
||||
conn = boto3.resource('s3', region_name='eu-west-1')
|
||||
source_bucket = conn.create_bucket(Bucket=source_bucket_name)
|
||||
target_bucket = 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')
|
||||
|
||||
move_failed_pdf(filename, ScanErrorType.ERROR)
|
||||
|
||||
assert 'ERROR/' + filename in [o.key for o in target_bucket.objects.all()]
|
||||
assert filename not in [o.key for o in source_bucket.objects.all()]
|
||||
|
||||
|
||||
@mock_s3
|
||||
@freeze_time(FROZEN_DATE_TIME)
|
||||
def test_move_failed_pdf_scan_failed(notify_api):
|
||||
filename = 'test.pdf'
|
||||
source_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||
target_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||
|
||||
conn = boto3.resource('s3', region_name='eu-west-1')
|
||||
source_bucket = conn.create_bucket(Bucket=source_bucket_name)
|
||||
target_bucket = 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')
|
||||
|
||||
move_failed_pdf(filename, ScanErrorType.FAILURE)
|
||||
|
||||
assert 'FAILURE/' + filename in [o.key for o in target_bucket.objects.all()]
|
||||
assert filename not in [o.key for o in source_bucket.objects.all()]
|
||||
|
||||
Reference in New Issue
Block a user