def test_update_letter_branding_rolls_back_db_changes_and_shows_error_if_saving_to_s3_fails( mocker, platform_admin_client, mock_get_letter_branding_by_id, fake_uuid ): mocker.patch('app.main.views.letter_branding.get_png_file_from_svg',) mock_client_update = mocker.patch('app.main.views.letter_branding.letter_branding_client.update_letter_branding') mocker.patch('app.main.views.letter_branding.upload_letter_logos', side_effect=BotoClientError({}, 'error')) temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=fake_uuid, unique_id=fake_uuid, filename='new_file.svg') response = platform_admin_client.post( url_for('.update_letter_branding', branding_id=fake_uuid, logo=temp_logo), data={ 'name': 'Updated name', 'operation': 'branding-details' }, follow_redirects=True ) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') assert response.status_code == 200 assert page.find('h1').text == 'Update letter branding' assert page.select_one('.error-message').text.strip() == 'Error saving uploaded file - try uploading again' assert mock_client_update.call_count == 2 assert mock_client_update.call_args_list == [ call(branding_id=fake_uuid, filename='{}-new_file'.format(fake_uuid), name='Updated name'), call(branding_id=fake_uuid, filename='hm-government', name='HM Government') ]
def test_get_document_with_boto_error(store): store.s3.get_object = mock.Mock(side_effect=BotoClientError( {'Error': { 'Code': 'Error code', 'Message': 'Error message' }}, 'GetObject')) with pytest.raises(DocumentStoreError): store.get('service-id', 'document-id', '0f0f0f')
def test_check_document_exists_with_unexpected_boto_error(store): store.s3.head_object = mock.Mock(side_effect=BotoClientError( {'Error': { 'Code': 'code', 'Message': 'Unhandled Exception' }}, 'HeadObject')) with pytest.raises(DocumentStoreError): store.check_document_exists('service-id', 'document-id', '0f0f0f')
def test_check_document_exists_when_document_is_not_in_s3(store): store.s3.head_object = mock.Mock(side_effect=BotoClientError( {'Error': { 'Code': '404', 'Message': 'Not Found' }}, 'HeadObject')) assert store.check_document_exists('service-id', 'document-id', '0f0f0f') is False
def test_scan_virus_boto_error(notify_antivirus, mocker): mocker.patch('app.celery.tasks._get_letter_pdf', side_effect=BotoClientError({}, "S3 Error")) mock_retry = mocker.patch('app.celery.tasks.scan_file.retry') scan_file(TEST_FILENAME) assert mock_retry.called
def test_get_document_metadata_when_document_is_not_in_s3(store): store.s3.head_object = mock.Mock(side_effect=BotoClientError( {'Error': { 'Code': '404', 'Message': 'Not Found' }}, 'HeadObject')) assert store.get_document_metadata('service-id', 'document-id', '0f0f0f') is None
def test_scan_virus_max_retries(notify_antivirus, mocker): mocker.patch('app.celery.tasks._get_letter_pdf', side_effect=BotoClientError({}, "S3 Error")) mocker.patch('app.celery.tasks.scan_file.retry', side_effect=MaxRetriesExceededError) mock_send_task = mocker.patch('app.notify_celery.send_task') scan_file(TEST_FILENAME) mock_send_task.assert_called_once_with( name='process-virus-scan-error', kwargs={'filename': TEST_FILENAME}, queue=QueueNames.LETTERS, )
def test_sanitise_and_upload_letter_raises_a_boto_error(mocker, client): mocker.patch('app.celery.tasks.s3download', side_effect=BotoClientError({}, 'operation-name')) mock_upload = mocker.patch('app.celery.tasks.s3upload') mock_celery = mocker.patch('app.celery.tasks.notify_celery.send_task') mock_logger = mocker.patch('app.celery.tasks.current_app.logger.exception') filename = 'filename.pdf' notification_id = 'abc-123' sanitise_and_upload_letter(notification_id, filename) assert not mock_upload.called assert not mock_celery.called mock_logger.assert_called_once_with( 'Error downloading {} from scan bucket or uploading to sanitise bucket for notification {}' .format(filename, notification_id))
def test_update_letter_branding_rolls_back_db_changes_and_shows_error_if_saving_to_s3_fails( mocker, platform_admin_client, mock_get_letter_branding_by_id, fake_uuid): mocker.patch("app.main.views.letter_branding.get_png_file_from_svg", ) mock_client_update = mocker.patch( "app.main.views.letter_branding.letter_branding_client.update_letter_branding" ) mocker.patch( "app.main.views.letter_branding.upload_letter_logos", side_effect=BotoClientError({}, "error"), ) temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=fake_uuid, unique_id=fake_uuid, filename="new_file.svg") response = platform_admin_client.post( url_for(".update_letter_branding", branding_id=fake_uuid, logo=temp_logo), data={ "name": "Updated name", "operation": "branding-details" }, follow_redirects=True, ) page = BeautifulSoup(response.data.decode("utf-8"), "html.parser") assert response.status_code == 200 assert page.find("h1").text == "Update letter branding" assert page.select_one(".error-message").text.strip( ) == "Error saving uploaded file - try uploading again" assert mock_client_update.call_count == 2 assert mock_client_update.call_args_list == [ call( branding_id=fake_uuid, filename="{}-new_file".format(fake_uuid), name="Updated name", ), call(branding_id=fake_uuid, filename="hm-government", name="HM Government"), ]
def test_create_pdf_for_templated_letter_boto_error( mocker, client, data_for_create_pdf_for_templated_letter_task): # handle boto error while uploading file mocker.patch('app.celery.tasks.s3upload', side_effect=BotoClientError({}, 'operation-name')) mock_celery = mocker.patch('app.celery.tasks.notify_celery.send_task') mock_logger = mocker.patch('app.celery.tasks.current_app.logger.info') mock_logger_exception = mocker.patch( 'app.celery.tasks.current_app.logger.exception') encrypted_data = current_app.encryption_client.encrypt( data_for_create_pdf_for_templated_letter_task) create_pdf_for_templated_letter(encrypted_data) assert not mock_celery.called mock_logger.assert_called_once_with( "Creating a pdf for notification with id abc-123") mock_logger_exception.assert_called_once_with( "Error uploading MY_LETTER.PDF to pdf bucket for notification abc-123")
def test_cbc_proxy_will_failover_to_second_lambda_if_boto_client_error( mocker, cbc_proxy_client, cbc ): cbc_proxy = cbc_proxy_client.get_proxy(cbc) ld_client_mock = mocker.patch.object( cbc_proxy, '_lambda_client', create=True, ) ld_client_mock.invoke.side_effect = BotoClientError({}, 'error') with pytest.raises(CBCProxyRetryableException) as e: cbc_proxy.create_and_send_broadcast( identifier='my-identifier', message_number='0000007b', headline='my-headline', description='test-description', areas=EXAMPLE_AREAS, sent='a-passed-through-sent-value', expires='a-passed-through-expires-value', channel="severe", ) assert e.match(f'Lambda failed for both {cbc}-1-proxy and {cbc}-2-proxy') assert ld_client_mock.invoke.call_args_list == [ call( FunctionName=f'{cbc}-1-proxy', InvocationType='RequestResponse', Payload=mocker.ANY, ), call( FunctionName=f'{cbc}-2-proxy', InvocationType='RequestResponse', Payload=mocker.ANY, ) ]
latest_ver = [ver for ver in versions if ver.is_latest] log.debug(latest_ver) if latest_ver: return latest_ver[0] else: return None def delete(self, key="", version_id=""): """ Delete object. Deletes latest ver of object""" k = key or self.key obj = self._s3.Object(self.bucket_name, k) if version_id: resp = obj.delete(VersionId=version_id) else: resp = obj.delete() log.debug(resp) return resp if __name__ == "__main__": # logging.basicConfig() # log.setLevel(logging.DEBUG) # b = Bucket(bucket="awsjar-testing-regular-bucket", key="object-with-no-versions") # b.put({"testing": 1234}) # b.delete() # x = b.get() # print("get", x) # x = b.is_versioning_enabled() # print(x) BotoClientError('asdf', {})