"""Test for Email logic tier.""" from unittest.mock import patch import pytest from podcast.logic import email from podcast.models import ad_action from podcast.models import episode as episode_model from podcast.models import podcast as podcast_model from podcast.models import transcription as transcription_model from podcast.utils.exc import OwsError from tests.utils import db_operations def test_send_podcast_spike_email(mock_current_admin_user): """Test send episode scheduled live email.""" email.send_podcast_spike_email( db_operations.user_data[1], {'episode_spikes': ['data']}) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"Sony Music Podcasts" ', 'subject': '[Spike Detected] 1 trending episodes on your show', 'template': 'podcast_episode_spike', 'to': ['user_admin@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 2, 'variables': { 'episode_spikes': ['data'], 'organization': 'sme', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email', } }) def test_send_transcription_finished(mock_current_admin_user): """Test send episode scheduled live email.""" transcription = transcription_model.get(1) email.send_transcription_finished(transcription) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[{podcast_name} - Update] Audio Transcription for an episode is complete', 'subject_variables': {'podcast_name': 'title'}, 'template': 'transcription_finished', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'transcription_url': 'http://localhost:8080/podcast/1/episode/1/edit/transcript?via=email', 'name': 'username', 'organization': 'orchard', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email', 'podcast_name': 'title', 'episode_name': 'title' } }) def test_send_episode_scheduled(mock_current_admin_user): """Test send episode scheduled live email only if favorited users are active.""" episodes = episode_model.get_episodes_by_ids([1])['items'] email.send_episode_scheduled(episodes[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[{podcast_name} - Update] An Episode is scheduled to go live', 'subject_variables': {'podcast_name': 'title'}, 'template': 'favorite_podcast_episode_scheduled', 'to': ['auth0-network-admin-user@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 6, 'variables': { 'podcast_name': 'title', 'episode_name': 'title', 'edit_episode_url': 'http://localhost:8080/podcast/1/episode/1/edit?via=email', 'name': 'network-admin-user', 'published_date': '06/01/2019 8:20AM UTC', 'organization': 'orchard', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) def test_notifications_disabled(mock_current_admin_user): """Test notifications not sent with users notifications are disabled.""" episodes = episode_model.get_episodes_by_ids([3])['items'] email.send_episode_scheduled(episodes[0]) email._send_sqs.assert_not_called() def test_send_episode_live(mock_current_admin_user): """Test send episode live email.""" episodes = episode_model.get_episodes_by_ids([1])['items'] podcast = podcast_model.get_podcast_by_id(episodes[0]['podcast_id']) email.send_episode_live( episodes[0], podcast, { 'id': 2, 'name': 'Cow', 'email': 'a@b.com', 'organization': 'sme', 'language': 'es' } ) email._send_sqs.assert_called_once_with({ 'lang': 'es', 'sender': '"Sony Music Podcasts" ', 'subject': '[{podcast_name} - Update] An Episode is published', 'subject_variables': {'podcast_name': 'title'}, 'template': 'favorite_podcast_episode_live', 'to': ['a@b.com'], 'type': 'GeneralNotification', 'user_id': 2, 'variables': { 'podcast_name': 'title', 'episode_name': 'title', 'name': 'Cow', 'published_date': '06/01/2019 8:20AM UTC', 'organization': 'sme', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) def test_send_episode_live_tomorrow(mock_current_admin_user): """Test send episode live tomorrow email.""" episodes = episode_model.get_episodes_by_ids([1])['items'] podcast = podcast_model.get_podcast_by_id(episodes[0]['podcast_id']) email.send_episode_live_tomorrow( episodes[0], podcast, { 'id': 2, 'name': 'Cow', 'email': 'a@b.com', 'organization': 'sme', 'language': 'es' } ) email._send_sqs.assert_called_once_with({ 'lang': 'es', 'sender': '"Sony Music Podcasts" ', 'subject': '[{podcast_name} - Update] An Episode is scheduled to go live tomorrow', 'subject_variables': {'podcast_name': 'title'}, 'template': 'favorite_podcast_episode_live_tomorrow', 'to': ['a@b.com'], 'type': 'GeneralNotification', 'user_id': 2, 'variables': { 'podcast_name': 'title', 'episode_name': 'title', 'name': 'Cow', 'published_date': '8:20AM UTC', 'organization': 'sme', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email', 'edit_episode_url': 'http://localhost:8080/podcast/1/episode/1/edit?via=email', } }) def test_send_ad_due_tomorrow(mock_current_admin_user): """Test send ad action due tomorrow.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) action = ad_action.get_ad_action_no_assets(1) email.send_ad_due_tomorrow(action) email._send_sqs.call_count == 2 email._send_sqs.assert_called_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] A request is due in 24 hours', 'template': 'host_read_ad_due_tomorrow', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'campaign_name': 'Random Campaign title', 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'name': 'username', 'order_name': 'order 1', 'organization': 'orchard', 'reporter_name': 'username', 'roll_type': 'mid', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) def test_send_ad_due_tomorrow_no_campaign(mock_current_admin_user): """Test send ad action due tomorrow fails if campaign doesn't exist.""" action = ad_action.get_ad_action_no_assets(8) with pytest.raises(OwsError) as err: email.send_ad_due_tomorrow(action) assert err.value.status == 404 def test_send_ad_due_tomorrow_no_order(mock_current_admin_user): """Test send ad action due tomorrow fails if order doesn't exist.""" action = ad_action.get_ad_action_no_assets(9) with pytest.raises(OwsError) as err: email.send_ad_due_tomorrow(action) assert err.value.status == 404 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_ad_past_due(mock_get_feature_flag, mock_current_admin_user): """Test send ad action past due.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] email.send_ad_past_due(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': "[Host-Read Ad] A request is past it's due date", 'template': 'host_read_ad_past_due', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'name': 'username', 'organization': 'orchard', 'reporter_name': 'username', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_ad_action_assigned_to_me(mock_get_feature_flag, mock_current_admin_user): """Test send ad action assigned to me email.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] email.send_ad_action_assigned_to_me(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] A request has been assigned to you', 'template': 'host_read_ad_assigned', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'due_date': '01/01/2019', 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'name': 'username', 'organization': 'orchard', 'reporter_name': 'username', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_not_send_ad_action_assigned_to_me(mock_get_feature_flag, mock_current_admin_user): """Test when assigned user is deleted, email not sent for ad action assigned to me.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] email.send_ad_action_assigned_to_me(actions[1]) email._send_sqs.assert_not_called() @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_not_send_ad_action_submitted(mock_get_feature_flag, mock_current_admin_user): """Test when reporter user is deleted, email not sent for ad action submitted.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] email.send_ad_action_submitted(actions[2]) email._send_sqs.assert_not_called() @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_ad_action_assigned_to_me_for_deleted_reporter(mock_get_feature_flag, mock_current_admin_user): """Test when assigned user is present and reporter user is deleted, email sent for ad action assigned to me.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] email.send_ad_action_assigned_to_me(actions[2]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] A request has been assigned to you', 'template': 'host_read_ad_assigned', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'due_date': '01/01/2019', 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'name': 'username', 'organization': 'orchard', 'reporter_name': 'Deleted User', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_ad_action_submitted(mock_get_feature_flag, mock_current_admin_user): """Test send ad action submitted email.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] # approval required email.send_ad_action_submitted(actions[0]) email._send_sqs.assert_called_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] Audio has been uploaded - please approve', 'template': 'host_read_ad_submitted', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'assignee_name': 'podcast-admin', 'organization': 'orchard', 'reporter_name': 'username', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email', 'ad_read_title': actions[0]['title'], 'requires_approval': actions[0]['requires_approval'], 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', } }) # approval not required email.send_ad_action_submitted(actions[1]) email._send_sqs.assert_called_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] Audio has been uploaded', 'template': 'host_read_ad_submitted', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'assignee_name': 'useradmin', 'organization': 'orchard', 'reporter_name': 'username', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email', 'ad_read_title': actions[1]['title'], 'requires_approval': actions[1]['requires_approval'], 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_ad_action_approved(mock_get_feature_flag, mock_current_admin_user): """Test send ad action approved email.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] email.send_ad_action_approved(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] Your upload was approved', 'template': 'host_read_ad_approved', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'name': 'username', 'organization': 'orchard', 'reporter_name': 'username', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email', 'ad_read_title': actions[0]['title'] } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_ad_action_rejected(mock_get_feature_flag, mock_current_admin_user): """Test send ad action rejected email.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] action = actions[0] action['rejection_reason'] = 'Cow' email.send_ad_action_rejected(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] Your upload was rejected', 'template': 'host_read_ad_rejected', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'name': 'username', 'organization': 'orchard', 'reporter_name': 'username', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email', 'ad_read_title': action['title'], 'ad_read_rejection': action['rejection_reason'], 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'due_date': '01/01/2019' } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_ad_action_more_than_one_assignee(mock_get_feature_flag, mock_current_admin_user): """Test send ad action emails with more than one assignee.""" episode_model.update_episode(1, {'published_date': '2050-11-26T16:52:01.000Z'}) actions = ad_action.get_ad_actions()['items'] action = actions[0] action['assignee_ids'] = [1, 2] email.send_ad_action_rejected(actions[0]) assert email._send_sqs.call_count == 2 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_ad_action_assigned_to_me_deleted(mock_get_feature_flag, mock_current_admin_user): """Test send email when ad action assigned to user is deleted.""" actions = ad_action.get_ad_actions()['items'] email.send_ad_action_assigned_to_me_deleted(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] Your ad request has been deleted', 'template': 'host_read_ad_deleted', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'name': 'username', 'organization': 'orchard', 'reporter_name': 'username', 'deleted_by': 'podcast-admin', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) def test_send_new_ad_action_comment(mock_current_admin_user): """Test send email when comment has been added on ad action.""" email.send_new_ad_action_comment(1, db_operations.ad_action_comments[0], 2) email._send_sqs.assert_called_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] New comment on ad request: Cool Ad', 'template': 'host_read_ad_new_comment', 'to': ['auth0-network-admin-user@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 6, 'variables': { 'name': 'network-admin-user', 'organization': 'orchard', 'host_read_ad_name': 'Cool Ad', 'host_read_ad_commentor': 'username', 'host_read_ad_comment': 'first test comment', 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_not_send_ad_action_assigned_to_me_deleted(mock_get_feature_flag, mock_current_admin_user): """Test when assigned user is deleted, email not sent for ad action assigned to user is deleted.""" actions = ad_action.get_ad_actions()['items'] email.send_ad_action_assigned_to_me_deleted(actions[1]) email._send_sqs.assert_not_called() @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_assignees_updated_on_ad_action_assigned_to_me(mock_get_feature_flag, mock_current_admin_user): """Test send email when assignees updated on ad action assigned to user.""" actions = ad_action.get_ad_actions()['items'] email.send_assignees_updated_on_ad_action_assigned_to_me(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] Your requests assignees have been updated', 'template': 'host_read_ad_assignees_updated', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'name': 'username', 'assignees_names': ['username'], 'organization': 'orchard', 'reporter_name': 'username', 'due_date': '01/01/2019', 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_due_date_updated_on_ad_action_assigned_to_me(mock_get_feature_flag, mock_current_admin_user): """Test send email when assignees updated on ad action assigned to user.""" actions = ad_action.get_ad_actions()['items'] email.send_due_date_updated_on_ad_action_assigned_to_me(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] Your requests due date has been updated', 'template': 'host_read_ad_due_date_updated', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'name': 'username', 'organization': 'orchard', 'reporter_name': 'username', 'due_date': '01/01/2019', 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } }) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_send_script_updated_on_ad_action_assigned_to_me(mock_get_feature_flag, mock_current_admin_user): """Test send email when assignees updated on ad action assigned to user.""" actions = ad_action.get_ad_actions()['items'] email.send_script_updated_on_ad_action_assigned_to_me(actions[0]) email._send_sqs.assert_called_once_with({ 'lang': 'en', 'sender': '"The Orchard Podcasts" ', 'subject': '[Host-Read Ad] A new script has been uploaded', 'template': 'host_read_ad_script_updated', 'to': ['user_email@theorchard.com'], 'type': 'GeneralNotification', 'user_id': 1, 'variables': { 'name': 'username', 'organization': 'orchard', 'reporter_name': 'username', 'due_date': '01/01/2019', 'host_read_ads_url': 'http://localhost:8080/host-read-ads?via=email', 'unsubscribe_url': 'http://localhost:8080/user-profile?via=email' } })