mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-19 05:58:53 -04:00
Endpoint to allow SES updates to occur
- update notification with delivery state
This commit is contained in:
@@ -5,3 +5,9 @@ def load_example_csv(file):
|
||||
file_path = os.path.join("test_csv_files", "{}.csv".format(file))
|
||||
with open(file_path) as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def load_example_ses(file):
|
||||
file_path = os.path.join("test_ses_responses", "{}.json".format(file))
|
||||
with open(file_path) as f:
|
||||
return f.read()
|
||||
|
||||
@@ -16,12 +16,36 @@ from app.dao.notifications_dao import (
|
||||
dao_get_notification_statistics_for_service,
|
||||
delete_successful_notifications_created_more_than_a_day_ago,
|
||||
delete_failed_notifications_created_more_than_a_week_ago,
|
||||
dao_get_notification_statistics_for_service_and_day
|
||||
dao_get_notification_statistics_for_service_and_day,
|
||||
update_notification_status_by_id,
|
||||
update_notification_status_by_to
|
||||
)
|
||||
from tests.app.conftest import sample_job
|
||||
from tests.app.conftest import sample_notification
|
||||
|
||||
|
||||
def test_should_by_able_to_update_status_by_id(sample_notification):
|
||||
assert Notification.query.get(sample_notification.id).status == 'sent'
|
||||
count = update_notification_status_by_id(sample_notification.id, 'delivered')
|
||||
assert count == 1
|
||||
assert Notification.query.get(sample_notification.id).status == 'delivered'
|
||||
|
||||
|
||||
def test_should_return_zero_count_if_no_notification_with_id():
|
||||
assert update_notification_status_by_id(str(uuid.uuid4()), 'delivered') == 0
|
||||
|
||||
|
||||
def test_should_return_zero_count_if_no_notification_with_to():
|
||||
assert update_notification_status_by_to('something', 'delivered') == 0
|
||||
|
||||
|
||||
def test_should_by_able_to_update_status_by_to(sample_notification):
|
||||
assert Notification.query.get(sample_notification.id).status == 'sent'
|
||||
count = update_notification_status_by_to(sample_notification.to, 'delivered')
|
||||
assert count == 1
|
||||
assert Notification.query.get(sample_notification.id).status == 'delivered'
|
||||
|
||||
|
||||
def test_should_be_able_to_get_statistics_for_a_service(sample_template):
|
||||
data = {
|
||||
'to': '+44709123456',
|
||||
@@ -568,7 +592,7 @@ def test_should_not_delete_sent_notifications_before_one_day(notify_db, notify_d
|
||||
|
||||
def test_should_not_delete_failed_notifications_before_seven_days(notify_db, notify_db_session):
|
||||
expired = datetime.utcnow() - timedelta(hours=24 * 7)
|
||||
valid = datetime.utcnow() - timedelta(hours=(24 * 6) + 23, minutes=59, seconds=59)
|
||||
valid = datetime.utcnow() - timedelta(hours=(24 * 6) + 23, minutes=59, seconds=59)
|
||||
sample_notification(notify_db, notify_db_session, created_at=expired, status="failed", to_field="expired")
|
||||
sample_notification(notify_db, notify_db_session, created_at=valid, status="failed", to_field="valid")
|
||||
assert len(Notification.query.all()) == 2
|
||||
|
||||
@@ -9,6 +9,7 @@ from app.dao.templates_dao import dao_get_all_templates_for_service
|
||||
from app.dao.services_dao import dao_update_service
|
||||
from app.dao.notifications_dao import get_notification_by_id
|
||||
from freezegun import freeze_time
|
||||
from tests.app import load_example_ses
|
||||
|
||||
|
||||
def test_get_notification_by_id(notify_api, sample_notification):
|
||||
@@ -1044,3 +1045,100 @@ def test_firetext_callback_should_update_notification_status_sent(notify_api, no
|
||||
)
|
||||
updated = get_notification_by_id(notification.id)
|
||||
assert updated.status == 'sent'
|
||||
|
||||
|
||||
def test_ses_callback_should_not_need_auth(notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=load_example_ses('ses_response'),
|
||||
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_ses_callback_should_fail_if_invalid_json(notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data="nonsense",
|
||||
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert response.status_code == 400
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == 'SES callback failed: invalid json'
|
||||
|
||||
|
||||
def test_ses_callback_should_fail_if_invalid_notification_type(notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
ses_response = json.loads(load_example_ses('ses_response'))
|
||||
ses_response['Message']['notificationType'] = 'Unknown'
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=json.dumps(ses_response),
|
||||
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert response.status_code == 400
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == 'SES callback failed: status Unknown not found'
|
||||
|
||||
|
||||
def test_ses_callback_should_fail_if_missing_destination(notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
ses_response = json.loads(load_example_ses('ses_response'))
|
||||
del(ses_response['Message']['mail']['destination'])
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=json.dumps(ses_response),
|
||||
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert response.status_code == 400
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == 'SES callback failed: destination missing'
|
||||
|
||||
|
||||
def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
ses_response = json.loads(load_example_ses('ses_response'))
|
||||
ses_response['Message']['mail']['destination'] = ['wont find this']
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=json.dumps(ses_response),
|
||||
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert response.status_code == 404
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == 'SES callback failed: notification not found. Status delivered'
|
||||
|
||||
|
||||
def test_ses_callback_should_update_notification_status(notify_api, sample_notification):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
assert get_notification_by_id(sample_notification.id).status == 'sent'
|
||||
|
||||
ses_response = json.loads(load_example_ses('ses_response'))
|
||||
ses_response['Message']['mail']['destination'] = [sample_notification.to]
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=json.dumps(ses_response),
|
||||
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert response.status_code == 200
|
||||
assert json_resp['result'] == 'success'
|
||||
assert json_resp['message'] == 'SES callback succeeded'
|
||||
assert get_notification_by_id(sample_notification.id).status == 'delivered'
|
||||
|
||||
Reference in New Issue
Block a user