1 changed files with 20 additions and 33 deletions
@ -1,58 +1,45 @@ |
|||||
from unittest.mock import Mock, call, patch |
# Ref: https://github.com/tiangolo/fastapi/issues/5623 |
||||
|
|
||||
|
from typing import Any, Dict, List |
||||
|
|
||||
import pytest |
|
||||
from fastapi import FastAPI, Security |
from fastapi import FastAPI, Security |
||||
from fastapi.security import SecurityScopes |
from fastapi.security import SecurityScopes |
||||
from fastapi.testclient import TestClient |
from fastapi.testclient import TestClient |
||||
|
from typing_extensions import Annotated |
||||
|
|
||||
|
|
||||
async def security1(scopes: SecurityScopes): |
async def security1(scopes: SecurityScopes): |
||||
return _security1(scopes.scopes) |
return scopes.scopes |
||||
|
|
||||
|
|
||||
def _security1(scopes): # pragma: no cover |
|
||||
pass |
|
||||
|
|
||||
|
|
||||
async def security2(scopes: SecurityScopes): |
async def security2(scopes: SecurityScopes): |
||||
return _security2(scopes.scopes) |
return scopes.scopes |
||||
|
|
||||
|
|
||||
def _security2(scopes): # pragma: no cover |
|
||||
pass |
|
||||
|
|
||||
|
|
||||
async def dep3( |
async def dep3( |
||||
dep=Security(security1, scopes=["scope1"]), |
dep1: Annotated[List[str], Security(security1, scopes=["scope1"])], |
||||
dep2=Security(security2, scopes=["scope2"]), |
dep2: Annotated[List[str], Security(security2, scopes=["scope2"])], |
||||
): |
): |
||||
return {} |
return {"dep1": dep1, "dep2": dep2} |
||||
|
|
||||
|
|
||||
@pytest.fixture |
|
||||
def mocks(): |
|
||||
with patch.dict(globals(), {"_security1": Mock()}): |
|
||||
with patch.dict(globals(), {"_security2": Mock()}): |
|
||||
yield |
|
||||
|
|
||||
|
|
||||
app = FastAPI() |
app = FastAPI() |
||||
|
|
||||
|
|
||||
@app.get("/recursive_scopes") |
@app.get("/recursive_scopes") |
||||
def recursive_scopes(dep=Security(dep3, scopes=["scope3"])): |
def recursive_scopes( |
||||
return {} |
dep3: Annotated[Dict[str, Any], Security(dep3, scopes=["scope3"])], |
||||
|
): |
||||
|
return dep3 |
||||
|
|
||||
|
|
||||
client = TestClient(app) |
client = TestClient(app) |
||||
|
|
||||
|
|
||||
# issue https://github.com/tiangolo/fastapi/issues/5623 |
def test_recursive_scopes(): |
||||
def test_recursive_scopes(mocks): |
response = client.get("recursive_scopes") |
||||
""" |
assert response.status_code == 200 |
||||
Test that scope recursion properly applies. Scopes added to a dependency should propagate |
assert response.json() == { |
||||
and be prepended correctly to all sub-dependencies. |
"dep1": ["scope3", "scope1"], |
||||
""" |
"dep2": ["scope3", "scope2"], |
||||
client.get("recursive_scopes") |
} |
||||
_security1.assert_has_calls([call(["scope3", "scope1"])]) |
|
||||
_security2.assert_has_calls([call(["scope3", "scope2"])]) |
|
||||
|
|||||
Loading…
Reference in new issue