forked from spec-first/connexion
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_api.py
More file actions
134 lines (99 loc) · 5.12 KB
/
test_api.py
File metadata and controls
134 lines (99 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import pathlib
import tempfile
from unittest.mock import MagicMock
import pytest
from connexion import FlaskApi
from connexion.exceptions import InvalidSpecification, ResolverError
from connexion.spec import canonical_base_path
from yaml import YAMLError
TEST_FOLDER = pathlib.Path(__file__).parent
def test_canonical_base_path():
assert canonical_base_path('') == ''
assert canonical_base_path('/') == ''
assert canonical_base_path('/api') == '/api'
assert canonical_base_path('/api/') == '/api'
def test_api():
api = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml", base_path="/api/v1.0")
assert api.blueprint.name == '/api/v1_0'
assert api.blueprint.url_prefix == '/api/v1.0'
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml")
assert api2.blueprint.name == '/v1_0'
assert api2.blueprint.url_prefix == '/v1.0'
api3 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml", base_path="/api/v1.0")
assert api3.blueprint.name == '/api/v1_0'
assert api3.blueprint.url_prefix == '/api/v1.0'
api4 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml")
assert api4.blueprint.name == '/v1_0'
assert api4.blueprint.url_prefix == '/v1.0'
def test_api_base_path_slash():
api = FlaskApi(TEST_FOLDER / "fixtures/simple/basepath-slash.yaml")
assert api.blueprint.name == ''
assert api.blueprint.url_prefix == ''
def test_template():
api1 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'test'})
assert api1.specification['info']['title'] == 'test'
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'other test'})
assert api2.specification['info']['title'] == 'other test'
def test_invalid_operation_does_stop_application_to_setup():
with pytest.raises(ImportError):
FlaskApi(TEST_FOLDER / "fixtures/op_error_api/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'})
with pytest.raises(ValueError):
FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'})
with pytest.raises(ImportError):
FlaskApi(TEST_FOLDER / "fixtures/module_not_implemented/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'})
with pytest.raises(ValueError):
FlaskApi(TEST_FOLDER / "fixtures/user_module_loading_error/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'})
def test_invalid_operation_does_not_stop_application_in_debug_mode():
api = FlaskApi(TEST_FOLDER / "fixtures/op_error_api/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
assert api.specification['info']['title'] == 'OK'
api = FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
assert api.specification['info']['title'] == 'OK'
api = FlaskApi(TEST_FOLDER / "fixtures/module_not_implemented/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
assert api.specification['info']['title'] == 'OK'
api = FlaskApi(TEST_FOLDER / "fixtures/user_module_loading_error/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
assert api.specification['info']['title'] == 'OK'
def test_other_errors_stop_application_to_setup():
# Errors should still result exceptions!
with pytest.raises(InvalidSpecification):
FlaskApi(TEST_FOLDER / "fixtures/bad_specs/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'})
def test_invalid_schema_file_structure():
with pytest.raises(InvalidSpecification):
FlaskApi(TEST_FOLDER / "fixtures/invalid_schema/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
def test_invalid_encoding():
with tempfile.NamedTemporaryFile(mode='wb', delete=False) as f:
f.write("swagger: '2.0'\ninfo:\n title: Foo 整\n version: v1\npaths: {}".encode('gbk'))
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
os.unlink(f.name)
def test_use_of_safe_load_for_yaml_swagger_specs():
with pytest.raises(YAMLError):
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(b'!!python/object:object {}\n')
try:
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
os.unlink(f.name)
except InvalidSpecification:
pytest.fail("Could load invalid YAML file, use yaml.safe_load!")
def test_validation_error_on_completely_invalid_swagger_spec():
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(b'[1]\n')
with pytest.raises(InvalidSpecification):
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
os.unlink(f.name)
@pytest.fixture
def mock_api_logger(monkeypatch):
mocked_logger = MagicMock(name='mocked_logger')
monkeypatch.setattr('connexion.apis.abstract.logger', mocked_logger)
return mocked_logger