Ensure that objects that are written to the database for individual tests are deleted when the test is exited.
406 lines
14 KiB
Python
406 lines
14 KiB
Python
import hashlib
|
|
from pathlib import (
|
|
Path,
|
|
PurePosixPath,
|
|
)
|
|
from shutil import copyfile
|
|
|
|
import pytest
|
|
import yaml
|
|
from starlette.status import HTTP_400_BAD_REQUEST, HTTP_422_UNPROCESSABLE_CONTENT
|
|
|
|
from dump_things_service import (
|
|
HTTP_200_OK,
|
|
HTTP_201_CREATED,
|
|
HTTP_406_NOT_ACCEPTABLE,
|
|
HTTP_409_CONFLICT,
|
|
)
|
|
from dump_things_service.abstract_config import (
|
|
Configuration,
|
|
TokenCollectionConfig,
|
|
TokenModes,
|
|
dump_things_config_iri,
|
|
dump_things_private_collection_name,
|
|
g_recoverable_error,
|
|
get_config_backends,
|
|
get_conflicting_paths,
|
|
read_config,
|
|
)
|
|
from dump_things_service.collection import check_recoverable_errors
|
|
from dump_things_service.endpoints.v0.collection_endpoints import CollectionRequest
|
|
from dump_things_service.endpoints.v0.token_endpoints import TokenRequest
|
|
from dump_things_service.exceptions import ConfigError
|
|
from dump_things_service.instance_state import get_instance_state
|
|
from dump_things_service.tests import schema_file
|
|
|
|
reference_config_file = Path(__file__).parent / 'assets' / 'reference_config.yaml'
|
|
test_schema_file = Path(__file__).parent / 'testschema.yaml'
|
|
|
|
collection_request_pattern = CollectionRequest(
|
|
name='',
|
|
schema=str(schema_file),
|
|
default_token='test_default_token',
|
|
curated=PurePosixPath('curate_dir'),
|
|
incoming=PurePosixPath('incoming_dir'),
|
|
)
|
|
|
|
|
|
def test_illegal_collection_name_detection(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
for name in (
|
|
'collections',
|
|
'tokens',
|
|
'admin_tokens',
|
|
dump_things_private_collection_name,
|
|
):
|
|
response = test_client.post(
|
|
'/collections',
|
|
json={
|
|
**collection_request_pattern.model_dump(mode='json', by_alias=True),
|
|
'name': name,
|
|
},
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_409_CONFLICT
|
|
|
|
|
|
@pytest.mark.skip(
|
|
reason='Reuse detection is disabled to support existing old configurations',
|
|
)
|
|
def test_collection_dir_reuse_detection(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
for curated_path, incoming_path in (
|
|
('curated/collection_1', 'incoming/XXXX'),
|
|
('curated/XXXX', 'incoming/collection_1'),
|
|
('curated/collection_1', 'incoming/collection_2'),
|
|
):
|
|
response = test_client.post(
|
|
'/collections',
|
|
json={
|
|
**collection_request_pattern.model_dump(mode='json', by_alias=True),
|
|
'curated': curated_path,
|
|
'incoming': incoming_path,
|
|
},
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_409_CONFLICT
|
|
|
|
|
|
def test_scanner_error_detection(tmp_path_factory):
|
|
tmp_path = tmp_path_factory.mktemp('config_scanner_test')
|
|
|
|
config_backend, _audit_backend = get_config_backends(tmp_path)
|
|
config_backend.add_record(
|
|
iri=dump_things_config_iri,
|
|
class_name='DumpThingsConfig',
|
|
json_object={'pid': dump_things_config_iri},
|
|
)
|
|
|
|
md5_hexdigest = hashlib.md5(dump_things_config_iri.encode()).hexdigest()
|
|
config_file_path = (
|
|
config_backend.root / 'DumpThingsConfig' / f'{md5_hexdigest}.yaml'
|
|
)
|
|
config_file_path.write_text('collections: ::: -\n sdsdfsdf: xxx')
|
|
with pytest.raises(ConfigError):
|
|
read_config(tmp_path, force_reload=True)
|
|
|
|
|
|
def test_structure_error_detection(tmp_path_factory):
|
|
tmp_path = tmp_path_factory.mktemp('config_scanner_test')
|
|
|
|
config_backend, _audit_backend = get_config_backends(tmp_path)
|
|
config_backend.add_record(
|
|
iri=dump_things_config_iri,
|
|
class_name='DumpThingsConfig',
|
|
json_object={'pid': dump_things_config_iri},
|
|
)
|
|
|
|
md5_hexdigest = hashlib.md5(dump_things_config_iri.encode()).hexdigest()
|
|
config_file_path = (
|
|
config_backend.root / 'DumpThingsConfig' / f'{md5_hexdigest}.yaml'
|
|
)
|
|
config_file_path.write_text('type: 1\n')
|
|
with pytest.raises(ConfigError):
|
|
read_config(tmp_path, force_reload=True)
|
|
|
|
|
|
def test_missing_incoming_detection(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
# Add a collection without incoming
|
|
collection_request = CollectionRequest(
|
|
name='missing_incoming_detection_test',
|
|
default_token='Test XXXXX (CURATOR)',
|
|
curated=PurePosixPath('missing_incoming_detection'),
|
|
schema=str(schema_file),
|
|
)
|
|
|
|
response = test_client.post(
|
|
'/collections',
|
|
json=collection_request.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_201_CREATED
|
|
|
|
# Add a write token that references the collection, expect this to
|
|
# fail because the collection does not contain an incoming path
|
|
token_request = TokenRequest(
|
|
name='missing-incoming-token',
|
|
user_id='missing_incoming_user',
|
|
collections={
|
|
'missing_incoming_detection_test': TokenCollectionConfig(
|
|
mode=TokenModes.CURATOR,
|
|
incoming_label='',
|
|
),
|
|
},
|
|
)
|
|
|
|
# Check that a write token for a collection without incoming path cannot
|
|
# be created.
|
|
response = test_client.post(
|
|
'/tokens',
|
|
json=token_request.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_406_NOT_ACCEPTABLE
|
|
|
|
# Remove the collection without incoming path
|
|
response = test_client.delete(
|
|
'/collections/missing_incoming_detection_test',
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Add a collection with incoming path
|
|
collection_request.incoming = PurePosixPath(
|
|
'missing_incoming_detection_test_incoming',
|
|
)
|
|
response = test_client.post(
|
|
'/collections',
|
|
json=collection_request.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_201_CREATED
|
|
|
|
# Check that a write token for a collection with an incoming path but a
|
|
# missing label cannot be created.
|
|
response = test_client.post(
|
|
'/tokens',
|
|
json=token_request.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_406_NOT_ACCEPTABLE
|
|
|
|
# Check that a write token for a collection with an incoming path can be created
|
|
token_request.collections['missing_incoming_detection_test'] = (
|
|
TokenCollectionConfig(
|
|
mode=TokenModes.CURATOR,
|
|
incoming_label='test_incoming_label',
|
|
)
|
|
)
|
|
response = test_client.post(
|
|
'/tokens',
|
|
json=token_request.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_201_CREATED
|
|
|
|
# Remove the collection with the incoming path
|
|
response = test_client.delete(
|
|
'/collections/missing_incoming_detection_test',
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
# Check that a creation attempt for the collection without incoming path fails
|
|
collection_request.incoming = None
|
|
response = test_client.post(
|
|
'/collections',
|
|
json=collection_request.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_406_NOT_ACCEPTABLE
|
|
|
|
|
|
def test_submission_tags_handling(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
response = test_client.get(
|
|
'/collections/collection_8',
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
json_obj = response.json()
|
|
assert json_obj['submission_tags'] == {
|
|
'submitter_id_tag': 'no_default_id_tag',
|
|
'submission_time_tag': 'no_default_time_tag',
|
|
}
|
|
|
|
response = test_client.get(
|
|
'/collections/collection_1',
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
json_obj = response.json()
|
|
assert json_obj['submission_tags'] == {
|
|
'submitter_id_tag': 'http://purl.obolibrary.org/obo/NCIT_C54269',
|
|
'submission_time_tag': 'http://semanticscience.org/resource/SIO_001083',
|
|
}
|
|
|
|
|
|
def test_config_structure():
|
|
with reference_config_file.open() as f:
|
|
config_dict = yaml.safe_load(f)
|
|
_ = Configuration(**config_dict)
|
|
|
|
|
|
def test_schema_location_error_detection(fastapi_client_simple, tmp_path):
|
|
test_client, store_path, admin_token = fastapi_client_simple
|
|
|
|
missing_file = f'{tmp_path}/does-not-exist.yaml'
|
|
collection_name = 'test_schema_location_error_detection'
|
|
faulty_schema_location_collection = CollectionRequest(
|
|
name=collection_name,
|
|
schema=f'file:///{missing_file}',
|
|
default_token='test_default_token',
|
|
curated=PurePosixPath('curate_dir/test_schema_location_error_detection'),
|
|
incoming=PurePosixPath('incoming_dir/test_schema_location_error_detection'),
|
|
)
|
|
response = test_client.post(
|
|
'/collections',
|
|
json=faulty_schema_location_collection.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_201_CREATED
|
|
|
|
# Check that the collection is in recoverable errors
|
|
assert collection_name in g_recoverable_error
|
|
|
|
# Put a schema file in the location, perform the periodic activities and
|
|
# check that the collection is no longer in recoverable errors.
|
|
copyfile(test_schema_file, missing_file)
|
|
abstract_configuration = read_config(store_path)
|
|
instance_state = get_instance_state()
|
|
check_recoverable_errors(
|
|
abstract_configuration=abstract_configuration,
|
|
instance_state=instance_state,
|
|
)
|
|
|
|
# Check that the collection is no longer in recoverable errors
|
|
assert collection_name not in g_recoverable_error
|
|
|
|
response = test_client.delete(
|
|
f"/collections/{collection_name}",
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
|
|
def test_schema_content_error_detection(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
faulty_schema_collection = CollectionRequest(
|
|
name='test_schema_content_error_detection',
|
|
schema='http://example.com/does-not-exist',
|
|
default_token='test_default_token',
|
|
curated=PurePosixPath('curated/test_schema_content_error_detection'),
|
|
incoming=PurePosixPath('incoming/test_schema_content_error_detection'),
|
|
)
|
|
response = test_client.post(
|
|
'/collections',
|
|
json=faulty_schema_collection.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_201_CREATED
|
|
|
|
# Ensure that the collection is in recoverable errors
|
|
assert faulty_schema_collection.name in g_recoverable_error
|
|
|
|
response = test_client.delete(
|
|
f"/collections/{faulty_schema_collection.name}",
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
assert faulty_schema_collection.name not in g_recoverable_error
|
|
|
|
|
|
def test_policy_location_error_detection(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
faulty_schema_collection = CollectionRequest(
|
|
name='test_faulty_schema',
|
|
schema=str(schema_file),
|
|
policy_info_location='http://example.com/does-not-exist',
|
|
default_token='test_default_token',
|
|
curated=PurePosixPath('curate_dir'),
|
|
incoming=PurePosixPath('incoming_dir'),
|
|
)
|
|
response = test_client.post(
|
|
'/collections',
|
|
json=faulty_schema_collection.model_dump(mode='json', by_alias=True),
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_400_BAD_REQUEST
|
|
|
|
|
|
def test_config_structure_error_detection(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
response = test_client.post(
|
|
'/collections',
|
|
json={'some_crazy': 'content'},
|
|
headers={'x-dumpthings-token': admin_token},
|
|
)
|
|
assert response.status_code == HTTP_422_UNPROCESSABLE_CONTENT
|
|
|
|
|
|
def test_is_directory_configuration_valid():
|
|
setup_info = (
|
|
('c0.0', 'curated/c1', 'incoming/c1'),
|
|
('c0.1', 'curated/c1/c2', 'incoming/c2'),
|
|
('c0.2', 'curated', 'incoming'),
|
|
('c0.3', 'curated_3', 'curated_3'),
|
|
('c1.0', 'curated1', 'incoming1'),
|
|
('c1.1', 'curated1/c1/c2', 'incoming1/c2'),
|
|
('c1.2', 'curated1/c1/c2', 'incoming1/c3'),
|
|
('c1.3', 'curated1', 'incoming_x'),
|
|
('c2.0', 'curated1/c1/c2', 'curated1'),
|
|
)
|
|
|
|
abstract_config = Configuration(
|
|
type='collections',
|
|
version=2,
|
|
collections={
|
|
collection_name: {
|
|
'schema': '',
|
|
'curated': PurePosixPath(curated),
|
|
'incoming': PurePosixPath(incoming),
|
|
'default_token': '',
|
|
}
|
|
for collection_name, curated, incoming in setup_info
|
|
},
|
|
)
|
|
conflicts = get_conflicting_paths(abstract_config)
|
|
assert len(conflicts) == 17
|
|
|
|
setup_info = (
|
|
('c1', 'curated/c1', 'incoming/c1'),
|
|
('c2', 'curated/c2', 'incoming/c2'),
|
|
('c3', 'common', 'common'),
|
|
)
|
|
|
|
abstract_config = Configuration(
|
|
type='collections',
|
|
version=2,
|
|
collections={
|
|
collection_name: {
|
|
'schema': '',
|
|
'curated': PurePosixPath(curated),
|
|
'incoming': PurePosixPath(incoming),
|
|
'default_token': '',
|
|
}
|
|
for collection_name, curated, incoming in setup_info
|
|
},
|
|
)
|
|
assert not get_conflicting_paths(abstract_config)
|