File: validate_apis_test.py

package info (click to toggle)
swagger-spec-validator 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 696 kB
  • sloc: python: 2,321; makefile: 28; sh: 2
file content (348 lines) | stat: -rw-r--r-- 10,003 bytes parent folder | download | duplicates (3)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import pytest

from swagger_spec_validator.common import SwaggerValidationError
from swagger_spec_validator.validator20 import validate_apis
from swagger_spec_validator.validator20 import validate_defaults_in_parameters

RESPONSES = {
    "default": {
        "description": "random description",
    },
}


def test_api_level_params_ok():
    # Parameters defined at the API level apply to all operations within that
    # API. Make sure we don't treat the API level parameters as an operation
    # since they are peers.
    apis = {
        "/tags/{tag-name}": {
            "parameters": [
                {"name": "tag-name", "in": "path", "type": "string", "required": True},
            ],
            "get": {
                "responses": RESPONSES,
            },
        },
    }
    # Success == no exception thrown
    validate_apis(apis, lambda x: x)


def test_api_level_x_hyphen_ok():
    # Elements starting with "x-" should be ignored
    apis = {
        "/tags/{tag-name}": {
            "x-ignore-me": "DO NOT LOOK AT ME!",
            "get": {
                "parameters": [
                    {
                        "name": "tag-name",
                        "in": "path",
                        "type": "string",
                    }
                ],
                "responses": RESPONSES,
            },
        }
    }
    # Success == no exception thrown
    validate_apis(apis, lambda x: x)


@pytest.mark.parametrize(
    "partial_parameter_spec",
    [
        {"type": "integer", "default": 1},
        {"type": "boolean", "default": True},
        {"type": "null", "default": None},
        {"type": "number", "default": 2},
        {"type": "number", "default": 3.4},
        {"type": "object", "default": {"a_random_property": "valid"}},
        {"type": "array", "items": {"type": "integer"}, "default": [5, 6, 7]},
        {"type": "string", "default": ""},
        {"type": "string", "default": None, "x-nullable": True},
        {"type": ["number", "boolean"], "default": 8},
        {"type": ["number", "boolean"], "default": False},
    ],
)
def test_api_check_default_succeed(partial_parameter_spec):
    apis = {
        "/api": {
            "get": {
                "parameters": [
                    dict({"name": "param", "in": "query"}, **partial_parameter_spec),
                ],
                "responses": RESPONSES,
            },
        },
    }

    # Success if no exception are raised
    validate_apis(apis, lambda x: x)


@pytest.mark.parametrize(
    "partial_parameter_spec, validator, instance",
    [
        [
            {"type": "integer", "default": "wrong_type"},
            "type",
            "wrong_type",
        ],
        [
            {"type": "boolean", "default": "wrong_type"},
            "type",
            "wrong_type",
        ],
        [
            {"type": "null", "default": "wrong_type"},
            "type",
            "wrong_type",
        ],
        [
            {"type": "number", "default": "wrong_type"},
            "type",
            "wrong_type",
        ],
        [
            {"type": "object", "default": "wrong_type"},
            "type",
            "wrong_type",
        ],
        [
            {"type": "array", "default": "wrong_type"},
            "type",
            "wrong_type",
        ],
        [
            {"type": "string", "default": -1},
            "type",
            -1,
        ],
        [
            {"type": "string", "minLength": 100, "default": "short_string"},
            "minLength",
            "short_string",
        ],
        [
            {"type": ["number", "boolean"], "default": "not_a_number_or_boolean"},
            "type",
            "not_a_number_or_boolean",
        ],
    ],
)
def test_api_check_default_fails(partial_parameter_spec, validator, instance):
    apis = {
        "/api": {
            "get": {
                "parameters": [
                    dict({"name": "param", "in": "query"}, **partial_parameter_spec),
                ],
                "responses": RESPONSES,
            },
        },
    }

    with pytest.raises(SwaggerValidationError) as excinfo:
        validate_apis(apis, lambda x: x)

    validation_error = excinfo.value.args[1]
    assert validation_error.instance == instance
    assert validation_error.validator == validator


