From bcd6054e6058d1903494db5b4caa6a647ad1f9ee Mon Sep 17 00:00:00 2001 From: Steve Astels Date: Tue, 20 Dec 2022 11:25:50 -0500 Subject: [PATCH] Improve team key error logging (#1715) --- app/notifications/rest.py | 2 +- app/notifications/validators.py | 2 +- tests/app/notifications/rest/test_send_notification.py | 10 +++++----- tests/app/notifications/test_validators.py | 6 +++--- tests/app/v2/notifications/test_post_notifications.py | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index f5b835c283..f6605bb4b7 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -176,7 +176,7 @@ def _service_allowed_to_send_to(notification, service): # FIXME: hard code it for now until we can get en/fr specific links and text if api_user.key_type == KEY_TYPE_TEAM: message = ( - "Can’t send to this recipient using a team-only API key " + f"Can’t send to this recipient using a team-only API key (service {service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}' ) else: diff --git a/app/notifications/validators.py b/app/notifications/validators.py index 88c6e85b79..1e281d3bef 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -276,7 +276,7 @@ def service_can_send_to_recipient(send_to, key_type: ApiKeyType, service: Servic # FIXME: hard code it for now until we can get en/fr specific links and text if key_type == KEY_TYPE_TEAM: message = ( - "Can’t send to this recipient using a team-only API key " + f"Can’t send to this recipient using a team-only API key (service {service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}' ) else: diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index 6879d9671d..3b85dfd8d0 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -492,7 +492,7 @@ def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session assert json.loads(response.get_data(as_text=True))["data"]["body"] == "hello\nthere" -def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker): +def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, sample_service, mocker): with notify_api.test_request_context(), notify_api.test_client() as client: mocker.patch("app.celery.provider_tasks.deliver_email.apply_async") data = { @@ -514,12 +514,12 @@ def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api assert response.status_code == 400 assert [ - "Can’t send to this recipient using a team-only API key " + f"Can’t send to this recipient using a team-only API key (service {sample_service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}' ] == json_resp["message"]["to"] -def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api, sample_template, mocker): +def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api, sample_template, sample_service, mocker): with notify_api.test_request_context(), notify_api.test_client() as client: mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async") @@ -541,7 +541,7 @@ def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api, assert response.status_code == 400 assert [ - "Can’t send to this recipient using a team-only API key " + f"Can’t send to this recipient using a team-only API key (service {sample_service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}' ] == json_resp["message"]["to"] @@ -849,7 +849,7 @@ def test_should_not_send_notification_to_non_safelist_recipient_in_trial_mode( ("Can’t send to this recipient when service is in trial mode " f'– see {get_document_url("en", "keys.html#live")}') if key_type == KEY_TYPE_NORMAL else ( - "Can’t send to this recipient using a team-only API key " + f"Can’t send to this recipient using a team-only API key (service {service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}' ) ) diff --git a/tests/app/notifications/test_validators.py b/tests/app/notifications/test_validators.py index eeb39a6f6b..63cd5eb195 100644 --- a/tests/app/notifications/test_validators.py +++ b/tests/app/notifications/test_validators.py @@ -403,7 +403,7 @@ def test_service_can_send_to_recipient_fails_when_ignoring_safelist( ) assert exec_info.value.status_code == 400 assert ( - exec_info.value.message == "Can’t send to this recipient using a team-only API key " + exec_info.value.message == f"Can’t send to this recipient using a team-only API key (service {sample_service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}' ) assert exec_info.value.fields == [] @@ -413,7 +413,7 @@ def test_service_can_send_to_recipient_fails_when_ignoring_safelist( @pytest.mark.parametrize( "key_type, error_message", [ - ("team", "Can’t send to this recipient using a team-only API key - see"), + ("team", "Can’t send to this recipient using a team-only API key"), ("normal", "Can’t send to this recipient when service is in trial mode – see "), ], ) # noqa @@ -438,7 +438,7 @@ def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(n service_can_send_to_recipient("0758964221", "team", live_service) assert e.value.status_code == 400 assert ( - e.value.message == "Can’t send to this recipient using a team-only API key " + e.value.message == f"Can’t send to this recipient using a team-only API key (service {live_service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}' ) assert e.value.fields == [] diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index a597c91c39..c7fccf76f9 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -589,7 +589,7 @@ def test_post_sms_notification_returns_400_if_number_not_safelisted(self, notify assert error_json["errors"] == [ { "error": "BadRequestError", - "message": "Can’t send to this recipient using a team-only API key " + "message": f"Can’t send to this recipient using a team-only API key (service {service.id}) " f'- see {get_document_url("en", "keys.html#team-and-safelist")}', } ]