2017-10-05 10:46:55 +01:00
|
|
|
|
import pytest
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from flask import session, url_for
|
2017-08-07 11:30:25 +01:00
|
|
|
|
|
2016-10-28 10:48:29 +01:00
|
|
|
|
from app.utils import is_gov_user
|
2016-03-31 15:17:05 +01:00
|
|
|
|
|
2015-12-14 17:12:28 +00:00
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_non_gov_user_cannot_see_add_service_button(
|
|
|
|
|
|
client,
|
|
|
|
|
|
mock_login,
|
|
|
|
|
|
mock_get_non_govuser,
|
|
|
|
|
|
api_nongov_user_active,
|
2018-03-08 16:51:53 +00:00
|
|
|
|
mock_get_organisations_and_services_for_user
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2016-10-30 09:20:53 +00:00
|
|
|
|
client.login(api_nongov_user_active)
|
2018-03-08 16:51:53 +00:00
|
|
|
|
response = client.get(url_for('main.choose_account'))
|
2016-10-30 09:20:53 +00:00
|
|
|
|
assert 'Add a new service' not in response.get_data(as_text=True)
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_get_should_render_add_service_template(
|
2017-08-08 11:13:08 +01:00
|
|
|
|
logged_in_client
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = logged_in_client.get(url_for('main.add_service'))
|
|
|
|
|
|
assert response.status_code == 200
|
2017-10-04 11:49:32 +01:00
|
|
|
|
assert 'About your service' in response.get_data(as_text=True)
|
2015-12-14 17:12:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_add_service_and_redirect_to_tour_when_no_services(
|
|
|
|
|
|
app_,
|
2017-02-03 12:07:21 +00:00
|
|
|
|
logged_in_client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_create_service,
|
|
|
|
|
|
mock_create_service_template,
|
|
|
|
|
|
mock_get_services_with_no_services,
|
|
|
|
|
|
api_user_active,
|
2017-10-31 11:22:57 +00:00
|
|
|
|
mock_create_or_update_free_sms_fragment_limit,
|
2018-09-03 11:46:41 +01:00
|
|
|
|
mock_get_all_email_branding,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = logged_in_client.post(
|
|
|
|
|
|
url_for('main.add_service'),
|
2017-10-04 11:49:32 +01:00
|
|
|
|
data={
|
|
|
|
|
|
'name': 'testing the post',
|
|
|
|
|
|
'organisation_type': 'local',
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2017-02-03 12:07:21 +00:00
|
|
|
|
assert mock_get_services_with_no_services.called
|
|
|
|
|
|
mock_create_service.assert_called_once_with(
|
2018-04-19 14:01:45 +01:00
|
|
|
|
service_name='testing the post',
|
|
|
|
|
|
organisation_type='local',
|
|
|
|
|
|
message_limit=app_.config['DEFAULT_SERVICE_LIMIT'],
|
|
|
|
|
|
restricted=True,
|
|
|
|
|
|
user_id=api_user_active.id,
|
|
|
|
|
|
email_from='testing.the.post'
|
2017-02-03 12:07:21 +00:00
|
|
|
|
)
|
2017-03-09 10:30:19 +00:00
|
|
|
|
mock_create_service_template.assert_called_once_with(
|
|
|
|
|
|
'Example text message template',
|
|
|
|
|
|
'sms',
|
|
|
|
|
|
(
|
|
|
|
|
|
'Hey ((name)), I’m trying out Notify. Today is '
|
|
|
|
|
|
'((day of week)) and my favourite colour is ((colour)).'
|
|
|
|
|
|
),
|
|
|
|
|
|
101,
|
|
|
|
|
|
)
|
2017-02-03 12:07:21 +00:00
|
|
|
|
assert session['service_id'] == 101
|
|
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
|
assert response.location == url_for(
|
2017-06-29 10:02:53 +01:00
|
|
|
|
'main.start_tour',
|
2017-02-03 12:07:21 +00:00
|
|
|
|
service_id=101,
|
2017-06-29 10:02:53 +01:00
|
|
|
|
template_id="Example%20text%20message%20template",
|
2017-02-03 12:07:21 +00:00
|
|
|
|
_external=True
|
|
|
|
|
|
)
|
2017-12-04 16:03:11 +00:00
|
|
|
|
mock_create_or_update_free_sms_fragment_limit.assert_called_once_with(101, 25000)
|
2015-12-15 10:17:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-10-05 10:46:55 +01:00
|
|
|
|
@pytest.mark.parametrize('organisation_type, free_allowance', [
|
|
|
|
|
|
('central', 250 * 1000),
|
|
|
|
|
|
('local', 25 * 1000),
|
|
|
|
|
|
('nhs', 25 * 1000),
|
|
|
|
|
|
])
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_add_service_and_redirect_to_dashboard_when_existing_service(
|
|
|
|
|
|
app_,
|
2017-02-03 12:07:21 +00:00
|
|
|
|
logged_in_client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_create_service,
|
|
|
|
|
|
mock_create_service_template,
|
|
|
|
|
|
mock_get_services,
|
2018-09-03 16:07:56 +01:00
|
|
|
|
mock_update_service,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
api_user_active,
|
2017-10-05 10:46:55 +01:00
|
|
|
|
organisation_type,
|
|
|
|
|
|
free_allowance,
|
2018-09-03 11:46:41 +01:00
|
|
|
|
mock_create_or_update_free_sms_fragment_limit,
|
|
|
|
|
|
mock_get_all_email_branding,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = logged_in_client.post(
|
|
|
|
|
|
url_for('main.add_service'),
|
2017-10-04 11:49:32 +01:00
|
|
|
|
data={
|
|
|
|
|
|
'name': 'testing the post',
|
2017-10-05 10:46:55 +01:00
|
|
|
|
'organisation_type': organisation_type,
|
2017-10-04 11:49:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
)
|
2017-02-03 12:07:21 +00:00
|
|
|
|
assert mock_get_services.called
|
|
|
|
|
|
mock_create_service.assert_called_once_with(
|
2018-04-19 14:01:45 +01:00
|
|
|
|
service_name='testing the post',
|
|
|
|
|
|
organisation_type=organisation_type,
|
|
|
|
|
|
message_limit=app_.config['DEFAULT_SERVICE_LIMIT'],
|
|
|
|
|
|
restricted=True,
|
|
|
|
|
|
user_id=api_user_active.id,
|
|
|
|
|
|
email_from='testing.the.post'
|
2017-02-03 12:07:21 +00:00
|
|
|
|
)
|
2017-12-04 16:03:11 +00:00
|
|
|
|
mock_create_or_update_free_sms_fragment_limit.assert_called_once_with(101, free_allowance)
|
2017-02-03 12:07:21 +00:00
|
|
|
|
assert len(mock_create_service_template.call_args_list) == 0
|
|
|
|
|
|
assert session['service_id'] == 101
|
|
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
|
assert response.location == url_for('main.service_dashboard', service_id=101, _external=True)
|
2016-04-28 16:44:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-09-03 16:07:56 +01:00
|
|
|
|
@pytest.mark.parametrize('organisation_type, email_address, expected_branding', [
|
|
|
|
|
|
('central', 'test@example.voa.gsi.gov.uk', '5'),
|
|
|
|
|
|
('central', 'test@example.voa.gov.uk', '5'),
|
|
|
|
|
|
('central', 'test@example.gov.uk', None),
|
|
|
|
|
|
# Anyone choosing ‘NHS’ for organisation type gets NHS branding no
|
|
|
|
|
|
# matter what their email domain is (but we look it up based on the
|
|
|
|
|
|
# `nhs.uk` domain to avoid hard-coding a branding ID anywhere)
|
|
|
|
|
|
('nhs', 'test@example.voa.gov.uk', '4'),
|
|
|
|
|
|
('nhs', 'test@nhs.uk', '4'),
|
2018-09-03 11:46:41 +01:00
|
|
|
|
])
|
|
|
|
|
|
def test_should_lookup_branding_for_known_domain(
|
|
|
|
|
|
app_,
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
active_user_with_permissions,
|
|
|
|
|
|
mock_create_service,
|
|
|
|
|
|
mock_get_services,
|
|
|
|
|
|
mock_update_service,
|
|
|
|
|
|
mock_create_or_update_free_sms_fragment_limit,
|
|
|
|
|
|
mock_get_all_email_branding,
|
2018-09-03 16:07:56 +01:00
|
|
|
|
organisation_type,
|
2018-09-03 11:46:41 +01:00
|
|
|
|
email_address,
|
|
|
|
|
|
expected_branding,
|
|
|
|
|
|
):
|
|
|
|
|
|
active_user_with_permissions.email_address = email_address
|
|
|
|
|
|
client_request.login(active_user_with_permissions)
|
|
|
|
|
|
client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
_data={
|
|
|
|
|
|
'name': 'testing the post',
|
2018-09-03 16:07:56 +01:00
|
|
|
|
'organisation_type': organisation_type,
|
2018-09-03 11:46:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_get_all_email_branding.assert_called_once_with()
|
|
|
|
|
|
assert mock_create_service.called is True
|
|
|
|
|
|
if expected_branding:
|
|
|
|
|
|
mock_update_service.assert_called_once_with(
|
|
|
|
|
|
101,
|
|
|
|
|
|
email_branding=expected_branding,
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
assert mock_update_service.called is False
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_return_form_errors_when_service_name_is_empty(
|
2017-08-08 11:13:08 +01:00
|
|
|
|
logged_in_client
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = logged_in_client.post(url_for('main.add_service'), data={})
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
assert 'Can’t be empty' in response.get_data(as_text=True)
|
2016-01-15 16:10:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_return_form_errors_with_duplicate_service_name_regardless_of_case(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
logged_in_client,
|
2017-08-07 11:30:25 +01:00
|
|
|
|
mock_create_duplicate_service,
|
2018-09-03 11:46:41 +01:00
|
|
|
|
mock_get_all_email_branding,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-10-04 11:49:32 +01:00
|
|
|
|
response = logged_in_client.post(
|
|
|
|
|
|
url_for('main.add_service'),
|
|
|
|
|
|
data={
|
|
|
|
|
|
'name': 'SERVICE ONE',
|
|
|
|
|
|
'organisation_type': 'central',
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
2017-08-08 11:13:08 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
2017-02-03 12:07:21 +00:00
|
|
|
|
assert 'This service name is already in use' in response.get_data(as_text=True)
|
2016-10-25 18:11:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_non_whitelist_user_cannot_access_create_service_page(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
logged_in_client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_non_govuser,
|
|
|
|
|
|
api_nongov_user_active,
|
|
|
|
|
|
):
|
2016-10-28 10:48:29 +01:00
|
|
|
|
assert not is_gov_user(api_nongov_user_active.email_address)
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = logged_in_client.get(url_for('main.add_service'))
|
2016-10-25 18:11:37 +01:00
|
|
|
|
assert response.status_code == 403
|
2016-10-28 10:48:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_non_whitelist_user_cannot_create_service(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
logged_in_client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_non_govuser,
|
|
|
|
|
|
api_nongov_user_active,
|
|
|
|
|
|
):
|
2016-10-28 10:48:29 +01:00
|
|
|
|
assert not is_gov_user(api_nongov_user_active.email_address)
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = logged_in_client.post(url_for('main.add_service'), data={'name': 'SERVICE TWO'})
|
2016-10-28 10:48:29 +01:00
|
|
|
|
assert response.status_code == 403
|