From 615ea6a98aefec839423f65ee50518de4d15f744 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Fri, 10 May 2019 15:32:24 +0100 Subject: [PATCH] Send verifcation email to a new reply-to email address --- app/config.py | 1 + app/service/rest.py | 32 ++++++++++++++++++++++++++++++-- tests/app/conftest.py | 14 ++++++++++++++ tests/app/service/test_rest.py | 21 +++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/app/config.py b/app/config.py index f204885b4..b2970c1e4 100644 --- a/app/config.py +++ b/app/config.py @@ -150,6 +150,7 @@ class Config(object): ORGANISATION_INVITATION_EMAIL_TEMPLATE_ID = '203566f0-d835-47c5-aa06-932439c86573' TEAM_MEMBER_EDIT_EMAIL_TEMPLATE_ID = 'c73f1d71-4049-46d5-a647-d013bdeca3f0' TEAM_MEMBER_EDIT_MOBILE_TEMPLATE_ID = '8a31520f-4751-4789-8ea1-fe54496725eb' + REPLY_TO_EMAIL_ADDRESS_VERIFICATION_TEMPLATE_ID = '2d1ff26c-4d20-4cb2-8918-a0bec4002c9e' BROKER_URL = 'sqs://' BROKER_TRANSPORT_OPTIONS = { diff --git a/app/service/rest.py b/app/service/rest.py index 8b2be1818..6bf76763a 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -12,6 +12,7 @@ from notifications_utils.timezones import convert_utc_to_bst from sqlalchemy.exc import IntegrityError from sqlalchemy.orm.exc import NoResultFound +from app.config import QueueNames from app.dao import notifications_dao from app.dao.dao_utils import dao_rollback from app.dao.date_util import get_financial_year @@ -77,13 +78,17 @@ from app.dao.service_letter_contact_dao import ( add_letter_contact_for_service, update_letter_contact ) +from app.dao.templates_dao import dao_get_template_by_id from app.dao.users_dao import get_user_by_id from app.errors import ( InvalidRequest, register_errors ) from app.letters.utils import letter_print_day -from app.models import LETTER_TYPE, NOTIFICATION_CANCELLED, Permission, Service, EmailBranding, LetterBranding +from app.models import ( + KEY_TYPE_NORMAL, LETTER_TYPE, NOTIFICATION_CANCELLED, Permission, Service, EmailBranding, LetterBranding +) +from app.notifications.process_notifications import persist_notification, send_notification_to_queue from app.schema_validation import validate from app.service import statistics from app.service.service_data_retention_schema import ( @@ -103,7 +108,8 @@ from app.schemas import ( api_key_schema, notification_with_template_schema, notifications_filter_schema, - detailed_service_schema + detailed_service_schema, + email_data_request_schema ) from app.user.users_schema import post_set_permissions_schema from app.utils import pagination_links @@ -644,6 +650,28 @@ def get_email_reply_to_address(service_id, reply_to_id): return jsonify(result.serialize()), 200 +@service_blueprint.route('/email-reply-to/verify', methods=['POST']) +def verify_new_service_reply_to_email_address(): + email_address, errors = email_data_request_schema.load(request.get_json()) + template = dao_get_template_by_id(current_app.config['REPLY_TO_EMAIL_ADDRESS_VERIFICATION_TEMPLATE_ID']) + service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID']) + saved_notification = persist_notification( + template_id=template.id, + template_version=template.version, + recipient=email_address["email"], + service=service, + personalisation='', + notification_type=template.template_type, + api_key_id=None, + key_type=KEY_TYPE_NORMAL, + reply_to_text=service.get_default_reply_to_email_address() + ) + + send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY) + + return jsonify(data={"id": saved_notification.id}), 201 + + @service_blueprint.route('//email-reply-to', methods=['POST']) def add_service_reply_to_email_address(service_id): # validate the service exists, throws ResultNotFound exception. diff --git a/tests/app/conftest.py b/tests/app/conftest.py index cbb113af3..e1e98605d 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -951,6 +951,20 @@ def password_reset_email_template(notify_db, ) +@pytest.fixture(scope='function') +def verify_reply_to_address_email_template(notify_db, notify_db_session): + service, user = notify_service(notify_db, notify_db_session) + + return create_custom_template( + service=service, + user=user, + template_config_name='REPLY_TO_EMAIL_ADDRESS_VERIFICATION_TEMPLATE_ID', + content="Hi,This address has been provided as the reply-to email address so we are verifying if it's working", + subject='Your GOV.UK Notify reply-to email address', + template_type='email' + ) + + @pytest.fixture(scope='function') def team_member_email_edit_template(notify_db, notify_db_session): service, user = notify_service(notify_db, notify_db_session) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index f57d523e2..3d630ad82 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -2491,6 +2491,27 @@ def test_get_email_reply_to_addresses_with_multiple_email_addresses(client, noti assert not json_response[1]['updated_at'] +@freeze_time("2016-01-01 11:09:00.061258") +def test_verify_new_service_reply_to_email_address_should_send_verification_email( + client, sample_user, mocker, verify_reply_to_address_email_template +): + mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') + data = json.dumps({'email': 'reply-here@example.gov.uk'}) + auth_header = create_authorization_header() + notify_service = verify_reply_to_address_email_template.service + response = client.post( + url_for('service.verify_new_service_reply_to_email_address'), + data=data, + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 201 + notification = Notification.query.first() + json_response = json.loads(response.get_data(as_text=True)) + assert json_response["data"] == {"id": str(notification.id)} + mocked.assert_called_once_with([str(notification.id)], queue="notify-internal-tasks") + assert notification.reply_to_text == notify_service.get_default_reply_to_email_address() + + def test_add_service_reply_to_email_address(client, sample_service): data = json.dumps({"email_address": "new@reply.com", "is_default": True}) response = client.post('/service/{}/email-reply-to'.format(sample_service.id),