"""Tests for bulk ingestion email logic.""" from unittest.mock import ANY import pytest from owsresponse import response from pytest_mock import MockerFixture from notifications.constants.email import BULK_INGESTION_BCC from notifications.logic import bulk_ingestion_email from notifications.logic.branding import BrandParams @pytest.fixture def mock_identity() -> dict[str, str]: """Mock identity for testing.""" return { 'id': '10436b38-5e11-472d-b6a4-bf1ee2b1b438', 'email': 'bburton.ext@sonymusic-pde.com', 'name': 'Ben', } @pytest.fixture def mock_bulk_session_id() -> str: """Mock bulk session ID.""" return '113773f6-853c-4c83-a24b-b2a5a48fa194' @pytest.fixture def mock_vendor_name() -> str: """Mock vendor name.""" return 'Kitty Wizard Records, LLC' @pytest.fixture def mock_template_value() -> str: """Mock template value for email body.""" return 'Bulk ingestion successful!' @pytest.fixture def mock_brand_params() -> BrandParams: """Mock brand parameters.""" return BrandParams(brand='orchard', domain='qaorch.com') @pytest.fixture def mock_metadata_file_link() -> str: """Mock metadata file link.""" return 'https://super-cool.com/metadata.json' @pytest.fixture def mock_ingestion_report_link() -> str: """Mock ingestion report link.""" return 'https://super-cool.com/report.pdf' def test_bulk_ingest_succeeded( mocker: MockerFixture, mock_identity: dict[str, str], mock_bulk_session_id: str, mock_brand_params: BrandParams, mock_metadata_file_link: str, ) -> None: """Test successful bulk ingestion email.""" mock_get_identity = mocker.patch( 'notifications.logic.bulk_ingestion_email.ows_users.get_identity', return_value=response.Response(mock_identity), ) mock_get_brand_params = mocker.patch( 'notifications.logic.branding.get_brand_params', return_value=mock_brand_params ) mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value) mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) result = bulk_ingestion_email.bulk_ingest_succeeded( { 'bulk_session_id': mock_bulk_session_id, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'total_products': 10, 'submitted_products_count': 5, 'vendor_name': mock_vendor_name, 'completed_on': '2023-10-01T13:00:00Z', 'metadata_file_link': mock_metadata_file_link, 'submit_products': True, 'assets_required': False, } ) mock_get_identity.assert_called_once_with(mock_identity['id']) mock_get_brand_params.assert_called_once_with(mock_identity) mock_render_template.assert_called_once_with( 'emails/bulk_ingestion/ingestion_success.jinja', **{ 'default_brand': mock_brand_params.brand, 'brand_domain': mock_brand_params.domain, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'content_app_url': mock_brand_params.content_app_url, 'workstation_sidecar_url': f'https://workstation.{mock_brand_params.domain}/catalog?showBulkSessions=true', 'bulk_session_id': mock_bulk_session_id, 'metadata_file_link': mock_metadata_file_link, 'total_products': 10, 'submitted_products_count': 5, 'vendor_name': mock_vendor_name, 'completed_on': 'October 1, 2023 at 1:00PM UTC', 'submit_products': True, 'assets_required': False, }, ) mock_send_email.assert_called_once_with( subject=ANY, body=mock_template_value, recipient=mock_identity['email'], bcc=[BULK_INGESTION_BCC], ) assert result.status == 202 assert result.message == mock_template_value def test_bulk_ingest_failure( mocker: MockerFixture, mock_identity: dict[str, str], mock_bulk_session_id: str, mock_vendor_name: str, mock_metadata_file_link: str, mock_ingestion_report_link: str, mock_brand_params: BrandParams, ): """Test failed bulk ingestion email.""" mock_get_identity = mocker.patch( 'notifications.logic.bulk_ingestion_email.ows_users.get_identity', return_value=response.Response(mock_identity), ) mock_get_brand_params = mocker.patch( 'notifications.logic.branding.get_brand_params', return_value=mock_brand_params ) mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value) mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) result = bulk_ingestion_email.bulk_ingest_failed( { 'bulk_session_id': mock_bulk_session_id, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'total_products': 10, 'vendor_name': mock_vendor_name, 'completed_on': '2023-10-01T13:00:00Z', 'metadata_file_link': mock_metadata_file_link, 'ingestion_report_link': mock_ingestion_report_link, 'submissions_failed': False, } ) mock_get_identity.assert_called_once_with(mock_identity['id']) mock_get_brand_params.assert_called_once_with(mock_identity) mock_render_template.assert_called_once_with( 'emails/bulk_ingestion/ingestion_failure.jinja', **{ 'default_brand': mock_brand_params.brand, 'brand_domain': mock_brand_params.domain, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'content_app_url': mock_brand_params.content_app_url, 'workstation_sidecar_url': f'https://workstation.{mock_brand_params.domain}/catalog?showBulkSessions=true', 'bulk_session_id': mock_bulk_session_id, 'metadata_file_link': mock_metadata_file_link, 'ingestion_report_link': mock_ingestion_report_link, 'submissions_failed': False, 'total_products': 10, 'vendor_name': mock_vendor_name, 'completed_on': 'October 1, 2023 at 1:00PM UTC', }, ) mock_send_email.assert_called_once_with( subject=ANY, body=mock_template_value, recipient=mock_identity['email'], bcc=[BULK_INGESTION_BCC], ) assert result.status == 202 assert result.message == mock_template_value def test_bulk_ingest_failed_submissions_failed( mocker: MockerFixture, mock_identity: dict[str, str], mock_bulk_session_id: str, mock_vendor_name: str, mock_metadata_file_link: str, mock_ingestion_report_link: str, mock_brand_params: BrandParams, ) -> None: """Test failed bulk ingestion email with failed submissions.""" mock_get_identity = mocker.patch( 'notifications.logic.bulk_ingestion_email.ows_users.get_identity', return_value=response.Response(mock_identity), ) mock_get_brand_params = mocker.patch( 'notifications.logic.branding.get_brand_params', return_value=mock_brand_params ) mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value) mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) result = bulk_ingestion_email.bulk_ingest_failed( { 'bulk_session_id': mock_bulk_session_id, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'total_products': 10, 'vendor_name': mock_vendor_name, 'completed_on': '2023-10-01T13:00:00Z', 'metadata_file_link': mock_metadata_file_link, 'ingestion_report_link': mock_ingestion_report_link, 'submissions_failed': True, } ) mock_get_identity.assert_called_once_with(mock_identity['id']) mock_get_brand_params.assert_called_once_with(mock_identity) mock_render_template.assert_called_once_with( # This is the key difference in this test. # A submission failure renders the success template. 'emails/bulk_ingestion/ingestion_success.jinja', **{ 'default_brand': mock_brand_params.brand, 'brand_domain': mock_brand_params.domain, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'content_app_url': mock_brand_params.content_app_url, 'workstation_sidecar_url': f'https://workstation.{mock_brand_params.domain}/catalog?showBulkSessions=true', 'bulk_session_id': mock_bulk_session_id, 'metadata_file_link': mock_metadata_file_link, 'ingestion_report_link': mock_ingestion_report_link, 'submissions_failed': True, 'total_products': 10, 'vendor_name': mock_vendor_name, 'completed_on': 'October 1, 2023 at 1:00PM UTC', }, ) mock_send_email.assert_called_once_with( subject=ANY, body=mock_template_value, recipient=mock_identity['email'], bcc=[BULK_INGESTION_BCC], ) assert result.status == 202 assert result.message == mock_template_value def test_bulk_ingest_failure_without_optional_fields( mocker: MockerFixture, mock_identity: dict[str, str], mock_bulk_session_id: str, mock_vendor_name: str, mock_metadata_file_link: str, mock_brand_params: BrandParams, ) -> None: """Test failed bulk ingestion email without optional fields.""" mock_get_identity = mocker.patch( 'notifications.logic.bulk_ingestion_email.ows_users.get_identity', return_value=response.Response(mock_identity), ) mock_get_brand_params = mocker.patch( 'notifications.logic.branding.get_brand_params', return_value=mock_brand_params ) mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value) mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) result = bulk_ingestion_email.bulk_ingest_failed( { 'bulk_session_id': mock_bulk_session_id, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'total_products': 10, 'vendor_name': mock_vendor_name, 'completed_on': '2023-10-01T13:00:00Z', 'metadata_file_link': mock_metadata_file_link, 'ingestion_report_link': None, 'submissions_failed': False, } ) mock_get_identity.assert_called_once_with(mock_identity['id']) mock_get_brand_params.assert_called_once_with(mock_identity) mock_render_template.assert_called_once_with( 'emails/bulk_ingestion/ingestion_failure.jinja', **{ 'default_brand': mock_brand_params.brand, 'brand_domain': mock_brand_params.domain, 'identity_id': mock_identity['id'], 'identity_name': mock_identity['name'], 'content_app_url': mock_brand_params.content_app_url, 'workstation_sidecar_url': f'https://workstation.{mock_brand_params.domain}/catalog?showBulkSessions=true', 'bulk_session_id': mock_bulk_session_id, 'metadata_file_link': mock_metadata_file_link, 'ingestion_report_link': None, 'submissions_failed': False, 'total_products': 10, 'vendor_name': mock_vendor_name, 'completed_on': 'October 1, 2023 at 1:00PM UTC', }, ) mock_send_email.assert_called_once_with( subject=ANY, body=mock_template_value, recipient=mock_identity['email'], bcc=[BULK_INGESTION_BCC], ) assert result.status == 202 assert result.message == mock_template_value