mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 21:49:37 -04:00
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:
@@ -1,25 +1,22 @@
|
||||
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,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_active,
|
||||
mock_get_user_by_email,
|
||||
mock_send_verify_email,
|
||||
):
|
||||
with client.session_transaction() as session:
|
||||
client_request.logout()
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {
|
||||
'id': api_user_active['id'],
|
||||
'email': api_user_active['email_address']}
|
||||
response = client.get(url_for('main.resend_email_verification'))
|
||||
assert response.status_code == 200
|
||||
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
page = client_request.get('main.resend_email_verification')
|
||||
|
||||
assert page.h1.string == 'Check your email'
|
||||
expected = "A new confirmation email has been sent to {}".format(api_user_active['email_address'])
|
||||
@@ -34,20 +31,19 @@ def test_should_render_email_verification_resend_show_email_address_and_resend_v
|
||||
f'/services/{SERVICE_ONE_ID}/templates',
|
||||
])
|
||||
def test_should_render_correct_resend_template_for_active_user(
|
||||
client,
|
||||
client_request,
|
||||
api_user_active,
|
||||
mock_get_user_by_email,
|
||||
mock_send_verify_code,
|
||||
redirect_url
|
||||
):
|
||||
with client.session_transaction() as session:
|
||||
client_request.logout()
|
||||
with client_request.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', next=redirect_url))
|
||||
assert response.status_code == 200
|
||||
page = client_request.get('main.check_and_resend_text_code', next=redirect_url)
|
||||
|
||||
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
|
||||
@@ -58,22 +54,20 @@ def test_should_render_correct_resend_template_for_active_user(
|
||||
|
||||
|
||||
def test_should_render_correct_resend_template_for_pending_user(
|
||||
client,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_pending,
|
||||
mock_send_verify_code,
|
||||
):
|
||||
|
||||
client_request.logout()
|
||||
mocker.patch('app.user_api_client.get_user_by_email', return_value=api_user_pending)
|
||||
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {
|
||||
'id': api_user_pending['id'],
|
||||
'email': api_user_pending['email_address']}
|
||||
response = client.get(url_for('main.check_and_resend_text_code'))
|
||||
assert response.status_code == 200
|
||||
page = client_request.get('main.check_and_resend_text_code')
|
||||
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
assert page.h1.string == 'Check your mobile number'
|
||||
|
||||
expected = 'Check your mobile phone number is correct and then resend the security code.'
|
||||
@@ -91,7 +85,7 @@ def test_should_render_correct_resend_template_for_pending_user(
|
||||
'+1800-555-555',
|
||||
])
|
||||
def test_should_resend_verify_code_and_update_mobile_for_pending_user(
|
||||
client,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_pending,
|
||||
mock_update_user_attribute,
|
||||
@@ -99,16 +93,21 @@ def test_should_resend_verify_code_and_update_mobile_for_pending_user(
|
||||
phone_number_to_register_with,
|
||||
redirect_url
|
||||
):
|
||||
client_request.logout()
|
||||
mock_update_user_attribute.reset_mock()
|
||||
mocker.patch('app.user_api_client.get_user_by_email', return_value=api_user_pending)
|
||||
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
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', 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, next=redirect_url)
|
||||
|
||||
client_request.post(
|
||||
'main.check_and_resend_text_code',
|
||||
next=redirect_url,
|
||||
_data={'mobile_number': phone_number_to_register_with},
|
||||
_expected_redirect=url_for('main.verify', _external=True, next=redirect_url),
|
||||
)
|
||||
|
||||
mock_update_user_attribute.assert_called_once_with(
|
||||
api_user_pending['id'],
|
||||
@@ -126,19 +125,22 @@ def test_should_resend_verify_code_and_update_mobile_for_pending_user(
|
||||
f'/services/{SERVICE_ONE_ID}/templates',
|
||||
])
|
||||
def test_check_and_redirect_to_two_factor_if_user_active(
|
||||
client,
|
||||
client_request,
|
||||
api_user_active,
|
||||
mock_get_user_by_email,
|
||||
mock_send_verify_code,
|
||||
redirect_url
|
||||
):
|
||||
with client.session_transaction() as session:
|
||||
client_request.logout()
|
||||
with client_request.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_verification_code', next=redirect_url))
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.two_factor_sms', _external=True, next=redirect_url)
|
||||
client_request.get(
|
||||
'main.check_and_resend_verification_code',
|
||||
next=redirect_url,
|
||||
_expected_redirect=url_for('main.two_factor_sms', _external=True, next=redirect_url)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('redirect_url', [
|
||||
@@ -146,23 +148,26 @@ def test_check_and_redirect_to_two_factor_if_user_active(
|
||||
f'/services/{SERVICE_ONE_ID}/templates',
|
||||
])
|
||||
def test_check_and_redirect_to_verify_if_user_pending(
|
||||
client,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_pending,
|
||||
mock_get_user_pending,
|
||||
mock_send_verify_code,
|
||||
redirect_url
|
||||
):
|
||||
|
||||
client_request.logout()
|
||||
mocker.patch('app.user_api_client.get_user_by_email', return_value=api_user_pending)
|
||||
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {
|
||||
'id': api_user_pending['id'],
|
||||
'email': api_user_pending['email_address']}
|
||||
response = client.get(url_for('main.check_and_resend_verification_code', next=redirect_url))
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.verify', _external=True, next=redirect_url)
|
||||
|
||||
client_request.get(
|
||||
'main.check_and_resend_verification_code',
|
||||
next=redirect_url,
|
||||
_expected_redirect=url_for('main.verify', _external=True, next=redirect_url),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('endpoint', [
|
||||
@@ -171,13 +176,14 @@ def test_check_and_redirect_to_verify_if_user_pending(
|
||||
'main.check_and_resend_verification_code',
|
||||
])
|
||||
def test_redirect_to_sign_in_if_not_logged_in(
|
||||
client,
|
||||
client_request,
|
||||
endpoint,
|
||||
):
|
||||
response = client.get(url_for(endpoint))
|
||||
|
||||
assert response.location == url_for('main.sign_in', _external=True)
|
||||
assert response.status_code == 302
|
||||
client_request.logout()
|
||||
client_request.get(
|
||||
endpoint,
|
||||
_expected_redirect=url_for('main.sign_in', _external=True),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('redirect_url', [
|
||||
@@ -185,20 +191,19 @@ def test_redirect_to_sign_in_if_not_logged_in(
|
||||
f'/services/{SERVICE_ONE_ID}/templates',
|
||||
])
|
||||
def test_should_render_correct_email_not_received_template_for_active_user(
|
||||
client,
|
||||
client_request,
|
||||
api_user_active,
|
||||
mock_get_user_by_email,
|
||||
mock_send_verify_code,
|
||||
redirect_url
|
||||
):
|
||||
with client.session_transaction() as session:
|
||||
client_request.logout()
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {
|
||||
'id': api_user_active['id'],
|
||||
'email': api_user_active['email_address']}
|
||||
response = client.get(url_for('main.email_not_received', next=redirect_url))
|
||||
assert response.status_code == 200
|
||||
page = client_request.get('main.email_not_received', next=redirect_url)
|
||||
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
assert page.h1.string == 'Resend email link'
|
||||
# there shouldn't be a form for updating mobile number
|
||||
assert page.find('form') is None
|
||||
|
||||
Reference in New Issue
Block a user