From c5bd685cef12d7c296b865c23ee249ab47e01bb5 Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Thu, 27 Apr 2017 16:55:39 +0100 Subject: [PATCH] Don't update sent notifications (dao) --- app/dao/notifications_dao.py | 18 ++++++++++++------ tests/app/dao/test_notification_dao.py | 21 +++++++++++++++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 6a759f261..377b47e3c 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -175,11 +175,14 @@ def _update_notification_status(notification, status): def update_notification_status_by_id(notification_id, status): notification = Notification.query.with_lockmode("update").filter( Notification.id == notification_id, - or_(Notification.status == NOTIFICATION_CREATED, + or_( + Notification.status == NOTIFICATION_CREATED, Notification.status == NOTIFICATION_SENDING, - Notification.status == NOTIFICATION_PENDING)).first() + Notification.status == NOTIFICATION_PENDING, + Notification.status == NOTIFICATION_SENT + )).first() - if not notification: + if not notification or notification.status == NOTIFICATION_SENT: return None return _update_notification_status( @@ -193,10 +196,13 @@ def update_notification_status_by_id(notification_id, status): def update_notification_status_by_reference(reference, status): notification = Notification.query.filter( Notification.reference == reference, - or_(Notification.status == NOTIFICATION_SENDING, - Notification.status == NOTIFICATION_PENDING)).first() + or_( + Notification.status == NOTIFICATION_SENDING, + Notification.status == NOTIFICATION_PENDING, + Notification.status == NOTIFICATION_SENT + )).first() - if not notification: + if not notification or notification.status == NOTIFICATION_SENT: return None return _update_notification_status( diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index e43f5f5de..8f67d2335 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -15,6 +15,7 @@ from app.models import ( TemplateStatistics, NOTIFICATION_STATUS_TYPES, NOTIFICATION_STATUS_TYPES_FAILED, + NOTIFICATION_SENT, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST @@ -353,11 +354,27 @@ def test_should_update_status_by_id_if_created(notify_db, notify_db_session): def test_should_not_update_status_by_reference_if_in_sent_status(notify_db, notify_db_session): - assert 1 == 2 + notification = sample_notification( + notify_db, + notify_db_session, + status=NOTIFICATION_SENT, + reference='foo' + ) + + update_notification_status_by_reference('foo', 'failed') + assert Notification.query.get(notification.id).status == NOTIFICATION_SENT def test_should_not_update_status_by_id_if_in_sent_status(notify_db, notify_db_session): - assert 1 == 2 + notification = sample_notification( + notify_db, + notify_db_session, + status=NOTIFICATION_SENT + ) + + update_notification_status_by_id(notification.id, 'failed') + + assert Notification.query.get(notification.id).status == NOTIFICATION_SENT def test_should_not_update_status_by_reference_if_not_sending(notify_db, notify_db_session):