From b9fffb72b4feed27eeda569310ac23894729950c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 30 Nov 2025 12:48:43 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Refactor=20test=20to=20avoid=20mock?= =?UTF-8?q?ing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dependency_scopes.py | 53 +++++++++++++-------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/tests/test_dependency_scopes.py b/tests/test_dependency_scopes.py index 957a0896e..10e5859cc 100644 --- a/tests/test_dependency_scopes.py +++ b/tests/test_dependency_scopes.py @@ -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.security import SecurityScopes from fastapi.testclient import TestClient +from typing_extensions import Annotated async def security1(scopes: SecurityScopes): - return _security1(scopes.scopes) - - -def _security1(scopes): # pragma: no cover - pass + return scopes.scopes async def security2(scopes: SecurityScopes): - return _security2(scopes.scopes) - - -def _security2(scopes): # pragma: no cover - pass + return scopes.scopes async def dep3( - dep=Security(security1, scopes=["scope1"]), - dep2=Security(security2, scopes=["scope2"]), + dep1: Annotated[List[str], Security(security1, scopes=["scope1"])], + dep2: Annotated[List[str], Security(security2, scopes=["scope2"])], ): - return {} - - -@pytest.fixture -def mocks(): - with patch.dict(globals(), {"_security1": Mock()}): - with patch.dict(globals(), {"_security2": Mock()}): - yield + return {"dep1": dep1, "dep2": dep2} app = FastAPI() @app.get("/recursive_scopes") -def recursive_scopes(dep=Security(dep3, scopes=["scope3"])): - return {} +def recursive_scopes( + dep3: Annotated[Dict[str, Any], Security(dep3, scopes=["scope3"])], +): + return dep3 client = TestClient(app) -# issue https://github.com/tiangolo/fastapi/issues/5623 -def test_recursive_scopes(mocks): - """ - Test that scope recursion properly applies. Scopes added to a dependency should propagate - and be prepended correctly to all sub-dependencies. - """ - client.get("recursive_scopes") - _security1.assert_has_calls([call(["scope3", "scope1"])]) - _security2.assert_has_calls([call(["scope3", "scope2"])]) +def test_recursive_scopes(): + response = client.get("recursive_scopes") + assert response.status_code == 200 + assert response.json() == { + "dep1": ["scope3", "scope1"], + "dep2": ["scope3", "scope2"], + }