Skip to content

Commit

Permalink
add security spec and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kushal-ti authored and jakemwood committed Aug 23, 2023
1 parent 0cc3f38 commit 1cc2604
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions chalice_spec/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
request: Optional[Type[BaseModel]] = None,
response: Optional[Union[Response, Type[BaseModel]]] = None,
responses: Optional[List[Response]] = None,
security: Optional[List[Dict[str, List[str]]]] = None,
):
self.summary = summary
self.description = description
Expand All @@ -45,6 +46,7 @@ def __init__(

self.content_types = content_types
self.request = request
self.security = security

if response and responses:
raise TypeError("You must only pass one of response or responses")
Expand Down Expand Up @@ -176,6 +178,8 @@ def _build_operation_from_operation(
operation["tags"] = method.tags
if method.parameters:
operation["parameters"] = method.parameters
if method.security:
operation["security"] = method.security

return operation

Expand Down
83 changes: 83 additions & 0 deletions tests/test_chalice.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,86 @@ def get_post():
}
},
}


# Test 10: test security
def test_security():
app, spec = setup_test()
bearer_auth = {"type": "http", "scheme": "bearer"}
spec.components.security_scheme("BearerAuth", bearer_auth)

@app.route(
"/security",
methods=["POST"],
docs=Docs(
post=Op(
request=AnotherSchema,
response=Resp(
code=201, description="Updated successfully!", model=TestSchema
),
security=[{"BearerAuth": []}]
)
),
)
def test():
pass

assert spec.to_dict() == {
"paths": {
"/security": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/AnotherSchema"}
}
}
},
"security": [{"BearerAuth": []}],
"tags": ["/security"],
"responses": {
"201": {
"description": "Updated successfully!",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestSchema"
}
}
},
}
},
}
}
},
"info": {"title": "Test Schema", "version": "0.0.0"},
"openapi": "3.0.1",
"components": {
"schemas": {
"AnotherSchema": {
"title": "AnotherSchema",
"type": "object",
"properties": {
"nintendo": {"title": "Nintendo", "type": "string"},
"atari": {"title": "Atari", "type": "string"},
},
"required": ["nintendo", "atari"],
},
"TestSchema": {
"title": "TestSchema",
"type": "object",
"properties": {
"hello": {"title": "Hello", "type": "string"},
"world": {"title": "World", "type": "integer"},
},
"required": ["hello", "world"],
},
},
"securitySchemes": {
"BearerAuth": {
"type": "http",
"scheme": "bearer"
}
}
},
}

0 comments on commit 1cc2604

Please sign in to comment.