mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Reinstating the 2 task model for API submitted notifications.
This is being done for the PaaS migration to allow us to keep traffic coming in whilst we migrate the database. uses the same tasks as the CSV uploaded notifications. Simple changes to not persist the notification, and call into a different task.
This commit is contained in:
@@ -104,7 +104,9 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
|
||||
def test_send_notification_with_placeholders_replaced(notify_api, sample_email_template_with_placeholders, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocked = mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
mocker.patch('app.encryption.decrypt', return_value={"key": "something_encrypted"})
|
||||
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
@@ -125,8 +127,13 @@ def test_send_notification_with_placeholders_replaced(notify_api, sample_email_t
|
||||
data.update({"template_version": sample_email_template_with_placeholders.version})
|
||||
|
||||
mocked.assert_called_once_with(
|
||||
[notification_id],
|
||||
queue="send-email"
|
||||
(
|
||||
str(sample_email_template_with_placeholders.service.id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
'2016-01-01T11:09:00.061258Z'
|
||||
),
|
||||
queue="db-email"
|
||||
)
|
||||
assert response.status_code == 201
|
||||
assert response_data['body'] == u'Hello Jo\nThis is an email from GOV.\u200BUK'
|
||||
@@ -199,7 +206,7 @@ def test_should_send_notification_if_restricted_and_a_service_user(notify_api,
|
||||
mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
||||
mocked = mocker.patch('app.celery.tasks.send_{}.apply_async'.format(template_type))
|
||||
|
||||
template = sample_template if template_type == 'sms' else sample_email_template
|
||||
to = template.service.created_by.mobile_number if template_type == 'sms' \
|
||||
@@ -260,7 +267,7 @@ def test_should_not_allow_template_from_another_service(notify_api,
|
||||
def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocked = mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
@@ -278,7 +285,12 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
|
||||
response_data = json.loads(response.data)['data']
|
||||
notification_id = response_data['notification']['id']
|
||||
|
||||
mocked.assert_called_once_with([notification_id], queue='send-sms')
|
||||
mocked.assert_called_once_with(
|
||||
(str(sample_template.service.id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
'2016-01-01T11:09:00.061258Z'),
|
||||
queue='db-sms')
|
||||
assert response.status_code == 201
|
||||
assert notification_id
|
||||
assert 'subject' not in response_data
|
||||
@@ -309,11 +321,11 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai
|
||||
assert data['message']['to'][0] == 'Not a valid email address'
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
@freeze_time("2016-01-01 11:00:00.000000")
|
||||
def test_should_allow_valid_email_notification(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
@@ -330,9 +342,15 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
|
||||
assert response.status_code == 201
|
||||
response_data = json.loads(response.get_data(as_text=True))['data']
|
||||
notification_id = response_data['notification']['id']
|
||||
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
|
||||
[notification_id],
|
||||
queue="send-email"
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
(
|
||||
str(sample_email_template.service.id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
'2016-01-01T11:00:00.000000Z'
|
||||
|
||||
),
|
||||
queue="db-email"
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
@@ -418,7 +436,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
|
||||
restricted):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=restricted)
|
||||
@@ -444,7 +462,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
|
||||
def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
email_template = create_sample_email_template(notify_db, notify_db_session, content='hello\nthere')
|
||||
|
||||
data = {
|
||||
@@ -464,7 +482,7 @@ def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session
|
||||
|
||||
def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
data = {
|
||||
'to': "not-someone-we-trust@email-address.com",
|
||||
'template': str(sample_email_template.id),
|
||||
@@ -479,17 +497,15 @@ def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
|
||||
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert [
|
||||
'Can’t send to this recipient using a team-only API key'
|
||||
] == json_resp['message']['to']
|
||||
assert ['Can’t send to this recipient using a team-only API key'] == json_resp['message']['to']
|
||||
|
||||
|
||||
def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
|
||||
data = {
|
||||
'to': '07123123123',
|
||||
@@ -504,18 +520,18 @@ def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert [
|
||||
'Can’t send to this recipient using a team-only API key'
|
||||
] == json_resp['message']['to']
|
||||
assert ['Can’t send to this recipient using a team-only API key'] == json_resp['message']['to']
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:00:00.000000")
|
||||
def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample_email_template, fake_uuid, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': sample_email_template.service.created_by.email_address,
|
||||
@@ -532,18 +548,27 @@ def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
notification_id = json.loads(response.data)['data']['notification']['id']
|
||||
|
||||
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with([fake_uuid], queue='send-email')
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service.id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
"2016-01-01T11:00:00.000000Z"),
|
||||
queue='db-email'
|
||||
)
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.parametrize('restricted', [True, False])
|
||||
@pytest.mark.parametrize('limit', [0, 1])
|
||||
@freeze_time("2016-01-01 11:00:00.000000")
|
||||
def test_should_send_sms_to_anyone_with_test_key(
|
||||
notify_api, sample_template, mocker, restricted, limit, fake_uuid
|
||||
notify_api, sample_template, mocker, restricted, limit, fake_uuid
|
||||
):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
@@ -566,18 +591,23 @@ def test_should_send_sms_to_anyone_with_test_key(
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]
|
||||
)
|
||||
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with([fake_uuid], queue='research-mode')
|
||||
notification_id = json.loads(response.data)['data']['notification']['id']
|
||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||
(str(sample_template.service.id), notification_id, "something_encrypted", "2016-01-01T11:00:00.000000Z"),
|
||||
queue='research-mode'
|
||||
)
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.parametrize('restricted', [True, False])
|
||||
@pytest.mark.parametrize('limit', [0, 1])
|
||||
@freeze_time("2016-01-01 11:00:00.000000")
|
||||
def test_should_send_email_to_anyone_with_test_key(
|
||||
notify_api, sample_email_template, mocker, restricted, limit, fake_uuid
|
||||
notify_api, sample_email_template, mocker, restricted, limit, fake_uuid
|
||||
):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': 'anyone123@example.com',
|
||||
@@ -599,15 +629,23 @@ def test_should_send_email_to_anyone_with_test_key(
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]
|
||||
)
|
||||
|
||||
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with([fake_uuid], queue='research-mode')
|
||||
notification_id = json.loads(response.data)['data']['notification']['id']
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service.id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
"2016-01-01T11:00:00.000000Z"),
|
||||
queue='research-mode'
|
||||
)
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:00:00.000000")
|
||||
def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_template, fake_uuid, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': sample_template.service.created_by.mobile_number,
|
||||
@@ -625,18 +663,28 @@ def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_t
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with([fake_uuid], queue='send-sms')
|
||||
notification_id = json.loads(response.data)['data']['notification']['id']
|
||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||
(str(sample_template.service.id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
"2016-01-01T11:00:00.000000Z"),
|
||||
queue='db-sms'
|
||||
)
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_type',
|
||||
['sms', 'email'])
|
||||
@freeze_time("2016-01-01 11:00:00.000000")
|
||||
def test_should_persist_notification(notify_api, sample_template,
|
||||
sample_email_template,
|
||||
template_type,
|
||||
fake_uuid, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
mocked = mocker.patch('app.celery.tasks.send_{}.apply_async'.format(template_type))
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
template = sample_template if template_type == 'sms' else sample_email_template
|
||||
to = sample_template.service.created_by.mobile_number if template_type == 'sms' \
|
||||
@@ -658,17 +706,19 @@ def test_should_persist_notification(notify_api, sample_template,
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
mocked.assert_called_once_with([fake_uuid], queue='send-{}'.format(template_type))
|
||||
notification_id = json.loads(response.data)['data']['notification']['id']
|
||||
|
||||
mocked.assert_called_once_with(
|
||||
(str(template.service.id), notification_id, "something_encrypted", '2016-01-01T11:00:00.000000Z'),
|
||||
queue='db-{}'.format(template_type))
|
||||
assert response.status_code == 201
|
||||
|
||||
notification = notifications_dao.get_notification_by_id(fake_uuid)
|
||||
assert notification.to == to
|
||||
assert notification.template_id == template.id
|
||||
assert notification.notification_type == template_type
|
||||
assert not notifications_dao.get_notification_by_id(fake_uuid)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_type',
|
||||
['sms', 'email'])
|
||||
@pytest.mark.skip(reason="This is not required for the duration of PaaS migration")
|
||||
def test_should_delete_notification_and_return_error_if_sqs_fails(
|
||||
client,
|
||||
sample_email_template,
|
||||
@@ -770,8 +820,7 @@ def test_should_not_persist_notification_or_send_sms_if_simulated_number(
|
||||
])
|
||||
@pytest.mark.parametrize('notification_type, to, _create_sample_template', [
|
||||
('sms', '07827992635', create_sample_template),
|
||||
('email', 'non_whitelist_recipient@mail.com', create_sample_email_template)]
|
||||
)
|
||||
('email', 'non_whitelist_recipient@mail.com', create_sample_email_template)])
|
||||
def test_should_not_send_notification_to_non_whitelist_recipient_in_trial_mode(
|
||||
client,
|
||||
notify_db,
|
||||
@@ -825,21 +874,20 @@ def test_should_not_send_notification_to_non_whitelist_recipient_in_trial_mode(
|
||||
])
|
||||
@pytest.mark.parametrize('notification_type, to, _create_sample_template', [
|
||||
('sms', '07123123123', create_sample_template),
|
||||
('email', 'whitelist_recipient@mail.com', create_sample_email_template)]
|
||||
)
|
||||
('email', 'whitelist_recipient@mail.com', create_sample_email_template)])
|
||||
def test_should_send_notification_to_whitelist_recipient(
|
||||
client,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notification_type,
|
||||
to,
|
||||
_create_sample_template,
|
||||
key_type,
|
||||
service_restricted,
|
||||
mocker
|
||||
client,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notification_type,
|
||||
to,
|
||||
_create_sample_template,
|
||||
key_type,
|
||||
service_restricted,
|
||||
mocker
|
||||
):
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=service_restricted)
|
||||
apply_async = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
|
||||
apply_async = mocker.patch('app.celery.tasks.send_{}.apply_async'.format(notification_type))
|
||||
template = _create_sample_template(notify_db, notify_db_session, service=service)
|
||||
if notification_type == 'sms':
|
||||
service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session,
|
||||
@@ -959,6 +1007,7 @@ def test_create_template_raises_invalid_request_when_content_too_large(
|
||||
@pytest.mark.parametrize("notification_type, send_to",
|
||||
[("sms", "07700 900 855"),
|
||||
("email", "sample@email.com")])
|
||||
@freeze_time("2016-01-01 11:00:00.000000")
|
||||
def test_send_notification_uses_priority_queue_when_template_is_marked_as_priority(client, notify_db,
|
||||
notify_db_session, mocker,
|
||||
notification_type, send_to):
|
||||
@@ -968,7 +1017,8 @@ def test_send_notification_uses_priority_queue_when_template_is_marked_as_priori
|
||||
template_type=notification_type,
|
||||
process_type='priority'
|
||||
)
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
mocked = mocker.patch('app.celery.tasks.send_{}.apply_async'.format(notification_type))
|
||||
|
||||
data = {
|
||||
'to': send_to,
|
||||
@@ -986,4 +1036,6 @@ def test_send_notification_uses_priority_queue_when_template_is_marked_as_priori
|
||||
notification_id = response_data['notification']['id']
|
||||
|
||||
assert response.status_code == 201
|
||||
mocked.assert_called_once_with([notification_id], queue='notify')
|
||||
mocked.assert_called_once_with(
|
||||
(str(sample.service_id), notification_id, "something_encrypted", '2016-01-01T11:00:00.000000Z'), queue='notify'
|
||||
)
|
||||
|
||||
@@ -77,6 +77,34 @@ def test_persist_notification_creates_and_save_to_db(sample_template, sample_api
|
||||
mocked_redis.assert_called_once_with(str(sample_template.service_id) + "-2016-01-01-count")
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_persist_notification_does_not_create_and_save_to_db_if_persist_is_false(
|
||||
sample_template, sample_api_key, sample_job, mocker
|
||||
):
|
||||
mocked_redis = mocker.patch('app.notifications.process_notifications.redis_store.get')
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
persist_notification(
|
||||
sample_template.id,
|
||||
sample_template.version,
|
||||
'+447111111111',
|
||||
sample_template.service,
|
||||
{},
|
||||
'sms',
|
||||
sample_api_key.id,
|
||||
sample_api_key.key_type,
|
||||
job_id=sample_job.id,
|
||||
job_row_number=100,
|
||||
reference="ref",
|
||||
persist=False)
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
|
||||
mocked_redis.assert_called_once_with(str(sample_template.service_id) + "-2016-01-01-count")
|
||||
|
||||
|
||||
def test_persist_notification_throws_exception_when_missing_template(sample_api_key):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
|
||||
Reference in New Issue
Block a user