2019-06-27 14:23:03 +01:00
|
|
|
import ast
|
|
|
|
|
import inspect
|
2019-11-04 11:07:55 +00:00
|
|
|
import re
|
2019-06-27 14:23:03 +01:00
|
|
|
|
2016-02-19 16:38:04 +00:00
|
|
|
import pytest
|
2019-11-04 11:07:55 +00:00
|
|
|
from flask import current_app, request
|
2018-02-20 11:22:17 +00:00
|
|
|
from werkzeug.exceptions import Forbidden, Unauthorized
|
|
|
|
|
|
|
|
|
|
from app.main.views.index import index
|
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.roles_and_permissions import (
|
2018-02-28 15:37:49 +00:00
|
|
|
translate_permissions_from_admin_roles_to_db,
|
|
|
|
|
translate_permissions_from_db_to_admin_roles,
|
|
|
|
|
)
|
2018-02-20 11:22:17 +00:00
|
|
|
from app.utils import user_has_permissions
|
2019-06-19 16:39:13 +01:00
|
|
|
from tests import service_json
|
|
|
|
|
from tests.conftest import (
|
|
|
|
|
ORGANISATION_ID,
|
|
|
|
|
ORGANISATION_TWO_ID,
|
|
|
|
|
SERVICE_ONE_ID,
|
|
|
|
|
SERVICE_TWO_ID,
|
|
|
|
|
)
|
2016-02-19 16:38:04 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def _test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
usr,
|
|
|
|
|
permissions,
|
|
|
|
|
will_succeed,
|
2019-11-01 10:43:01 +00:00
|
|
|
kwargs=None,
|
2017-02-03 10:42:01 +00:00
|
|
|
):
|
2018-02-28 18:13:29 +00:00
|
|
|
request.view_args.update({'service_id': 'foo'})
|
2017-02-03 12:07:21 +00:00
|
|
|
if usr:
|
|
|
|
|
client.login(usr)
|
2018-02-28 18:13:29 +00:00
|
|
|
|
2019-11-01 10:43:01 +00:00
|
|
|
decorator = user_has_permissions(*permissions, **(kwargs or {}))
|
2017-02-03 12:07:21 +00:00
|
|
|
decorated_index = decorator(index)
|
2018-02-28 18:13:29 +00:00
|
|
|
|
2017-02-03 12:07:21 +00:00
|
|
|
if will_succeed:
|
2017-10-18 14:51:26 +01:00
|
|
|
decorated_index()
|
2017-02-03 12:07:21 +00:00
|
|
|
else:
|
|
|
|
|
try:
|
2019-07-01 15:22:08 +01:00
|
|
|
if (
|
|
|
|
|
decorated_index().location != '/sign-in?next=%2F' or
|
|
|
|
|
decorated_index().status_code != 302
|
|
|
|
|
):
|
|
|
|
|
pytest.fail("Failed to throw a forbidden or unauthorised exception")
|
2017-02-03 12:07:21 +00:00
|
|
|
except (Forbidden, Unauthorized):
|
|
|
|
|
pass
|
2016-03-09 13:51:56 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_user_has_permissions_on_endpoint_fail(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
mocker,
|
2019-06-19 16:39:13 +01:00
|
|
|
mock_get_service,
|
2017-02-03 10:42:01 +00:00
|
|
|
):
|
2016-03-18 16:20:37 +00:00
|
|
|
user = _user_with_permissions()
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
2016-03-09 13:51:56 +00:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-03-18 16:20:37 +00:00
|
|
|
user,
|
2018-02-28 17:22:20 +00:00
|
|
|
['send_messages'],
|
2018-03-01 11:34:53 +00:00
|
|
|
will_succeed=False)
|
2016-03-03 09:02:56 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_user_has_permissions_success(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
mocker,
|
|
|
|
|
):
|
2016-03-18 16:20:37 +00:00
|
|
|
user = _user_with_permissions()
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
2016-03-09 13:51:56 +00:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-03-18 16:20:37 +00:00
|
|
|
user,
|
2018-02-28 17:22:20 +00:00
|
|
|
['manage_service'],
|
2018-03-01 11:34:53 +00:00
|
|
|
will_succeed=True)
|
2016-03-03 09:02:56 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_user_has_permissions_or(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
mocker,
|
|
|
|
|
):
|
2016-03-18 16:20:37 +00:00
|
|
|
user = _user_with_permissions()
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
2016-03-09 13:51:56 +00:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-03-18 16:20:37 +00:00
|
|
|
user,
|
2018-02-28 17:22:20 +00:00
|
|
|
['send_messages', 'manage_service'],
|
2018-03-01 11:34:53 +00:00
|
|
|
will_succeed=True)
|
2016-03-03 09:02:56 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_user_has_permissions_multiple(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
mocker,
|
|
|
|
|
):
|
2016-03-18 16:20:37 +00:00
|
|
|
user = _user_with_permissions()
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
2016-03-09 13:51:56 +00:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-03-18 16:20:37 +00:00
|
|
|
user,
|
2018-02-28 17:22:20 +00:00
|
|
|
['manage_templates', 'manage_service'],
|
2016-03-18 16:20:37 +00:00
|
|
|
will_succeed=True)
|
2016-03-04 10:09:46 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_exact_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
mocker,
|
|
|
|
|
):
|
2016-03-18 16:20:37 +00:00
|
|
|
user = _user_with_permissions()
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
2016-03-09 13:51:56 +00:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-03-18 16:20:37 +00:00
|
|
|
user,
|
2018-02-28 17:22:20 +00:00
|
|
|
['manage_service', 'manage_templates'],
|
2018-03-01 11:34:53 +00:00
|
|
|
will_succeed=True)
|
2016-03-17 14:25:28 +00:00
|
|
|
|
|
|
|
|
|
2018-03-01 10:30:17 +00:00
|
|
|
def test_platform_admin_user_can_access_page_that_has_no_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
platform_admin_user,
|
|
|
|
|
mocker,
|
|
|
|
|
):
|
2016-03-18 16:20:37 +00:00
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=platform_admin_user)
|
2016-03-17 14:25:28 +00:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-03-17 14:25:28 +00:00
|
|
|
platform_admin_user,
|
|
|
|
|
[],
|
2018-03-01 11:34:53 +00:00
|
|
|
will_succeed=True)
|
2016-03-17 14:25:28 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_platform_admin_user_can_not_access_page(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
platform_admin_user,
|
|
|
|
|
mocker,
|
2019-06-19 16:39:13 +01:00
|
|
|
mock_get_service,
|
2017-02-03 10:42:01 +00:00
|
|
|
):
|
2016-03-18 16:20:37 +00:00
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=platform_admin_user)
|
2016-03-17 14:25:28 +00:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-03-17 14:25:28 +00:00
|
|
|
platform_admin_user,
|
|
|
|
|
[],
|
2016-03-21 15:36:47 +00:00
|
|
|
will_succeed=False,
|
2018-02-28 18:13:29 +00:00
|
|
|
kwargs={'restrict_admin_usage': True})
|
2016-03-18 16:20:37 +00:00
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_no_user_returns_401_unauth(
|
2017-02-03 12:07:21 +00:00
|
|
|
client
|
2017-02-03 10:42:01 +00:00
|
|
|
):
|
2016-10-24 17:36:53 +01:00
|
|
|
from flask_login import current_user
|
2017-02-03 12:07:21 +00:00
|
|
|
assert not current_user.is_authenticated
|
2016-10-24 17:36:53 +01:00
|
|
|
_test_permissions(
|
2017-02-03 12:07:21 +00:00
|
|
|
client,
|
2016-10-24 17:36:53 +01:00
|
|
|
None,
|
|
|
|
|
[],
|
|
|
|
|
will_succeed=False)
|
|
|
|
|
|
|
|
|
|
|
2018-03-01 11:34:53 +00:00
|
|
|
def test_user_has_permissions_for_organisation(
|
|
|
|
|
client,
|
|
|
|
|
mocker,
|
|
|
|
|
):
|
|
|
|
|
user = _user_with_permissions()
|
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['organisations'] = ['org_1', 'org_2']
|
2018-03-01 11:34:53 +00:00
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
|
|
|
|
client.login(user)
|
|
|
|
|
|
|
|
|
|
request.view_args = {'org_id': 'org_2'}
|
|
|
|
|
|
|
|
|
|
@user_has_permissions()
|
|
|
|
|
def index():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
index()
|
|
|
|
|
|
|
|
|
|
|
2018-03-01 12:08:22 +00:00
|
|
|
def test_platform_admin_can_see_orgs_they_dont_have(
|
|
|
|
|
client,
|
|
|
|
|
platform_admin_user,
|
|
|
|
|
mocker,
|
|
|
|
|
):
|
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
|
|
|
platform_admin_user['organisations'] = []
|
2018-03-01 12:08:22 +00:00
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=platform_admin_user)
|
|
|
|
|
client.login(platform_admin_user)
|
|
|
|
|
|
|
|
|
|
request.view_args = {'org_id': 'org_2'}
|
|
|
|
|
|
|
|
|
|
@user_has_permissions()
|
|
|
|
|
def index():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
index()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cant_use_decorator_without_view_args(
|
|
|
|
|
client,
|
|
|
|
|
platform_admin_user,
|
|
|
|
|
mocker,
|
|
|
|
|
):
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=platform_admin_user)
|
|
|
|
|
client.login(platform_admin_user)
|
|
|
|
|
|
|
|
|
|
request.view_args = {}
|
|
|
|
|
|
|
|
|
|
@user_has_permissions()
|
|
|
|
|
def index():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
|
index()
|
|
|
|
|
|
|
|
|
|
|
2018-03-01 11:34:53 +00:00
|
|
|
def test_user_doesnt_have_permissions_for_organisation(
|
|
|
|
|
client,
|
|
|
|
|
mocker,
|
|
|
|
|
):
|
|
|
|
|
user = _user_with_permissions()
|
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['organisations'] = ['org_1', 'org_2']
|
2018-03-01 11:34:53 +00:00
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
|
|
|
|
client.login(user)
|
|
|
|
|
|
|
|
|
|
request.view_args = {'org_id': 'org_3'}
|
|
|
|
|
|
|
|
|
|
@user_has_permissions()
|
|
|
|
|
def index():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with pytest.raises(Forbidden):
|
|
|
|
|
index()
|
|
|
|
|
|
|
|
|
|
|
2018-11-19 15:26:43 +00:00
|
|
|
def test_user_with_no_permissions_to_service_goes_to_templates(
|
|
|
|
|
client,
|
|
|
|
|
mocker
|
|
|
|
|
):
|
|
|
|
|
user = _user_with_permissions()
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
|
|
|
|
client.login(user)
|
|
|
|
|
request.view_args = {'service_id': 'bar'}
|
|
|
|
|
|
|
|
|
|
@user_has_permissions()
|
|
|
|
|
def index():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
index()
|
|
|
|
|
|
|
|
|
|
|
2016-03-18 16:20:37 +00:00
|
|
|
def _user_with_permissions():
|
|
|
|
|
user_data = {'id': 999,
|
|
|
|
|
'name': 'Test User',
|
|
|
|
|
'password': 'somepassword',
|
|
|
|
|
'email_address': 'test@user.gov.uk',
|
|
|
|
|
'mobile_number': '+4412341234',
|
|
|
|
|
'state': 'active',
|
|
|
|
|
'failed_login_count': 0,
|
2018-02-28 18:13:29 +00:00
|
|
|
'permissions': {'foo': ['manage_users', 'manage_templates', 'manage_settings']},
|
2018-03-01 11:34:53 +00:00
|
|
|
'platform_admin': False,
|
|
|
|
|
'organisations': ['org_1', 'org_2'],
|
2019-06-05 12:14:50 +01:00
|
|
|
'services': ['foo', 'bar'],
|
|
|
|
|
'current_session_id': None,
|
2016-03-18 16:20:37 +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
|
|
|
return user_data
|
2018-02-28 15:37:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_translate_permissions_from_db_to_admin_roles():
|
|
|
|
|
db_perms = ['send_texts', 'send_emails', 'send_letters', 'manage_templates', 'some_unknown_permission']
|
|
|
|
|
roles = translate_permissions_from_db_to_admin_roles(db_perms)
|
|
|
|
|
assert roles == {'send_messages', 'manage_templates', 'some_unknown_permission'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_translate_permissions_from_admin_roles_to_db():
|
|
|
|
|
roles = ['send_messages', 'manage_templates', 'some_unknown_permission']
|
|
|
|
|
db_perms = translate_permissions_from_admin_roles_to_db(roles)
|
|
|
|
|
assert db_perms == {'send_texts', 'send_emails', 'send_letters', 'manage_templates', 'some_unknown_permission'}
|
2019-06-19 16:39:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
'user_services, user_organisations, expected_status, organisation_checked',
|
|
|
|
|
(
|
|
|
|
|
([SERVICE_ONE_ID], [], 200, False),
|
|
|
|
|
([SERVICE_ONE_ID, SERVICE_TWO_ID], [], 200, False),
|
|
|
|
|
([], [ORGANISATION_ID], 200, True),
|
|
|
|
|
([SERVICE_ONE_ID], [ORGANISATION_ID], 200, False),
|
|
|
|
|
([], [], 403, True),
|
|
|
|
|
([SERVICE_TWO_ID], [], 403, True),
|
|
|
|
|
([SERVICE_TWO_ID], [ORGANISATION_ID], 200, True),
|
|
|
|
|
([SERVICE_ONE_ID, SERVICE_TWO_ID], [ORGANISATION_ID], 200, False),
|
|
|
|
|
([], [ORGANISATION_TWO_ID], 403, True),
|
|
|
|
|
([], [ORGANISATION_ID, ORGANISATION_TWO_ID], 200, True),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
def test_services_pages_that_org_users_are_allowed_to_see(
|
|
|
|
|
client_request,
|
|
|
|
|
mocker,
|
|
|
|
|
api_user_active,
|
|
|
|
|
mock_get_usage,
|
|
|
|
|
mock_get_billable_units,
|
|
|
|
|
mock_get_free_sms_fragment_limit,
|
|
|
|
|
mock_get_service,
|
|
|
|
|
mock_get_invites_for_service,
|
|
|
|
|
mock_get_users_by_service,
|
|
|
|
|
mock_get_template_folders,
|
2020-03-20 08:42:33 +00:00
|
|
|
mock_get_organisation,
|
2019-06-19 16:39:13 +01:00
|
|
|
mock_has_jobs,
|
|
|
|
|
user_services,
|
|
|
|
|
user_organisations,
|
|
|
|
|
expected_status,
|
|
|
|
|
organisation_checked,
|
|
|
|
|
):
|
|
|
|
|
api_user_active['services'] = user_services
|
|
|
|
|
api_user_active['organisations'] = user_organisations
|
|
|
|
|
api_user_active['permissions'] = {
|
|
|
|
|
service_id: ['manage_service']
|
|
|
|
|
for service_id in user_services
|
|
|
|
|
}
|
|
|
|
|
service = service_json(
|
|
|
|
|
name='SERVICE WITH ORG',
|
|
|
|
|
id_=SERVICE_ONE_ID,
|
|
|
|
|
users=[api_user_active['id']],
|
|
|
|
|
organisation_id=ORGANISATION_ID,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mock_get_service = mocker.patch(
|
|
|
|
|
'app.notify_client.service_api_client.service_api_client.get_service',
|
|
|
|
|
return_value={'data': service}
|
|
|
|
|
)
|
|
|
|
|
client_request.login(
|
|
|
|
|
api_user_active,
|
|
|
|
|
service=service if SERVICE_ONE_ID in user_services else None,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
endpoints = (
|
|
|
|
|
'main.usage',
|
|
|
|
|
'main.manage_users',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for endpoint in endpoints:
|
|
|
|
|
client_request.get(
|
|
|
|
|
endpoint,
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
_expected_status=expected_status,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert mock_get_service.called is organisation_checked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_service_navigation_for_org_user(
|
|
|
|
|
client_request,
|
|
|
|
|
mocker,
|
|
|
|
|
api_user_active,
|
|
|
|
|
mock_get_usage,
|
|
|
|
|
mock_get_billable_units,
|
|
|
|
|
mock_get_free_sms_fragment_limit,
|
|
|
|
|
mock_get_service,
|
|
|
|
|
mock_get_invites_for_service,
|
|
|
|
|
mock_get_users_by_service,
|
2020-03-20 08:42:33 +00:00
|
|
|
mock_get_organisation,
|
2019-06-19 16:39:13 +01:00
|
|
|
):
|
|
|
|
|
api_user_active['services'] = []
|
|
|
|
|
api_user_active['organisations'] = [ORGANISATION_ID]
|
|
|
|
|
service = service_json(
|
|
|
|
|
id_=SERVICE_ONE_ID,
|
|
|
|
|
organisation_id=ORGANISATION_ID,
|
|
|
|
|
)
|
|
|
|
|
mocker.patch(
|
|
|
|
|
'app.service_api_client.get_service',
|
|
|
|
|
return_value={'data': service}
|
|
|
|
|
)
|
|
|
|
|
client_request.login(api_user_active, service=service)
|
|
|
|
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
'main.usage',
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
)
|
|
|
|
|
assert [
|
|
|
|
|
item.text.strip() for item in page.select('nav.navigation a')
|
|
|
|
|
] == [
|
|
|
|
|
'Usage',
|
2020-02-03 11:35:29 +00:00
|
|
|
'Team members',
|
2019-06-19 16:39:13 +01:00
|
|
|
]
|
2019-06-27 14:23:03 +01:00
|
|
|
|
|
|
|
|
|
2020-02-11 11:08:35 +00:00
|
|
|
@pytest.mark.parametrize('user_organisations, expected_menu_items, expected_status', [
|
|
|
|
|
(
|
|
|
|
|
[],
|
|
|
|
|
(
|
|
|
|
|
'Templates',
|
|
|
|
|
'Sent messages',
|
2020-03-16 12:08:38 +00:00
|
|
|
'Uploads',
|
2020-02-11 11:08:35 +00:00
|
|
|
'Team members',
|
|
|
|
|
),
|
|
|
|
|
403,
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
[ORGANISATION_ID],
|
|
|
|
|
(
|
|
|
|
|
'Templates',
|
|
|
|
|
'Sent messages',
|
2020-03-16 12:08:38 +00:00
|
|
|
'Uploads',
|
2020-02-11 11:08:35 +00:00
|
|
|
'Team members',
|
|
|
|
|
'Usage',
|
|
|
|
|
),
|
|
|
|
|
200,
|
|
|
|
|
),
|
|
|
|
|
])
|
|
|
|
|
def test_service_user_without_manage_service_permission_can_see_usage_page_when_org_user(
|
2020-02-03 16:05:46 +00:00
|
|
|
client_request,
|
|
|
|
|
mocker,
|
|
|
|
|
active_caseworking_user,
|
|
|
|
|
mock_has_no_jobs,
|
|
|
|
|
mock_get_usage,
|
|
|
|
|
mock_get_billable_units,
|
|
|
|
|
mock_get_free_sms_fragment_limit,
|
|
|
|
|
mock_get_service,
|
|
|
|
|
mock_get_invites_for_service,
|
|
|
|
|
mock_get_users_by_service,
|
2020-03-20 08:42:33 +00:00
|
|
|
mock_get_organisation,
|
2020-02-11 11:08:35 +00:00
|
|
|
mock_get_service_templates,
|
|
|
|
|
mock_get_template_folders,
|
|
|
|
|
user_organisations,
|
|
|
|
|
expected_status,
|
|
|
|
|
expected_menu_items,
|
2020-02-03 16:05:46 +00:00
|
|
|
):
|
|
|
|
|
active_caseworking_user['services'] = [SERVICE_ONE_ID]
|
2020-02-11 11:08:35 +00:00
|
|
|
active_caseworking_user['organisations'] = user_organisations
|
2020-02-03 16:05:46 +00:00
|
|
|
service = service_json(
|
|
|
|
|
id_=SERVICE_ONE_ID,
|
|
|
|
|
organisation_id=ORGANISATION_ID,
|
|
|
|
|
)
|
|
|
|
|
mocker.patch(
|
|
|
|
|
'app.service_api_client.get_service',
|
|
|
|
|
return_value={'data': service}
|
|
|
|
|
)
|
|
|
|
|
client_request.login(active_caseworking_user, service=service)
|
|
|
|
|
page = client_request.get(
|
2020-02-11 11:08:35 +00:00
|
|
|
'main.choose_template',
|
2020-02-03 16:05:46 +00:00
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
)
|
2020-02-11 11:08:35 +00:00
|
|
|
assert tuple(
|
2020-02-03 16:05:46 +00:00
|
|
|
item.text.strip() for item in page.select('nav.navigation a')
|
2020-02-11 11:08:35 +00:00
|
|
|
) == expected_menu_items
|
|
|
|
|
|
|
|
|
|
client_request.get(
|
|
|
|
|
'main.usage',
|
|
|
|
|
service_id=SERVICE_ONE_ID,
|
|
|
|
|
_expected_status=expected_status,
|
|
|
|
|
)
|
2020-02-03 16:05:46 +00:00
|
|
|
|
|
|
|
|
|
2019-06-27 14:23:03 +01:00
|
|
|
def get_name_of_decorator_from_ast_node(node):
|
|
|
|
|
if isinstance(node, ast.Name):
|
|
|
|
|
return str(node.id)
|
|
|
|
|
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
|
|
|
|
|
return get_name_of_decorator_from_ast_node(node.func)
|
2019-07-01 15:22:08 +01:00
|
|
|
if isinstance(node, ast.Attribute):
|
|
|
|
|
return node.value.id
|
2019-07-01 13:45:21 +01:00
|
|
|
return '{}.{}'.format(node.func.value.id, node.func.attr)
|
2019-06-27 14:23:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_decorators_for_function(function):
|
|
|
|
|
for node in ast.walk(ast.parse(inspect.getsource(function))):
|
|
|
|
|
if isinstance(node, ast.FunctionDef):
|
|
|
|
|
for decorator in node.decorator_list:
|
|
|
|
|
yield get_name_of_decorator_from_ast_node(decorator)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SERVICE_ID_ARGUMENT = 'service_id'
|
|
|
|
|
ORGANISATION_ID_ARGUMENT = 'org_id'
|
|
|
|
|
|
|
|
|
|
|
2019-07-01 15:22:08 +01:00
|
|
|
def get_routes_and_decorators(argument_name=None):
|
2019-06-27 14:23:03 +01:00
|
|
|
import app.main.views as views
|
|
|
|
|
for module_name, module in inspect.getmembers(views):
|
|
|
|
|
for function_name, function in inspect.getmembers(module):
|
2019-07-01 15:22:08 +01:00
|
|
|
if inspect.isfunction(function):
|
2019-06-27 14:23:03 +01:00
|
|
|
decorators = list(get_decorators_for_function(function))
|
2019-07-01 15:22:08 +01:00
|
|
|
if 'main.route' in decorators and (
|
|
|
|
|
not argument_name or
|
|
|
|
|
argument_name in inspect.signature(function).parameters.keys()
|
|
|
|
|
):
|
2019-06-27 14:23:03 +01:00
|
|
|
yield '{}.{}'.format(module_name, function_name), decorators
|
|
|
|
|
|
|
|
|
|
|
2019-07-01 13:45:21 +01:00
|
|
|
def format_decorators(decorators, indent=8):
|
|
|
|
|
return '\n'.join(
|
|
|
|
|
'{}@{}'.format(' ' * indent, decorator)
|
|
|
|
|
for decorator in decorators
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-06-27 14:23:03 +01:00
|
|
|
def test_code_to_extract_decorators_works_with_known_examples():
|
|
|
|
|
assert (
|
|
|
|
|
'templates.choose_template',
|
2019-11-13 13:38:02 +00:00
|
|
|
['main.route', 'main.route', 'main.route', 'main.route', 'main.route', 'main.route', 'user_has_permissions'],
|
2019-06-27 14:23:03 +01:00
|
|
|
) in list(
|
2019-07-01 15:22:08 +01:00
|
|
|
get_routes_and_decorators(SERVICE_ID_ARGUMENT)
|
2019-06-27 14:23:03 +01:00
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
'organisations.organisation_dashboard',
|
2019-07-01 15:22:08 +01:00
|
|
|
['main.route', 'user_has_permissions'],
|
2019-06-27 14:23:03 +01:00
|
|
|
) in list(
|
2019-07-01 15:22:08 +01:00
|
|
|
get_routes_and_decorators(ORGANISATION_ID_ARGUMENT)
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
'platform_admin.platform_admin',
|
|
|
|
|
['main.route', 'user_is_platform_admin'],
|
|
|
|
|
) in list(
|
|
|
|
|
get_routes_and_decorators()
|
2019-06-27 14:23:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-07-01 15:22:08 +01:00
|
|
|
def test_routes_have_permissions_decorators():
|
2019-06-27 14:23:03 +01:00
|
|
|
|
|
|
|
|
for endpoint, decorators in (
|
2019-07-01 15:22:08 +01:00
|
|
|
list(get_routes_and_decorators(SERVICE_ID_ARGUMENT)) +
|
|
|
|
|
list(get_routes_and_decorators(ORGANISATION_ID_ARGUMENT))
|
2019-06-27 14:23:03 +01:00
|
|
|
):
|
2019-07-01 13:45:21 +01:00
|
|
|
file, function = endpoint.split('.')
|
2019-07-01 15:22:08 +01:00
|
|
|
|
|
|
|
|
assert 'user_is_logged_in' not in decorators, (
|
|
|
|
|
'@user_is_logged_in used on service or organisation specific endpoint\n'
|
|
|
|
|
'Use @user_has_permissions() or @user_is_platform_admin only\n'
|
|
|
|
|
'app/main/views/{}.py::{}\n'
|
|
|
|
|
).format(file, function)
|
|
|
|
|
|
2019-06-27 14:23:03 +01:00
|
|
|
if 'user_is_platform_admin' in decorators:
|
2019-07-01 15:22:08 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
assert 'user_has_permissions' in decorators, (
|
|
|
|
|
'Missing @user_has_permissions decorator\n'
|
|
|
|
|
'Use @user_has_permissions() or @user_is_platform_admin instead\n'
|
|
|
|
|
'app/main/views/{}.py::{}\n'
|
|
|
|
|
).format(file, function)
|
|
|
|
|
|
2019-11-01 10:43:01 +00:00
|
|
|
for _endpoint, decorators in get_routes_and_decorators():
|
2019-07-01 15:22:08 +01:00
|
|
|
|
|
|
|
|
assert 'login_required' not in decorators, (
|
|
|
|
|
'@login_required found\n'
|
|
|
|
|
'For consistency, use @user_is_logged_in() instead (from app.utils)\n'
|
|
|
|
|
'app/main/views/{}.py::{}\n'
|
|
|
|
|
).format(file, function)
|
|
|
|
|
|
|
|
|
|
if 'user_is_platform_admin' in decorators:
|
|
|
|
|
assert 'user_has_permissions' not in decorators, (
|
|
|
|
|
'@user_has_permissions and @user_is_platform_admin decorating same function\n'
|
|
|
|
|
'You can only use one of these at a time\n'
|
|
|
|
|
'app/main/views/{}.py::{}\n'
|
|
|
|
|
).format(file, function)
|
|
|
|
|
assert 'user_is_logged_in' not in decorators, (
|
|
|
|
|
'@user_is_logged_in used with @user_is_platform_admin\n'
|
|
|
|
|
'Use @user_is_platform_admin only\n'
|
|
|
|
|
'app/main/views/{}.py::{}\n'
|
|
|
|
|
).format(file, function)
|
2019-11-04 11:07:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_routes_require_uuids(client_request):
|
|
|
|
|
for rule in current_app.url_map.iter_rules():
|
|
|
|
|
for param in re.findall('<([^>]*)>', rule.rule):
|
|
|
|
|
if '_id' in param and not param.startswith('uuid:'):
|
|
|
|
|
pytest.fail((
|
|
|
|
|
'Should be <uuid:{}> in {}'
|
|
|
|
|
).format(param, rule.rule))
|