"""Test for the email logic.""" from unittest.mock import patch import flask from owsresponse import response from notifications import config from notifications.constants.email import ( PARTICIPANT_DATA_ISSUE_RECIPIENT, PARTICIPANT_DATA_ISSUE_SUBJECT, PARTICIPANT_DATA_ISSUE_TEMPLATE, SOUND_RECORDING_DATA_ISSUE_RECIPIENT, SOUND_RECORDING_DATA_ISSUE_SUBJECT, SOUND_RECORDING_DATA_ISSUE_TEMPLATE, ) from notifications.logic import email app = flask.Flask(config.SERVICE_NAME) identity_id = '03e83149-3c7f-4587-940b-ce83dec34bc8' identity = {'name': 'Jacob Fowler', 'email': 'jfowler@theorchard.com'} participant_payload = { 'application': {'name': 'orchard-go', 'version': '2.0.1'}, 'category': 'DATA_INACCURATE', 'message': 'Has wrong number of followers', 'participant': { 'id': '700cb3c2-fe8c-42eb-b1e0-dd376be6c15c', 'name': 'Phoebe Bridgers', 'chartmetric_id': '122237', }, 'referrer': 'PARTICIPANT_SCREEN', } sound_recording_payload = { 'application': {'name': 'orchard-go', 'version': '2.0.1'}, 'category': 'DATA_INACCURATE', 'message': 'Has wrong number of followers', 'artist_name': 'Phoebe Bridgers', 'sound_recording': {'isrc': 'USJ5G2020003', 'name': 'Kyoto'}, 'referrer': 'PARTICIPANT_SCREEN', } templated_email = 'Phoebe Bridgers has the wrong number of followers' @patch('notifications.connectors.ses.send_email', return_value={}) @patch( 'notifications.models.ows_users.get_identity', return_value=response.Response(status=200, message=identity), ) @patch( 'notifications.models.identity.get_brand_for_identity', return_value=response.Response('orchard'), ) def test_report_participant_data_issue( mock_get_brand_for_identity, mock_get_identity, mock_send_email ): """Test report participant data issue.""" with app.test_request_context('/identity/123/report/participant-data'): result = email.report_participant_data_issue(identity_id, participant_payload) mock_get_identity.assert_called_with(identity_id) mock_get_brand_for_identity.assert_called_with(identity_id) mock_send_email.assert_called_with( subject=PARTICIPANT_DATA_ISSUE_SUBJECT.format( name=participant_payload['participant']['name'] ), recipient=PARTICIPANT_DATA_ISSUE_RECIPIENT['ORCHARD'], text_body=flask.render_template( PARTICIPANT_DATA_ISSUE_TEMPLATE, **{ 'name': identity['name'], 'email': identity['email'], 'identity_id': identity_id, 'referrer': participant_payload['referrer'], 'participant': { 'name': participant_payload['participant']['name'], 'id': participant_payload['participant']['id'], 'chartmetric_id': participant_payload['participant']['chartmetric_id'], }, 'application': { 'name': participant_payload['application']['name'], 'version': participant_payload['application']['version'], }, 'category': participant_payload['category'], 'message': participant_payload['message'], }, ), ) assert result.status == 202 @patch('notifications.connectors.ses.send_email', return_value={}) @patch('flask.render_template', return_value=templated_email) @patch('notifications.models.ows_users.get_identity', return_value=response.Response(status=500)) @patch( 'notifications.models.identity.get_brand_for_identity', return_value=response.Response(status=500), ) def test_report_participant_data_issue_ows_users_fail( mock_get_brand_for_identity, mock_get_identity, mock_render_template, mock_send_email ): """Test ows-users failure on report participant data issue.""" result = email.report_participant_data_issue(identity_id, participant_payload) mock_get_identity.assert_called_with(identity_id) mock_render_template.assert_not_called() mock_send_email.assert_not_called() assert mock_get_brand_for_identity.call_count == 1 assert result.status == 500 @patch('notifications.connectors.ses.send_email', return_value={}) @patch( 'notifications.models.ows_users.get_identity', return_value=response.Response(status=200, message=identity), ) @patch( 'notifications.models.identity.get_brand_for_identity', return_value=response.Response('orchard'), ) def test_report_sound_recording_data_issue( mock_get_brand_for_identity, mock_get_identity, mock_send_email ): """Test report sound recording data issue.""" with app.test_request_context('/identity/123/report/sound-recording-data'): result = email.report_sound_recording_data_issue(identity_id, sound_recording_payload) mock_get_identity.assert_called_with(identity_id) mock_get_brand_for_identity(identity_id) mock_send_email.assert_called_with( subject=SOUND_RECORDING_DATA_ISSUE_SUBJECT.format( name=sound_recording_payload['sound_recording']['name'], isrc=sound_recording_payload['sound_recording']['isrc'], ), recipient=SOUND_RECORDING_DATA_ISSUE_RECIPIENT['ORCHARD'], text_body=flask.render_template( SOUND_RECORDING_DATA_ISSUE_TEMPLATE, **{ 'name': identity['name'], 'email': identity['email'], 'identity_id': identity_id, 'referrer': sound_recording_payload['referrer'], 'sound_recording': {'isrc': 'USJ5G2020003', 'name': 'Kyoto'}, 'artist_name': 'Phoebe Bridgers', 'application': { 'name': sound_recording_payload['application']['name'], 'version': sound_recording_payload['application']['version'], }, 'category': sound_recording_payload['category'], 'message': sound_recording_payload['message'], }, ), ) assert result.status == 202 @patch('notifications.connectors.ses.send_email', return_value={}) @patch('flask.render_template', return_value=templated_email) @patch('notifications.models.ows_users.get_identity', return_value=response.Response(status=500)) @patch( 'notifications.models.identity.get_brand_for_identity', return_value=response.Response(status=500), ) def test_report_sound_recording_data_issue_ows_users_fail( mock_get_brand_for_identity, mock_get_identity, mock_render_template, mock_send_email ): """Test ows-users failure on report participant data issue.""" result = email.report_sound_recording_data_issue(identity_id, sound_recording_payload) mock_get_identity.assert_called_with(identity_id) mock_render_template.assert_not_called() mock_send_email.assert_not_called() assert mock_get_brand_for_identity.call_count == 1 assert result.status == 500