-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_builder.py
More file actions
299 lines (253 loc) · 9.9 KB
/
Copy pathtest_builder.py
File metadata and controls
299 lines (253 loc) · 9.9 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
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
from ellar.app import AppFactory
from ellar.common import Controller, get, put, serialize_object
from ellar.openapi.builder import OpenAPIDocumentBuilder
from ellar.openapi.openapi_v3 import APIKeyIn
@Controller
class CatController:
@get("/create")
async def create_cat(self):
return {"message": "created"}
@put("/{cat_id:int}")
async def update_cat(self, cat_id: int):
return {"message": "created", "cat_id": cat_id}
def convert_server(url, description=None, **variables):
return {"url": url, "description": description, "variables": variables}
def test_builder_defaults():
builder = OpenAPIDocumentBuilder()
assert builder._build["info"]["title"] == "Ellar API Docs"
assert builder._build["info"]["version"] == "1.0.0"
assert builder._build["tags"] == []
assert builder._build["openapi"] == "3.1.0"
def test_set_openapi_version_works():
builder = OpenAPIDocumentBuilder()
builder.set_openapi_version("2.0.0")
assert builder._build["openapi"] == "2.0.0"
def test_builder_set_title_works():
builder = OpenAPIDocumentBuilder()
builder.set_title("Some new title")
assert builder._build["info"]["title"] == "Some new title"
def test_builder_set_version_works():
builder = OpenAPIDocumentBuilder()
builder.set_version("2.0.0")
assert builder._build["info"]["version"] == "2.0.0"
def test_builder_set_description_works():
description = "Whatever description available"
builder = OpenAPIDocumentBuilder()
builder.set_description(description)
assert builder._build["info"]["description"] == description
def test_builder_set_term_of_service_works():
terms_of_service = "What terms of service available"
builder = OpenAPIDocumentBuilder().set_term_of_service(terms_of_service)
assert builder._build["info"]["termsOfService"] == terms_of_service
def test_builder_set_contact_works():
details = {
"name": "Eadwin",
"url": "https://github.com/eadwinCode",
"email": "eadwin@gmail.com",
}
builder = OpenAPIDocumentBuilder().set_contact(**details)
assert builder._build["info"]["contact"] == details
def test_builder_set_license_works():
details = {"name": "Yahoo", "url": "https://yahoo.com"}
builder = OpenAPIDocumentBuilder().set_license(**details)
assert builder._build["info"]["license"] == details
def test_builder_set_external_doc_works():
details = {
"description": "More detailed documentation can be foound here",
"url": "https://external-doc.com",
}
builder = OpenAPIDocumentBuilder().set_external_doc(**details)
assert builder._build["externalDocs"] == details
def test_add_security_requirements():
builder = OpenAPIDocumentBuilder().add_security_requirements(
name="a", requirements=["b", "c"]
)
assert builder._build["security"] == [{"a": ["b", "c"]}]
def test_add_api_key():
builder = OpenAPIDocumentBuilder().add_api_key(
openapi_in=APIKeyIn.cookie, openapi_description="Cookie description"
)
assert builder._build["components"]["securitySchemes"] == {
"api_key": {
"description": "Cookie description",
"in": "cookie",
"name": "api_key",
"type": "apiKey",
}
}
def test_add_cookie_auth():
builder = OpenAPIDocumentBuilder().add_cookie_auth(
cookie_name="test-cookie", openapi_description="Cookie description"
)
assert builder._build["components"]["securitySchemes"] == {
"cookie": {
"description": "Cookie description",
"in": "cookie",
"name": "test-cookie",
"type": "apiKey",
}
}
def test_add_basic_auth():
builder = OpenAPIDocumentBuilder().add_basic_auth()
assert builder._build["components"]["securitySchemes"] == {
"basic": {
"description": None,
"name": "basic",
"scheme": "basic",
"type": "http",
}
}
def test_add_bearer_auth():
builder = OpenAPIDocumentBuilder().add_bearer_auth()
assert builder._build["components"]["securitySchemes"] == {
"bearer": {
"bearerFormat": "JWT",
"description": None,
"name": "bearer",
"scheme": "bearer",
"type": "http",
}
}
def test_builder_add_server_works():
server1 = {
"url": "{server}/v1",
"description": "Servers",
"server": {"default": "https://staging.server.com"},
}
server2 = {
"url": "https://{environment}.example.com/v2",
"environment": {"default": "api", "enum": ["api", "api.dev", "api.staging"]},
}
builder = OpenAPIDocumentBuilder().add_server(**server1).add_server(**server2)
servers = builder._build["servers"]
assert len(servers) == 2
assert servers[0] == convert_server(**server1)
assert servers[1] == convert_server(**server2)
def test_builder_add_tags_works():
tag1 = {"name": "tag1", "description": "some tag1 description"}
tag2 = {
"name": "tag2",
"description": "some tag2 description",
"external_doc_url": "https://tag2.com",
"external_doc_description": "some tag2 link description",
}
builder = OpenAPIDocumentBuilder().add_tags(**tag1).add_tags(**tag2)
tags = builder._build["tags"]
tag2_result = {
"name": "tag2",
"description": "some tag2 description",
"externalDocs": {
"url": "https://tag2.com",
"description": "some tag2 link description",
},
}
assert len(tags) == 2
assert tags[0] == tag1
assert tags[1] == tag2_result
def test_builder_build_document_adds_error_schemas():
app = AppFactory.create_app()
builder = OpenAPIDocumentBuilder()
schema = builder.build_document(app)
scheme_dict = serialize_object(schema.dict(exclude_none=True))
assert "HTTPValidationError" in scheme_dict["components"]["schemas"]
assert "ValidationError" in scheme_dict["components"]["schemas"]
def test_builder_build_document_has_correct_schema():
app = AppFactory.create_app(controllers=(CatController,))
builder = OpenAPIDocumentBuilder()
schema = builder.build_document(app)
scheme_dict = serialize_object(schema.dict(exclude_none=True))
assert "HTTPValidationError" in scheme_dict["components"]["schemas"]
assert "ValidationError" in scheme_dict["components"]["schemas"]
assert scheme_dict == {
"openapi": "3.1.0",
"info": {"title": "Ellar API Docs", "version": "1.0.0"},
"paths": {
"/cat/create": {
"get": {
"tags": ["cat"],
"operationId": "create_cat_create_get__cat",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"title": "Response Model",
}
}
},
}
},
}
},
"/cat/{cat_id}": {
"put": {
"tags": ["cat"],
"operationId": "update_cat__cat_id__put__cat",
"parameters": [
{
"required": True,
"schema": {"type": "integer", "title": "Cat Id"},
"name": "cat_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"title": "Response Model",
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {"$ref": "#/components/schemas/ValidationError"},
"type": "array",
"title": "Details",
}
},
"type": "object",
"required": ["detail"],
"title": "HTTPValidationError",
},
"ValidationError": {
"properties": {
"loc": {
"items": {"type": "string"},
"type": "array",
"title": "Location",
},
"msg": {"type": "string", "title": "Message"},
"type": {"type": "string", "title": "Error Type"},
},
"type": "object",
"required": ["loc", "msg", "type"],
"title": "ValidationError",
},
}
},
"tags": [{"name": "cat"}],
}