Turn on redirects for check_and_resend_text_code

This is part of the work to make sure user is redirected
to the page they initially were meant to visit after
they sign in.
This commit is contained in:
Pea Tyczynska
2020-10-09 11:39:01 +01:00
parent d089424501
commit c3b7481e11
3 changed files with 22 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
from flask import redirect, render_template, session, url_for
from flask import redirect, render_template, request, session, url_for
from app import user_api_client
from app.main import main
@@ -19,16 +19,17 @@ def resend_email_verification():
@redirect_to_sign_in
def check_and_resend_text_code():
user = User.from_email_address(session['user_details']['email'])
redirect_url = request.args.get('next')
if user.state == 'active':
# this is a verified user and therefore redirect to page to request resend without edit mobile
return render_template('views/verification-not-received.html')
return render_template('views/verification-not-received.html', redirect_url=redirect_url)
form = TextNotReceivedForm(mobile_number=user.mobile_number)
if form.validate_on_submit():
user.send_verify_code(to=form.mobile_number.data)
user.update(mobile_number=form.mobile_number.data)
return redirect(url_for('.verify'))
return redirect(url_for('.verify', next=redirect_url))
return render_template('views/text-not-received.html', form=form)

View File

@@ -16,7 +16,7 @@
{{ govukButton({
"element": "a",
"text": "Resend security code",
"href": url_for('main.check_and_resend_verification_code')
"href": url_for('main.check_and_resend_verification_code', next=redirect_url)
}) }}
</p>

View File

@@ -27,23 +27,32 @@ def test_should_render_email_verification_resend_show_email_address_and_resend_v
mock_send_verify_email.assert_called_with(api_user_active['id'], api_user_active['email_address'])
@pytest.mark.parametrize('redirect_url', [
None,
'blob',
])
def test_should_render_correct_resend_template_for_active_user(
client,
api_user_active,
mock_get_user_by_email,
mock_send_verify_code,
redirect_url
):
with client.session_transaction() as session:
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
response = client.get(url_for('main.check_and_resend_text_code'))
response = client.get(url_for('main.check_and_resend_text_code', next=redirect_url))
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.h1.string == 'Resend security code'
# there shouldn't be a form for updating mobile number
assert page.find('form') is None
assert page.find('a', class_="govuk-button")['href'] == url_for(
'main.check_and_resend_verification_code',
next=redirect_url
)
def test_should_render_correct_resend_template_for_pending_user(
@@ -71,6 +80,10 @@ def test_should_render_correct_resend_template_for_pending_user(
assert page.find('form').input['value'] == api_user_pending['mobile_number']
@pytest.mark.parametrize('redirect_url', [
None,
'blob',
])
@pytest.mark.parametrize('phone_number_to_register_with', [
'+447700900460',
'+1800-555-555',
@@ -82,6 +95,7 @@ def test_should_resend_verify_code_and_update_mobile_for_pending_user(
mock_update_user_attribute,
mock_send_verify_code,
phone_number_to_register_with,
redirect_url
):
mocker.patch('app.user_api_client.get_user_by_email', return_value=api_user_pending)
@@ -89,10 +103,10 @@ def test_should_resend_verify_code_and_update_mobile_for_pending_user(
session['user_details'] = {
'id': api_user_pending['id'],
'email': api_user_pending['email_address']}
response = client.post(url_for('main.check_and_resend_text_code'),
response = client.post(url_for('main.check_and_resend_text_code', next=redirect_url),
data={'mobile_number': phone_number_to_register_with})
assert response.status_code == 302
assert response.location == url_for('main.verify', _external=True)
assert response.location == url_for('main.verify', _external=True, next=redirect_url)
mock_update_user_attribute.assert_called_once_with(
api_user_pending['id'],