Coverage for tests/test_security_scopes.py: 100%
27 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1from typing import Dict 1abcdefg
3import pytest 1abcdefg
4from fastapi import Depends, FastAPI, Security 1abcdefg
5from fastapi.testclient import TestClient 1abcdefg
6from typing_extensions import Annotated 1abcdefg
9@pytest.fixture(name="call_counter") 1abcdefg
10def call_counter_fixture(): 1abcdefg
11 return {"count": 0} 1abcdefg
14@pytest.fixture(name="app") 1abcdefg
15def app_fixture(call_counter: Dict[str, int]): 1abcdefg
16 def get_db(): 1abcdefg
17 call_counter["count"] += 1 1hijklmn
18 return f"db_{call_counter['count']}" 1hijklmn
20 def get_user(db: Annotated[str, Depends(get_db)]): 1abcdefg
21 return "user" 1hijklmn
23 app = FastAPI() 1abcdefg
25 @app.get("/") 1abcdefg
26 def endpoint( 1abcdefg
27 db: Annotated[str, Depends(get_db)],
28 user: Annotated[str, Security(get_user, scopes=["read"])],
29 ):
30 return {"db": db} 1hijklmn
32 return app 1abcdefg
35@pytest.fixture(name="client") 1abcdefg
36def client_fixture(app: FastAPI): 1abcdefg
37 return TestClient(app) 1abcdefg
40def test_security_scopes_dependency_called_once( 1abcdefg
41 client: TestClient, call_counter: Dict[str, int]
42):
43 response = client.get("/") 1hijklmn
45 assert response.status_code == 200 1hijklmn
46 assert call_counter["count"] == 1 1hijklmn