Commit 89b7823e authored by parciak's avatar parciak
Browse files

Documentation to test cases and changing invalid archive ids to return 200.


Signed-off-by: parciak's avatarMarcel Parciak <marcel.parciak@gmail.com>
parent 0c9b1886
......@@ -102,10 +102,11 @@ def receive(payload: awmodels.RequestReceive, background_tasks: BackgroundTasks)
archive_id = payload.params.message.payload.archive_id
if not stores.is_valid_archive(archive_id):
raise errors.ActiveWorkflowException(
"Invalid value for `archive_id`",
"The `archive_id` you supplied is not available in CDSTAR.",
response.result.errors.append(
f"Archive {archive_id} is not available in CDSTAR."
)
return response
if utils.annotation_in_progress(archive_id):
response.result.logs.append(
f"Archive {archive_id} is currently being processed."
......
......@@ -18,12 +18,15 @@ from annotator import config
client = TestClient(app)
generated_files_count = 4
file_list_limit = 2
generated_files_count = 250
file_list_limit = 50
@pytest.fixture(scope="module")
def cdstar_archive():
"""
Creates a cdstar Archive consisting of dummy data and uploads it to CDSTAR for testing.
"""
cdstar = pycdstar3.CDStar(
config.BasicSettings().cdstar_uri,
auth=(config.BasicSettings().cdstar_user, config.BasicSettings().cdstar_pass),
......@@ -42,6 +45,9 @@ def cdstar_archive():
def delete_metadata(archive_id: str):
"""
Deletes metadata from CouchDB that has been created using the agent.
"""
with cloudant.couchdb(
config.BasicSettings().couch_user,
config.BasicSettings().couch_pass,
......@@ -57,6 +63,10 @@ def delete_metadata(archive_id: str):
def is_schemaorg_jsonld(json: Dict[str, Any]) -> bool:
"""
Return true if `json` contains `@context`, `@id` and `@type` as well as `@context` refers to
`http://schema.org`.
"""
keys = json.keys()
return (
"@context" in keys
......@@ -67,11 +77,18 @@ def is_schemaorg_jsonld(json: Dict[str, Any]) -> bool:
def is_schemaorg_reference(reference: Dict[str, Any]) -> bool:
"""
Returns true if `reference` contains a valid JSON-LD reference, i.e. contains keys `@type`
and `@id`.
"""
keys = reference.keys()
return "@type" in keys and "@id" in keys
def get_json_of_uri(uri: str, auth: Tuple[str, str] = None) -> Dict[str, Any]:
"""
Retrieves the JSON response of `uri` under BasicAuth `auth` if supplied.
"""
# Make the request and assert it was successful
r = requests.get(uri, auth=auth)
assert r.status_code >= 200 and r.status_code <= 299
......@@ -83,18 +100,26 @@ def get_json_of_uri(uri: str, auth: Tuple[str, str] = None) -> Dict[str, Any]:
def test_invalid_request_01():
"""
Tests if an invalid request yields a 405 response.
"""
response = client.get(f"/{endpoint_name}")
assert response.status_code == 405
def test_invalid_request_02():
"""
Tests if a invalid method yields an user error response.
"""
payload = {"method": "invalid"}
response = client.post(f"/{endpoint_name}", json=payload)
# TODO: should that be a 422?
assert response.status_code == 422
assert response.status_code >= 400 and response.status_code <= 499
def test_register_01():
"""
Tests if a register request yields a valid AW response.
"""
register_request = {"method": "register", "params": {}}
response = client.post(f"/{endpoint_name}", json=register_request)
assert response.status_code == 200
......@@ -109,9 +134,9 @@ def test_register_01():
assert type(response_data["result"]["default_options"]) == dict
def test_receive_01(cdstar_archive):
def post_valid_receive_request(cdstar_archive: str) -> Dict[str, Any]:
"""
Tests if a JSON-LD using Schema.org is uploaded to CouchDB.
Tests if a valid Receive request to the agent yields a valid AW response.
"""
register_request = {
"method": "receive",
......@@ -124,6 +149,19 @@ def test_receive_01(cdstar_archive):
}
response = client.post(f"/{endpoint_name}", json=register_request)
assert response.status_code >= 200 and response.status_code <= 299
json = response.json()
assert json
assert "result" in json.keys()
for key in ["errors", "logs", "memory", "messages"]:
assert key in json["result"].keys()
return json
def test_receive_01(cdstar_archive):
"""
Tests if a JSON-LD using Schema.org is uploaded to CouchDB.
"""
post_valid_receive_request(cdstar_archive)
assert os.path.exists(
os.path.join(tempfile.gettempdir(), f"meta_{cdstar_archive}.json")
)
......@@ -144,17 +182,7 @@ def test_receive_02(cdstar_archive):
"""
Tests if the uploaded archive metadata matches the CDSTAR archive.
"""
register_request = {
"method": "receive",
"params": {
"message": {"payload": {"archive_id": cdstar_archive}},
"options": {"annotation": {}},
"memory": {},
"credentials": {},
},
}
response = client.post(f"/{endpoint_name}", json=register_request)
assert response.status_code >= 200 and response.status_code <= 299
post_valid_receive_request(cdstar_archive)
# external request: use requests directly instead of the TestClient
cdstar_json = get_json_of_uri(
......@@ -185,17 +213,7 @@ def test_receive_03(cdstar_archive):
"""
Tests if all files of an CDSTAR archive are contained in the CouchDB as metadata.
"""
register_request = {
"method": "receive",
"params": {
"message": {"payload": {"archive_id": cdstar_archive}},
"options": {"annotation": {}},
"memory": {},
"credentials": {},
},
}
response = client.post(f"/{endpoint_name}", json=register_request)
assert response.status_code >= 200 and response.status_code <= 299
post_valid_receive_request(cdstar_archive)
# Check if metadata is available for each file of CDSTAR
for file_offset in range(0, generated_files_count, file_list_limit):
......@@ -237,17 +255,7 @@ def test_receive_04(cdstar_archive):
"""
Checks the validity of refences in the metadata store.
"""
register_request = {
"method": "receive",
"params": {
"message": {"payload": {"archive_id": cdstar_archive}},
"options": {"annotation": {}},
"memory": {},
"credentials": {},
},
}
response = client.post(f"/{endpoint_name}", json=register_request)
assert response.status_code >= 200 and response.status_code <= 299
post_valid_receive_request(cdstar_archive)
meta_json = get_json_of_uri(
f"{config.BasicSettings().couch_uri}/{config.BasicSettings().couch_db}/{cdstar_archive}",
......@@ -278,21 +286,13 @@ def test_receive_04(cdstar_archive):
def test_receive_invalid_archive_01():
register_request = {
"method": "receive",
"params": {
"message": {"payload": {"archive_id": "invalid_archive_id"}},
"options": {},
"memory": {},
"credentials": {},
},
}
response = client.post(f"/{endpoint_name}", json=register_request)
assert response.status_code == 400
response_data = response.json()
assert "result" in response_data.keys()
for key in ["errors", "logs", "memory", "messages"]:
assert key in response_data["result"]
"""
Tests supplying an invalid archive id to receive.
"""
response_data = post_valid_receive_request("invalid_archive_id")
# Assume an error message is supplied while no logs or messages are emitted.
assert len(response_data["result"]["errors"]) >= 1
assert len(response_data["result"]["logs"]) == 0
assert len(response_data["result"]["messages"]) == 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment