From 65787191039e72a60a01a7a606525880d8e1c381 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Mon, 12 Oct 2020 12:01:39 +0100 Subject: [PATCH] Test next redirects with realistic URL This change has been made following PR review, to ensure that special signs are transformed correctly when passing through the next URL. --- tests/app/main/views/test_code_not_received.py | 12 +++++++----- tests/app/main/views/test_forgot_password.py | 5 +++-- tests/app/main/views/test_new_password.py | 4 ++-- tests/app/main/views/test_sign_in.py | 16 ++++++++++------ tests/app/main/views/test_two_factor.py | 8 ++++---- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/tests/app/main/views/test_code_not_received.py b/tests/app/main/views/test_code_not_received.py index 6e8a2b245..11405790f 100644 --- a/tests/app/main/views/test_code_not_received.py +++ b/tests/app/main/views/test_code_not_received.py @@ -2,6 +2,8 @@ import pytest from bs4 import BeautifulSoup from flask import url_for +from tests.conftest import SERVICE_ONE_ID + def test_should_render_email_verification_resend_show_email_address_and_resend_verify_email( client, @@ -29,7 +31,7 @@ def test_should_render_email_verification_resend_show_email_address_and_resend_v @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_should_render_correct_resend_template_for_active_user( client, @@ -82,7 +84,7 @@ def test_should_render_correct_resend_template_for_pending_user( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) @pytest.mark.parametrize('phone_number_to_register_with', [ '+447700900460', @@ -121,7 +123,7 @@ def test_should_resend_verify_code_and_update_mobile_for_pending_user( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_check_and_redirect_to_two_factor_if_user_active( client, @@ -141,7 +143,7 @@ def test_check_and_redirect_to_two_factor_if_user_active( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_check_and_redirect_to_verify_if_user_pending( client, @@ -180,7 +182,7 @@ def test_redirect_to_sign_in_if_not_logged_in( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_should_render_correct_email_not_received_template_for_active_user( client, diff --git a/tests/app/main/views/test_forgot_password.py b/tests/app/main/views/test_forgot_password.py index dc7d4fd7e..f3a106da6 100644 --- a/tests/app/main/views/test_forgot_password.py +++ b/tests/app/main/views/test_forgot_password.py @@ -4,6 +4,7 @@ from notifications_python_client.errors import HTTPError import app from tests import user_json +from tests.conftest import SERVICE_ONE_ID def test_should_render_forgot_password(client): @@ -42,11 +43,11 @@ def test_forgot_password_sends_next_link_with_reset_password_email_request( sample_user = user_json(email_address='test@user.gov.uk') mocker.patch('app.user_api_client.send_reset_password_url', return_value=None) response = client.post( - url_for('.forgot_password') + "?next=blob", + url_for('.forgot_password') + f"?next=/services/{SERVICE_ONE_ID}/templates", data={'email_address': sample_user['email_address']}) assert response.status_code == 200 app.user_api_client.send_reset_password_url.assert_called_once_with( - sample_user['email_address'], next_string="blob" + sample_user['email_address'], next_string=f'/services/{SERVICE_ONE_ID}/templates' ) diff --git a/tests/app/main/views/test_new_password.py b/tests/app/main/views/test_new_password.py index e7641438d..867c25a6b 100644 --- a/tests/app/main/views/test_new_password.py +++ b/tests/app/main/views/test_new_password.py @@ -6,7 +6,7 @@ from flask import url_for from itsdangerous import SignatureExpired from notifications_utils.url_safe_token import generate_token -from tests.conftest import url_for_endpoint_with_token +from tests.conftest import SERVICE_ONE_ID, url_for_endpoint_with_token def test_should_render_new_password_template( @@ -39,7 +39,7 @@ def test_should_return_404_when_email_address_does_not_exist( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_should_redirect_to_two_factor_when_password_reset_is_successful( app_, diff --git a/tests/app/main/views/test_sign_in.py b/tests/app/main/views/test_sign_in.py index e22d929db..29a7d4d05 100644 --- a/tests/app/main/views/test_sign_in.py +++ b/tests/app/main/views/test_sign_in.py @@ -5,7 +5,7 @@ from bs4 import BeautifulSoup from flask import url_for from app.models.user import User -from tests.conftest import normalize_spaces +from tests.conftest import SERVICE_ONE_ID, normalize_spaces def test_render_sign_in_template_for_new_user( @@ -31,10 +31,14 @@ def test_render_sign_in_template_with_next_link_for_password_reset( client_request ): client_request.logout() - page = client_request.get('main.sign_in', _optional_args="?next=blob", _test_page_title=False) + page = client_request.get( + 'main.sign_in', + _optional_args=f"?next=/services/{SERVICE_ONE_ID}/templates", + _test_page_title=False + ) forgot_password_link = page.find('a', class_="govuk-link govuk-link--no-visited-state page-footer-secondary-link") assert forgot_password_link.text == 'Forgotten your password?' - assert forgot_password_link['href'] == url_for('main.forgot_password') + "?next=blob" + assert forgot_password_link['href'] == url_for('main.forgot_password', next=f'/services/{SERVICE_ONE_ID}/templates') def test_sign_in_explains_session_timeout(client): @@ -104,7 +108,7 @@ def test_logged_in_user_redirects_to_account( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) @pytest.mark.parametrize('email_address, password', [ ('valid@example.gov.uk', 'val1dPassw0rd!'), @@ -133,7 +137,7 @@ def test_process_sms_auth_sign_in_return_2fa_template( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_process_email_auth_sign_in_return_2fa_template( client, @@ -197,7 +201,7 @@ def test_should_return_redirect_when_user_is_pending( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_should_attempt_redirect_when_user_is_pending( client, diff --git a/tests/app/main/views/test_two_factor.py b/tests/app/main/views/test_two_factor.py index a6f6b381a..64204a870 100644 --- a/tests/app/main/views/test_two_factor.py +++ b/tests/app/main/views/test_two_factor.py @@ -12,7 +12,7 @@ from tests.conftest import ( @pytest.mark.parametrize('request_url', ['two_factor_email_sent', 'revalidate_email_sent']) -@pytest.mark.parametrize('redirect_url', [None, 'blob']) +@pytest.mark.parametrize('redirect_url', [None, f'/services/{SERVICE_ONE_ID}/templates']) @pytest.mark.parametrize('email_resent, page_title', [ (None, 'Check your email'), (True, 'Email resent') @@ -38,7 +38,7 @@ def test_two_factor_email_sent_page( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_should_render_two_factor_page( client, @@ -353,7 +353,7 @@ def test_valid_two_factor_email_link_logs_in_user( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_two_factor_email_link_has_expired( app_, @@ -397,7 +397,7 @@ def test_two_factor_email_link_is_invalid( @pytest.mark.parametrize('redirect_url', [ None, - 'blob', + f'/services/{SERVICE_ONE_ID}/templates', ]) def test_two_factor_email_link_is_already_used( client,