"""Tests for audience_email.""" import uuid from unittest.mock import ANY import flask import pytest from owsresponse import response from notifications import config from notifications.constants.email import AUDIENCE_AUDIENCE_FILE_EXPORTED_TEMPLATE from notifications.logic import audience_email app = flask.Flask(config.SERVICE_NAME) @pytest.fixture def mock_identity(): """Mock identity.""" return { 'id': str(uuid.uuid4()), 'active': True, 'default_brand': 'theorchard', 'email': 'mockuser@example.com', 'localization': 'en', 'name': 'Mock User', } def test_audience_shopify_store_sync_completed(mocker, mock_identity): """Test audience_shopify_store_sync_completed.""" mock_render_template = mocker.patch('flask.render_template', return_value='Mock') mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) mock_get_identity = mocker.patch( 'notifications.models.ows_users.get_identity', return_value=response.Response(mock_identity), ) identity_id = mock_identity['id'] store_id = str(uuid.uuid4()) store_domain = 'mock-store.myshopify.com' result = audience_email.shopify_store_sync_completed( identity_id=identity_id, store_id=store_id, store_domain=store_domain, is_multiartist_store=True, ) assert result.status == 204 mock_get_identity.assert_called_once_with(identity_id) mock_render_template.assert_called_once() mock_send_email.assert_called_once_with( subject='Your Shopify store has been synced', body=ANY, recipient=mock_identity['email'], ) def test_audience_shopify_store_sync_completed_identity_not_found(mocker, mock_identity): """Test audience_shopify_store_sync_completed when identity_not_found.""" mock_get_identity = mocker.patch( 'notifications.models.ows_users.get_identity', return_value=response.create_not_found_response('Identity not found'), ) identity_id = str(uuid.uuid4()) store_id = str(uuid.uuid4()) store_domain = 'mock-store.myshopify.com' result = audience_email.shopify_store_sync_completed( identity_id=identity_id, store_id=store_id, store_domain=store_domain, is_multiartist_store=True, ) mock_get_identity.assert_called_once_with(identity_id) assert result.status == 404 def test_audience_file_exported(mocker, mock_identity): """Test audience_file_exported.""" mock_render_template = mocker.patch('flask.render_template', return_value='Mock') mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) mock_get_identity = mocker.patch( 'notifications.models.ows_users.get_identity', return_value=response.Response(mock_identity), ) identity_id = mock_identity['id'] audience_name = 'test audience' filename = 'zip/test.zip' password = 'secret' result = audience_email.audience_file_exported( identity_id=identity_id, audience_name=audience_name, filename=filename, password=password, ) assert result.status == 204 mock_get_identity.assert_called_once_with(identity_id) mock_render_template.assert_called_once_with( AUDIENCE_AUDIENCE_FILE_EXPORTED_TEMPLATE, **{ 'full_name': mock_identity['name'], 'default_brand': 'orchard', 'brand_domain': 'qaorch.com', 'preview_text': 'Please find the password for your custom audience below', 'audience_name': audience_name, 'filename': filename, 'password': password, 'pref_base_url': 'https://settings.qaorch.com', }, ) mock_send_email.assert_called_once_with( subject='Your Audience file has been exported', body=ANY, recipient=mock_identity['email'], ) def test_audience_file_exported_identity_not_found(mocker, mock_identity): """Test audience_file_exported when identity_not_found.""" mock_get_identity = mocker.patch( 'notifications.models.ows_users.get_identity', return_value=response.create_not_found_response('Identity not found'), ) identity_id = str(uuid.uuid4()) audience_name = 'test audience' filename = 'zip/test.zip' password = 'secret' result = audience_email.audience_file_exported( identity_id=identity_id, audience_name=audience_name, filename=filename, password=password, ) mock_get_identity.assert_called_once_with(identity_id) assert result.status == 404 def test_ad_reporting_data_sync_completed_meta(mocker, mock_identity): """Test ad_reporting_data_sync_completed.""" mock_render_template = mocker.patch('flask.render_template', return_value='Mock') mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) mock_get_identity = mocker.patch( 'notifications.models.ows_users.get_identity', return_value=response.Response(mock_identity), ) identity_id = mock_identity['id'] ad_reporting_platform = 'META' ad_accounts = ['ad account1', 'ad account2'] result = audience_email.ad_reporting_data_sync_completed( identity_id=identity_id, ad_reporting_platform=ad_reporting_platform, ad_accounts=ad_accounts, ) assert result.status == 204 mock_get_identity.assert_called_once_with(identity_id) mock_render_template.assert_called_once() mock_send_email.assert_called_once_with( subject='Your Meta reporting data has been synced', body=ANY, recipient=mock_identity['email'], ) def test_ad_reporting_data_sync_completed_google(mocker, mock_identity): """Test ad_reporting_data_sync_completed.""" mock_render_template = mocker.patch('flask.render_template', return_value='Mock') mock_send_email = mocker.patch('notifications.connectors.ses.send_html_email', return_value={}) mock_get_identity = mocker.patch( 'notifications.models.ows_users.get_identity', return_value=response.Response(mock_identity), ) identity_id = mock_identity['id'] ad_reporting_platform = 'GOOGLE' ad_accounts = ['ad account1', 'ad account2'] result = audience_email.ad_reporting_data_sync_completed( identity_id=identity_id, ad_reporting_platform=ad_reporting_platform, ad_accounts=ad_accounts, ) assert result.status == 204 mock_get_identity.assert_called_once_with(identity_id) mock_render_template.assert_called_once() mock_send_email.assert_called_once_with( subject='Your Google reporting data has been synced', body=ANY, recipient=mock_identity['email'], ) def test_ad_reporting_data_sync_completed_identity_not_found(mocker, mock_identity): """Test ad_reporting_data_sync_completed when identity_not_found.""" mock_get_identity = mocker.patch( 'notifications.models.ows_users.get_identity', return_value=response.create_not_found_response('Identity not found'), ) identity_id = str(uuid.uuid4()) ad_reporting_platform = 'TIKTOK' ad_accounts = ['ad account1', 'ad account2'] result = audience_email.ad_reporting_data_sync_completed( identity_id=identity_id, ad_reporting_platform=ad_reporting_platform, ad_accounts=ad_accounts, ) mock_get_identity.assert_called_once_with(identity_id) assert result.status == 404