Files
notifications-admin/tests/app/main/views/test_template_history.py
Chris Hill-Scott 628e344b36 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-06-05 11:13:41 +01:00

71 lines
1.8 KiB
Python

from flask import url_for
def test_view_template_version(
logged_in_client,
api_user_active,
mock_login,
mock_get_service,
mock_get_template_version,
mock_get_user,
mock_get_user_by_email,
mock_has_permissions,
fake_uuid,
):
service_id = fake_uuid
template_id = fake_uuid
version = 1
all_versions_link = url_for(
'main.view_template_versions',
service_id=service_id,
template_id=template_id
)
resp = logged_in_client.get(url_for(
'.view_template_version',
service_id=service_id,
template_id=template_id,
version=version))
assert resp.status_code == 200
resp_data = resp.get_data(as_text=True)
template = mock_get_template_version(service_id, template_id, version)
assert api_user_active['name'] in resp_data
assert template['data']['content'] in resp_data
assert all_versions_link in resp_data
mock_get_template_version.assert_called_with(
service_id,
template_id,
version
)
def test_view_template_versions(
logged_in_client,
api_user_active,
mock_login,
mock_get_service,
mock_get_template_versions,
mock_get_service_template,
mock_get_user,
mock_get_user_by_email,
mock_has_permissions,
fake_uuid,
):
service_id = fake_uuid
template_id = fake_uuid
resp = logged_in_client.get(url_for(
'.view_template_versions',
service_id=service_id,
template_id=template_id
))
assert resp.status_code == 200
resp_data = resp.get_data(as_text=True)
versions = mock_get_template_versions(service_id, template_id)
assert api_user_active['name'] in resp_data
assert versions['data'][0]['content'] in resp_data
mock_get_template_versions.assert_called_with(
service_id,
template_id
)