Skip to content

Commit 1324f58

Browse files
committed
feat: message group id for notification entry points
1 parent 802e5e1 commit 1324f58

File tree

11 files changed

+123
-27
lines changed

11 files changed

+123
-27
lines changed

app/notifications/process_notifications.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ def increment_daily_limit_cache(service_id, notification_type):
218218
redis_store.incr(cache_key)
219219

220220

221-
def send_notification_to_queue_detached(key_type, notification_type, notification_id, queue=None):
221+
def send_notification_to_queue_detached(
222+
key_type, notification_type, notification_id, queue=None, message_group_id=None
223+
):
222224
if key_type == KEY_TYPE_TEST:
223225
queue = QueueNames.RESEARCH_MODE
224226

@@ -236,14 +238,20 @@ def send_notification_to_queue_detached(key_type, notification_type, notificatio
236238
deliver_task = get_pdf_for_templated_letter
237239

238240
try:
239-
deliver_task.apply_async([str(notification_id)], queue=queue)
241+
deliver_task.apply_async([str(notification_id)], queue=queue, MessageGroupId=message_group_id)
240242
except Exception:
241243
dao_delete_notifications_by_id(notification_id)
242244
raise
243245

244246

245247
def send_notification_to_queue(notification, queue=None):
246-
send_notification_to_queue_detached(notification.key_type, notification.notification_type, notification.id, queue)
248+
send_notification_to_queue_detached(
249+
notification.key_type,
250+
notification.notification_type,
251+
notification.id,
252+
queue,
253+
message_group_id=str(notification.service_id),
254+
)
247255

248256

249257
def simulated_recipient(to_address, notification_type):

app/v2/notifications/post_notifications.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def process_sms_or_email_notification(
271271
key_type=api_user.key_type,
272272
notification_type=notification_type,
273273
notification_id=notification_id,
274+
message_group_id=str(service.id),
274275
)
275276
else:
276277
current_app.logger.info(
@@ -373,12 +374,17 @@ def process_letter_notification(
373374
postage=postage,
374375
)
375376

376-
get_pdf_for_templated_letter.apply_async([str(notification.id)], queue=queue)
377+
get_pdf_for_templated_letter.apply_async(
378+
[str(notification.id)],
379+
queue=queue,
380+
MessageGroupId=str(service.id),
381+
)
377382

378383
if test_key and current_app.config["TEST_LETTERS_FAKE_DELIVERY"]:
379384
create_fake_letter_callback.apply_async(
380385
[notification.id, notification.billable_units, notification.postage],
381386
queue=queue,
387+
MessageGroupId=str(service.id),
382388
)
383389

384390
resp = create_response_for_post_notification(
@@ -428,10 +434,15 @@ def process_precompiled_letter_notifications(*, letter_data, api_key, service, t
428434
name=TaskNames.SCAN_FILE,
429435
kwargs={"filename": filename},
430436
queue=QueueNames.ANTIVIRUS,
437+
MessageGroupId=str(service.id),
431438
)
432439
else:
433440
# stub out antivirus in dev
434-
sanitise_letter.apply_async([filename], queue=QueueNames.LETTERS)
441+
sanitise_letter.apply_async(
442+
[filename],
443+
queue=QueueNames.LETTERS,
444+
MessageGroupId=str(service.id),
445+
)
435446

436447
return resp
437448

tests/app/celery/test_letters_pdf_tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ def test_resanitise_pdf_calls_template_preview_with_letter_details(
891891
"allow_international_letters": expected_international_letters_allowed,
892892
},
893893
queue=QueueNames.SANITISE_LETTERS,
894+
MessageGroupId=str(sample_letter_notification.service_id),
894895
)
895896

896897

tests/app/delivery/test_send_to_providers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ def test_send_email_to_provider_should_call_response_task_if_test_key(sample_ema
366366
send_to_providers.send_email_to_provider(notification)
367367

368368
assert not app.aws_ses_client.send_email.called
369-
app.delivery.send_to_providers.send_email_response.assert_called_once_with(str(reference), "john@smith.com")
369+
app.delivery.send_to_providers.send_email_response.assert_called_once_with(
370+
str(reference), "john@smith.com", notification.service_id
371+
)
370372
persisted_notification = Notification.query.filter_by(id=notification.id).one()
371373
assert persisted_notification.to == "john@smith.com"
372374
assert persisted_notification.template_id == sample_email_template.id

tests/app/notifications/test_process_notification.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,17 +667,19 @@ def test_send_notification_to_queue(
667667
mocker,
668668
):
669669
mocked = mocker.patch(f"app.celery.{expected_task}.apply_async")
670-
Notification = namedtuple("Notification", ["id", "key_type", "notification_type", "created_at"])
670+
Notification = namedtuple("Notification", ["id", "key_type", "notification_type", "created_at", "service_id"])
671+
service_id = uuid.uuid4()
671672
notification = Notification(
672673
id=uuid.uuid4(),
673674
key_type=key_type,
674675
notification_type=notification_type,
675676
created_at=datetime.datetime(2016, 11, 11, 16, 8, 18),
677+
service_id=service_id,
676678
)
677679

678680
send_notification_to_queue(notification=notification, queue=requested_queue)
679681

680-
mocked.assert_called_once_with([str(notification.id)], queue=expected_queue)
682+
mocked.assert_called_once_with([str(notification.id)], queue=expected_queue, MessageGroupId=str(service_id))
681683

682684

683685
def test_send_notification_to_queue_throws_exception_deletes_notification(sample_notification, mocker):
@@ -687,7 +689,11 @@ def test_send_notification_to_queue_throws_exception_deletes_notification(sample
687689
)
688690
with pytest.raises(Boto3Error):
689691
send_notification_to_queue(sample_notification)
690-
mocked.assert_called_once_with([str(sample_notification.id)], queue="send-sms-tasks")
692+
mocked.assert_called_once_with(
693+
[str(sample_notification.id)],
694+
queue="send-sms-tasks",
695+
MessageGroupId=str(sample_notification.service_id),
696+
)
691697

692698
assert Notification.query.count() == 0
693699
assert NotificationHistory.query.count() == 0

tests/app/organisation/test_invite_rest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ def test_create_invited_org_user(
6969
assert notification.personalisation["url"].startswith(expected_start_of_invite_url.format(hostnames=hostnames))
7070
assert len(notification.personalisation["url"]) > len(expected_start_of_invite_url.format(hostnames=hostnames))
7171

72-
mocked.assert_called_once_with([(str(notification.id))], queue="notify-internal-tasks")
72+
mocked.assert_called_once_with(
73+
[(str(notification.id))],
74+
queue="notify-internal-tasks",
75+
MessageGroupId=str(notification.service_id),
76+
)
7377

7478

7579
def test_create_invited_user_invalid_email(admin_request, sample_organisation, sample_user, mocker):

tests/app/service_invite/test_service_invite_rest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ def test_create_invited_user(
6565
assert len(notification.personalisation["url"]) > len(expected_start_of_invite_url.format(hostnames=hostnames))
6666
assert str(notification.template_id) == current_app.config["INVITATION_EMAIL_TEMPLATE_ID"]
6767

68-
mocked.assert_called_once_with([(str(notification.id))], queue="notify-internal-tasks")
68+
mocked.assert_called_once_with(
69+
[(str(notification.id))],
70+
queue="notify-internal-tasks",
71+
MessageGroupId=str(notification.service_id),
72+
)
6973

7074

7175
def test_create_invited_user_without_auth_type(admin_request, sample_service, mocker, invitation_email_template):

tests/app/user/test_rest.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,11 @@ def test_send_user_reset_password_should_send_reset_password_link(
659659
)
660660

661661
notification = Notification.query.first()
662-
mocked.assert_called_once_with([str(notification.id)], queue="notify-internal-tasks")
662+
mocked.assert_called_once_with(
663+
[str(notification.id)],
664+
queue="notify-internal-tasks",
665+
MessageGroupId=str(notification.service_id),
666+
)
663667
assert notification.reply_to_text == notify_service.get_default_reply_to_email_address()
664668

665669

@@ -716,7 +720,11 @@ def test_send_user_reset_password_reset_password_link_contains_redirect_link_if_
716720

717721
notification = Notification.query.first()
718722
assert "?next=blob" in notification.content
719-
mocked.assert_called_once_with([str(notification.id)], queue="notify-internal-tasks")
723+
mocked.assert_called_once_with(
724+
[str(notification.id)],
725+
queue="notify-internal-tasks",
726+
MessageGroupId=str(notification.service_id),
727+
)
720728

721729

722730
def test_send_user_reset_password_should_return_400_when_email_is_missing(admin_request, mocker):
@@ -775,7 +783,11 @@ def test_send_already_registered_email(admin_request, sample_user, already_regis
775783
)
776784

777785
notification = Notification.query.first()
778-
mocked.assert_called_once_with(([str(notification.id)]), queue="notify-internal-tasks")
786+
mocked.assert_called_once_with(
787+
([str(notification.id)]),
788+
queue="notify-internal-tasks",
789+
MessageGroupId=str(notification.service_id),
790+
)
779791
assert notification.reply_to_text == notify_service.get_default_reply_to_email_address()
780792

781793

@@ -807,7 +819,11 @@ def test_send_user_confirm_new_email_returns_204(
807819
)
808820

809821
notification = Notification.query.first()
810-
mocked.assert_called_once_with(([str(notification.id)]), queue="notify-internal-tasks")
822+
mocked.assert_called_once_with(
823+
([str(notification.id)]),
824+
queue="notify-internal-tasks",
825+
MessageGroupId=str(notification.service_id),
826+
)
811827
assert notification.reply_to_text == notify_service.get_default_reply_to_email_address()
812828

813829

tests/app/user/test_rest_verify.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ def test_send_user_sms_code(client, sample_user, sms_code_template, mocker):
211211
assert notification.reply_to_text == notify_service.get_default_sms_sender()
212212

213213
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
214-
([str(notification.id)]), queue="notify-internal-tasks"
214+
([str(notification.id)]),
215+
queue="notify-internal-tasks",
216+
MessageGroupId=str(notification.service_id),
215217
)
216218

217219

@@ -236,7 +238,9 @@ def test_send_user_code_for_sms_with_optional_to_field(client, sample_user, sms_
236238
notification = Notification.query.first()
237239
assert notification.to == to_number
238240
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
239-
([str(notification.id)]), queue="notify-internal-tasks"
241+
([str(notification.id)]),
242+
queue="notify-internal-tasks",
243+
MessageGroupId=str(notification.service_id),
240244
)
241245

242246

@@ -307,7 +311,11 @@ def test_send_new_user_email_verification(
307311
assert resp.status_code == 204
308312
notification = Notification.query.first()
309313
assert VerifyCode.query.count() == 0
310-
mocked.assert_called_once_with(([str(notification.id)]), queue="notify-internal-tasks")
314+
mocked.assert_called_once_with(
315+
([str(notification.id)]),
316+
queue="notify-internal-tasks",
317+
MessageGroupId=str(notification.service_id),
318+
)
311319
assert notification.reply_to_text == notify_service.get_default_reply_to_email_address()
312320
assert notification.personalisation["name"] == "Test User"
313321
assert notification.personalisation["url"].startswith(expected_url_starts_with.format(hostnames=hostnames))
@@ -413,7 +421,11 @@ def test_send_user_email_code(
413421
assert str(noti.template_id) == current_app.config["EMAIL_2FA_TEMPLATE_ID"]
414422
assert noti.personalisation["name"] == "Test User"
415423
assert noti.personalisation["url"].startswith(expected_auth_url.format(hostnames=hostnames))
416-
deliver_email.assert_called_once_with([str(noti.id)], queue="notify-internal-tasks")
424+
deliver_email.assert_called_once_with(
425+
[str(noti.id)],
426+
queue="notify-internal-tasks",
427+
MessageGroupId=str(noti.service_id),
428+
)
417429

418430

419431
def test_send_user_email_code_with_urlencoded_next_param(admin_request, mocker, sample_user, email_2fa_code_template):

tests/app/v2/notifications/test_post_letter_notifications.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ def test_post_letter_notification_returns_201(api_client_request, sample_letter_
7171
)
7272
assert not resp_json["scheduled_for"]
7373
assert not notification.reply_to_text
74-
mock.assert_called_once_with([str(notification.id)], queue=QueueNames.CREATE_LETTERS_PDF)
74+
mock.assert_called_once_with(
75+
[str(notification.id)],
76+
queue=QueueNames.CREATE_LETTERS_PDF,
77+
MessageGroupId=str(sample_letter_template.service_id),
78+
)
7579

7680

7781
def test_post_letter_notification_sets_postage(api_client_request, notify_db_session, mocker):
@@ -288,7 +292,11 @@ def test_post_letter_notification_with_test_key_creates_pdf_and_sets_status_to_d
288292

289293
notification = Notification.query.one()
290294

291-
fake_create_letter_task.assert_called_once_with([str(notification.id)], queue="research-mode-tasks")
295+
fake_create_letter_task.assert_called_once_with(
296+
[str(notification.id)],
297+
queue="research-mode-tasks",
298+
MessageGroupId=str(sample_letter_template.service_id),
299+
)
292300
assert not fake_create_dvla_response_task.called
293301
assert notification.status == NOTIFICATION_DELIVERED
294302
assert notification.updated_at is not None
@@ -322,7 +330,11 @@ def test_post_letter_notification_with_test_key_creates_pdf_and_sets_status_to_s
322330

323331
notification = Notification.query.one()
324332

325-
fake_create_letter_task.assert_called_once_with([str(notification.id)], queue="research-mode-tasks")
333+
fake_create_letter_task.assert_called_once_with(
334+
[str(notification.id)],
335+
queue="research-mode-tasks",
336+
MessageGroupId=str(sample_letter_template.service_id),
337+
)
326338
assert fake_create_dvla_response_task.called
327339
assert notification.status == NOTIFICATION_SENDING
328340

@@ -563,7 +575,11 @@ def test_post_letter_notification_is_delivered_but_still_creates_pdf_if_in_trial
563575

564576
notification = Notification.query.one()
565577
assert notification.status == NOTIFICATION_DELIVERED
566-
fake_create_letter_task.assert_called_once_with([str(notification.id)], queue="research-mode-tasks")
578+
fake_create_letter_task.assert_called_once_with(
579+
[str(notification.id)],
580+
queue="research-mode-tasks",
581+
MessageGroupId=str(sample_trial_letter_template.service_id),
582+
)
567583

568584

569585
def test_post_letter_notification_is_delivered_and_has_pdf_uploaded_to_test_letters_bucket_using_test_key(

0 commit comments

Comments
 (0)