Replace uses of client.get and client.post

We have a `client_request` fixture which does a bunch of useful stuff
like:
- checking the status code of the response
- returning a `BeautifulSoup` object

Lots of our tests still use an older fixture called `client`. This is
not as good because it:
- returns a raw `Response` object
- doesn’t do the additional checks
- means our tests contain a lot of repetetive boilerplate like `page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')`

This commit converts all the tests which had a `client.get(…)` or
`client.post(…)` statement to use their equivalents on `client_request`
instead.

Subsequent commits will remove uses of `client` in other tests, but
doing it this way means the work can be broken up into more manageable
chunks.
This commit is contained in:
Chris Hill-Scott
2022-01-04 15:40:42 +00:00
parent 07318b2d11
commit 7e707db4b2
24 changed files with 951 additions and 726 deletions

View File

@@ -1,14 +1,14 @@
import pytest
from bs4 import BeautifulSoup
from flask import Response, url_for
from flask_wtf.csrf import CSRFError
from notifications_python_client.errors import HTTPError
def test_bad_url_returns_page_not_found(client):
response = client.get('/bad_url')
assert response.status_code == 404
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
def test_bad_url_returns_page_not_found(client_request):
page = client_request.get_url(
'/bad_url',
_expected_status=404,
)
assert page.h1.string.strip() == 'Page not found'
assert page.title.string.strip() == 'Page not found GOV.UK Notify'
@@ -55,20 +55,19 @@ def test_csrf_returns_400(client_request, mocker):
assert page.title.string.strip() == 'Sorry, theres a problem with the service GOV.UK Notify'
def test_csrf_redirects_to_sign_in_page_if_not_signed_in(client, mocker):
def test_csrf_redirects_to_sign_in_page_if_not_signed_in(client_request, mocker):
csrf_err = CSRFError('400 Bad Request: The CSRF tokens do not match.')
mocker.patch('app.main.views.index.render_template', side_effect=csrf_err)
response = client.get('/cookies')
assert response.status_code == 302
assert response.location == url_for('main.sign_in', next='/cookies', _external=True)
client_request.logout()
client_request.get_url(
'/cookies',
_expected_redirect=url_for('main.sign_in', next='/cookies', _external=True),
)
def test_405_returns_something_went_wrong_page(client, mocker):
response = client.post('/')
def test_405_returns_something_went_wrong_page(client_request, mocker):
page = client_request.post_url('/', _expected_status=405)
assert response.status_code == 405
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.h1.string.strip() == 'Sorry, theres a problem with GOV.UK Notify'
assert page.title.string.strip() == 'Sorry, theres a problem with the service GOV.UK Notify'