Files
notifications-admin/tests/app/main/views/test_add_service.py
Chris Hill-Scott d5656a4dc2 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 ¯\_(ツ)_/¯
2016-02-05 15:11:44 +00:00

68 lines
3.5 KiB
Python

from flask import url_for
from app.main.dao import services_dao
def test_get_should_render_add_service_template(app_,
api_user_active,
mock_login,
mock_get_service,
mock_get_services,
mock_get_user,
mock_get_user_by_email):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
response = client.get(url_for('main.add_service'))
assert response.status_code == 200
assert 'Add a new service' in response.get_data(as_text=True)
def test_should_add_service_and_redirect_to_next_page(app_,
mock_login,
mock_create_service,
mock_get_services,
api_user_active,
mock_get_user,
mock_get_user_by_email):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
response = client.post(
url_for('main.add_service'),
data={'name': 'testing the post'})
assert response.status_code == 302
assert response.location == url_for('main.service_dashboard', service_id=101, _external=True)
assert mock_create_service.called
assert mock_get_services.called
def test_should_return_form_errors_when_service_name_is_empty(app_,
api_user_active,
mock_get_service,
mock_get_services,
mock_get_user,
mock_get_user_by_email,
mock_login):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
response = client.post(url_for('main.add_service'), data={})
assert response.status_code == 200
assert 'Service name can not be empty' in response.get_data(as_text=True)
def test_should_return_form_errors_with_duplicate_service_name(app_,
mock_login,
mock_get_services,
mock_get_user,
api_user_active,
mock_get_user_by_email):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
response = client.post(
url_for('main.add_service'), data={'name': 'service_one'})
assert response.status_code == 200
assert 'Service name already exists' in response.get_data(as_text=True)
assert mock_get_services.called