"""Tests for distribution_email."""
from unittest.mock import ANY
import pytest
from notifications.constants.email import DISTRIBUTION_CC
from notifications.logic import distribution_email
@pytest.fixture
def mock_payload():
"""Mock payload."""
return {
'product': {'id': 2958087, 'name': 'Vanishing Point', 'upc': '195497264421'},
'recipients': ['bburton@theorchard.com', 'lseal@theorchard.com'],
'delivery_stores': [{'id': 286, 'name': 'Spotify'}, {'id': 1, 'name': 'iTunes/Apple'}],
'processed_at': '2023-05-09T00:00Z',
'update': {'sale_start_date': '2023-05-20'},
}
@pytest.fixture
def mock_failure_payload():
"""Mock failure payload."""
return {
'product': {'id': 2958087, 'name': 'Vanishing Point', 'upc': '195497264421'},
'recipients': ['bburton@theorchard.com', 'lseal@theorchard.com'],
'delivery_stores': [{'id': 286, 'name': 'Spotify'}, {'id': 1, 'name': 'iTunes/Apple'}],
'update': {'sale_start_date': '2023-05-20'},
}
@pytest.fixture
def mock_warning_payload():
"""Mock warning payload."""
return {
'product': {'id': 2958087, 'name': 'Vanishing Point', 'upc': '195497264421'},
'recipients': ['bburton@theorchard.com', 'lseal@theorchard.com'],
'process_at': '2023-05-20T12:00:01Z',
}
@pytest.fixture
def mock_ws_scheduled_update_failure_payload():
"""Mock ws scheduled update failure payload."""
return {
'product': {'id': 2958087, 'name': 'Vanishing Point', 'upc': '195497264421'},
'recipients': ['bburton@theorchard.com', 'lseal@theorchard.com'],
}
@pytest.fixture
def mock_ws_scheduled_update_processed_payload():
"""Mock ws scheduled update processed payload."""
return {
'product': {'id': 2958087, 'name': 'Vanishing Point', 'upc': '195497264421'},
'recipients': ['bburton@theorchard.com', 'lseal@theorchard.com'],
}
def test_scheduled_update_processed(mocker, mock_payload):
"""Test email notifying that a scheduled metadata update completed."""
mock_template_value = 'Super cool email'
mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value)
mock_send_email = mocker.patch(
'notifications.connectors.ses.send_html_email_bulk', return_value={}
)
result = distribution_email.scheduled_update_processed(mock_payload)
assert result.status == 202
assert result.message == mock_template_value
mock_render_template.assert_called_once()
mock_send_email.assert_called_once_with(
subject=ANY,
body=mock_template_value,
recipients=mock_payload['recipients'],
cc=[DISTRIBUTION_CC],
)
def test_scheduled_update_failed(mocker, mock_failure_payload):
"""Test email notifying that a scheduled metadata update failed."""
mock_template_value = 'Failure!'
mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value)
mock_send_email = mocker.patch(
'notifications.connectors.ses.send_html_email_bulk', return_value={}
)
result = distribution_email.scheduled_update_failed(mock_failure_payload)
assert result.status == 202
assert result.message == mock_template_value
mock_render_template.assert_called_once()
mock_send_email.assert_called_once_with(
subject=ANY,
body=mock_template_value,
recipients=mock_failure_payload['recipients'],
cc=[DISTRIBUTION_CC],
)
def test_scheduled_update_warning(mocker, mock_warning_payload):
"""Test email notifying that a scheduled metadata update failed."""
mock_template_value = 'Warning!'
mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value)
mock_send_email = mocker.patch(
'notifications.connectors.ses.send_html_email_bulk', return_value={}
)
result = distribution_email.scheduled_update_failed(mock_warning_payload)
assert result.status == 202
assert result.message == mock_template_value
mock_render_template.assert_called_once()
mock_send_email.assert_called_once_with(
subject=ANY,
body=mock_template_value,
recipients=mock_warning_payload['recipients'],
cc=[DISTRIBUTION_CC],
)
def test_ws_scheduled_update_failed(mocker, mock_ws_scheduled_update_failure_payload):
"""Test email notifying that a ws scheduled metadata update failed."""
mock_template_value = 'Failure!'
mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value)
mock_send_email = mocker.patch(
'notifications.connectors.ses.send_html_email_bulk', return_value={}
)
result = distribution_email.ws_scheduled_update_failed(mock_ws_scheduled_update_failure_payload)
assert result.status == 202
assert result.message == mock_template_value
mock_render_template.assert_called_once()
mock_send_email.assert_called_once_with(
subject=ANY,
body=mock_template_value,
recipients=mock_ws_scheduled_update_failure_payload['recipients'],
cc=[DISTRIBUTION_CC],
)
def test_ws_scheduled_update_processed(mocker, mock_ws_scheduled_update_processed_payload):
"""Test email notifying that a ws scheduled metadata update processed."""
mock_template_value = 'Failure!'
mock_render_template = mocker.patch('flask.render_template', return_value=mock_template_value)
mock_send_email = mocker.patch(
'notifications.connectors.ses.send_html_email_bulk', return_value={}
)
result = distribution_email.ws_scheduled_update_processed(
mock_ws_scheduled_update_processed_payload
)
assert result.status == 202
assert result.message == mock_template_value
mock_render_template.assert_called_once()
mock_send_email.assert_called_once_with(
subject=ANY,
body=mock_template_value,
recipients=mock_ws_scheduled_update_processed_payload['recipients'],
cc=[DISTRIBUTION_CC],
)