From c3b7481e119b89693141dc59cd4451e17ccbc2a1 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Fri, 9 Oct 2020 11:39:01 +0100 Subject: [PATCH] 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. --- app/main/views/code_not_received.py | 7 ++++--- .../views/verification-not-received.html | 2 +- .../app/main/views/test_code_not_received.py | 20 ++++++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/main/views/code_not_received.py b/app/main/views/code_not_received.py index ac51b3be1..5b75325b1 100644 --- a/app/main/views/code_not_received.py +++ b/app/main/views/code_not_received.py @@ -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) diff --git a/app/templates/views/verification-not-received.html b/app/templates/views/verification-not-received.html index 5b15d35dc..085b9c210 100644 --- a/app/templates/views/verification-not-received.html +++ b/app/templates/views/verification-not-received.html @@ -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) }) }}

diff --git a/tests/app/main/views/test_code_not_received.py b/tests/app/main/views/test_code_not_received.py index 23712125e..9fd5452e3 100644 --- a/tests/app/main/views/test_code_not_received.py +++ b/tests/app/main/views/test_code_not_received.py @@ -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'],