1
from fastapi import FastAPI, Security
2
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
3
from fastapi.testclient import TestClient
7
security = HTTPBase(scheme="Other")
11
def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)):
12
return {"scheme": credentials.scheme, "credentials": credentials.credentials}
15
client = TestClient(app)
18
def test_security_http_base():
19
response = client.get("/users/me", headers={"Authorization": "Other foobar"})
20
assert response.status_code == 200, response.text
21
assert response.json() == {"scheme": "Other", "credentials": "foobar"}
24
def test_security_http_base_no_credentials():
25
response = client.get("/users/me")
26
assert response.status_code == 403, response.text
27
assert response.json() == {"detail": "Not authenticated"}
30
def test_openapi_schema():
31
response = client.get("/openapi.json")
32
assert response.status_code == 200, response.text
33
assert response.json() == {
35
"info": {"title": "FastAPI", "version": "0.1.0"},
41
"description": "Successful Response",
42
"content": {"application/json": {"schema": {}}},
45
"summary": "Read Current User",
46
"operationId": "read_current_user_users_me_get",
47
"security": [{"HTTPBase": []}],
52
"securitySchemes": {"HTTPBase": {"type": "http", "scheme": "Other"}}