diff --git a/app/__init__.py b/app/__init__.py index 30770000a..05d86f698 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -92,7 +92,7 @@ db = SQLAlchemy( ) migrate = None notify_celery = NotifyCelery() -aws_ses_client = AwsSesClient() +aws_ses_client = None aws_ses_stub_client = None aws_sns_client = AwsSnsClient() aws_cloudwatch_client = None @@ -158,8 +158,6 @@ def create_app(application): logging.init_app(application) aws_sns_client.init_app(application) - aws_ses_client.init_app() - # start lazy initialization for gevent migrate = Migrate() migrate.init_app(application, db=db) @@ -169,6 +167,8 @@ def create_app(application): document_download_client.init_app(application) aws_cloudwatch_client = AwsCloudwatchClient() aws_cloudwatch_client.init_app(application) + aws_ses_client = AwsSesClient() + aws_ses_client.init_app() aws_ses_stub_client = AwsSesStubClient() aws_ses_stub_client.init_app(stub_url=application.config["SES_STUB_URL"]) diff --git a/tests/app/clients/test_aws_ses.py b/tests/app/clients/test_aws_ses.py index 302fe2dd9..32cd502c0 100644 --- a/tests/app/clients/test_aws_ses.py +++ b/tests/app/clients/test_aws_ses.py @@ -1,19 +1,22 @@ import json from unittest import mock -from unittest.mock import ANY, Mock +from unittest.mock import ANY, MagicMock, Mock import botocore import pytest -from app import AwsSesStubClient, aws_ses_client from app.clients.email import EmailClientNonRetryableException from app.clients.email.aws_ses import ( AwsSesClientException, AwsSesClientThrottlingSendRateException, get_aws_responses, ) +from app.clients.email.aws_ses_stub import AwsSesStubClient from app.enums import NotificationStatus, StatisticsType +# comes from app/__init__.py but we mock it for the tests +aws_ses_client = MagicMock() + def test_should_return_correct_details_for_delivery(): response_dict = get_aws_responses("Delivery") diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index a06d13a51..e2040d6c0 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -1,6 +1,6 @@ import json from collections import namedtuple -from unittest.mock import ANY +from unittest.mock import ANY, MagicMock import pytest from flask import current_app @@ -142,7 +142,9 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist template=sample_email_template_with_html, ) db_notification.personalisation = {"name": "Jo"} - mocker.patch("app.aws_ses_client.send_email", return_value="reference") + mock_ses_client = MagicMock() + mock_ses_client.send_email.return_value = "reference" + mocker.patch("app.aws_ses_client", mock_ses_client) send_to_providers.send_email_to_provider(db_notification) app.aws_ses_client.send_email.assert_called_once_with( f'"Sample service" ', @@ -176,7 +178,9 @@ def test_should_not_send_email_message_when_service_is_inactive_notifcation_is_i sample_service, sample_notification, mocker ): sample_service.active = False - send_mock = mocker.patch("app.aws_ses_client.send_email", return_value="reference") + mock_ses_client = MagicMock() + mock_ses_client.send_email.return_value = "reference" + send_mock = mocker.patch("app.aws_ses_client", mock_ses_client) mock_s3 = mocker.patch("app.delivery.send_to_providers.get_phone_number_from_s3") mock_s3.return_value = "2028675309" @@ -419,7 +423,8 @@ def test_send_email_to_provider_should_not_send_to_provider_when_status_is_not_c notification = create_notification( template=sample_email_template, status=NotificationStatus.SENDING ) - mocker.patch("app.aws_ses_client.send_email") + mock_ses_client = MagicMock() + mocker.patch("app.aws_ses_client", mock_ses_client) mocker.patch("app.delivery.send_to_providers.send_email_response") mocker.patch("app.delivery.send_to_providers.update_notification_message_id") @@ -438,7 +443,9 @@ def test_send_email_to_provider_should_not_send_to_provider_when_status_is_not_c def test_send_email_should_use_service_reply_to_email( sample_service, sample_email_template, mocker ): - mocker.patch("app.aws_ses_client.send_email", return_value="reference") + mock_ses_client = MagicMock() + mock_ses_client.send_email.return_value = "reference" + mocker.patch("app.aws_ses_client", mock_ses_client) mock_redis = mocker.patch("app.delivery.send_to_providers.redis_store") mock_redis.get.return_value = "test@example.com".encode("utf-8") @@ -819,8 +826,9 @@ def test_send_email_to_provider_uses_reply_to_from_notification( "test@example.com".encode("utf-8"), json.dumps({}).encode("utf-8"), ] - - mocker.patch("app.aws_ses_client.send_email", return_value="reference") + mock_ses_client = MagicMock() + mock_ses_client.send_email.return_value = "reference" + mocker.patch("app.aws_ses_client", mock_ses_client) db_notification = create_notification( template=sample_email_template, @@ -878,7 +886,9 @@ def test_send_sms_to_provider_should_use_normalised_to(mocker, client, sample_te def test_send_email_to_provider_should_user_normalised_to( mocker, client, sample_email_template ): - send_mock = mocker.patch("app.aws_ses_client.send_email", return_value="reference") + mock_ses_client = MagicMock() + mock_ses_client.send_email.return_value = "reference" + send_mock = mocker.patch("app.aws_ses_client", mock_ses_client) notification = create_notification( template=sample_email_template, ) @@ -995,7 +1005,9 @@ def test_send_email_to_provider_should_return_template_if_found_in_redis( "app.dao.templates_dao.dao_get_template_by_id_and_service_id" ) mock_get_service = mocker.patch("app.dao.services_dao.dao_fetch_service_by_id") - send_mock = mocker.patch("app.aws_ses_client.send_email", return_value="reference") + mock_ses_client = MagicMock() + mock_ses_client.send_email.return_value = "reference" + send_mock = mocker.patch("app.aws_ses_client", mock_ses_client) notification = create_notification( template=sample_email_template, )