Coverage for tests/test_security_scopes_dont_propagate.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1# Ref: https://github.com/tiangolo/fastapi/issues/5623 

2 

3from typing import Any, Dict, List 1abcdefg

4 

5from fastapi import FastAPI, Security 1abcdefg

6from fastapi.security import SecurityScopes 1abcdefg

7from fastapi.testclient import TestClient 1abcdefg

8from typing_extensions import Annotated 1abcdefg

9 

10 

11async def security1(scopes: SecurityScopes): 1abcdefg

12 return scopes.scopes 1hijklmn

13 

14 

15async def security2(scopes: SecurityScopes): 1abcdefg

16 return scopes.scopes 1hijklmn

17 

18 

19async def dep3( 1abcdefg

20 dep1: Annotated[List[str], Security(security1, scopes=["scope1"])], 

21 dep2: Annotated[List[str], Security(security2, scopes=["scope2"])], 

22): 

23 return {"dep1": dep1, "dep2": dep2} 1hijklmn

24 

25 

26app = FastAPI() 1abcdefg

27 

28 

29@app.get("/scopes") 1abcdefg

30def get_scopes( 1abcdefg

31 dep3: Annotated[Dict[str, Any], Security(dep3, scopes=["scope3"])], 

32): 

33 return dep3 1hijklmn

34 

35 

36client = TestClient(app) 1abcdefg

37 

38 

39def test_security_scopes_dont_propagate(): 1abcdefg

40 response = client.get("/scopes") 1hijklmn

41 assert response.status_code == 200 1hijklmn

42 assert response.json() == { 1hijklmn

43 "dep1": ["scope3", "scope1"], 

44 "dep2": ["scope3", "scope2"], 

45 }