Ensure that objects that are written to the database for individual tests are deleted when the test is exited.
285 lines
8.6 KiB
Python
285 lines
8.6 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from itertools import count
|
|
from typing import Sequence
|
|
|
|
import pytest
|
|
import yaml
|
|
from starlette.status import HTTP_201_CREATED
|
|
|
|
from dump_things_service import (
|
|
HTTP_200_OK,
|
|
HTTP_404_NOT_FOUND,
|
|
)
|
|
from dump_things_service.instance_state import get_instance_state
|
|
|
|
delete_record = {
|
|
'schema_type': 'abc:Person',
|
|
'pid': 'abc:delete-me',
|
|
'given_name': 'Detlef',
|
|
}
|
|
|
|
|
|
def _add_records(
|
|
test_client,
|
|
urls: Sequence[str],
|
|
tokens: Sequence[str],
|
|
json_objects: Sequence[dict],
|
|
):
|
|
assert len(urls) == len(tokens) == len(json_objects), \
|
|
'URLs, tokens, and JSON objects must have the same length'
|
|
|
|
for i, (url, token, json_object) in enumerate(zip(urls, tokens, json_objects)):
|
|
response = test_client.post(
|
|
url,
|
|
headers={'x-dumpthings-token': token},
|
|
json=json_object,
|
|
)
|
|
assert response.status_code in (HTTP_200_OK, HTTP_201_CREATED)
|
|
|
|
|
|
def _remove_records(
|
|
test_client,
|
|
urls: Sequence[str],
|
|
tokens: Sequence[str],
|
|
json_objects: Sequence[dict],
|
|
):
|
|
assert len(urls) == len(tokens) == len(json_objects), \
|
|
'URLs, tokens, and JSON objects must have the same length'
|
|
|
|
for i, (url, token, json_object) in enumerate(zip(urls, tokens, json_objects)):
|
|
response = test_client.delete(
|
|
f'{url}?pid={json_object["pid"]}',
|
|
headers={'x-dumpthings-token': token},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('paginate', ['', 'p/'])
|
|
@pytest.mark.parametrize('class_name', ['', 'Person'])
|
|
def test_read_curated_records(
|
|
fastapi_client_simple,
|
|
paginate,
|
|
class_name,
|
|
):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
response = test_client.get(
|
|
f'/collection_1/curated/records/{paginate}{class_name}',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
json_object = response.json()
|
|
|
|
if 'items' in json_object:
|
|
assert len(json_object['items']) == 3, f"Expected 3 items, got {len(json_object['items'])}: {json_object['items']}"
|
|
else:
|
|
assert len(json_object) == 3
|
|
|
|
for pattern, occurrences in (('%25wolf%25', 1), ('%25cura%25', 2)):
|
|
test_client, _, _ = fastapi_client_simple
|
|
response = test_client.get(
|
|
f'/collection_8/curated/records/{paginate}{class_name}?matching={pattern}',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
json_object = response.json()
|
|
if 'items' in json_object:
|
|
assert len(json_object['items']) == occurrences
|
|
else:
|
|
assert len(json_object) == occurrences
|
|
|
|
|
|
def test_read_curated_records_by_pid(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
response = test_client.get(
|
|
'/no_such_collection/curated/records/',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_unknown_collection(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
response = test_client.get(
|
|
'/no_such_collection/curated/records/',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_curated_delete(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
response = test_client.post(
|
|
'/collection_8/curated/record/Person',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
json=delete_record,
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
response = test_client.get(
|
|
'/collection_8/curated/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json()['pid'] == 'abc:delete-me'
|
|
|
|
response = test_client.delete(
|
|
'/collection_8/curated/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() is True
|
|
|
|
response = test_client.get(
|
|
'/collection_8/curated/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert response.json() is None
|
|
|
|
response = test_client.delete(
|
|
'/collection_8/curated/record?pid=abc:delete-me',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_audit_backend(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
record_id = 'abc:audit-trailed'
|
|
names = 'Frederick', 'Johny'
|
|
tokens = 'token_1_xxxxx', 'token_curator'
|
|
user_names = 'test_user_1_curator', 'test_curator'
|
|
json_objects = tuple(
|
|
{
|
|
'schema_type': 'abc:Person',
|
|
'pid': record_id,
|
|
'given_name': name,
|
|
}
|
|
for name in names
|
|
)
|
|
|
|
for i in range(2):
|
|
response = test_client.post(
|
|
f'/collection_1/curated/record/Person?author_id=author_{i}@www.org',
|
|
headers={'x-dumpthings-token': tokens[i]},
|
|
json=json_objects[i],
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
instance_state = get_instance_state()
|
|
audit_backend = instance_state.audit_backends['collection_1'][0]
|
|
|
|
changes = audit_backend.get_audit_log(record_id)
|
|
_ensure_entries_in_audit_log(
|
|
changes,
|
|
[
|
|
(
|
|
user_names[i],
|
|
f'author_{i}@www.org',
|
|
json_objects[i],
|
|
)
|
|
for i in range(2)
|
|
]
|
|
)
|
|
|
|
response = test_client.delete(
|
|
f'/collection_1/curated/record?pid={record_id}',
|
|
headers={'x-dumpthings-token': tokens[i]},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
|
|
def _ensure_entries_in_audit_log(
|
|
audit_log: dict,
|
|
records: list[tuple[str, str, dict]],
|
|
):
|
|
""" Check that the entries defined in `records` are present in the `audit_log`.
|
|
|
|
:param audit_log: the audit log to check
|
|
:param records: the records that are expected to be present in the audit log,
|
|
each record is a tuple of (user_name, author_id, json_object)
|
|
:return: None
|
|
"""
|
|
found = 0
|
|
for time_stamp, entry in audit_log.items():
|
|
user_name, author_id, _diff, yaml_doc = entry
|
|
if any(
|
|
record[0] == user_name and
|
|
record[1] == author_id and
|
|
record[2] == yaml.safe_load(yaml_doc)
|
|
for record in records
|
|
):
|
|
found += 1
|
|
assert found == len(records), f"Expected {len(records)} matching entries in audit log, found {found}"
|
|
|
|
|
|
|
|
def test_audit_backend_auto_flush(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
record_id = 'abc:audit-trailed'
|
|
names = 'Robert', 'Anton'
|
|
tokens = 'token_1_xxxxx', 'token_curator'
|
|
json_objects = tuple(
|
|
{
|
|
'schema_type': 'abc:Person',
|
|
'pid': record_id,
|
|
'given_name': name,
|
|
}
|
|
for name in names
|
|
)
|
|
|
|
for i in range(2):
|
|
response = test_client.post(
|
|
f'/collection_1/curated/record/Person?author_id=author_{i}@www.org',
|
|
headers={'x-dumpthings-token': tokens[i]},
|
|
json=json_objects[i],
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
instance_state = get_instance_state()
|
|
audit_backend = instance_state.audit_backends['collection_1'][0]
|
|
|
|
assert audit_backend.current_change_set, 'expected unpersisted changes in audit log'
|
|
|
|
for i in count():
|
|
if not audit_backend.current_change_set:
|
|
break
|
|
if (i + 1) == 10:
|
|
msg = 'auto flush did not trigger within 10 seconds'
|
|
raise ValueError(msg)
|
|
time.sleep(1)
|
|
|
|
response = test_client.delete(
|
|
f'/collection_1/curated/record?pid={record_id}',
|
|
headers={'x-dumpthings-token': 'token_1_xxxxx'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
|
|
|
|
def test_inbox_creation_on_curation(fastapi_client_simple):
|
|
test_client, store_path, _ = fastapi_client_simple
|
|
|
|
collection_name = 'collection_1'
|
|
test_record = {
|
|
'pid': 'abc:inbox_creation_on_curation',
|
|
}
|
|
|
|
incoming_label = 'non-existing-abcdefgh'
|
|
|
|
assert not (store_path / 'incoming' / collection_name / incoming_label).exists()
|
|
response = test_client.post(
|
|
f'/{collection_name}/incoming/{incoming_label}/record/Thing',
|
|
headers={'x-dumpthings-token': 'token_curator'},
|
|
json=test_record,
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
assert (store_path / 'incoming' / collection_name / incoming_label).exists()
|