From c2b6a9df80f90d6f25cd294906a1a6967f2f0be2 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 7 Mar 2022 15:03:46 +0000 Subject: [PATCH] Allow admin app to specify domain for registration email MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This follows the pattern for invite emails where the admin app tells the API which domain to use when generating the link. This will starting working once the admin change is merged: - [ ] TBC It won’t break anything if it’s merged before the admin change. --- app/user/rest.py | 11 ++++++++--- tests/app/user/test_rest_verify.py | 26 +++++++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/user/rest.py b/app/user/rest.py index ac6989266..d3823f8c2 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -374,6 +374,8 @@ def send_user_confirm_new_email(user_id): @user_blueprint.route('//email-verification', methods=['POST']) def send_new_user_email_verification(user_id): + request_json = request.get_json() + # when registering, we verify all users' email addresses using this function user_to_send_to = get_user_by_id(user_id=user_id) @@ -387,7 +389,10 @@ def send_new_user_email_verification(user_id): service=service, personalisation={ 'name': user_to_send_to.name, - 'url': _create_verification_url(user_to_send_to) + 'url': _create_verification_url( + user_to_send_to, + base_url=request_json.get('admin_base_url'), + ), }, notification_type=template.template_type, api_key_id=None, @@ -556,10 +561,10 @@ def _create_reset_password_url(email, next_redirect, base_url=None): return full_url -def _create_verification_url(user): +def _create_verification_url(user, base_url): data = json.dumps({'user_id': str(user.id), 'email': user.email_address}) url = '/verify-email/' - return url_with_token(data, url, current_app.config) + return url_with_token(data, url, current_app.config, base_url=base_url) def _create_confirmation_url(user, email_address): diff --git a/tests/app/user/test_rest_verify.py b/tests/app/user/test_rest_verify.py index 1078416a6..99e40d6a3 100644 --- a/tests/app/user/test_rest_verify.py +++ b/tests/app/user/test_rest_verify.py @@ -292,15 +292,29 @@ def test_send_sms_code_returns_204_when_too_many_codes_already_created(client, s assert VerifyCode.query.count() == 5 -def test_send_new_user_email_verification(client, - sample_user, - mocker, - email_verification_template): +@pytest.mark.parametrize('post_data, expected_url_starts_with', ( + ( + {}, + 'http://localhost', + ), + ( + {'admin_base_url': 'https://example.com'}, + 'https://example.com', + ), +)) +def test_send_new_user_email_verification( + client, + sample_user, + mocker, + email_verification_template, + post_data, + expected_url_starts_with, +): mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') auth_header = create_admin_authorization_header() resp = client.post( url_for('user.send_new_user_email_verification', user_id=str(sample_user.id)), - data=json.dumps({}), + data=json.dumps(post_data), headers=[('Content-Type', 'application/json'), auth_header]) notify_service = email_verification_template.service assert resp.status_code == 204 @@ -308,6 +322,8 @@ def test_send_new_user_email_verification(client, assert VerifyCode.query.count() == 0 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() + assert notification.personalisation['name'] == 'Test User' + assert notification.personalisation['url'].startswith(expected_url_starts_with) def test_send_email_verification_returns_404_for_bad_input_data(client, notify_db_session, mocker):