Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
46 lines
1014 B
Python
46 lines
1014 B
Python
# Ref: https://github.com/tiangolo/fastapi/issues/5623
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
from fastapi import FastAPI, Security
|
|
from fastapi.security import SecurityScopes
|
|
from fastapi.testclient import TestClient
|
|
from typing_extensions import Annotated
|
|
|
|
|
|
async def security1(scopes: SecurityScopes):
|
|
return scopes.scopes
|
|
|
|
|
|
async def security2(scopes: SecurityScopes):
|
|
return scopes.scopes
|
|
|
|
|
|
async def dep3(
|
|
dep1: Annotated[List[str], Security(security1, scopes=["scope1"])],
|
|
dep2: Annotated[List[str], Security(security2, scopes=["scope2"])],
|
|
):
|
|
return {"dep1": dep1, "dep2": dep2}
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/scopes")
|
|
def get_scopes(
|
|
dep3: Annotated[Dict[str, Any], Security(dep3, scopes=["scope3"])],
|
|
):
|
|
return dep3
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_security_scopes_dont_propagate():
|
|
response = client.get("/scopes")
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"dep1": ["scope3", "scope1"],
|
|
"dep2": ["scope3", "scope2"],
|
|
}
|