| 1 |
# Ref: https://github.com/tiangolo/fastapi/issues/5623 |
| 2 |
|
| 3 |
from typing import Annotated, Any |
| 4 |
|
| 5 |
from fastapi import FastAPI, Security |
| 6 |
from fastapi.security import SecurityScopes |
| 7 |
from fastapi.testclient import TestClient |
| 8 |
|
| 9 |
|
| 10 |
async def security1(scopes: SecurityScopes): |
| 11 |
return scopes.scopes |
| 12 |
|
| 13 |
|
| 14 |
async def security2(scopes: SecurityScopes): |
| 15 |
return scopes.scopes |
| 16 |
|
| 17 |
|
| 18 |
async def dep3( |
| 19 |
dep1: Annotated[list[str], Security(security1, scopes=["scope1"])], |
| 20 |
dep2: Annotated[list[str], Security(security2, scopes=["scope2"])], |
| 21 |
): |
| 22 |
return {"dep1": dep1, "dep2": dep2} |
| 23 |
|
| 24 |
|
| 25 |
app = FastAPI() |
| 26 |
|
| 27 |
|
| 28 |
@app.get("/scopes") |
| 29 |
def get_scopes( |
| 30 |
dep3: Annotated[dict[str, Any], Security(dep3, scopes=["scope3"])], |
| 31 |
): |
| 32 |
return dep3 |
| 33 |
|
| 34 |
|
| 35 |
client = TestClient(app) |
| 36 |
|
| 37 |
|
| 38 |
def test_security_scopes_dont_propagate(): |
| 39 |
response = client.get("/scopes") |
| 40 |
assert response.status_code == 200 |
| 41 |
assert response.json() == { |
| 42 |
"dep1": ["scope3", "scope1"], |
| 43 |
"dep2": ["scope3", "scope2"], |
| 44 |
} |