Ensure that objects that are written to the database for individual tests are deleted when the test is exited.
139 lines
4.8 KiB
Python
139 lines
4.8 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, result in (
|
|
('p1', {}, ('1=0', ())),
|
|
(
|
|
'p1',
|
|
{'p1', 'p2'},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') is NULL or json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') in :active_policies",
|
|
('policy_annotation_tag', 'active_policies'),
|
|
),
|
|
),
|
|
(
|
|
'p1',
|
|
{'p2'},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') in :active_policies",
|
|
('policy_annotation_tag', 'active_policies'),
|
|
),
|
|
),
|
|
(
|
|
None,
|
|
{'p1'},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') is NULL or json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') in :active_policies",
|
|
('policy_annotation_tag', 'active_policies'),
|
|
),
|
|
),
|
|
(
|
|
None,
|
|
{},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') is NULL",
|
|
('policy_annotation_tag',),
|
|
),
|
|
),
|
|
):
|
|
r = _SQLiteBackend._get_policy_clause(
|
|
policy_annotation_tag='tag',
|
|
default_policy=default_policy,
|
|
active_policies=active_policies,
|
|
)
|
|
assert r == result, f"Expected: '{result}', 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/Person',
|
|
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, _, admin_token = fastapi_client_simple
|
|
|
|
collection_request = {
|
|
'name': 'collection_x_test_policy_mapping_errors',
|
|
'default_token': 'test_default_token',
|
|
'curated': 'curated/collection_x_test_policy_mapping_errors',
|
|
'schema': test_schema_location,
|
|
'incoming': 'incoming/collection_x_test_policy_mapping_errors',
|
|
'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.post(
|
|
'/collections',
|
|
headers={'x-dumpthings-token': admin_token},
|
|
json=collection_request,
|
|
)
|
|
|
|
if 200 <= response.status_code < 300:
|
|
# Delete the test collection
|
|
delete_response = test_client.delete(
|
|
f"/collections/{collection_request['name']}",
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert delete_response.status_code == HTTP_200_OK
|
|
|
|
assert response.status_code == HTTP_400_BAD_REQUEST
|