- 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:
Rebecca Law
2020-03-25 12:39:15 +00:00
parent a13bcc6697
commit db4b4d929d
7 changed files with 105 additions and 10 deletions

View File

@@ -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)