Add mocked get_services to test_client.login

Because the redirect after logging in checks the number of services a user has,
this now needs to be mocked.

Right now this means adding `mock_get_login` to any tests that need a login.
This must be one of the first mocks, so that it can be overridden by any use
of `mock_get_services`, for tests that specifically want to rely on a quantity
of mocked services, or their contents.

This is a bit fragile, but there’s already a TODO in the code to make it better
so ¯\_(ツ)_/¯
This commit is contained in:
Chris Hill-Scott
2016-02-05 15:08:36 +00:00
parent 939954cd64
commit d5656a4dc2
4 changed files with 27 additions and 13 deletions

View File

@@ -4,11 +4,11 @@ from app.main.dao import services_dao
def test_get_should_render_add_service_template(app_, def test_get_should_render_add_service_template(app_,
api_user_active, api_user_active,
mock_login,
mock_get_service, mock_get_service,
mock_get_services, mock_get_services,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email):
mock_login):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active)
@@ -18,12 +18,12 @@ def test_get_should_render_add_service_template(app_,
def test_should_add_service_and_redirect_to_next_page(app_, def test_should_add_service_and_redirect_to_next_page(app_,
mock_login,
mock_create_service, mock_create_service,
mock_get_services, mock_get_services,
api_user_active, api_user_active,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email):
mock_login):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active)
@@ -52,11 +52,11 @@ def test_should_return_form_errors_when_service_name_is_empty(app_,
def test_should_return_form_errors_with_duplicate_service_name(app_, def test_should_return_form_errors_with_duplicate_service_name(app_,
mock_login,
mock_get_services, mock_get_services,
mock_get_user, mock_get_user,
api_user_active, api_user_active,
mock_get_user_by_email, mock_get_user_by_email):
mock_login):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active)

View File

@@ -4,10 +4,10 @@ import pytest
def test_should_show_choose_services_page(app_, def test_should_show_choose_services_page(app_,
mock_login,
mock_get_user, mock_get_user,
api_user_active, api_user_active,
mock_get_services, mock_get_services):
mock_login):
with app_.test_request_context(): with app_.test_request_context():
with app_.test_client() as client: with app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active)

View File

@@ -6,6 +6,7 @@ import moto
def test_choose_sms_template(app_, def test_choose_sms_template(app_,
api_user_active, api_user_active,
mock_login,
mock_get_user, mock_get_user,
mock_get_service_templates, mock_get_service_templates,
mock_check_verify_code, mock_check_verify_code,
@@ -25,6 +26,7 @@ def test_choose_sms_template(app_,
def test_upload_empty_csvfile_returns_to_upload_page(app_, def test_upload_empty_csvfile_returns_to_upload_page(app_,
api_user_active, api_user_active,
mock_login,
mock_get_user, mock_get_user,
mock_get_service_templates, mock_get_service_templates,
mock_check_verify_code, mock_check_verify_code,
@@ -45,9 +47,9 @@ def test_upload_empty_csvfile_returns_to_upload_page(app_,
def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors(app_, def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors(app_,
mocker, mocker,
api_user_active, api_user_active,
mock_login,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email,
mock_login,
mock_get_service_template): mock_get_service_template):
contents = 'phone\n+44 123\n+44 456' contents = 'phone\n+44 123\n+44 456'
@@ -72,9 +74,9 @@ def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors(app_,
def test_upload_csvfile_with_valid_phone_shows_all_numbers(app_, def test_upload_csvfile_with_valid_phone_shows_all_numbers(app_,
mocker, mocker,
api_user_active, api_user_active,
mock_login,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email,
mock_login,
mock_get_service_template): mock_get_service_template):
contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986' # noqa contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986' # noqa

View File

@@ -398,11 +398,23 @@ def mock_get_no_api_keys(mocker):
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def mock_login(mocker, mock_get_user, mock_update_user): def mock_login(mocker, mock_get_user, mock_update_user):
def _verify_code(user_id, code, code_type): def _verify_code(user_id, code, code_type):
return True, '' return True, ''
return mocker.patch(
'app.user_api_client.check_verify_code', def _no_services(user_id=None):
side_effect=_verify_code) return {'data': []}
return (
mocker.patch(
'app.user_api_client.check_verify_code',
side_effect=_verify_code
),
mocker.patch(
'app.notifications_api_client.get_services',
side_effect=_no_services
)
)
@pytest.fixture(scope='function') @pytest.fixture(scope='function')