2017-11-01 15:47:05 +00:00
|
|
|
|
import pytest
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from flask import Response, url_for
|
|
|
|
|
|
from flask_wtf.csrf import CSRFError
|
2017-10-30 16:59:24 +00:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2016-03-11 10:16:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-04 15:40:42 +00:00
|
|
|
|
def test_bad_url_returns_page_not_found(client_request):
|
|
|
|
|
|
page = client_request.get_url(
|
|
|
|
|
|
'/bad_url',
|
|
|
|
|
|
_expected_status=404,
|
|
|
|
|
|
)
|
2019-11-12 17:06:06 +00:00
|
|
|
|
assert page.h1.string.strip() == 'Page not found'
|
2019-11-18 17:04:38 +00:00
|
|
|
|
assert page.title.string.strip() == 'Page not found – GOV.UK Notify'
|
2017-10-30 16:59:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_service_before_request_handles_404(client_request, mocker):
|
|
|
|
|
|
exc = HTTPError(Response(status=404), 'Not found')
|
|
|
|
|
|
get_service = mocker.patch('app.service_api_client.get_service', side_effect=exc)
|
|
|
|
|
|
|
|
|
|
|
|
client_request.get(
|
|
|
|
|
|
'main.service_dashboard',
|
|
|
|
|
|
service_id='00000000-0000-0000-0000-000000000000',
|
|
|
|
|
|
_expected_status=404
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
get_service.assert_called_once_with('00000000-0000-0000-0000-000000000000')
|
2017-11-01 16:43:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-11-01 15:47:05 +00:00
|
|
|
|
@pytest.mark.parametrize('url', [
|
|
|
|
|
|
'/new-password/MALFORMED_TOKEN',
|
|
|
|
|
|
'/user-profile/email/confirm/MALFORMED_TOKEN',
|
|
|
|
|
|
'/verify-email/MALFORMED_TOKEN'
|
|
|
|
|
|
])
|
2021-12-31 12:08:14 +00:00
|
|
|
|
def test_malformed_token_returns_page_not_found(client_request, url):
|
|
|
|
|
|
page = client_request.get_url(url, _expected_status=404)
|
2017-11-01 15:47:05 +00:00
|
|
|
|
|
2019-11-12 17:06:06 +00:00
|
|
|
|
assert page.h1.string.strip() == 'Page not found'
|
2017-11-01 15:47:05 +00:00
|
|
|
|
flash_banner = page.find('div', class_='banner-dangerous').string.strip()
|
|
|
|
|
|
assert flash_banner == "There’s something wrong with the link you’ve used."
|
2019-11-18 17:04:38 +00:00
|
|
|
|
assert page.title.string.strip() == 'Page not found – GOV.UK Notify'
|
2017-11-28 12:11:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
2021-12-31 12:08:14 +00:00
|
|
|
|
def test_csrf_returns_400(client_request, mocker):
|
2017-11-28 12:11:29 +00:00
|
|
|
|
# we turn off CSRF handling for tests, so fake a CSRF response here.
|
|
|
|
|
|
csrf_err = CSRFError('400 Bad Request: The CSRF tokens do not match.')
|
|
|
|
|
|
mocker.patch('app.main.views.index.render_template', side_effect=csrf_err)
|
|
|
|
|
|
|
2021-12-31 12:08:14 +00:00
|
|
|
|
page = client_request.get_url(
|
|
|
|
|
|
'/cookies',
|
|
|
|
|
|
_expected_status=400,
|
|
|
|
|
|
_test_page_title=False,
|
|
|
|
|
|
)
|
2017-11-28 12:11:29 +00:00
|
|
|
|
|
2020-02-20 17:46:48 +00:00
|
|
|
|
assert page.h1.string.strip() == 'Sorry, there’s a problem with GOV.UK Notify'
|
|
|
|
|
|
assert page.title.string.strip() == 'Sorry, there’s a problem with the service – GOV.UK Notify'
|
2017-11-28 12:11:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-04 15:40:42 +00:00
|
|
|
|
def test_csrf_redirects_to_sign_in_page_if_not_signed_in(client_request, mocker):
|
2017-11-28 12:11:29 +00:00
|
|
|
|
csrf_err = CSRFError('400 Bad Request: The CSRF tokens do not match.')
|
|
|
|
|
|
mocker.patch('app.main.views.index.render_template', side_effect=csrf_err)
|
|
|
|
|
|
|
2022-01-04 15:40:42 +00:00
|
|
|
|
client_request.logout()
|
|
|
|
|
|
client_request.get_url(
|
|
|
|
|
|
'/cookies',
|
|
|
|
|
|
_expected_redirect=url_for('main.sign_in', next='/cookies', _external=True),
|
|
|
|
|
|
)
|
2018-06-22 17:36:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-04 15:40:42 +00:00
|
|
|
|
def test_405_returns_something_went_wrong_page(client_request, mocker):
|
|
|
|
|
|
page = client_request.post_url('/', _expected_status=405)
|
2018-06-22 17:36:58 +01:00
|
|
|
|
|
2020-02-20 17:46:48 +00:00
|
|
|
|
assert page.h1.string.strip() == 'Sorry, there’s a problem with GOV.UK Notify'
|
|
|
|
|
|
assert page.title.string.strip() == 'Sorry, there’s a problem with the service – GOV.UK Notify'
|