Files
notifications-admin/tests/app/main/views/test_find_users.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

148 lines
5.3 KiB
Python

from flask import url_for
from lxml import html
from tests import user_json
def test_find_users_by_email_page_loads_correctly(client_request, platform_admin_user):
client_request.login(platform_admin_user)
document = client_request.get('main.find_users_by_email')
assert document.h1.text.strip() == 'Find users by email'
assert len(document.find_all('input', {'type': 'search'})) > 0
def test_find_users_by_email_displays_users_found(
client_request,
platform_admin_user,
mocker
):
client_request.login(platform_admin_user)
mocker.patch(
'app.user_api_client.find_users_by_full_or_partial_email',
return_value={"data": [user_json()]},
autospec=True,
)
document = client_request.post(
'main.find_users_by_email',
_data={"search": "twilight.sparkle"},
_expected_status=200
)
assert any(element.text.strip() == 'test@gov.uk' for element in document.find_all(
'a', {'class': 'browse-list-link'}, href=True)
)
assert any(element.text.strip() == 'Test User' for element in document.find_all('p', {'class': 'browse-list-hint'}))
assert document.find('a', {'class': 'browse-list-link'}).text.strip() == 'test@gov.uk'
assert document.find('p', {'class': 'browse-list-hint'}).text.strip() == 'Test User'
def test_find_users_by_email_displays_multiple_users(
client_request,
platform_admin_user,
mocker
):
client_request.login(platform_admin_user)
mocker.patch(
'app.user_api_client.find_users_by_full_or_partial_email',
return_value={"data": [user_json(name="Apple Jack"), user_json(name="Apple Bloom")]},
autospec=True,
)
document = client_request.post('main.find_users_by_email', _data={"search": "apple"}, _expected_status=200)
assert any(
element.text.strip() == 'Apple Jack' for element in document.find_all('p', {'class': 'browse-list-hint'})
)
assert any(
element.text.strip() == 'Apple Bloom' for element in document.find_all('p', {'class': 'browse-list-hint'})
)
def test_find_users_by_email_displays_message_if_no_users_found(
client_request,
platform_admin_user,
mocker
):
client_request.login(platform_admin_user)
mocker.patch('app.user_api_client.find_users_by_full_or_partial_email', return_value={"data": []}, autospec=True)
document = client_request.post(
'main.find_users_by_email', _data={"search": "twilight.sparkle"}, _expected_status=200
)
assert document.find('p', {'class': 'browse-list-hint'}).text.strip() == 'No users found.'
def test_find_users_by_email_validates_against_empty_search_submission(
client_request,
platform_admin_user,
mocker
):
client_request.login(platform_admin_user)
document = client_request.post('main.find_users_by_email', _data={"search": ""}, _expected_status=400)
expected_message = "You need to enter full or partial email address to search by."
assert document.find('span', {'class': 'error-message'}).text.strip() == expected_message
def test_user_information_page_shows_information_about_user(
client,
platform_admin_user,
mocker
):
mocker.patch('app.user_api_client.get_user', side_effect=[
platform_admin_user,
user_json(name="Apple Bloom", services=[1, 2])
], autospec=True)
mocker.patch(
'app.user_api_client.get_organisations_and_services_for_user',
return_value={'organisations': [], 'services_without_organisations': [
{"id": 1, "name": "Fresh Orchard Juice"},
{"id": 2, "name": "Nature Therapy"},
]},
autospec=True
)
client.login(platform_admin_user)
response = client.get(url_for('main.user_information', user_id=345))
assert response.status_code == 200
document = html.fromstring(response.get_data(as_text=True))
assert document.xpath("//h1/text()[normalize-space()='Apple Bloom']")
assert document.xpath("//p/text()[normalize-space()='test@gov.uk']")
assert document.xpath("//p/text()[normalize-space()='+447700900986']")
assert document.xpath("//h2/text()[normalize-space()='Services']")
assert document.xpath("//a/text()[normalize-space()='Fresh Orchard Juice']")
assert document.xpath("//a/text()[normalize-space()='Nature Therapy']")
assert document.xpath("//h2/text()[normalize-space()='Last login']")
assert not document.xpath("//p/text()[normalize-space()='0 failed login attempts']")
def test_user_information_page_displays_if_there_are_failed_login_attempts(
client,
platform_admin_user,
mocker
):
mocker.patch('app.user_api_client.get_user', side_effect=[
platform_admin_user,
user_json(name="Apple Bloom", failed_login_count=2)
], autospec=True)
mocker.patch(
'app.user_api_client.get_organisations_and_services_for_user',
return_value={'organisations': [], 'services_without_organisations': [
{"id": 1, "name": "Fresh Orchard Juice"},
{"id": 2, "name": "Nature Therapy"},
]},
autospec=True
)
client.login(platform_admin_user)
response = client.get(url_for('main.user_information', user_id=345))
assert response.status_code == 200
document = html.fromstring(response.get_data(as_text=True))
assert document.xpath("//p/text()[normalize-space()='2 failed login attempts']")