Beispiel #1
0
    def test_url_rule(self, request_factory):
        from django.urls import resolve
        request = request_factory.get('/admin/auth/group/1/')
        request.resolver_match = resolve('/admin/auth/group/1/')

        openapi_request = DjangoOpenAPIRequest(request)

        path = {
            'object_id': '1',
        }
        query = {}
        headers = {
            'Cookie': '',
        }
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.full_url_pattern == \
            request._current_scheme_host + "/admin/auth/group/{object_id}/"
        assert openapi_request.body == request.body
        assert openapi_request.mimetype == request.content_type
Beispiel #2
0
    def test_simple(self, request_factory):
        from django.urls import resolve

        request = request_factory.get("/admin/")
        request.resolver_match = resolve("/admin/")

        openapi_request = DjangoOpenAPIRequest(request)

        path = {}
        query = {}
        headers = Headers({
            "Cookie": "",
        })
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        assert openapi_request.method == request.method.lower()
        assert (
            openapi_request.full_url_pattern == request._current_scheme_host +
            request.path)
        assert openapi_request.body == request.body
        assert openapi_request.mimetype == request.content_type
Beispiel #3
0
    def validate_schema(self, request, response):
        result = ResponseValidator(self.create_schema()).validate(
            DjangoOpenAPIRequest(request), DjangoOpenAPIResponse(response)
        )

        result.raise_for_errors()
        assert result.errors == []
Beispiel #4
0
    def test_simple(self, request_factory):
        from django.urls import resolve
        request = request_factory.get('/admin/')
        request.resolver_match = resolve('/admin/')

        openapi_request = DjangoOpenAPIRequest(request)

        path = {}
        query = {}
        headers = {
            'Cookie': '',
        }
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        assert openapi_request.host_url == request._current_scheme_host
        assert openapi_request.path == request.path
        assert openapi_request.method == request.method.lower()
        assert openapi_request.path_pattern == request.path
        assert openapi_request.body == request.body
        assert openapi_request.mimetype == request.content_type
Beispiel #5
0
 def test_request_validator_path_pattern(self, django_spec,
                                         request_factory):
     from django.urls import resolve
     validator = RequestValidator(django_spec)
     request = request_factory.get('/admin/auth/group/1/')
     request.resolver_match = resolve('/admin/auth/group/1/')
     openapi_request = DjangoOpenAPIRequest(request)
     result = validator.validate(openapi_request)
     assert not result.errors
Beispiel #6
0
    def validate_schema(self, request, response):
        assert 200 <= response.status_code < 300, response.status_code

        if isinstance(response.data, list):
            assert len(response.data) > 0, "Cannot validate an empty list"

        response["Content-Type"] = "application/json"
        result = ResponseValidator(self.create_schema()).validate(
            DjangoOpenAPIRequest(request), DjangoOpenAPIResponse(response))

        result.raise_for_errors()
        assert result.errors == []
Beispiel #7
0
def parse(request):
    """ Parses a request using Tator's generated OpenAPI spec.
    """
    if parse.validator is None:
        generator = CustomGenerator(title='Tator REST API')
        spec = generator.get_schema(parser=True)
        openapi_spec = create_spec(spec)
        parse.validator = RequestValidator(openapi_spec)
    openapi_request = DjangoOpenAPIRequest(request)
    if openapi_request.mimetype.startswith('application/json'):
        openapi_request.mimetype = 'application/json'
    result = parse.validator.validate(openapi_request)
    result.raise_for_errors()
    out = {
        **result.parameters.path,
        **result.parameters.query,
    }
    if result.body:
        out['body'] = result.body
        if not isinstance(result.body, list):
            out = {**result.body, **out}
    return out
Beispiel #8
0
    def test_no_resolver(self, request_factory):
        request = request_factory.get('/admin/')

        openapi_request = DjangoOpenAPIRequest(request)

        path = {}
        query = {}
        headers = {
            'Cookie': '',
        }
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.full_url_pattern == \
            request._current_scheme_host + request.path
        assert openapi_request.body == request.body
        assert openapi_request.mimetype == request.content_type
Beispiel #9
0
    def get(self, request, pk=None):
        with open(settings.OPENAPI_SPEC_PATH) as file:
            spec_yaml = file.read()
        spec_dict = yaml.safe_load(spec_yaml)
        spec = create_spec(spec_dict)

        openapi_request = DjangoOpenAPIRequest(request)

        request_validator = RequestValidator(spec)
        result = request_validator.validate(openapi_request)
        result.raise_for_errors()

        response_dict = {
            "test": "test_val",
        }
        django_response = JsonResponse(response_dict)
        django_response['X-Rate-Limit'] = '12'

        openapi_response = DjangoOpenAPIResponse(django_response)
        validator = ResponseValidator(spec)
        result = validator.validate(openapi_request, openapi_response)
        result.raise_for_errors()

        return django_response