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
|
2019-05-03 13:35:52 +01:00
|
|
|
|
from tests import organisation_json
|
2019-12-20 15:02:47 +00:00
|
|
|
|
from tests.conftest import normalize_spaces
|
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,
|
2019-05-28 16:11:54 +01:00
|
|
|
|
mock_get_organisations,
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-03 13:35:52 +01:00
|
|
|
|
@pytest.mark.parametrize('org_json', (
|
|
|
|
|
|
None,
|
|
|
|
|
|
organisation_json(organisation_type=None),
|
|
|
|
|
|
))
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_get_should_render_add_service_template(
|
2019-04-18 12:44:18 +01:00
|
|
|
|
client_request,
|
2019-05-03 13:35:52 +01:00
|
|
|
|
mocker,
|
|
|
|
|
|
org_json,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2019-05-03 13:35:52 +01:00
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.organisations_client.get_organisation_by_domain',
|
|
|
|
|
|
return_value=org_json,
|
|
|
|
|
|
)
|
2019-03-26 12:35:32 +00:00
|
|
|
|
page = client_request.get('main.add_service')
|
2019-04-18 12:44:18 +01:00
|
|
|
|
assert page.select_one('h1').text.strip() == 'About your service'
|
|
|
|
|
|
assert page.select_one('input[name=name]')['value'] == ''
|
|
|
|
|
|
assert [
|
|
|
|
|
|
label.text.strip() for label in page.select('.multiple-choice label')
|
|
|
|
|
|
] == [
|
|
|
|
|
|
'Central government',
|
|
|
|
|
|
'Local government',
|
2019-07-15 18:03:59 +01:00
|
|
|
|
'NHS – central government agency or public body',
|
2019-07-16 15:37:27 +01:00
|
|
|
|
'NHS Trust or Clinical Commissioning Group',
|
|
|
|
|
|
'GP practice',
|
2019-07-15 18:03:59 +01:00
|
|
|
|
'Emergency service',
|
|
|
|
|
|
'School or college',
|
|
|
|
|
|
'Other',
|
2019-04-18 12:44:18 +01:00
|
|
|
|
]
|
|
|
|
|
|
assert [
|
|
|
|
|
|
radio['value'] for radio in page.select('.multiple-choice input')
|
|
|
|
|
|
] == [
|
|
|
|
|
|
'central',
|
|
|
|
|
|
'local',
|
2019-07-15 18:03:59 +01:00
|
|
|
|
'nhs_central',
|
|
|
|
|
|
'nhs_local',
|
2019-08-27 16:26:50 +01:00
|
|
|
|
'nhs_gp',
|
2019-07-15 18:03:59 +01:00
|
|
|
|
'emergency_service',
|
|
|
|
|
|
'school_or_college',
|
|
|
|
|
|
'other',
|
2019-04-18 12:44:18 +01:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_should_not_render_radios_if_org_type_known(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
):
|
2019-12-20 15:02:47 +00:00
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.organisations_client.get_organisation_by_domain',
|
|
|
|
|
|
return_value=organisation_json(organisation_type='central'),
|
|
|
|
|
|
)
|
2019-04-18 12:44:18 +01:00
|
|
|
|
page = client_request.get('main.add_service')
|
|
|
|
|
|
assert page.select_one('h1').text.strip() == 'About your service'
|
|
|
|
|
|
assert page.select_one('input[name=name]')['value'] == ''
|
|
|
|
|
|
assert not page.select('.multiple-choice')
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-01 11:18:20 +01:00
|
|
|
|
def test_show_different_page_if_user_org_type_is_local(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
):
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.organisations_client.get_organisation_by_domain',
|
|
|
|
|
|
return_value=organisation_json(organisation_type='local'),
|
|
|
|
|
|
)
|
|
|
|
|
|
page = client_request.get('main.add_service')
|
|
|
|
|
|
assert page.select_one('h1').text.strip() == 'About your service'
|
|
|
|
|
|
assert page.select_one('input[name=name]')['value'] == ''
|
|
|
|
|
|
assert page.select_one('main .govuk-body').text.strip() == (
|
2020-07-01 11:49:11 +01:00
|
|
|
|
'Give your service a name that tells users what your '
|
2020-07-01 11:18:20 +01:00
|
|
|
|
'messages are about, as well as who they’re from. For example:')
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-07 10:31:16 +01:00
|
|
|
|
@pytest.mark.parametrize('email_address', (
|
|
|
|
|
|
# User’s email address doesn’t matter when the organisation is known
|
|
|
|
|
|
'test@example.gov.uk',
|
|
|
|
|
|
'test@example.nhs.uk',
|
|
|
|
|
|
))
|
2019-04-18 12:44:18 +01:00
|
|
|
|
@pytest.mark.parametrize('inherited, posted, persisted, sms_limit', (
|
|
|
|
|
|
(None, 'central', 'central', 250000),
|
2019-08-29 17:45:17 +01:00
|
|
|
|
(None, 'nhs_central', 'nhs_central', 250000),
|
|
|
|
|
|
(None, 'nhs_gp', 'nhs_gp', 25000),
|
|
|
|
|
|
(None, 'nhs_local', 'nhs_local', 25000),
|
|
|
|
|
|
(None, 'local', 'local', 25000),
|
|
|
|
|
|
(None, 'emergency_service', 'emergency_service', 25000),
|
|
|
|
|
|
(None, 'school_or_college', 'school_or_college', 25000),
|
|
|
|
|
|
(None, 'other', 'other', 25000),
|
2019-04-18 12:44:18 +01:00
|
|
|
|
('central', None, 'central', 250000),
|
2019-07-15 18:03:59 +01:00
|
|
|
|
('nhs_central', None, 'nhs_central', 250000),
|
|
|
|
|
|
('nhs_local', None, 'nhs_local', 25000),
|
2019-04-18 12:44:18 +01:00
|
|
|
|
('local', None, 'local', 25000),
|
2019-07-15 18:03:59 +01:00
|
|
|
|
('emergency_service', None, 'emergency_service', 25000),
|
|
|
|
|
|
('school_or_college', None, 'school_or_college', 25000),
|
|
|
|
|
|
('other', None, 'other', 25000),
|
2019-04-18 12:44:18 +01:00
|
|
|
|
('central', 'local', 'central', 250000),
|
|
|
|
|
|
))
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_add_service_and_redirect_to_tour_when_no_services(
|
2019-04-18 12:44:18 +01:00
|
|
|
|
mocker,
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request,
|
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,
|
2019-04-18 12:44:18 +01:00
|
|
|
|
inherited,
|
2019-05-07 10:31:16 +01:00
|
|
|
|
email_address,
|
2019-04-18 12:44:18 +01:00
|
|
|
|
posted,
|
|
|
|
|
|
persisted,
|
|
|
|
|
|
sms_limit,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
api_user_active['email_address'] = email_address
|
2019-05-07 10:31:16 +01:00
|
|
|
|
client_request.login(api_user_active)
|
2019-12-20 15:02:47 +00:00
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.organisations_client.get_organisation_by_domain',
|
|
|
|
|
|
return_value=organisation_json(organisation_type=inherited),
|
|
|
|
|
|
)
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
_data={
|
2017-10-04 11:49:32 +01:00
|
|
|
|
'name': 'testing the post',
|
2019-04-18 12:44:18 +01:00
|
|
|
|
'organisation_type': posted,
|
2019-03-26 12:35:32 +00:00
|
|
|
|
},
|
|
|
|
|
|
_expected_status=302,
|
|
|
|
|
|
_expected_redirect=url_for(
|
|
|
|
|
|
'main.start_tour',
|
|
|
|
|
|
service_id=101,
|
|
|
|
|
|
template_id="Example%20text%20message%20template",
|
|
|
|
|
|
_external=True,
|
|
|
|
|
|
),
|
2017-10-04 11:49:32 +01:00
|
|
|
|
)
|
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',
|
2019-04-18 12:44:18 +01:00
|
|
|
|
organisation_type=persisted,
|
|
|
|
|
|
message_limit=50,
|
2018-04-19 14:01:45 +01:00
|
|
|
|
restricted=True,
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
user_id=api_user_active['id'],
|
2019-02-12 14:02:21 +00:00
|
|
|
|
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
|
2019-04-18 12:44:18 +01:00
|
|
|
|
mock_create_or_update_free_sms_fragment_limit.assert_called_once_with(101, sms_limit)
|
2015-12-15 10:17:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-05-15 16:16:23 +01:00
|
|
|
|
def test_add_service_has_to_choose_org_type(
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
mock_create_service,
|
|
|
|
|
|
mock_create_service_template,
|
|
|
|
|
|
mock_get_services_with_no_services,
|
|
|
|
|
|
api_user_active,
|
|
|
|
|
|
mock_create_or_update_free_sms_fragment_limit,
|
|
|
|
|
|
mock_get_all_email_branding,
|
|
|
|
|
|
):
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.organisations_client.get_organisation_by_domain',
|
|
|
|
|
|
return_value=None,
|
|
|
|
|
|
)
|
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
_data={
|
|
|
|
|
|
'name': 'testing the post',
|
|
|
|
|
|
},
|
|
|
|
|
|
_expected_status=200,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert normalize_spaces(page.select_one('.error-message').text) == (
|
2020-04-22 17:49:03 +01:00
|
|
|
|
'Select the type of organisation'
|
2019-05-15 16:16:23 +01:00
|
|
|
|
)
|
|
|
|
|
|
assert mock_create_service.called is False
|
|
|
|
|
|
assert mock_create_service_template.called is False
|
|
|
|
|
|
assert mock_create_or_update_free_sms_fragment_limit.called is False
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-07 10:31:16 +01:00
|
|
|
|
@pytest.mark.parametrize('email_address', (
|
|
|
|
|
|
'test@nhs.net',
|
|
|
|
|
|
'test@nhs.uk',
|
|
|
|
|
|
'test@example.NhS.uK',
|
|
|
|
|
|
'test@EXAMPLE.NHS.NET',
|
|
|
|
|
|
))
|
2019-07-16 15:37:27 +01:00
|
|
|
|
def test_get_should_only_show_nhs_org_types_radios_if_user_has_nhs_email(
|
2019-05-07 10:31:16 +01:00
|
|
|
|
client_request,
|
2019-07-16 15:37:27 +01:00
|
|
|
|
mocker,
|
2019-05-07 10:31:16 +01:00
|
|
|
|
api_user_active,
|
|
|
|
|
|
email_address,
|
|
|
|
|
|
):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
api_user_active['email_address'] = email_address
|
2019-05-07 10:31:16 +01:00
|
|
|
|
client_request.login(api_user_active)
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.organisations_client.get_organisation_by_domain',
|
|
|
|
|
|
return_value=None,
|
|
|
|
|
|
)
|
2019-07-16 15:37:27 +01:00
|
|
|
|
page = client_request.get('main.add_service')
|
|
|
|
|
|
assert page.select_one('h1').text.strip() == 'About your service'
|
|
|
|
|
|
assert page.select_one('input[name=name]')['value'] == ''
|
|
|
|
|
|
assert [
|
|
|
|
|
|
label.text.strip() for label in page.select('.multiple-choice label')
|
|
|
|
|
|
] == [
|
|
|
|
|
|
'NHS – central government agency or public body',
|
|
|
|
|
|
'NHS Trust or Clinical Commissioning Group',
|
|
|
|
|
|
'GP practice',
|
|
|
|
|
|
]
|
|
|
|
|
|
assert [
|
|
|
|
|
|
radio['value'] for radio in page.select('.multiple-choice input')
|
|
|
|
|
|
] == [
|
|
|
|
|
|
'nhs_central',
|
|
|
|
|
|
'nhs_local',
|
2019-08-27 16:26:50 +01:00
|
|
|
|
'nhs_gp',
|
2019-07-16 15:37:27 +01:00
|
|
|
|
]
|
2019-05-07 10:31:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
2019-04-04 11:15:42 +01:00
|
|
|
|
@pytest.mark.parametrize('organisation_type, free_allowance', [
|
|
|
|
|
|
('central', 250 * 1000),
|
|
|
|
|
|
('local', 25 * 1000),
|
2019-07-15 18:03:59 +01:00
|
|
|
|
('nhs_central', 250 * 1000),
|
|
|
|
|
|
('nhs_local', 25 * 1000),
|
|
|
|
|
|
('school_or_college', 25 * 1000),
|
|
|
|
|
|
('emergency_service', 25 * 1000),
|
|
|
|
|
|
('other', 25 * 1000),
|
2017-10-05 10:46:55 +01:00
|
|
|
|
])
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_add_service_and_redirect_to_dashboard_when_existing_service(
|
|
|
|
|
|
app_,
|
2020-03-20 08:42:33 +00:00
|
|
|
|
mocker,
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_create_service,
|
|
|
|
|
|
mock_create_service_template,
|
|
|
|
|
|
mock_get_services,
|
2020-03-20 08:42:33 +00:00
|
|
|
|
mock_get_no_organisation_by_domain,
|
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
|
|
|
|
):
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
_data={
|
2017-10-04 11:49:32 +01:00
|
|
|
|
'name': 'testing the post',
|
2017-10-05 10:46:55 +01:00
|
|
|
|
'organisation_type': organisation_type,
|
2019-03-26 12:35:32 +00:00
|
|
|
|
},
|
|
|
|
|
|
_expected_status=302,
|
|
|
|
|
|
_expected_redirect=url_for(
|
|
|
|
|
|
'main.service_dashboard',
|
|
|
|
|
|
service_id=101,
|
|
|
|
|
|
_external=True,
|
|
|
|
|
|
)
|
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,
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
user_id=api_user_active['id'],
|
2019-02-12 14:02:21 +00:00
|
|
|
|
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
|
2016-04-28 16:44:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_return_form_errors_when_service_name_is_empty(
|
2019-04-18 12:44:18 +01:00
|
|
|
|
client_request,
|
|
|
|
|
|
mock_get_organisation_by_domain,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2019-03-26 12:35:32 +00:00
|
|
|
|
page = client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
data={},
|
|
|
|
|
|
_expected_status=200,
|
|
|
|
|
|
)
|
2019-09-13 10:15:05 +01:00
|
|
|
|
assert 'Cannot be empty' in page.text
|
2016-01-15 16:10:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-11-08 17:12:32 +00:00
|
|
|
|
def test_add_service_fails_if_service_name_has_less_than_2_alphanumeric_characters(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
mock_get_organisation_by_domain,
|
|
|
|
|
|
):
|
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
data={"name": "."},
|
|
|
|
|
|
_expected_status=200,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert page.find("span", {"class": "error-message"})
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_return_form_errors_with_duplicate_service_name_regardless_of_case(
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request,
|
2017-08-07 11:30:25 +01:00
|
|
|
|
mock_create_duplicate_service,
|
2019-04-18 12:44:18 +01:00
|
|
|
|
mock_get_organisation_by_domain,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2019-03-26 12:35:32 +00:00
|
|
|
|
page = client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
_data={
|
2017-10-04 11:49:32 +01:00
|
|
|
|
'name': 'SERVICE ONE',
|
|
|
|
|
|
'organisation_type': 'central',
|
|
|
|
|
|
},
|
2019-03-26 12:35:32 +00:00
|
|
|
|
_expected_status=200,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert page.select_one('.error-message').text.strip() == (
|
|
|
|
|
|
'This service name is already in use'
|
2017-10-04 11:49:32 +01:00
|
|
|
|
)
|
2016-10-25 18:11:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-06-12 09:15:23 +01:00
|
|
|
|
def test_non_government_user_cannot_access_create_service_page(
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_non_govuser,
|
|
|
|
|
|
api_nongov_user_active,
|
2019-05-28 16:11:54 +01:00
|
|
|
|
mock_get_organisations,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
assert is_gov_user(api_nongov_user_active['email_address']) is False
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request.get(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
_expected_status=403,
|
|
|
|
|
|
)
|
2016-10-28 10:48:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-06-12 09:15:23 +01:00
|
|
|
|
def test_non_government_user_cannot_create_service(
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_non_govuser,
|
|
|
|
|
|
api_nongov_user_active,
|
2019-05-28 16:11:54 +01:00
|
|
|
|
mock_get_organisations,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
|
assert is_gov_user(api_nongov_user_active['email_address']) is False
|
2019-03-26 12:35:32 +00:00
|
|
|
|
client_request.post(
|
|
|
|
|
|
'main.add_service',
|
|
|
|
|
|
_data={'name': 'SERVICE TWO'},
|
|
|
|
|
|
_expected_status=403,
|
|
|
|
|
|
)
|