def test_create_letters_pdf_sets_billable_units(mocker, sample_letter_notification): mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', return_value=(b'\x00\x01', 1)) mocker.patch('app.letters.utils.s3upload') create_letters_pdf(sample_letter_notification.id) noti = Notification.query.filter(Notification.reference == sample_letter_notification.reference).one() assert noti.billable_units == 1
def test_create_letters_pdf_handles_request_errors(mocker, sample_letter_notification): mock_get_letters_pdf = mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', side_effect=RequestException) mock_retry = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.retry') create_letters_pdf(sample_letter_notification.id) assert mock_get_letters_pdf.called assert mock_retry.called
def test_create_letters_pdf_sets_technical_failure_max_retries(mocker, sample_letter_notification): mock_get_letters_pdf = mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', side_effect=RequestException) mock_retry = mocker.patch( 'app.celery.letters_pdf_tasks.create_letters_pdf.retry', side_effect=MaxRetriesExceededError) mock_update_noti = mocker.patch('app.celery.letters_pdf_tasks.update_notification_status_by_id') create_letters_pdf(sample_letter_notification.id) assert mock_get_letters_pdf.called assert mock_retry.called mock_update_noti.assert_called_once_with(sample_letter_notification.id, 'technical-failure')
def test_create_letters_pdf_calls_s3upload(mocker, sample_letter_notification): mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', return_value=(b'\x00\x01', '1')) mock_s3 = mocker.patch('app.letters.utils.s3upload') create_letters_pdf(sample_letter_notification.id) mock_s3.assert_called_with( bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'], file_location='2017-12-04/NOTIFY.FOO.D.2.C.C.20171204173100.PDF', filedata=b'\x00\x01', region=current_app.config['AWS_REGION'])
def test_create_letters_pdf_calls_s3upload(mocker, sample_letter_notification): mocker.patch("app.celery.letters_pdf_tasks.get_letters_pdf", return_value=(b"\x00\x01", "1")) mock_s3 = mocker.patch("app.letters.utils.s3upload") create_letters_pdf(sample_letter_notification.id) mock_s3.assert_called_with( bucket_name=current_app.config["LETTERS_PDF_BUCKET_NAME"], file_location="2017-12-04/NOTIFY.FOO.D.2.C.C.20171204173100.PDF", filedata=b"\x00\x01", region=current_app.config["AWS_REGION"], )
def test_create_letters_pdf_calls_s3upload_for_test_letters(mocker, sample_letter_template): mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', return_value=(b'\x00\x01', '1')) mock_s3 = mocker.patch('app.letters.utils.s3upload') notification = create_notification(template=sample_letter_template, reference='FOO', key_type='test') create_letters_pdf(notification.id) mock_s3.assert_called_with( bucket_name=current_app.config['TEST_LETTERS_BUCKET_NAME'], file_location='NOTIFY.FOO.D.2.C.C.20171204173100.PDF', filedata=b'\x00\x01', region=current_app.config['AWS_REGION'] )
def test_create_letters_gets_the_right_logo_when_service_has_no_logo( notify_api, mocker, sample_letter_notification ): mock_get_letters_pdf = mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', return_value=(b'\x00\x01', 1)) mocker.patch('app.letters.utils.s3upload') mocker.patch('app.celery.letters_pdf_tasks.update_notification_status_by_id') create_letters_pdf(sample_letter_notification.id) mock_get_letters_pdf.assert_called_once_with( sample_letter_notification.template, contact_block=sample_letter_notification.reply_to_text, filename=None, values=sample_letter_notification.personalisation )
def test_create_letters_pdf_handles_s3_errors(mocker, sample_letter_notification): mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', return_value=(b'\x00\x01', 1)) error_response = { 'Error': { 'Code': 'InvalidParameterValue', 'Message': 'some error message from amazon', 'Type': 'Sender' } } mock_s3 = mocker.patch('app.letters.utils.s3upload', side_effect=ClientError(error_response, 'operation_name')) mock_retry = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.retry') create_letters_pdf(sample_letter_notification.id) assert mock_s3.called assert mock_retry.called
def test_create_letters_pdf_calls_s3upload(mocker, sample_letter_notification): mocker.patch('app.celery.letters_pdf_tasks.get_letters_pdf', return_value=(b'\x00\x01', '1')) mock_s3 = mocker.patch('app.letters.utils.s3upload') create_letters_pdf(sample_letter_notification.id) filename = get_letter_pdf_filename( reference=sample_letter_notification.reference, crown=sample_letter_notification.service.crown) mock_s3.assert_called_with( bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'], file_location=filename, filedata=b'\x00\x01', region=current_app.config['AWS_REGION'])
def test_create_letters_gets_the_right_logo_when_service_has_letter_branding_logo( notify_api, mocker, sample_letter_notification): letter_branding = create_letter_branding(name="test brand", filename="test-brand") sample_letter_notification.service.letter_branding = letter_branding mock_get_letters_pdf = mocker.patch( "app.celery.letters_pdf_tasks.get_letters_pdf", return_value=(b"\x00\x01", 1)) mocker.patch("app.letters.utils.s3upload") mocker.patch( "app.celery.letters_pdf_tasks.update_notification_status_by_id") create_letters_pdf(sample_letter_notification.id) mock_get_letters_pdf.assert_called_once_with( sample_letter_notification.template, contact_block=sample_letter_notification.reply_to_text, filename=sample_letter_notification.service.letter_branding.filename, values=sample_letter_notification.personalisation, )
def test_create_letters_pdf_handles_s3_errors(mocker, sample_letter_notification): mocker.patch("app.celery.letters_pdf_tasks.get_letters_pdf", return_value=(b"\x00\x01", 1)) error_response = { "Error": { "Code": "InvalidParameterValue", "Message": "some error message from amazon", "Type": "Sender", } } mock_s3 = mocker.patch( "app.letters.utils.s3upload", side_effect=ClientError(error_response, "operation_name"), ) mock_retry = mocker.patch( "app.celery.letters_pdf_tasks.create_letters_pdf.retry") create_letters_pdf(sample_letter_notification.id) assert mock_s3.called assert mock_retry.called
def test_create_letters_pdf_non_existent_notification(notify_api, mocker, fake_uuid): with pytest.raises(expected_exception=NoResultFound): create_letters_pdf(fake_uuid)