-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_open_api.py
More file actions
86 lines (67 loc) · 2.65 KB
/
Copy pathtest_open_api.py
File metadata and controls
86 lines (67 loc) · 2.65 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
from ellar.auth.handlers import (
CookieAPIKeyAuthenticationHandler,
HeaderAPIKeyAuthenticationHandler,
HttpBasicAuthenticationHandler,
HttpBearerAuthenticationHandler,
QueryAPIKeyAuthenticationHandler,
)
from ellar.common import serialize_object
from ellar.core import Reflector
from ellar.di import injectable
from ellar.openapi import OpenAPIDocumentBuilder
from ellar.testing import Test
@injectable()
class QueryAuth(QueryAPIKeyAuthenticationHandler):
def __init__(self, reflector: Reflector) -> None:
super().__init__()
self.reflector = reflector
async def authentication_handler(self, connection, key):
if key == "querysecretkey":
return key
class HeaderAuth(HeaderAPIKeyAuthenticationHandler):
async def authentication_handler(self, connection, key):
if key == "headersecretkey":
return key
class CookieAuth(CookieAPIKeyAuthenticationHandler):
openapi_name = "API Key Auth"
async def authentication_handler(self, connection, key):
if key == "cookiesecretkey":
return key
class BasicAuth(HttpBasicAuthenticationHandler):
openapi_name = "API Authentication"
async def authentication_handler(self, connection, credentials):
if credentials.username == "admin" and credentials.password == "secret":
return credentials.username
@injectable()
class BearerAuth(HttpBearerAuthenticationHandler):
openapi_name = "JWT Authentication"
async def authentication_handler(self, connection, credentials):
if credentials.credentials == "bearertoken":
return credentials.credentials
test_module = Test.create_test_module()
app = test_module.create_application()
app.add_authentication_schemes(BearerAuth, HeaderAuth, QueryAuth, CookieAuth, BasicAuth)
def test_openapi_auth_schema():
document = serialize_object(OpenAPIDocumentBuilder().build_document(app))
assert document["components"]["securitySchemes"] == {
"JWT Authentication": {
"type": "http",
"scheme": "bearer",
"name": "JWT Authentication",
},
"HeaderAuth": {"type": "apiKey", "in": "header", "name": "key"},
"QueryAuth": {"type": "apiKey", "in": "query", "name": "key"},
"API Key Auth": {"type": "apiKey", "in": "cookie", "name": "key"},
"API Authentication": {
"type": "http",
"scheme": "basic",
"name": "API Authentication",
},
}
assert document["security"] == [
{"JWT Authentication": []},
{"HeaderAuth": []},
{"QueryAuth": []},
{"API Key Auth": []},
{"API Authentication": []},
]