Allow admin app to specify domain for registration email

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.
This commit is contained in:
Chris Hill-Scott
2022-03-07 15:03:46 +00:00
parent 520d621893
commit c2b6a9df80
2 changed files with 29 additions and 8 deletions

View File

@@ -374,6 +374,8 @@ def send_user_confirm_new_email(user_id):
@user_blueprint.route('/<uuid:user_id>/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):

View File

@@ -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,
@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):
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):