| 1 |
# Ref: https://github.com/fastapi/fastapi/issues/14454 |
| 2 |
|
| 3 |
from typing import Annotated |
| 4 |
|
| 5 |
from fastapi import APIRouter, Depends, FastAPI, Security |
| 6 |
from fastapi.security import OAuth2AuthorizationCodeBearer |
| 7 |
from fastapi.testclient import TestClient |
| 8 |
from inline_snapshot import snapshot |
| 9 |
|
| 10 |
oauth2_scheme = OAuth2AuthorizationCodeBearer( |
| 11 |
authorizationUrl="authorize", |
| 12 |
tokenUrl="token", |
| 13 |
auto_error=True, |
| 14 |
scopes={"read": "Read access", "write": "Write access"}, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
async def get_token(token: Annotated[str, Depends(oauth2_scheme)]) -> str: |
| 19 |
return token |
| 20 |
|
| 21 |
|
| 22 |
app = FastAPI(dependencies=[Depends(get_token)]) |
| 23 |
|
| 24 |
|
| 25 |
@app.get("/") |
| 26 |
async def root(): |
| 27 |
return {"message": "Hello World"} |
| 28 |
|
| 29 |
|
| 30 |
@app.get( |
| 31 |
"/with-oauth2-scheme", |
| 32 |
dependencies=[Security(oauth2_scheme, scopes=["read", "write"])], |
| 33 |
) |
| 34 |
async def read_with_oauth2_scheme(): |
| 35 |
return {"message": "Admin Access"} |
| 36 |
|
| 37 |
|
| 38 |
@app.get( |
| 39 |
"/with-get-token", dependencies=[Security(get_token, scopes=["read", "write"])] |
| 40 |
) |
| 41 |
async def read_with_get_token(): |
| 42 |
return {"message": "Admin Access"} |
| 43 |
|
| 44 |
|
| 45 |
router = APIRouter(dependencies=[Security(oauth2_scheme, scopes=["read"])]) |
| 46 |
|
| 47 |
|
| 48 |
@router.get("/items/") |
| 49 |
async def read_items(token: str | None = Depends(oauth2_scheme)): |
| 50 |
return {"token": token} |
| 51 |
|
| 52 |
|
| 53 |
@router.post("/items/") |
| 54 |
async def create_item( |
| 55 |
token: str | None = Security(oauth2_scheme, scopes=["read", "write"]), |
| 56 |
): |
| 57 |
return {"token": token} |
| 58 |
|
| 59 |
|
| 60 |
app.include_router(router) |
| 61 |
|
| 62 |
client = TestClient(app) |
| 63 |
|
| 64 |
|
| 65 |
def test_root(): |
| 66 |
response = client.get("/", headers={"Authorization": "Bearer testtoken"}) |
| 67 |
assert response.status_code == 200, response.text |
| 68 |
assert response.json() == {"message": "Hello World"} |
| 69 |
|
| 70 |
|
| 71 |
def test_read_with_oauth2_scheme(): |
| 72 |
response = client.get( |
| 73 |
"/with-oauth2-scheme", headers={"Authorization": "Bearer testtoken"} |
| 74 |
) |
| 75 |
assert response.status_code == 200, response.text |
| 76 |
assert response.json() == {"message": "Admin Access"} |
| 77 |
|
| 78 |
|
| 79 |
def test_read_with_get_token(): |
| 80 |
response = client.get( |
| 81 |
"/with-get-token", headers={"Authorization": "Bearer testtoken"} |
| 82 |
) |
| 83 |
assert response.status_code == 200, response.text |
| 84 |
assert response.json() == {"message": "Admin Access"} |
| 85 |
|
| 86 |
|
| 87 |
def test_read_token(): |
| 88 |
response = client.get("/items/", headers={"Authorization": "Bearer testtoken"}) |
| 89 |
assert response.status_code == 200, response.text |
| 90 |
assert response.json() == {"token": "testtoken"} |
| 91 |
|
| 92 |
|
| 93 |
def test_create_token(): |
| 94 |
response = client.post("/items/", headers={"Authorization": "Bearer testtoken"}) |
| 95 |
assert response.status_code == 200, response.text |
| 96 |
assert response.json() == {"token": "testtoken"} |
| 97 |
|
| 98 |
|
| 99 |
def test_openapi_schema(): |
| 100 |
response = client.get("/openapi.json") |
| 101 |
assert response.status_code == 200, response.text |
| 102 |
assert response.json() == snapshot( |
| 103 |
{ |
| 104 |
"openapi": "3.1.0", |
| 105 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 106 |
"paths": { |
| 107 |
"/": { |
| 108 |
"get": { |
| 109 |
"summary": "Root", |
| 110 |
"operationId": "root__get", |
| 111 |
"responses": { |
| 112 |
"200": { |
| 113 |
"description": "Successful Response", |
| 114 |
"content": {"application/json": {"schema": {}}}, |
| 115 |
} |
| 116 |
}, |
| 117 |
"security": [{"OAuth2AuthorizationCodeBearer": []}], |
| 118 |
} |
| 119 |
}, |
| 120 |
"/with-oauth2-scheme": { |
| 121 |
"get": { |
| 122 |
"summary": "Read With Oauth2 Scheme", |
| 123 |
"operationId": "read_with_oauth2_scheme_with_oauth2_scheme_get", |
| 124 |
"responses": { |
| 125 |
"200": { |
| 126 |
"description": "Successful Response", |
| 127 |
"content": {"application/json": {"schema": {}}}, |
| 128 |
} |
| 129 |
}, |
| 130 |
"security": [ |
| 131 |
{"OAuth2AuthorizationCodeBearer": ["read", "write"]} |
| 132 |
], |
| 133 |
} |
| 134 |
}, |
| 135 |
"/with-get-token": { |
| 136 |
"get": { |
| 137 |
"summary": "Read With Get Token", |
| 138 |
"operationId": "read_with_get_token_with_get_token_get", |
| 139 |
"responses": { |
| 140 |
"200": { |
| 141 |
"description": "Successful Response", |
| 142 |
"content": {"application/json": {"schema": {}}}, |
| 143 |
} |
| 144 |
}, |
| 145 |
"security": [ |
| 146 |
{"OAuth2AuthorizationCodeBearer": ["read", "write"]} |
| 147 |
], |
| 148 |
} |
| 149 |
}, |
| 150 |
"/items/": { |
| 151 |
"get": { |
| 152 |
"summary": "Read Items", |
| 153 |
"operationId": "read_items_items__get", |
| 154 |
"responses": { |
| 155 |
"200": { |
| 156 |
"description": "Successful Response", |
| 157 |
"content": {"application/json": {"schema": {}}}, |
| 158 |
} |
| 159 |
}, |
| 160 |
"security": [ |
| 161 |
{"OAuth2AuthorizationCodeBearer": ["read"]}, |
| 162 |
], |
| 163 |
}, |
| 164 |
"post": { |
| 165 |
"summary": "Create Item", |
| 166 |
"operationId": "create_item_items__post", |
| 167 |
"responses": { |
| 168 |
"200": { |
| 169 |
"description": "Successful Response", |
| 170 |
"content": {"application/json": {"schema": {}}}, |
| 171 |
} |
| 172 |
}, |
| 173 |
"security": [ |
| 174 |
{"OAuth2AuthorizationCodeBearer": ["read", "write"]}, |
| 175 |
], |
| 176 |
}, |
| 177 |
}, |
| 178 |
}, |
| 179 |
"components": { |
| 180 |
"securitySchemes": { |
| 181 |
"OAuth2AuthorizationCodeBearer": { |
| 182 |
"type": "oauth2", |
| 183 |
"flows": { |
| 184 |
"authorizationCode": { |
| 185 |
"scopes": { |
| 186 |
"read": "Read access", |
| 187 |
"write": "Write access", |
| 188 |
}, |
| 189 |
"authorizationUrl": "authorize", |
| 190 |
"tokenUrl": "token", |
| 191 |
} |
| 192 |
}, |
| 193 |
} |
| 194 |
} |
| 195 |
}, |
| 196 |
} |
| 197 |
) |