def assertIsError(response, status): """This is an error response object with a specific status code. You can view the specification here: - https://github.com/schul-cloud/resources-api-v1/blob/f0ce9acfde59563822071207bd176baf648db8b4/api-definition/swagger.yaml#L292 - updated: https://github.com/schul-cloud/resources-api-v1/blob/master/api-definition/swagger.yaml#L292 - Error specification: """ response = to_dict(response) pprint(("response:", response)) assertIsResponse(response, None) get_schemas()["error"].validate(response) error = response["errors"][0] assert error["status"] == str(status), "{} == {}".format(repr(error["status"]), repr(str(status)))
def test_response_schema(search_response): """If the request was successful, it must match the search-response schema. Schema: - https://github.com/schul-cloud/resources-api-v1/tree/master/schemas/search-response The response is successful of a status code 200 is returned. """ schema = get_schemas()["search-response"] schema.validate(search_response)
def get(path): """Return a search response.""" response.headers["Content-Type"] = "application/vnd.api+json" if request.headers["Accept"] not in [ "application/vnd.api+json", "application/*", "*/*" ]: return api_error(406, "Not Acceptable", "Accept should be application/vnd.api+json.") if not "Q" in request.query or len(request.query) >= 2: return api_error(400, "Bad Request", "I do not understand other parameters than Q.") response.set_header("Content-Type", "application/vnd.api+json") self_href = "http://" + request.headers.get( "host", "localhost") + "?" + request.query_string example_resources = get_schemas()["resource"].get_valid_examples() resources = [{ "type": "resource", "attributes": data, "id": str(i) } for i, data in enumerate(example_resources)] count = len(resources) limit = 10 if count > limit: count = limit resources = resources[:count] result = { "jsonapi": JSONAPI, "links": { "self": { "href": self_href, "meta": { "limit": limit, "offset": 0, "count": count } }, "first": self_href, "last": (self_href if count else None), "prev": None, "next": None, }, "data": resources } return json.dumps(result)
def test_all_invalid_examples_fail(self): for schema in get_schemas().values(): for i in schema.get_invalid_examples(): self.assertFalse(schema.is_valid(i)) self.assertRaises(ValidationFailed, lambda: schema.validate(i))
def test_all_valid_examples_succeed(self): for schema in get_schemas().values(): for v in schema.get_valid_examples(): self.assertTrue(schema.is_valid(v)) schema.validate(v)
def test_valid_and_invalid_examples_are_not_the_same(self): for schema in get_schemas().values(): for v in schema.get_valid_examples(): for i in schema.get_invalid_examples(): self.assertNotEqual(v, i, "A valid example can not be invalid.")
def test_there_are_invalid_examples(self): for schema in get_schemas().values(): self.assertTrue(schema.get_invalid_examples())
def test_there_are_schemas(self): for name in ["error", "search-response", "resource"]: self.assertTrue(name in get_schemas())
from schul_cloud_resources_api_v1.schema import get_schemas from pytest import mark from schul_cloud_search_tests.tests.assertions import ( assertIsError, ERROR_CLIENT_REQUEST, Q, ERROR_SERVER_RESPONSE, ERROR) from pprint import pprint import copy @mark.parametrize("response", get_schemas()["search-response"].get_valid_examples()) def test_valid_query_is_returned(search_engine, response): """Valid results are passed through the search engine""" offset = response["links"]["self"]["meta"]["offset"] query = {Q:"test2", "page[offset]":str(offset)} result = search_engine.host(response, query).request() data = result.json() pprint(data) assert data == response @mark.parametrize("response", get_schemas()["search-response"].get_invalid_examples()) def test_invalid_response_is_detected(search_engine, response): """Detect when the search engine returns a response which does not fit the schema """ result = search_engine.host(response).request() assertIsError(result, ERROR_SERVER_RESPONSE) @mark.parametrize("invalid_response", ["{", "", b"asd", "{}", "[]"]) def test_invalid_return_values_are_handled(search_engine, invalid_response): """Test that a invalid responses still create a useful output.
""" This module contains the assertions and error definitions. Please see the specification for the errors. - https://github.com/schul-cloud/schul_cloud_search_tests/blob/master/README.rst#specification """ from schul_cloud_resources_server_tests.tests.assertions import assertIsError from schul_cloud_resources_server_tests.errors import errors from schul_cloud_resources_api_v1.schema import get_schemas from copy import deepcopy ERROR_CLIENT_REQUEST = 400 # https://httpstatuses.com/400 Q = "Q" # The query string ERROR_SERVER_RESPONSE = 409 # https://httpstatuses.com/409 ERROR = get_schemas()["error"].get_valid_examples()[0] def get_error(error_code): """Return an error with the specified code.""" error = deepcopy(ERROR) error["errors"][0]["status"] = str(error_code) error["errors"][0]["title"] = errors[int(error_code)] return error def assertServerReplyIsWrong(response): """Make sure the response is because the server responded in an unspecified manner. """ assert response.status_code == ERROR_SERVER_RESPONSE