Ensure that objects that are written to the database for individual tests are deleted when the test is exited.
483 lines
16 KiB
Python
483 lines
16 KiB
Python
from .. import (
|
|
HTTP_200_OK,
|
|
HTTP_400_BAD_REQUEST,
|
|
HTTP_401_UNAUTHORIZED,
|
|
HTTP_403_FORBIDDEN,
|
|
HTTP_404_NOT_FOUND,
|
|
HTTP_503_SERVICE_UNAVAILABLE,
|
|
__version__,
|
|
)
|
|
from . import schema_file
|
|
from .common import get_std_collection_names
|
|
from .create_store import (
|
|
given_name,
|
|
pid,
|
|
)
|
|
from .test_utils import basic_write_locations
|
|
|
|
extra_record = {
|
|
'schema_type': 'abc:Person',
|
|
'pid': 'abc:aaaa',
|
|
'given_name': 'DavidÖÄÜ',
|
|
}
|
|
delete_record = {
|
|
'schema_type': 'abc:Person',
|
|
'pid': 'abc:delete-me',
|
|
'given_name': 'Detlef',
|
|
}
|
|
unicode_name = 'AlienÖÄÜ-ß👽'
|
|
unicode_bytes = unicode_name.encode('utf-8')
|
|
unicode_record = {
|
|
'schema_type': 'abc:Person',
|
|
'pid': 'abc:unicode-test',
|
|
'given_name': unicode_name,
|
|
}
|
|
|
|
|
|
def test_search_by_pid(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
for collection_name in get_std_collection_names():
|
|
response = test_client.get(
|
|
f'/{collection_name}/record?pid={pid}',
|
|
headers={'x-dumpthings-token': 'token-all'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() == {
|
|
'schema_type': 'abc:Person',
|
|
'pid': pid,
|
|
'given_name': given_name,
|
|
}
|
|
|
|
|
|
def test_get_all(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
for i in range(1, 9):
|
|
response = test_client.get(
|
|
f'/collection_{i}/records/',
|
|
headers={'x-dumpthings-token': 'basic_access'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert len(response.json()) in (1, 3), (
|
|
f'Expected 1 or 3 records in the collection_{i}, got {len(response.json())}'
|
|
)
|
|
for i in range(1, 9):
|
|
response = test_client.get(
|
|
f'/collection_{i}/records/p/',
|
|
headers={'x-dumpthings-token': 'basic_access'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json()['total'] in (1, 3), (
|
|
f'Expected total to be 1 or 3 in the collection_{i}, got {response.json()["total"]}'
|
|
)
|
|
|
|
|
|
def test_delete(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
response = test_client.post(
|
|
'/collection_1/record/Person',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
json=delete_record,
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Check that the record exists
|
|
response = test_client.get(
|
|
'/collection_1/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json()['pid'] == 'abc:delete-me'
|
|
|
|
response = test_client.delete(
|
|
'/collection_1/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() is True
|
|
|
|
response = test_client.get(
|
|
'/collection_1/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() is None
|
|
|
|
response = test_client.delete(
|
|
'/collection_1/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_hashed_token(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
response = test_client.get(
|
|
f'/collection_1/record?pid={pid}',
|
|
headers={'x-dumpthings-token': 'token-hashed'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() == {
|
|
'schema_type': 'abc:Person',
|
|
'pid': pid,
|
|
'given_name': given_name,
|
|
}
|
|
# Ensure that the hashed token value is not compared verbatim
|
|
response = test_client.get(
|
|
f'/collection_1/record?pid={pid}',
|
|
headers={
|
|
'x-dumpthings-token': '1d6a827ef04170562ec35925c7057352dfcf69d24eff1171d5d1eaad0c10e66b',
|
|
},
|
|
)
|
|
assert response.status_code == HTTP_401_UNAUTHORIZED
|
|
|
|
|
|
def test_search_by_class(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
for i in range(1, 9):
|
|
response = test_client.get(
|
|
f'/collection_{i}/records/Thing',
|
|
headers={'x-dumpthings-token': 'token-all'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
json_result = response.json()
|
|
if len(json_result) == 3:
|
|
assert response.json() == [
|
|
{
|
|
'given_name': 'curated',
|
|
'pid': 'abc:curated',
|
|
'schema_type': 'abc:Person',
|
|
},
|
|
{
|
|
'given_name': 'mode_curated',
|
|
'pid': 'abc:mode_test',
|
|
'schema_type': 'abc:Person',
|
|
'annotations': {
|
|
'https://example.org/dumpthings/policy_set': 'policy_1',
|
|
},
|
|
},
|
|
{
|
|
'given_name': 'WolfgangÖÄß',
|
|
'pid': 'abc:some_timee@x.com',
|
|
'schema_type': 'abc:Person',
|
|
},
|
|
]
|
|
else:
|
|
# If only one record is present, it is the global test record
|
|
assert json_result == [
|
|
{
|
|
'given_name': 'WolfgangÖÄß',
|
|
'pid': 'abc:some_timee@x.com',
|
|
'schema_type': 'abc:Person',
|
|
},
|
|
]
|
|
|
|
|
|
def test_search_by_pid_no_token(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
for i in range(1, 9):
|
|
response = test_client.get(
|
|
f'/collection_{i}/record?pid={pid}',
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() == {
|
|
'schema_type': 'abc:Person',
|
|
'pid': pid,
|
|
'given_name': given_name,
|
|
}
|
|
|
|
|
|
def test_store_record(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
# Store a record in two collections
|
|
for i, token in basic_write_locations:
|
|
response = test_client.post(
|
|
f'/collection_{i}/record/Person',
|
|
headers={'x-dumpthings-token': token},
|
|
json=extra_record,
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Check that the existing record and the new records can be retrieved
|
|
# from both collections
|
|
for i, token in basic_write_locations:
|
|
response = test_client.get(
|
|
f'/collection_{i}/record?pid={extra_record["pid"]}',
|
|
headers={'x-dumpthings-token': token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() == extra_record
|
|
|
|
# Check that other collections do not report the new record
|
|
for i in range(3, 6):
|
|
response = test_client.get(
|
|
f'/collection_{i}/records/Person',
|
|
headers={'x-dumpthings-token': 'basic_access'},
|
|
)
|
|
# The following assertion works because the stored records don't contain
|
|
# annotations. If they would, the result would have to be cleaned
|
|
# before the containment-check.
|
|
assert extra_record not in response.json()
|
|
|
|
# Check that subclasses are retrieved
|
|
for i, token in basic_write_locations:
|
|
response = test_client.get(
|
|
f'/collection_{i}/records/Thing',
|
|
headers={'x-dumpthings-token': token},
|
|
)
|
|
assert extra_record in response.json()
|
|
assert {
|
|
'schema_type': 'abc:Person',
|
|
'pid': pid,
|
|
'given_name': given_name,
|
|
} in response.json()
|
|
|
|
# Check pagination
|
|
for i, token in basic_write_locations:
|
|
response = test_client.get(
|
|
f'/collection_{i}/records/p/Thing',
|
|
headers={'x-dumpthings-token': token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
for key in ('items', 'total', 'page', 'size', 'pages'):
|
|
assert key in response.json()
|
|
assert extra_record in response.json()['items']
|
|
assert {
|
|
'schema_type': 'abc:Person',
|
|
'pid': pid,
|
|
'given_name': given_name,
|
|
} in response.json()['items']
|
|
|
|
|
|
def test_encoding(fastapi_client_simple):
|
|
test_client, store_path, _ = fastapi_client_simple
|
|
|
|
# Store a record with non-ASCII characters in collections via the API. that
|
|
# will trigger the YAML-dumping, which should be checked
|
|
response = test_client.post(
|
|
'/collection_1/record/Person',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
json=unicode_record,
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Check that no '\\x'-encoding is present in files
|
|
for item in store_path.glob('**/*.yaml'):
|
|
encoded_content = item.read_bytes()
|
|
assert b'\\x' not in encoded_content
|
|
if b'Alien' in encoded_content:
|
|
assert unicode_bytes in encoded_content
|
|
|
|
|
|
def test_global_store_write_fails(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
for i in range(1, 9):
|
|
# Since we provide no token, the default token will be used. This will
|
|
# only allow reading from curated, not posting.
|
|
response = test_client.post(
|
|
f'/collection_{i}/record/Person',
|
|
json={'pid': extra_record['pid']},
|
|
)
|
|
assert response.status_code == HTTP_403_FORBIDDEN
|
|
|
|
|
|
def test_funky_pid(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
record_pid = 'dlflatsocial:contributors/someone'
|
|
for i, token in basic_write_locations:
|
|
response = test_client.post(
|
|
f'/collection_{i}/record/Person',
|
|
headers={'x-dumpthings-token': token},
|
|
json={'pid': record_pid},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Try to find it
|
|
for i, token in basic_write_locations:
|
|
response = test_client.get(
|
|
f'/collection_{i}/record?pid={record_pid}',
|
|
headers={'x-dumpthings-token': token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
|
|
def test_token_store_priority(fastapi_client_simple):
|
|
test_client, _store_dir, _ = fastapi_client_simple
|
|
|
|
# Post a record with the same pid as the global store's test record, but
|
|
# with different content.
|
|
response = test_client.post(
|
|
'/collection_1/record/Person',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
json={'pid': pid, 'given_name': 'DavidÖÄß'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Check that the new record is returned with the token
|
|
response = test_client.get(
|
|
f'/collection_1/record?pid={pid}',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json()['given_name'] == 'DavidÖÄß'
|
|
|
|
# Check that the global test record is returned with basic access
|
|
response = test_client.get(
|
|
f'/collection_1/record?pid={pid}',
|
|
headers={'x-dumpthings-token': 'basic_access'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json()['given_name'] == given_name
|
|
|
|
|
|
def test_unknown_token(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
# Check that fetching with an unknown token is handled gracefully
|
|
response = test_client.get(
|
|
'/collection_1/record?pid=abc:unknown-token',
|
|
headers={'x-dumpthings-token': 'unknown_token'},
|
|
)
|
|
assert response.status_code == HTTP_401_UNAUTHORIZED
|
|
|
|
# Check that posting with an unknown token is handled gracefully
|
|
response = test_client.post(
|
|
'/collection_1/record/Person',
|
|
json={'pid': 'abc:unknown-token'},
|
|
headers={'x-dumpthings-token': 'unknown_token'},
|
|
)
|
|
assert response.status_code == HTTP_401_UNAUTHORIZED
|
|
|
|
|
|
def test_curie_expansion(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
# Check that the pid is expanded correctly
|
|
response = test_client.get(
|
|
'/collection_1/record?pid=http%3A%2F%2Fexample.org%2Fperson-schema%2Fabc%2Fmode_test',
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() == {
|
|
'schema_type': 'abc:Person',
|
|
'pid': 'abc:mode_test',
|
|
'given_name': 'mode_curated',
|
|
'annotations': {'https://example.org/dumpthings/policy_set': 'policy_1'},
|
|
}
|
|
|
|
|
|
def test_server(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
response = test_client.get(
|
|
'/server',
|
|
)
|
|
test_schema_classes = [
|
|
'Thing',
|
|
'Agent',
|
|
'InstantaneousEvent',
|
|
'Person',
|
|
]
|
|
flat_social_classes = [
|
|
'Thing',
|
|
'Property',
|
|
'ValueSpecification',
|
|
'FlatThing',
|
|
'FlatProperty',
|
|
'AnnotationTag',
|
|
'Organization',
|
|
'Person',
|
|
'Project',
|
|
]
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() == {
|
|
'version': __version__,
|
|
'collections': [
|
|
{
|
|
'name': collection_name,
|
|
'schema': str(schema_file),
|
|
'classes': test_schema_classes,
|
|
}
|
|
for collection_name in get_std_collection_names()
|
|
]
|
|
+ [
|
|
{
|
|
'name': f'collection_dlflatsocial-{i}',
|
|
'schema': 'https://concepts.datalad.org/s/flat-social/unreleased.yaml',
|
|
'classes': flat_social_classes,
|
|
}
|
|
for i in range(1, 3)
|
|
],
|
|
}
|
|
|
|
|
|
def test_ignore_classes(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
for class_name in ('Organization', 'Project'):
|
|
response = test_client.post(
|
|
f'/collection_dlflatsocial-1/record/{class_name}',
|
|
headers={'x-dumpthings-token': 'token-all'},
|
|
json={'pid': f'dlflatsocial:c_{class_name}'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
response = test_client.post(
|
|
f'/collection_dlflatsocial-2/record/{class_name}',
|
|
headers={'x-dumpthings-token': 'token-all'},
|
|
json={'pid': f'dlflatsocial:c_{class_name}'},
|
|
)
|
|
assert response.status_code == HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_maintenance(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
# Ensure that only curators can put a collection in maintenance mode
|
|
response = test_client.post(
|
|
'/maintenance',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
json={'collection': 'collection_1', 'active': True},
|
|
)
|
|
assert response.status_code == HTTP_400_BAD_REQUEST
|
|
|
|
# Ensure unknown collections are caught in maintenance mode
|
|
response = test_client.post(
|
|
'/maintenance',
|
|
headers={'x-dumpthings-token': 'token_curator'},
|
|
json={'collection': 'collection_unknown', 'active': True},
|
|
)
|
|
assert response.status_code == HTTP_404_NOT_FOUND
|
|
|
|
response = test_client.post(
|
|
'/maintenance',
|
|
headers={'x-dumpthings-token': 'token_curator'},
|
|
json={'collection': 'collection_1', 'active': True},
|
|
)
|
|
|
|
response = test_client.get(
|
|
'/collection_1/record?pid=abc:something/',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_503_SERVICE_UNAVAILABLE
|
|
|
|
# Ensure unknown collections are caught in maintenance mode
|
|
response = test_client.get(
|
|
'/collection_unknown/record?pid=abc:something/',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_404_NOT_FOUND
|
|
|
|
# Deactivate maintenance mode
|
|
response = test_client.post(
|
|
'/maintenance',
|
|
headers={'x-dumpthings-token': 'token_curator'},
|
|
json={'collection': 'collection_1', 'active': False},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Ensure that
|
|
response = test_client.get(
|
|
'/collection_1/record?pid=abc:something/',
|
|
headers={'x-dumpthings-token': 'token-1'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|