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.
This commit is contained in:
Chris Hill-Scott
2019-05-23 15:27:35 +01:00
parent d66cabf300
commit 628e344b36
56 changed files with 961 additions and 872 deletions

View File

@@ -40,6 +40,7 @@ from tests.conftest import (
no_sms_senders,
normalize_spaces,
platform_admin_user,
sample_invite,
)
FAKE_TEMPLATE_ID = uuid4()
@@ -707,6 +708,8 @@ def test_should_check_if_estimated_volumes_provided(
def test_should_check_for_sending_things_right(
client_request,
mocker,
service_one,
fake_uuid,
mock_get_service_organisation,
single_sms_sender,
count_of_users_with_manage_service,
@@ -724,14 +727,20 @@ def test_should_check_for_sending_things_right(
'email': list(range(0, count_of_email_templates)),
'sms': [],
}.get(template_type)
mock_count_users = mocker.patch(
'app.models.service.user_api_client.get_count_of_users_with_permission',
return_value=count_of_users_with_manage_service
active_user_with_permissions,
mock_get_users = mocker.patch(
'app.models.user.Users.client',
return_value=(
[active_user_with_permissions(fake_uuid)] * count_of_users_with_manage_service +
[active_user_no_settings_permission(fake_uuid)]
)
)
mock_count_invites = mocker.patch(
'app.models.service.invite_api_client.get_count_of_invites_with_permission',
return_value=count_of_invites_with_manage_service
mock_get_invites = mocker.patch(
'app.models.user.InvitedUsers.client',
return_value=(
([sample_invite(mocker, service_one)] * count_of_invites_with_manage_service) +
[sample_invite(mocker, service_one, permissions='view_activity')]
)
)
mock_templates = mocker.patch(
@@ -769,8 +778,8 @@ def test_should_check_for_sending_things_right(
assert normalize_spaces(checklist_items[2].text) == expected_templates_checklist_item
assert normalize_spaces(checklist_items[3].text) == expected_reply_to_checklist_item
mock_count_users.assert_called_once_with(SERVICE_ONE_ID, 'manage_service')
mock_count_invites.assert_called_once_with(SERVICE_ONE_ID, 'manage_service')
mock_get_users.assert_called_once_with(SERVICE_ONE_ID)
mock_get_invites.assert_called_once_with(SERVICE_ONE_ID)
assert mock_templates.called is True
if count_of_email_templates:
@@ -947,8 +956,8 @@ def test_should_check_for_sms_sender_on_go_live(
}.get(template_type, count_of_sms_templates)))
mocker.patch(
'app.main.views.service_settings.user_api_client.get_count_of_users_with_permission',
return_value=99,
'app.models.service.Service.has_team_members',
return_value=True,
)
mock_templates = mocker.patch(
'app.models.service.Service.all_templates',
@@ -1014,8 +1023,8 @@ def test_should_check_for_mou_on_request_to_go_live(
expected_item,
):
mocker.patch(
'app.main.views.service_settings.user_api_client.get_count_of_users_with_permission',
return_value=0,
'app.models.service.Service.has_team_members',
return_value=False,
)
mocker.patch(
'app.models.service.Service.all_templates',
@@ -1061,8 +1070,8 @@ def test_non_gov_user_is_told_they_cant_go_live(
mock_get_service_organisation,
):
mocker.patch(
'app.main.views.service_settings.user_api_client.get_count_of_users_with_permission',
return_value=0,
'app.models.service.Service.has_team_members',
return_value=False,
)
mocker.patch(
'app.models.service.Service.all_templates',
@@ -1364,8 +1373,8 @@ def test_should_redirect_after_request_to_go_live(
subject='Request to go live - service one',
message=ANY,
ticket_type=ZendeskClient.TYPE_QUESTION,
user_name=active_user_with_permissions.name,
user_email=active_user_with_permissions.email_address,
user_name=active_user_with_permissions['name'],
user_email=active_user_with_permissions['email_address'],
tags=[
'notify_request_to_go_live',
'notify_request_to_go_live_incomplete',
@@ -1402,7 +1411,7 @@ def test_should_redirect_after_request_to_go_live(
)
mock_update_service.assert_called_once_with(
SERVICE_ONE_ID,
go_live_user=active_user_with_permissions.id
go_live_user=active_user_with_permissions['id']
)