2018-01-11 22:36:13 +00:00
|
|
|
import pytest
|
2018-02-20 11:22:17 +00:00
|
|
|
|
2019-07-01 15:22:08 +01:00
|
|
|
from app.models.user import AnonymousUser, User
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_anonymous_user(app_):
|
|
|
|
|
assert AnonymousUser().is_authenticated is False
|
|
|
|
|
assert AnonymousUser().logged_in_elsewhere() is False
|
|
|
|
|
assert AnonymousUser().default_organisation.name is None
|
|
|
|
|
assert AnonymousUser().default_organisation.crown is None
|
|
|
|
|
assert AnonymousUser().default_organisation.agreement_signed is None
|
|
|
|
|
assert AnonymousUser().default_organisation.domains == []
|
|
|
|
|
assert AnonymousUser().default_organisation.organisation_type is None
|
|
|
|
|
assert AnonymousUser().default_organisation.request_to_go_live_notes is None
|
2016-01-23 23:14:50 +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
|
|
|
def test_user(app_):
|
2016-01-23 23:14:50 +00:00
|
|
|
user_data = {'id': 1,
|
|
|
|
|
'name': 'Test User',
|
|
|
|
|
'email_address': 'test@user.gov.uk',
|
|
|
|
|
'mobile_number': '+4412341234',
|
|
|
|
|
'state': 'pending',
|
2019-06-13 19:00:17 +01:00
|
|
|
'failed_login_count': 0,
|
|
|
|
|
'platform_admin': False,
|
2016-01-23 23:14:50 +00:00
|
|
|
}
|
|
|
|
|
user = User(user_data)
|
|
|
|
|
|
|
|
|
|
assert user.id == 1
|
|
|
|
|
assert user.name == 'Test User'
|
|
|
|
|
assert user.email_address == 'test@user.gov.uk'
|
|
|
|
|
assert user.mobile_number == '+4412341234'
|
|
|
|
|
assert user.state == 'pending'
|
|
|
|
|
|
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 has ten failed logins before being locked
|
|
|
|
|
assert user.max_failed_login_count == app_.config['MAX_FAILED_LOGIN_COUNT'] == 10
|
2016-01-23 23:14:50 +00:00
|
|
|
assert user.failed_login_count == 0
|
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 user.locked is False
|
2016-01-23 23:14:50 +00:00
|
|
|
|
|
|
|
|
# set failed logins to threshold
|
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.failed_login_count = app_.config['MAX_FAILED_LOGIN_COUNT']
|
|
|
|
|
assert user.locked is True
|
2018-01-11 22:36:13 +00:00
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
|
user.has_permissions('to_do_bad_things')
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_activate_user(app_, api_user_pending, mock_activate_user):
|
|
|
|
|
assert User(api_user_pending).activate() == User(api_user_pending)
|
|
|
|
|
mock_activate_user.assert_called_once_with(api_user_pending['id'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_activate_user_already_active(app_, api_user_active, mock_activate_user):
|
|
|
|
|
assert User(api_user_active).activate() == User(api_user_active)
|
|
|
|
|
assert mock_activate_user.called is False
|
2019-06-13 19:00:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('is_platform_admin, value_in_session, expected_result', [
|
|
|
|
|
(True, True, False),
|
|
|
|
|
(True, False, True),
|
|
|
|
|
(True, None, True),
|
|
|
|
|
(False, True, False),
|
|
|
|
|
(False, False, False),
|
|
|
|
|
(False, None, False),
|
|
|
|
|
])
|
2019-06-14 12:32:47 +01:00
|
|
|
def test_platform_admin_flag_set_in_session(client, mocker, is_platform_admin, value_in_session, expected_result):
|
2019-06-13 19:00:17 +01:00
|
|
|
session_dict = {}
|
|
|
|
|
if value_in_session is not None:
|
2019-06-14 12:32:47 +01:00
|
|
|
session_dict['disable_platform_admin_view'] = value_in_session
|
2019-06-13 19:00:17 +01:00
|
|
|
|
|
|
|
|
mocker.patch.dict('app.models.user.session', values=session_dict, clear=True)
|
|
|
|
|
|
|
|
|
|
assert User({'platform_admin': is_platform_admin}).platform_admin == expected_result
|