Coverage for tests / test_security_scopes.py: 100%
26 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Annotated 1abcd
3import pytest 1abcd
4from fastapi import Depends, FastAPI, Security 1abcd
5from fastapi.testclient import TestClient 1abcd
8@pytest.fixture(name="call_counter") 1abcd
9def call_counter_fixture(): 1abcd
10 return {"count": 0} 1efgh
13@pytest.fixture(name="app") 1abcd
14def app_fixture(call_counter: dict[str, int]): 1abcd
15 def get_db(): 1efgh
16 call_counter["count"] += 1 1ijkl
17 return f"db_{call_counter['count']}" 1ijkl
19 def get_user(db: Annotated[str, Depends(get_db)]): 1efgh
20 return "user" 1ijkl
22 app = FastAPI() 1efgh
24 @app.get("/") 1efgh
25 def endpoint( 1efgh
26 db: Annotated[str, Depends(get_db)],
27 user: Annotated[str, Security(get_user, scopes=["read"])],
28 ):
29 return {"db": db} 1ijkl
31 return app 1efgh
34@pytest.fixture(name="client") 1abcd
35def client_fixture(app: FastAPI): 1abcd
36 return TestClient(app) 1efgh
39def test_security_scopes_dependency_called_once( 1abcd
40 client: TestClient, call_counter: dict[str, int]
41):
42 response = client.get("/") 1ijkl
44 assert response.status_code == 200 1ijkl
45 assert call_counter["count"] == 1 1ijkl