dump-things-service/dump_things_service/tests/test_policies.py
Christian Monch 48697365d9
Some checks failed
Codespell / Check for spelling errors (push) Failing after 42s
Codespell / Check for spelling errors (pull_request) Failing after 42s
Ruff / Code linting (push) Successful in 51s
Ruff / Code linting (pull_request) Successful in 53s
Type annotation (PR) / check (pull_request) Failing after 1m20s
Test execution / Test-all (push) Successful in 1m27s
refactor: rename authorized_for to authorized_by
The configuration property that maps policies to
users is now called `authorized_by` to reflect
that a policy is matching and granting the
user access,
2026-10-02 08:46:39 +02:00

115 lines
3.9 KiB
Python

import pytest
from dump_things_service import (
HTTP_200_OK,
HTTP_400_BAD_REQUEST,
)
from dump_things_service.backends.sqlite import _SQLiteBackend
from dump_things_service.tests.fixtures import test_schema_location
def test_policy_clause():
for default_policy, active_policies, results in (
('p1', {}, '1=0'),
(
'p1',
{'p1', 'p2'},
[
"json_extract(object, '$.annotations.\"tag\"') is NULL or json_extract(object, '$.annotations.\"tag\"') in ('p1', 'p2')",
"json_extract(object, '$.annotations.\"tag\"') is NULL or json_extract(object, '$.annotations.\"tag\"') in ('p2', 'p1')",
],
),
('p1', {'p2'}, ["json_extract(object, '$.annotations.\"tag\"') in ('p2')"]),
(
None,
{'p1'},
[
"json_extract(object, '$.annotations.\"tag\"') is NULL or json_extract(object, '$.annotations.\"tag\"') in ('p1')"
],
),
(None, {}, ['json_extract(object, \'$.annotations."tag"\') is NULL']),
):
r = _SQLiteBackend._get_policy_clause(
policy_annotation_tag='tag',
default_policy=default_policy,
active_policies=active_policies,
)
assert r in results, f"Expected: any of '{results}', got: {r}"
r = _SQLiteBackend._get_policy_clause(
policy_annotation_tag=None,
default_policy=default_policy,
active_policies=active_policies,
)
assert r == '1=1', f"Expected: '1=1', got: {r}"
def test_policy_functionality(fastapi_client_simple):
test_client, _, _ = fastapi_client_simple
# Check that policy-less users can not access any record if a default
# policy is present.
response = test_client.get(
'/collection_8/records/Thing',
headers={'x-dumpthings-token': 'token-8'},
)
assert response.status_code == HTTP_200_OK
json_result = response.json()
assert json_result == []
# Check that users with default policy can access all records with no
# policy annotation or with the default policy annotation, but not records
# with other policy annotations.
response = test_client.get(
'/collection_8/records/Thing',
headers={'x-dumpthings-token': 'token-8-default-policy'},
)
assert response.status_code == HTTP_200_OK
json_result = response.json()
assert len(json_result) == 2
# Check that users without default policy can access records that have
# a different policy
response = test_client.get(
'/collection_8/records/Thing',
headers={'x-dumpthings-token': 'token-8-policy-1'},
)
assert response.status_code == HTTP_200_OK
json_result = response.json()
assert len(json_result) == 1
# The following will fail until the collection-creation code checks for
# non-existing auth sources in the policy mapping.
@pytest.mark.xfail
def test_policy_mapping_errors(fastapi_client_simple):
test_client, store_path, admin_token = fastapi_client_simple
collection_request = {
'name': 'collection_8',
'default_token': 'test_default_token',
'curated': 'curated/collection_8',
'schema': test_schema_location,
'incoming': 'incoming/collection_8',
'backend': {
'type': 'sqlite',
},
'policy_annotation_tag': 'https://example.org/dumpthings/policy_set',
'default_policy': 'p1',
'policy_mapping': [
{
'auth_source': 'xyz-something-not-existing',
'user_id': 'user_x',
'authorized_by': ['p1', 'p2'],
}
],
}
# Check that mappings to non-existing auth sources are rejected with a
# correct error message.
response = test_client.put(
'/collections',
headers={'x-dumpthings-token': admin_token},
json=collection_request,
)
assert response.status_code == HTTP_400_BAD_REQUEST