mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-24 00:07:02 -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:
@@ -2,7 +2,6 @@ import json
|
||||
import uuid
|
||||
from unittest.mock import Mock
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
from flask import session as flask_session
|
||||
from flask import url_for
|
||||
from itsdangerous import SignatureExpired
|
||||
@@ -12,25 +11,24 @@ from app.main.views.verify import activate_user
|
||||
|
||||
|
||||
def test_should_return_verify_template(
|
||||
client,
|
||||
client_request,
|
||||
api_user_active,
|
||||
mock_send_verify_code,
|
||||
):
|
||||
client_request.logout()
|
||||
# TODO this lives here until we work out how to
|
||||
# reassign the session after it is lost mid register process
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {'email_address': api_user_active['email_address'], 'id': api_user_active['id']}
|
||||
response = client.get(url_for('main.verify'))
|
||||
assert response.status_code == 200
|
||||
page = client_request.get('main.verify')
|
||||
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
assert page.h1.text == 'Check your phone'
|
||||
message = page.select('main p')[0].text
|
||||
assert message == "We’ve sent you a text message with a security code."
|
||||
|
||||
|
||||
def test_should_redirect_to_add_service_when_sms_code_is_correct(
|
||||
client,
|
||||
client_request,
|
||||
api_user_active,
|
||||
mocker,
|
||||
mock_update_user_attribute,
|
||||
@@ -41,25 +39,26 @@ def test_should_redirect_to_add_service_when_sms_code_is_correct(
|
||||
api_user_active['current_session_id'] = str(uuid.UUID(int=1))
|
||||
mocker.patch('app.user_api_client.get_user', return_value=api_user_active)
|
||||
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {'email_address': api_user_active['email_address'], 'id': api_user_active['id']}
|
||||
# user's only just created their account so no session in the cookie
|
||||
session.pop('current_session_id', None)
|
||||
|
||||
response = client.post(url_for('main.verify'),
|
||||
data={'sms_code': '12345'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.add_service', first='first', _external=True)
|
||||
client_request.post(
|
||||
'main.verify',
|
||||
_data={'sms_code': '12345'},
|
||||
_expected_redirect=url_for('main.add_service', first='first', _external=True),
|
||||
)
|
||||
|
||||
# make sure the current_session_id has changed to what the API returned
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
assert session['current_session_id'] == str(uuid.UUID(int=1))
|
||||
|
||||
mock_check_verify_code.assert_called_once_with(api_user_active['id'], '12345', 'sms')
|
||||
|
||||
|
||||
def test_should_activate_user_after_verify(
|
||||
client,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_pending,
|
||||
mock_send_verify_code,
|
||||
@@ -67,14 +66,14 @@ def test_should_activate_user_after_verify(
|
||||
mock_create_event,
|
||||
mock_activate_user,
|
||||
):
|
||||
client_request.logout()
|
||||
mocker.patch('app.user_api_client.get_user', return_value=api_user_pending)
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {
|
||||
'email_address': api_user_pending['email_address'],
|
||||
'id': api_user_pending['id']
|
||||
}
|
||||
client.post(url_for('main.verify'),
|
||||
data={'sms_code': '12345'})
|
||||
client_request.post('main.verify', _data={'sms_code': '12345'})
|
||||
assert mock_activate_user.called
|
||||
|
||||
|
||||
@@ -100,7 +99,7 @@ def test_should_return_200_when_sms_code_is_wrong(
|
||||
|
||||
|
||||
def test_verify_email_redirects_to_verify_if_token_valid(
|
||||
client,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_pending,
|
||||
mock_get_user_pending,
|
||||
@@ -110,67 +109,72 @@ def test_verify_email_redirects_to_verify_if_token_valid(
|
||||
token_data = {"user_id": api_user_pending['id'], "secret_code": 'UNUSED'}
|
||||
mocker.patch('app.main.views.verify.check_token', return_value=json.dumps(token_data))
|
||||
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
session['user_details'] = {
|
||||
'email_address': api_user_pending['email_address'],
|
||||
'id': api_user_pending['id'],
|
||||
}
|
||||
|
||||
response = client.get(url_for('main.verify_email', token='notreal'))
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.verify', _external=True)
|
||||
client_request.get(
|
||||
'main.verify_email',
|
||||
token='notreal',
|
||||
_expected_redirect=url_for('main.verify', _external=True),
|
||||
)
|
||||
|
||||
assert not mock_check_verify_code.called
|
||||
mock_send_verify_code.assert_called_once_with(api_user_pending['id'], 'sms', api_user_pending['mobile_number'])
|
||||
|
||||
with client.session_transaction() as session:
|
||||
with client_request.session_transaction() as session:
|
||||
assert session['user_details'] == {'email': api_user_pending['email_address'], 'id': api_user_pending['id']}
|
||||
|
||||
|
||||
def test_verify_email_redirects_to_email_sent_if_token_expired(
|
||||
client,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_pending,
|
||||
):
|
||||
client_request.logout()
|
||||
mocker.patch('app.main.views.verify.check_token', side_effect=SignatureExpired('expired'))
|
||||
|
||||
response = client.get(url_for('main.verify_email', token='notreal'))
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.resend_email_verification', _external=True)
|
||||
client_request.get(
|
||||
'main.verify_email',
|
||||
token='notreal',
|
||||
_expected_redirect=url_for('main.resend_email_verification', _external=True),
|
||||
)
|
||||
|
||||
|
||||
def test_verify_email_redirects_to_sign_in_if_user_active(
|
||||
client,
|
||||
client_request,
|
||||
mocker,
|
||||
api_user_active,
|
||||
mock_get_user,
|
||||
mock_send_verify_code,
|
||||
mock_check_verify_code,
|
||||
):
|
||||
client_request.logout()
|
||||
token_data = {"user_id": api_user_active['id'], "secret_code": 12345}
|
||||
mocker.patch('app.main.views.verify.check_token', return_value=json.dumps(token_data))
|
||||
|
||||
response = client.get(url_for('main.verify_email', token='notreal'), follow_redirects=True)
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
page = client_request.get('main.verify_email', token='notreal', _follow_redirects=True)
|
||||
|
||||
assert page.h1.text == 'Sign in'
|
||||
flash_banner = page.find('div', class_='banner-dangerous').string.strip()
|
||||
assert flash_banner == "That verification link has expired."
|
||||
|
||||
|
||||
def test_verify_redirects_to_sign_in_if_not_logged_in(
|
||||
client
|
||||
client_request
|
||||
):
|
||||
response = client.get(url_for('main.verify'))
|
||||
|
||||
assert response.location == url_for('main.sign_in', _external=True)
|
||||
assert response.status_code == 302
|
||||
client_request.logout()
|
||||
client_request.get(
|
||||
'main.verify',
|
||||
_expected_redirect=url_for('main.sign_in', _external=True),
|
||||
)
|
||||
|
||||
|
||||
def test_activate_user_redirects_to_service_dashboard_if_user_already_belongs_to_service(
|
||||
mocker,
|
||||
client,
|
||||
client_request,
|
||||
service_one,
|
||||
sample_invite,
|
||||
api_user_active,
|
||||
|
||||
Reference in New Issue
Block a user