mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
- If the task runs twice and the notification already exists ignore the primary key constraint.
- Remove prints - Add some more tests - Only allow the new method to run for emails
This commit is contained in:
@@ -1698,3 +1698,41 @@ def test_save_api_email(sample_email_template, mocker):
|
||||
assert str(notifications[0].id) == data['id']
|
||||
assert notifications[0].created_at == datetime(2020, 3, 25, 14, 30)
|
||||
mock_send_email_to_provider.assert_called_once_with([data['id']], queue=QueueNames.SEND_EMAIL)
|
||||
|
||||
|
||||
@freeze_time('2020-03-25 14:30')
|
||||
def test_save_api_email_dont_retry_if_notification_already_exists(sample_email_template, mocker):
|
||||
mock_send_email_to_provider = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
api_key = create_api_key(service=sample_email_template.service)
|
||||
data = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"template_id": str(sample_email_template.id),
|
||||
"template_version": sample_email_template.version,
|
||||
"to": "jane.citizen@example.com",
|
||||
"service_id": str(sample_email_template.service_id),
|
||||
"personalisation": None,
|
||||
"notification_type": sample_email_template.template_type,
|
||||
"api_key_id": str(api_key.id),
|
||||
"key_type": api_key.key_type,
|
||||
"client_reference": 'our email',
|
||||
"reply_to_text": "our.email@gov.uk",
|
||||
"document_download_count": 0,
|
||||
"status": NOTIFICATION_CREATED,
|
||||
"created_at": datetime.utcnow().strftime(DATETIME_FORMAT),
|
||||
}
|
||||
|
||||
encrypted = encryption.encrypt(
|
||||
data
|
||||
)
|
||||
assert len(Notification.query.all()) == 0
|
||||
save_api_email(encrypted)
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
# call the task again with the same notification
|
||||
save_api_email(encrypted)
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
assert str(notifications[0].id) == data['id']
|
||||
assert notifications[0].created_at == datetime(2020, 3, 25, 14, 30)
|
||||
# should only have sent the notification once.
|
||||
mock_send_email_to_provider.assert_called_once_with([data['id']], queue=QueueNames.SEND_EMAIL)
|
||||
|
||||
@@ -65,7 +65,7 @@ def test_cloudfoundry_config_has_different_defaults():
|
||||
def test_queue_names_all_queues_correct():
|
||||
# Need to ensure that all_queues() only returns queue names used in API
|
||||
queues = QueueNames.all_queues()
|
||||
assert len(queues) == 14
|
||||
assert len(queues) == 15
|
||||
assert set([
|
||||
QueueNames.PRIORITY,
|
||||
QueueNames.PERIODIC,
|
||||
@@ -81,4 +81,5 @@ def test_queue_names_all_queues_correct():
|
||||
QueueNames.CALLBACKS,
|
||||
QueueNames.LETTERS,
|
||||
QueueNames.SMS_CALLBACKS,
|
||||
QueueNames.SAVE_API_EMAIL
|
||||
]) == set(queues)
|
||||
|
||||
@@ -976,4 +976,58 @@ def test_post_notifications_saves_email_to_queue(client, notify_db_session, mock
|
||||
assert json_resp['id']
|
||||
assert json_resp['content']['body'] == "Dear citizen, have a nice day"
|
||||
assert json_resp['template']['id'] == str(template.id)
|
||||
save_email_task.assert_called_once_with([mock.ANY], queue='save-api-email')
|
||||
save_email_task.assert_called_once_with([mock.ANY], queue='save-api-email-tasks')
|
||||
|
||||
|
||||
def test_post_notifications_doesnt_save_email_to_queue_for_test_emails(client, notify_db_session, mocker):
|
||||
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
|
||||
mocked_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
|
||||
service = create_service(service_id='539d63a1-701d-400d-ab11-f3ee2319d4d4', service_name='high volume service')
|
||||
# create_api_key(service=service, key_type='test')
|
||||
template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
|
||||
data = {
|
||||
"email_address": "joe.citizen@example.com",
|
||||
"template_id": template.id,
|
||||
"personalisation": {"message": "Dear citizen, have a nice day"}
|
||||
}
|
||||
response = client.post(
|
||||
path='/v2/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'),
|
||||
create_authorization_header(service_id=service.id, key_type='test')]
|
||||
)
|
||||
|
||||
json_resp = response.get_json()
|
||||
|
||||
assert response.status_code == 201
|
||||
assert json_resp['id']
|
||||
assert json_resp['content']['body'] == "Dear citizen, have a nice day"
|
||||
assert json_resp['template']['id'] == str(template.id)
|
||||
assert mocked_send_task.called
|
||||
assert not save_email_task.called
|
||||
|
||||
|
||||
def test_post_notifications_doesnt_save_email_to_queue_for_sms(client, notify_db_session, mocker):
|
||||
save_email_task = mocker.patch("app.celery.tasks.save_api_email.apply_async")
|
||||
mocked_send_task = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
|
||||
service = create_service(service_id='539d63a1-701d-400d-ab11-f3ee2319d4d4', service_name='high volume service')
|
||||
template = create_template(service=service, content='((message))', template_type=SMS_TYPE)
|
||||
data = {
|
||||
"phone_number": '+447700900855',
|
||||
"template_id": template.id,
|
||||
"personalisation": {"message": "Dear citizen, have a nice day"}
|
||||
}
|
||||
response = client.post(
|
||||
path='/v2/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), create_authorization_header(service_id=service.id)]
|
||||
)
|
||||
|
||||
json_resp = response.get_json()
|
||||
|
||||
assert response.status_code == 201
|
||||
assert json_resp['id']
|
||||
assert mocked_send_task.called
|
||||
assert not save_email_task.called
|
||||
|
||||
Reference in New Issue
Block a user