-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for requires with Security
- Loading branch information
Showing
4 changed files
with
189 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
tests/security/http/test_security_api_key_with_requires.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
from typing import Any | ||
|
||
from lilya.middleware import DefineMiddleware | ||
from lilya.middleware.request_context import RequestContextMiddleware | ||
from pydantic import BaseModel | ||
|
||
from esmerald import Gateway, Requires, Security, get | ||
from esmerald.security.api_key import APIKeyInCookie | ||
from esmerald.testclient import create_client | ||
|
||
api_key = APIKeyInCookie(name="key") | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
|
||
|
||
def get_current_user(oauth_header: str = Security(api_key)): | ||
user = User(username=oauth_header) | ||
return user | ||
|
||
|
||
@get("/users/me", security=[api_key]) | ||
def read_current_user(current_user: User = Requires(get_current_user)) -> Any: | ||
return current_user | ||
|
||
|
||
def test_security_api_key(): | ||
with create_client( | ||
routes=[ | ||
Gateway(handler=read_current_user), | ||
], | ||
middleware=[DefineMiddleware(RequestContextMiddleware)], | ||
) as client: | ||
response = client.get("/users/me", cookies={"key": "secret"}) | ||
assert response.status_code == 200, response.text | ||
assert response.json() == {"username": "secret"} | ||
|
||
|
||
def test_security_api_key_no_key(): | ||
with create_client( | ||
routes=[ | ||
Gateway(handler=read_current_user), | ||
], | ||
middleware=[DefineMiddleware(RequestContextMiddleware)], | ||
) as client: | ||
response = client.get("/users/me") | ||
assert response.status_code == 403, response.text | ||
assert response.json() == {"detail": "Not authenticated"} | ||
|
||
|
||
def test_openapi_schema(): | ||
with create_client( | ||
routes=[ | ||
Gateway(handler=read_current_user), | ||
], | ||
enable_openapi=True, | ||
) as client: | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
|
||
assert response.json() == { | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "Esmerald", | ||
"summary": "Esmerald application", | ||
"description": "Highly scalable, performant, easy to learn and for every application.", | ||
"contact": {"name": "admin", "email": "[email protected]"}, | ||
"version": client.app.version, | ||
}, | ||
"servers": [{"url": "/"}], | ||
"paths": { | ||
"/users/me": { | ||
"get": { | ||
"summary": "Read Current User", | ||
"description": "", | ||
"operationId": "read_current_user_users_me_get", | ||
"deprecated": False, | ||
"security": [ | ||
{ | ||
"APIKeyInCookie": { | ||
"type": "apiKey", | ||
"name": "key", | ||
"in": "cookie", | ||
"scheme_name": "APIKeyInCookie", | ||
} | ||
} | ||
], | ||
"requestBody": { | ||
"required": True, | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/DataField"} | ||
} | ||
}, | ||
}, | ||
"responses": { | ||
"200": { | ||
"description": "Successful response", | ||
"content": {"application/json": {"schema": {"type": "string"}}}, | ||
}, | ||
"422": { | ||
"description": "Validation Error", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/HTTPValidationError" | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"DataField": { | ||
"properties": {"current_user": {"$ref": "#/components/schemas/User"}}, | ||
"type": "object", | ||
"required": ["current_user"], | ||
"title": "DataField", | ||
}, | ||
"HTTPValidationError": { | ||
"properties": { | ||
"detail": { | ||
"items": {"$ref": "#/components/schemas/ValidationError"}, | ||
"type": "array", | ||
"title": "Detail", | ||
} | ||
}, | ||
"type": "object", | ||
"title": "HTTPValidationError", | ||
}, | ||
"User": { | ||
"properties": {"username": {"type": "string", "title": "Username"}}, | ||
"type": "object", | ||
"required": ["username"], | ||
"title": "User", | ||
}, | ||
"ValidationError": { | ||
"properties": { | ||
"loc": { | ||
"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]}, | ||
"type": "array", | ||
"title": "Location", | ||
}, | ||
"msg": {"type": "string", "title": "Message"}, | ||
"type": {"type": "string", "title": "Error Type"}, | ||
}, | ||
"type": "object", | ||
"required": ["loc", "msg", "type"], | ||
"title": "ValidationError", | ||
}, | ||
}, | ||
"securitySchemes": { | ||
"APIKeyInCookie": {"type": "apiKey", "name": "key", "in": "cookie"} | ||
}, | ||
}, | ||
} |