Coverage for tests/test_dependency_paramless.py: 100%

35 statements  

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

1from typing import Union 1abcdefg

2 

3from fastapi import FastAPI, HTTPException, Security 1abcdefg

4from fastapi.security import ( 1abcdefg

5 OAuth2PasswordBearer, 

6 SecurityScopes, 

7) 

8from fastapi.testclient import TestClient 1abcdefg

9from typing_extensions import Annotated 1abcdefg

10 

11app = FastAPI() 1abcdefg

12 

13oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") 1abcdefg

14 

15 

16def process_auth( 1abcdefg

17 credentials: Annotated[Union[str, None], Security(oauth2_scheme)], 

18 security_scopes: SecurityScopes, 

19): 

20 # This is an incorrect way of using it, this is not checking if the scopes are 

21 # provided by the token, only if the endpoint is requesting them, but the test 

22 # here is just to check if FastAPI is indeed registering and passing the scopes 

23 # correctly when using Security with parameterless dependencies. 

24 if "a" not in security_scopes.scopes or "b" not in security_scopes.scopes: 1hivjkwlmxnoypqzrsAtuB

25 raise HTTPException(detail="a or b not in scopes", status_code=401) 1vwxyzAB

26 return {"token": credentials, "scopes": security_scopes.scopes} 1hijklmnopqrstu

27 

28 

29@app.get("/get-credentials") 1abcdefg

30def get_credentials( 1abcdefg

31 credentials: Annotated[dict, Security(process_auth, scopes=["a", "b"])], 

32): 

33 return credentials 1hjlnprt

34 

35 

36@app.get( 1abcdefg

37 "/parameterless-with-scopes", 

38 dependencies=[Security(process_auth, scopes=["a", "b"])], 

39) 

40def get_parameterless_with_scopes(): 1abcdefg

41 return {"status": "ok"} 1ikmoqsu

42 

43 

44@app.get( 1abcdefg

45 "/parameterless-without-scopes", 

46 dependencies=[Security(process_auth)], 

47) 

48def get_parameterless_without_scopes(): 1abcdefg

49 return {"status": "ok"} 1CDEFGHI

50 

51 

52client = TestClient(app) 1abcdefg

53 

54 

55def test_get_credentials(): 1abcdefg

56 response = client.get("/get-credentials", headers={"authorization": "Bearer token"}) 1hjlnprt

57 assert response.status_code == 200, response.text 1hjlnprt

58 assert response.json() == {"token": "token", "scopes": ["a", "b"]} 1hjlnprt

59 

60 

61def test_parameterless_with_scopes(): 1abcdefg

62 response = client.get( 1ikmoqsu

63 "/parameterless-with-scopes", headers={"authorization": "Bearer token"} 

64 ) 

65 assert response.status_code == 200, response.text 1ikmoqsu

66 assert response.json() == {"status": "ok"} 1ikmoqsu

67 

68 

69def test_parameterless_without_scopes(): 1abcdefg

70 response = client.get( 1vwxyzAB

71 "/parameterless-without-scopes", headers={"authorization": "Bearer token"} 

72 ) 

73 assert response.status_code == 401, response.text 1vwxyzAB

74 assert response.json() == {"detail": "a or b not in scopes"} 1vwxyzAB

75 

76 

77def test_call_get_parameterless_without_scopes_for_coverage(): 1abcdefg

78 assert get_parameterless_without_scopes() == {"status": "ok"} 1CDEFGHI