Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator changes: always check unversioned '/versions' and handle rich HTML pages #665

Merged
merged 3 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions optimade/validator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,7 @@ def wrapper(
else:
result, msg = test_fn(validator, *args, **kwargs)

except json.JSONDecodeError as exc:
msg = (
"Critical: unable to parse server response as JSON. "
f"{exc.__class__.__name__}: {exc}"
)
raise exc
except (ResponseError, ValidationError) as exc:
except (json.JSONDecodeError, ResponseError, ValidationError) as exc:
msg = f"{exc.__class__.__name__}: {exc}"
raise exc
except Exception as exc:
Expand Down
29 changes: 19 additions & 10 deletions optimade/validator/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,18 +922,24 @@ def _test_versions_endpoint(self):
for versioned base URLs.

"""
expected_status_code = 200

# First, check that there is a versions endpoint in the appropriate place:
# If passed a versioned URL, then strip that version from
# the URL before looking for `/versions`.
if re.match(VERSIONS_REGEXP, self.base_url_parsed.path) is not None:
expected_status_code = 404
self.client.base_url = "/".join(self.client.base_url.split("/")[:-1])

response, _ = self._get_endpoint(
CONF.versions_endpoint, expected_status_code=expected_status_code
CONF.versions_endpoint, expected_status_code=200
)

if expected_status_code == 200:
self._test_versions_endpoint_content(
response, request=CONF.versions_endpoint
)
self._test_versions_endpoint_content(response, request=CONF.versions_endpoint)

# If passed a versioned URL, first reset the URL of the client to the
# versioned one, then that this versioned URL does NOT host a versions endpoint
if re.match(VERSIONS_REGEXP, self.base_url_parsed.path) is not None:
self.client.base_url = self.base_url
self._get_endpoint(CONF.versions_endpoint, expected_status_code=404)

@test_case
def _test_versions_endpoint_content(
Expand Down Expand Up @@ -1212,9 +1218,12 @@ def _get_endpoint(

if response.status_code not in expected_status_code:
message = f"Request to '{request_str}' returned HTTP code {response.status_code} and not expected {expected_status_code}."
message += "\nError(s):"
for error in response.json().get("errors", []):
message += f'\n {error.get("title", "N/A")}: {error.get("detail", "N/A")} ({error.get("source", {}).get("pointer", "N/A")})'
message += "\nAdditional details:"
try:
for error in response.json().get("errors", []):
message += f'\n {error.get("title", "N/A")}: {error.get("detail", "N/A")} ({error.get("source", {}).get("pointer", "N/A")})'
except json.JSONDecodeError:
message += f"\n Could not parse response as JSON. Content type was {response.headers.get('content-type')!r}."
raise ResponseError(message)

return response, f"received expected response: {response}."
7 changes: 2 additions & 5 deletions tests/validator/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,14 @@ def test_expected_failure_test_case():
assert validator.results.internal_failure_count == 0

assert output[0] is None
assert (
output[1]
== "Critical: unable to parse server response as JSON. JSONDecodeError: Dummy JSON error: line 1 column 1 (char 0)"
)
assert output[1] == "JSONDecodeError: Dummy JSON error: line 1 column 1 (char 0)"
assert (
validator.results.optional_failure_messages[-1][0]
== "http://example.org/test_request - dummy_test_case - failed with error"
)
assert (
validator.results.optional_failure_messages[-1][1]
== "Critical: unable to parse server response as JSON. JSONDecodeError: Dummy JSON error: line 1 column 1 (char 0)"
== "JSONDecodeError: Dummy JSON error: line 1 column 1 (char 0)"
)


Expand Down