more mocks

This commit is contained in:
Kenneth Kehl
2025-10-02 10:49:05 -07:00
parent 96b8424a40
commit 6d9e996817
3 changed files with 29 additions and 14 deletions

View File

@@ -92,7 +92,7 @@ db = SQLAlchemy(
) )
migrate = None migrate = None
notify_celery = NotifyCelery() notify_celery = NotifyCelery()
aws_ses_client = AwsSesClient() aws_ses_client = None
aws_ses_stub_client = None aws_ses_stub_client = None
aws_sns_client = AwsSnsClient() aws_sns_client = AwsSnsClient()
aws_cloudwatch_client = None aws_cloudwatch_client = None
@@ -158,8 +158,6 @@ def create_app(application):
logging.init_app(application) logging.init_app(application)
aws_sns_client.init_app(application) aws_sns_client.init_app(application)
aws_ses_client.init_app()
# start lazy initialization for gevent # start lazy initialization for gevent
migrate = Migrate() migrate = Migrate()
migrate.init_app(application, db=db) migrate.init_app(application, db=db)
@@ -169,6 +167,8 @@ def create_app(application):
document_download_client.init_app(application) document_download_client.init_app(application)
aws_cloudwatch_client = AwsCloudwatchClient() aws_cloudwatch_client = AwsCloudwatchClient()
aws_cloudwatch_client.init_app(application) aws_cloudwatch_client.init_app(application)
aws_ses_client = AwsSesClient()
aws_ses_client.init_app()
aws_ses_stub_client = AwsSesStubClient() aws_ses_stub_client = AwsSesStubClient()
aws_ses_stub_client.init_app(stub_url=application.config["SES_STUB_URL"]) aws_ses_stub_client.init_app(stub_url=application.config["SES_STUB_URL"])

View File

@@ -1,19 +1,22 @@
import json import json
from unittest import mock from unittest import mock
from unittest.mock import ANY, Mock from unittest.mock import ANY, MagicMock, Mock
import botocore import botocore
import pytest import pytest
from app import AwsSesStubClient, aws_ses_client
from app.clients.email import EmailClientNonRetryableException from app.clients.email import EmailClientNonRetryableException
from app.clients.email.aws_ses import ( from app.clients.email.aws_ses import (
AwsSesClientException, AwsSesClientException,
AwsSesClientThrottlingSendRateException, AwsSesClientThrottlingSendRateException,
get_aws_responses, get_aws_responses,
) )
from app.clients.email.aws_ses_stub import AwsSesStubClient
from app.enums import NotificationStatus, StatisticsType 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(): def test_should_return_correct_details_for_delivery():
response_dict = get_aws_responses("Delivery") response_dict = get_aws_responses("Delivery")

View File

@@ -1,6 +1,6 @@
import json import json
from collections import namedtuple from collections import namedtuple
from unittest.mock import ANY from unittest.mock import ANY, MagicMock
import pytest import pytest
from flask import current_app 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, template=sample_email_template_with_html,
) )
db_notification.personalisation = {"name": "Jo"} 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) send_to_providers.send_email_to_provider(db_notification)
app.aws_ses_client.send_email.assert_called_once_with( app.aws_ses_client.send_email.assert_called_once_with(
f'"Sample service" <sample.service@{cloud_config.ses_email_domain}>', f'"Sample service" <sample.service@{cloud_config.ses_email_domain}>',
@@ -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, sample_notification, mocker
): ):
sample_service.active = False 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 = mocker.patch("app.delivery.send_to_providers.get_phone_number_from_s3")
mock_s3.return_value = "2028675309" 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( notification = create_notification(
template=sample_email_template, status=NotificationStatus.SENDING 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.send_email_response")
mocker.patch("app.delivery.send_to_providers.update_notification_message_id") 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( def test_send_email_should_use_service_reply_to_email(
sample_service, sample_email_template, mocker 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 = mocker.patch("app.delivery.send_to_providers.redis_store")
mock_redis.get.return_value = "test@example.com".encode("utf-8") 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"), "test@example.com".encode("utf-8"),
json.dumps({}).encode("utf-8"), json.dumps({}).encode("utf-8"),
] ]
mock_ses_client = MagicMock()
mocker.patch("app.aws_ses_client.send_email", return_value="reference") mock_ses_client.send_email.return_value = "reference"
mocker.patch("app.aws_ses_client", mock_ses_client)
db_notification = create_notification( db_notification = create_notification(
template=sample_email_template, 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( def test_send_email_to_provider_should_user_normalised_to(
mocker, client, sample_email_template 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( notification = create_notification(
template=sample_email_template, 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" "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") 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( notification = create_notification(
template=sample_email_template, template=sample_email_template,
) )