Skip to content

Commit 2ec02e4

Browse files
committed
Adds failing test
Adds flask_wrapper.yaml spec
1 parent 4874851 commit 2ec02e4

File tree

2 files changed

+82
-45
lines changed

2 files changed

+82
-45
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Basic OpenAPI specification used with test_wrappers.TestFlaskOpenAPIIValidation
4+
version: "0.1"
5+
servers:
6+
- url: 'http://localhost'
7+
paths:
8+
'/browse/{id}/':
9+
parameters:
10+
- name: id
11+
in: path
12+
required: true
13+
description: the ID of the resource to retrieve
14+
schema:
15+
type: integer
16+
get:
17+
responses:
18+
default:
19+
description: Return the resource.

tests/integration/test_wrappers.py

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,55 @@
88
from openapi_core.wrappers.flask import (
99
FlaskOpenAPIRequest, FlaskOpenAPIResponse,
1010
)
11+
from openapi_core.shortcuts import create_spec
12+
from openapi_core.validation.response.validators import ResponseValidator
13+
14+
15+
@pytest.fixture
16+
def environ_factory():
17+
return create_environ
18+
19+
20+
@pytest.fixture
21+
def map():
22+
return Map([
23+
# Static URLs
24+
Rule('/', endpoint='static/index'),
25+
Rule('/about', endpoint='static/about'),
26+
Rule('/help', endpoint='static/help'),
27+
# Knowledge Base
28+
Subdomain('kb', [
29+
Rule('/', endpoint='kb/index'),
30+
Rule('/browse/', endpoint='kb/browse'),
31+
Rule('/browse/<int:id>/', endpoint='kb/browse'),
32+
Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse')
33+
])
34+
], default_subdomain='www')
1135

1236

13-
class TestFlaskOpenAPIRequest(object):
14-
37+
@pytest.fixture
38+
def request_factory(map, environ_factory):
1539
server_name = 'localhost'
1640

17-
@pytest.fixture
18-
def environ_factory(self):
19-
return create_environ
20-
21-
@pytest.fixture
22-
def map(self):
23-
return Map([
24-
# Static URLs
25-
Rule('/', endpoint='static/index'),
26-
Rule('/about', endpoint='static/about'),
27-
Rule('/help', endpoint='static/help'),
28-
# Knowledge Base
29-
Subdomain('kb', [
30-
Rule('/', endpoint='kb/index'),
31-
Rule('/browse/', endpoint='kb/browse'),
32-
Rule('/browse/<int:id>/', endpoint='kb/browse'),
33-
Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse')
34-
])
35-
], default_subdomain='www')
36-
37-
@pytest.fixture
38-
def request_factory(self, map, environ_factory):
39-
def create_request(method, path, subdomain=None, query_string=None):
40-
environ = environ_factory(query_string=query_string)
41-
req = Request(environ)
42-
urls = map.bind_to_environ(
43-
environ, server_name=self.server_name, subdomain=subdomain)
44-
req.url_rule, req.view_args = urls.match(
45-
path, method, return_rule=True)
46-
return req
47-
return create_request
48-
49-
@pytest.fixture
50-
def openapi_request(self, request):
51-
return FlaskOpenAPIRequest(request)
41+
def create_request(method, path, subdomain=None, query_string=None):
42+
environ = environ_factory(query_string=query_string)
43+
req = Request(environ)
44+
urls = map.bind_to_environ(
45+
environ, server_name=server_name, subdomain=subdomain)
46+
req.url_rule, req.view_args = urls.match(
47+
path, method, return_rule=True)
48+
return req
49+
return create_request
50+
51+
52+
@pytest.fixture
53+
def response_factory():
54+
def create_response(data, status_code=200):
55+
return Response(data, status=status_code)
56+
return create_response
57+
58+
59+
class TestFlaskOpenAPIRequest(object):
5260

5361
def test_simple(self, request_factory, request):
5462
request = request_factory('GET', '/', subdomain='www')
@@ -73,8 +81,7 @@ def test_simple(self, request_factory, request):
7381
assert openapi_request.mimetype == request.mimetype
7482

7583
def test_multiple_values(self, request_factory, request):
76-
request = request_factory(
77-
'GET', '/', subdomain='www', query_string='a=b&a=c')
84+
request = request_factory('GET', '/', subdomain='www', query_string='a=b&a=c')
7885

7986
openapi_request = FlaskOpenAPIRequest(request)
8087

@@ -122,12 +129,6 @@ def test_url_rule(self, request_factory, request):
122129

123130
class TestFlaskOpenAPIResponse(object):
124131

125-
@pytest.fixture
126-
def response_factory(self):
127-
def create_response(data, status_code=200):
128-
return Response(data, status=status_code)
129-
return create_response
130-
131132
def test_invalid_server(self, response_factory):
132133
response = response_factory('Not Found', status_code=404)
133134

@@ -137,3 +138,20 @@ def test_invalid_server(self, response_factory):
137138
assert openapi_response.data == response.data
138139
assert openapi_response.status_code == response._status_code
139140
assert openapi_response.mimetype == response.mimetype
141+
142+
143+
class TestFlaskOpenAPIRequestValidation(object):
144+
145+
specfile = 'data/v3.0/flask_wrapper.yaml'
146+
147+
def test_response_validator_path_pattern(
148+
self,
149+
factory,
150+
request_factory,
151+
response_factory):
152+
validator = ResponseValidator(create_spec(factory.spec_from_file(self.specfile)))
153+
request = request_factory('GET', '/browse/12/', subdomain='kb')
154+
openapi_request = FlaskOpenAPIRequest(request)
155+
openapi_response = FlaskOpenAPIResponse(response_factory('Some item', status_code=200))
156+
result = validator.validate(openapi_request, openapi_response)
157+
assert not result.errors

0 commit comments

Comments
 (0)