2019-04-03 11:47:46 +01:00
|
|
|
import uuid
|
2016-04-27 16:39:17 +01:00
|
|
|
from unittest.mock import ANY
|
|
|
|
|
|
2019-04-03 11:47:46 +01:00
|
|
|
from app.event_handlers import (
|
2021-03-05 12:19:36 +00:00
|
|
|
create_add_user_to_service_event,
|
2021-07-08 15:30:31 +01:00
|
|
|
create_archive_service_event,
|
2019-05-22 14:05:19 +01:00
|
|
|
create_archive_user_event,
|
2021-03-04 12:09:28 +00:00
|
|
|
create_broadcast_account_type_change_event,
|
2019-04-03 11:47:46 +01:00
|
|
|
create_email_change_event,
|
|
|
|
|
create_mobile_number_change_event,
|
2020-03-10 13:18:32 +00:00
|
|
|
create_remove_user_from_service_event,
|
2021-07-12 16:00:16 +01:00
|
|
|
create_resume_service_event,
|
2021-07-08 15:20:24 +01:00
|
|
|
create_suspend_service_event,
|
2019-04-03 11:47:46 +01:00
|
|
|
on_user_logged_in,
|
|
|
|
|
)
|
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
|
|
|
from app.models.user import User
|
2016-04-27 16:39:17 +01:00
|
|
|
|
|
|
|
|
|
2021-07-08 17:14:56 +01:00
|
|
|
def event_dict(**extra):
|
|
|
|
|
return {
|
|
|
|
|
'browser_fingerprint': {'browser': ANY, 'version': ANY, 'platform': ANY, 'user_agent_string': ''},
|
|
|
|
|
'ip_address': ANY,
|
|
|
|
|
**extra
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-07-08 17:06:10 +01:00
|
|
|
def test_on_user_logged_in_calls_events_api(client, api_user_active, mock_events):
|
|
|
|
|
on_user_logged_in('_notify_admin', User(api_user_active))
|
2021-07-08 17:14:56 +01:00
|
|
|
|
|
|
|
|
mock_events.assert_called_with('sucessful_login', event_dict(
|
|
|
|
|
user_id=str(api_user_active['id'])
|
|
|
|
|
))
|
2016-04-27 16:39:17 +01:00
|
|
|
|
2019-04-03 11:47:46 +01:00
|
|
|
|
2021-07-08 17:06:10 +01:00
|
|
|
def test_create_email_change_event_calls_events_api(client, mock_events):
|
2021-07-12 15:37:22 +01:00
|
|
|
kwargs = {
|
|
|
|
|
"user_id": str(uuid.uuid4()),
|
|
|
|
|
"updated_by_id": str(uuid.uuid4()),
|
|
|
|
|
"original_email_address": 'original@example.com',
|
|
|
|
|
"new_email_address": 'new@example.com'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create_email_change_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('update_user_email', event_dict(**kwargs))
|
2019-04-03 11:47:46 +01:00
|
|
|
|
|
|
|
|
|
2021-07-08 17:06:10 +01:00
|
|
|
def test_create_add_user_to_service_event_calls_events_api(client, mock_events):
|
2021-07-12 15:37:22 +01:00
|
|
|
kwargs = {
|
|
|
|
|
"user_id": str(uuid.uuid4()),
|
|
|
|
|
"invited_by_id": str(uuid.uuid4()),
|
|
|
|
|
"service_id": str(uuid.uuid4())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create_add_user_to_service_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('add_user_to_service', event_dict(**kwargs))
|
2021-03-05 12:19:36 +00:00
|
|
|
|
|
|
|
|
|
2021-07-08 17:06:10 +01:00
|
|
|
def test_create_remove_user_from_service_event_calls_events_api(client, mock_events):
|
2021-07-12 15:37:22 +01:00
|
|
|
kwargs = {
|
|
|
|
|
"user_id": str(uuid.uuid4()),
|
|
|
|
|
"removed_by_id": str(uuid.uuid4()),
|
|
|
|
|
"service_id": str(uuid.uuid4())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create_remove_user_from_service_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('remove_user_from_service', event_dict(**kwargs))
|
2020-03-10 13:18:32 +00:00
|
|
|
|
|
|
|
|
|
2021-07-08 17:06:10 +01:00
|
|
|
def test_create_mobile_number_change_event_calls_events_api(client, mock_events):
|
2021-07-12 15:37:22 +01:00
|
|
|
kwargs = {
|
|
|
|
|
"user_id": str(uuid.uuid4()),
|
|
|
|
|
"updated_by_id": str(uuid.uuid4()),
|
|
|
|
|
"original_mobile_number": '07700900000',
|
|
|
|
|
"new_mobile_number": '07700900999'
|
|
|
|
|
}
|
2019-05-22 14:05:19 +01:00
|
|
|
|
2021-07-12 15:37:22 +01:00
|
|
|
create_mobile_number_change_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('update_user_mobile_number', event_dict(**kwargs))
|
2019-05-22 14:05:19 +01:00
|
|
|
|
|
|
|
|
|
2021-07-12 15:37:22 +01:00
|
|
|
def test_create_archive_user_event_calls_events_api(client, mock_events):
|
|
|
|
|
kwargs = {
|
|
|
|
|
"user_id": str(uuid.uuid4()),
|
|
|
|
|
"archived_by_id": str(uuid.uuid4())
|
|
|
|
|
}
|
2019-05-22 14:05:19 +01:00
|
|
|
|
2021-07-12 15:37:22 +01:00
|
|
|
create_archive_user_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('archive_user', event_dict(**kwargs))
|
2021-03-04 12:09:28 +00:00
|
|
|
|
|
|
|
|
|
2021-07-08 17:06:10 +01:00
|
|
|
def test_create_broadcast_account_type_change_event(client, mock_events):
|
2021-07-12 15:37:22 +01:00
|
|
|
kwargs = {
|
|
|
|
|
"service_id": str(uuid.uuid4()),
|
|
|
|
|
"changed_by_id": str(uuid.uuid4()),
|
|
|
|
|
"service_mode": 'training',
|
|
|
|
|
"broadcast_channel": 'severe',
|
|
|
|
|
"provider_restriction": None
|
|
|
|
|
}
|
2021-07-08 15:20:24 +01:00
|
|
|
|
2021-07-12 15:37:22 +01:00
|
|
|
create_broadcast_account_type_change_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('change_broadcast_account_type', event_dict(**kwargs))
|
2021-07-08 15:20:24 +01:00
|
|
|
|
|
|
|
|
|
2021-07-12 15:37:22 +01:00
|
|
|
def test_suspend_service(client, mock_events):
|
|
|
|
|
kwargs = {
|
|
|
|
|
"service_id": str(uuid.uuid4()),
|
|
|
|
|
"suspended_by_id": str(uuid.uuid4())
|
|
|
|
|
}
|
2021-07-08 15:20:24 +01:00
|
|
|
|
2021-07-12 15:37:22 +01:00
|
|
|
create_suspend_service_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('suspend_service', event_dict(**kwargs))
|
2021-07-08 15:30:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_archive_service(client, mock_events):
|
2021-07-12 15:37:22 +01:00
|
|
|
kwargs = {
|
|
|
|
|
"service_id": str(uuid.uuid4()),
|
|
|
|
|
"archived_by_id": str(uuid.uuid4())
|
|
|
|
|
}
|
2021-07-08 15:30:31 +01:00
|
|
|
|
2021-07-12 15:37:22 +01:00
|
|
|
create_archive_service_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('archive_service', event_dict(**kwargs))
|
2021-07-12 16:00:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_resume_service(client, mock_events):
|
|
|
|
|
kwargs = {
|
|
|
|
|
"service_id": str(uuid.uuid4()),
|
|
|
|
|
"resumed_by_id": str(uuid.uuid4())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create_resume_service_event(**kwargs)
|
|
|
|
|
mock_events.assert_called_with('resume_service', event_dict(**kwargs))
|