def test_validate_defaults_in_parameters_succeed():
    # Success if no exception are raised
    validate_defaults_in_parameters(
        params_spec=[{"type": "integer"}],
        deref=lambda x: x,
    )


def test_validate_defaults_in_parameters_fails():
    with pytest.raises(SwaggerValidationError):
        validate_defaults_in_parameters(
            params_spec=[
                {"type": "integer", "default": "wrong_type"},
            ],
            deref=lambda x: x,
        )


@pytest.mark.parametrize(
    "apis",
    [
        {
            "/api": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "responses": {},
                },
                "post": {
                    "operationId": "duplicateOperationId",
                    "responses": {},
                },
            },
        },
        {
            "/api1": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "responses": {},
                },
            },
            "/api2": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "responses": {},
                },
            },
        },
        {
            "/api1": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "tags": ["tag1", "tag2"],
                    "responses": {},
                },
            },
            "/api2": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "tags": ["tag1"],
                    "responses": {},
                },
            },
        },
    ],
)
def test_duplicate_operationIds_fails(apis):
    with pytest.raises(SwaggerValidationError) as excinfo:
        validate_apis(apis, lambda x: x)

    swagger_validation_error = excinfo.value
    error_message = swagger_validation_error.args[0]

    assert error_message == "Duplicate operationId: duplicateOperationId"


@pytest.mark.parametrize(
    "apis",
    [
        {
            "/api1": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "tags": ["tag1"],
                    "responses": {},
                },
            },
            "/api2": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "tags": ["tag2"],
                    "responses": {},
                },
            },
            "/api3": {
                "get": {
                    "operationId": "duplicateOperationId",
                    "responses": {},
                },
            },
        },
    ],
)
def test_duplicate_operationIds_fails_if_tags_differ(apis):
    with pytest.raises(SwaggerValidationError) as excinfo:
        validate_apis(apis, lambda x: x)

    swagger_validation_error = excinfo.value
    error_message = swagger_validation_error.args[0]

    assert error_message == "Duplicate operationId: duplicateOperationId"


def test_invalid_inline_models_in_responses_fails():
    apis = {
        "/endpoint": {
            "get": {
                "responses": {
                    "200": {
                        "description": "desc",
                        "schema": {
                            "type": "object",
                            "properties": {"prop": {"type": "array"}},
                        },
                    },
                },
            },
        },
    }
    with pytest.raises(SwaggerValidationError) as excinfo:
        validate_apis(apis, lambda x: x)
    assert (
        str(excinfo.value) == "Definition of type array must define `items` property "
        "(definition #/paths//endpoint/get/responses/200/properties/prop)."
    )


def test_invalid_inline_models_in_operation_body_parameters_fails():
    apis = {
        "/endpoint": {
            "get": {
                "parameters": [
                    {
                        "in": "body",
                        "name": "body",
                        "schema": {
                            "type": "object",
                            "properties": {"prop": {"type": "array"}},
                        },
                    }
                ],
                "responses": {
                    "200": {
                        "description": "desc",
                    },
                },
            },
        },
    }
    with pytest.raises(SwaggerValidationError) as excinfo:
        validate_apis(apis, lambda x: x)
    assert (
        str(excinfo.value) == "Definition of type array must define `items` property "
        "(definition #/paths//endpoint/get/parameters/0/schema/properties/prop)."
    )


def test_invalid_inline_models_in_api_body_parameters_fails():
    apis = {
        "/endpoint": {
            "parameters": [
                {
                    "in": "body",
                    "name": "body",
                    "schema": {
                        "type": "object",
                        "properties": {"prop": {"type": "array"}},
                    },
                }
            ],
            "get": {
                "responses": {
                    "200": {
                        "description": "desc",
                    },
                },
            },
        },
    }
    with pytest.raises(SwaggerValidationError) as excinfo:
        validate_apis(apis, lambda x: x)
    assert (
        str(excinfo.value) == "Definition of type array must define `items` property "
        "(definition #/paths//endpoint/parameters/0/schema/properties/prop)."
    )