Coverage for tests / test_security_scopes.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Annotated 1abdc

2 

3import pytest 1abdc

4from fastapi import Depends, FastAPI, Security 1abdc

5from fastapi.testclient import TestClient 1abdc

6 

7 

8@pytest.fixture(name="call_counter") 1abdc

9def call_counter_fixture(): 1abdc

10 return {"count": 0} 1abc

11 

12 

13@pytest.fixture(name="app") 1abdc

14def app_fixture(call_counter: dict[str, int]): 1abdc

15 def get_db(): 1abc

16 call_counter["count"] += 1 1efg

17 return f"db_{call_counter['count']}" 1efg

18 

19 def get_user(db: Annotated[str, Depends(get_db)]): 1abc

20 return "user" 1efg

21 

22 app = FastAPI() 1abc

23 

24 @app.get("/") 1abc

25 def endpoint( 1abc

26 db: Annotated[str, Depends(get_db)], 

27 user: Annotated[str, Security(get_user, scopes=["read"])], 

28 ): 

29 return {"db": db} 1efg

30 

31 return app 1abc

32 

33 

34@pytest.fixture(name="client") 1abdc

35def client_fixture(app: FastAPI): 1abdc

36 return TestClient(app) 1abc

37 

38 

39def test_security_scopes_dependency_called_once( 1abdc

40 client: TestClient, call_counter: dict[str, int] 

41): 

42 response = client.get("/") 1efg

43 

44 assert response.status_code == 200 1efg

45 assert call_counter["count"] == 1 1efg