Skip to content

Commit

Permalink
Added: required flag for testing so we can pass tests when a non-requ…
Browse files Browse the repository at this point in the history
…ired endpoint is not implemented
  • Loading branch information
GeraldIr committed Nov 28, 2024
1 parent 7017823 commit 5256fc7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 41 deletions.
27 changes: 14 additions & 13 deletions src/openeo_test_suite/lib/compliance_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_endpoint(
expected_status_codes: Union[list[int], int] = [200],
return_response: bool = False,
mock_response_content: bytes = None,
required: bool = True,
):
full_endpoint_url = f"{base_url}{endpoint_path}"
session = Session()
Expand Down Expand Up @@ -77,9 +78,9 @@ def test_endpoint(
except Exception as e:
print_test_results(e, endpoint_path=endpoint_path, test_name=test_name)
if return_response:
return check_test_results(e), response
return check_test_results(e, required=required), response
else:
return check_test_results(e)
return check_test_results(e, required=required)
else:
if return_response:
return "", response
Expand Down Expand Up @@ -186,12 +187,13 @@ def print_test_results(e: Exception, endpoint_path: str, test_name: str = "?"):
print("")


def check_test_results(e: Exception):
def check_test_results(e: Exception, required: bool = True):
"""
prints the results of a openapi-core validation test
e: the exception that was raised as a part of the response validation test
test_name: the name of the test that ought to be displayed
required: is the endpoint required as per the spec.
"""

# message is important
Expand All @@ -203,7 +205,7 @@ def check_test_results(e: Exception):
elif e.actual_status_code == 500:
fail_log = "Endpoint expects an id and the item does not exist."
elif e.actual_status_code == 404 or e.actual_status_code == 501:
fail_log = "Endpoint is not implemented, only an error if it is REQUIRED."
fail_log = "Endpoint is not implemented." if required else ""
elif e.actual_status_code == 410:
fail_log = "Endpoint is not providing requested resource as it is gone. Logs are not provided if job is queued or created."
else:
Expand Down Expand Up @@ -294,14 +296,14 @@ def get_spec_path():
return _guess_root() / "openapi.yaml"


def load_payloads_from_directory(directory_path: str) -> Iterator[dict]:
def load_payloads_from_directory(directory_path: str) -> Iterator[str]:
for filename in pathlib.Path(directory_path).glob("*.json"):
file_path = os.path.join(directory_path, filename)
with open(file_path, "r") as file:
try:
# Load the JSON data from the file
data = json.load(file)
yield data
yield json.dumps(data)
except json.JSONDecodeError:
_log.error(f"Error decoding JSON in file: {filename}")
except Exception as e:
Expand All @@ -316,7 +318,7 @@ def set_uuid_in_job(json_data):
# Set the 'id' field to the generated UUID
json_data["process"]["id"] = new_id
# Return the modified JSON object
return new_id, json_data
return new_id, json.dumps(json_data)


def delete_id_resource(
Expand All @@ -343,19 +345,18 @@ def put_process_graphs(base_url: str, bearer_token: str): # TODO id and so fort

try:
for payload in payloads:
id, payload = set_uuid_in_udp(payload)
id = str(uuid.uuid4().hex)
created_udp_ids.append(id)
response = requests.put(
f"{base_url}process_graphs/{id}",
requests.put(
f"{base_url}/process_graphs/{id}",
data=payload,
headers={
"Content-Type": "application/json",
"Authorization": f"{bearer_token}",
},
)
print(response)
except Exception as e:
_log.error(f"Failed to create process graph: {e}")
print(f"Failed to create process graph: {e}")
return created_udp_ids


Expand Down Expand Up @@ -388,7 +389,7 @@ def post_jobs(base_url: str, bearer_token: str):

response = requests.post(
full_endpoint_url,
data=json.dumps(payload),
data=payload,
headers={
"Content-Type": "application/json",
"Authorization": f"{bearer_token}",
Expand Down
33 changes: 5 additions & 28 deletions src/openeo_test_suite/tests/general/test_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ def test_GET_service_types(base_url: str, spec: Spec, bearer_token: str):
test_name = "Service Types"

fail_log = conformance_util.test_endpoint(
base_url=base_url, endpoint_path=endpoint_path, test_name=test_name, spec=spec
base_url=base_url,
endpoint_path=endpoint_path,
test_name=test_name,
spec=spec,
required=False,
)

assert fail_log == ""
Expand Down Expand Up @@ -231,33 +235,6 @@ def test_GET_me(base_url: str, spec: Spec, bearer_token: str):

assert fail_log == ""

"""
setup: collect list of collection ids
testing: test response by API for GET requests of all the collection ids
cleanup: None
"""
fail_log = ""

collection_ids = [
collection["id"]
for collection in requests.get((f"{base_url}collections")).json()["collections"]
]

# prepare list of endpoints
special_GET_endpoints_no_auth = [
(f"collections/{collection_id}", f"Test for collection/{collection_id}")
for collection_id in collection_ids
]

# Run through all the special GET endpoints and test their response to a proper request.
for path, test_name in special_GET_endpoints_no_auth:
fail_log += conformance_util.test_endpoint(
base_url=base_url, endpoint_path=path, test_name=test_name, spec=spec
)

assert fail_log == ""


def test_GET_process_graphs(base_url: str, spec: Spec, bearer_token: str):
"""
Expand Down

0 comments on commit 5256fc7

Please sign in to comment.