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
|
2021-06-09 15:38:28 +01:00
|
|
|
from flask import current_app
|
2018-02-20 11:22:17 +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
|
|
|
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,
|
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
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,
|
2021-04-13 13:59:51 +01:00
|
|
|
mock_get_api_keys,
|
2020-02-11 11:08:35 +00:00
|
|
|
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))
|