Skip to content

Commit 28600c1

Browse files
authored
feat: add service suspension check for notifications (#2679)
1 parent 266f50b commit 28600c1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

app/v2/notifications/post_notifications.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ def post_bulk():
167167
status_code=415,
168168
)
169169

170+
# Fail fast if the authenticated service is not active
171+
if not getattr(authenticated_service, "active", True):
172+
raise BadRequestError(message="Service is suspended", status_code=403)
173+
170174
max_rows = current_app.config["CSV_MAX_ROWS"]
171175
epoch_seeding_bounce = current_app.config["FF_BOUNCE_RATE_SEED_EPOCH_MS"]
172176
if epoch_seeding_bounce:
@@ -291,6 +295,10 @@ def post_notification(notification_type: NotificationType):
291295
else:
292296
abort(404)
293297

298+
# Fail fast if the authenticated service is not active
299+
if not getattr(authenticated_service, "active", True):
300+
raise BadRequestError(message="Service is suspended", status_code=403)
301+
294302
epoch_seeding_bounce = current_app.config["FF_BOUNCE_RATE_SEED_EPOCH_MS"]
295303
if epoch_seeding_bounce:
296304
_seed_bounce_data(epoch_seeding_bounce, str(authenticated_service.id))

tests/app/v2/notifications/test_post_notifications.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,3 +2872,43 @@ def test_post_bulk_returns_429_if_over_rate_limit(
28722872
headers=[("Content-Type", "application/json"), auth_header],
28732873
)
28742874
assert response.status_code == 429
2875+
2876+
2877+
def test_post_notification_returns_403_if_service_suspended(client, notify_db_session, mocker):
2878+
"""Assert a suspended service cannot send a single notification"""
2879+
2880+
# create a service and set it to inactive
2881+
service = create_service()
2882+
service.active = False
2883+
2884+
template = create_template(service=service, template_type=SMS_TYPE)
2885+
data = {"phone_number": "+16502532222", "template_id": str(template.id)}
2886+
auth_header = create_authorization_header(service_id=service.id)
2887+
2888+
response = client.post(
2889+
path="/v2/notifications/sms",
2890+
data=json.dumps(data),
2891+
headers=[("Content-Type", "application/json"), auth_header],
2892+
)
2893+
2894+
assert response.status_code == 403
2895+
2896+
2897+
def test_post_bulk_returns_403_if_service_suspended(client, notify_db_session, mocker):
2898+
"""Assert a suspended service cannot create a bulk job"""
2899+
mocker.patch("app.v2.notifications.post_notifications.create_bulk_job", return_value=None)
2900+
2901+
service = create_service()
2902+
service.active = False
2903+
2904+
template = create_template(service=service, template_type=EMAIL_TYPE)
2905+
data = {"name": "job_name", "template_id": str(template.id), "rows": [["email address"], ["[email protected]"]]}
2906+
auth_header = create_authorization_header(service_id=service.id)
2907+
2908+
response = client.post(
2909+
"/v2/notifications/bulk",
2910+
data=json.dumps(data),
2911+
headers=[("Content-Type", "application/json"), auth_header],
2912+
)
2913+
2914+
assert response.status_code == 403

0 commit comments

Comments
 (0)