From 431d2162c065887c2fd87763ef7aa3159fbabcee Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Thu, 16 May 2019 15:31:23 +0100 Subject: [PATCH] Fail reply-to email verification if no delivery within 5 minutes --- app/main/views/service_settings.py | 7 +++-- tests/app/main/views/test_service_settings.py | 30 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index 57471b131..95704a49d 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -460,9 +460,12 @@ def get_verify_reply_to_address_partials(service_id, notification_id): email_address=notification["to"], is_default=True if is_default == "True" else False ) - if notification["status"] in ["failed", "permanent-failure", "technical-failure", "temporary-failure"]: + created_at_no_tz = notification["created_at"][:-6] + seconds_since_sending = (datetime.utcnow() - datetime.strptime(created_at_no_tz, '%Y-%m-%dT%H:%M:%S.%f')).seconds + if notification["status"] in [ + "failed", "permanent-failure", "technical-failure", "temporary-failure" + ] or (notification["status"] == "sending" and seconds_since_sending > 300): verification_status = "failure" - # also include condition for when lots of time passes return { 'status': render_template( 'views/service-settings/email-reply-to/_verify-updates.html', diff --git a/tests/app/main/views/test_service_settings.py b/tests/app/main/views/test_service_settings.py index 6adbdf3cb..2d7dac890 100644 --- a/tests/app/main/views/test_service_settings.py +++ b/tests/app/main/views/test_service_settings.py @@ -2032,7 +2032,7 @@ def test_add_reply_to_email_address_sends_test_notification( @pytest.mark.parametrize("is_default", [True, False]) @pytest.mark.parametrize("status,expected_failure,expected_success", [ ("delivered", 0, 1), - ("pending", 0, 0), + ("sending", 0, 0), ("permanent-failure", 1, 0), ]) def test_verify_reply_to_address( @@ -2070,12 +2070,28 @@ def test_verify_reply_to_address( ) -def test_add_reply_to_email_address_success(): - pass - - -def test_add_reply_to_email_address_failure(): - pass +@freeze_time("2018-06-01 11:11:00.061258") +def test_add_reply_to_email_address_fails_if_notification_not_received(mocker, client_request, fake_uuid): + notification = { + "id": fake_uuid, + "status": "sending", + "to": "email@example.gov.uk", + "service_id": SERVICE_ONE_ID, + "template_id": TEMPLATE_ONE_ID, + "notification_type": "email", + "created_at": '2018-06-01T11:05:52.499230+00:00' + } + mocker.patch('app.notification_api_client.get_notification', return_value=notification) + mock_add_reply_to_email_address = mocker.patch('app.service_api_client.add_reply_to_email_address') + page = client_request.get( + 'main.verify_reply_to_address', + service_id=SERVICE_ONE_ID, + notification_id=notification["id"], + _optional_args="?is_default={}".format(False) + ) + expected_banner = page.find_all('div', class_='banner-dangerous')[0] + assert expected_banner.text.strip() == "Sorry dawg, this email address doesn't seem to be working :d" + mock_add_reply_to_email_address.assert_not_called() @pytest.mark.parametrize('fixture, data, api_default_args', [