Home / File/ test_security_scopes_sub_dependency.py — fastapi Source File

test_security_scopes_sub_dependency.py — fastapi Source File

Architecture documentation for test_security_scopes_sub_dependency.py, a python file in the fastapi codebase. 5 imports, 0 dependents.

File python FastAPI Responses 5 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  31e6d116_fac5_2949_d314_49a3be00eaa8["test_security_scopes_sub_dependency.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  31e6d116_fac5_2949_d314_49a3be00eaa8 --> 0dda2280_3359_8460_301c_e98c77e78185
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  31e6d116_fac5_2949_d314_49a3be00eaa8 --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  31e6d116_fac5_2949_d314_49a3be00eaa8 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  35c4ea20_c454_5afd_6cda_f0818bbcc650["__init__.py"]
  31e6d116_fac5_2949_d314_49a3be00eaa8 --> 35c4ea20_c454_5afd_6cda_f0818bbcc650
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  31e6d116_fac5_2949_d314_49a3be00eaa8 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style 31e6d116_fac5_2949_d314_49a3be00eaa8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

# Ref: https://github.com/fastapi/fastapi/discussions/6024#discussioncomment-8541913


from typing import Annotated

import pytest
from fastapi import Depends, FastAPI, Security
from fastapi.security import SecurityScopes
from fastapi.testclient import TestClient


@pytest.fixture(name="call_counts")
def call_counts_fixture():
    return {
        "get_db_session": 0,
        "get_current_user": 0,
        "get_user_me": 0,
        "get_user_items": 0,
    }


@pytest.fixture(name="app")
def app_fixture(call_counts: dict[str, int]):
    def get_db_session():
        call_counts["get_db_session"] += 1
        return f"db_session_{call_counts['get_db_session']}"

    def get_current_user(
        security_scopes: SecurityScopes,
        db_session: Annotated[str, Depends(get_db_session)],
    ):
        call_counts["get_current_user"] += 1
        return {
            "user": f"user_{call_counts['get_current_user']}",
            "scopes": security_scopes.scopes,
            "db_session": db_session,
        }

    def get_user_me(
        current_user: Annotated[dict, Security(get_current_user, scopes=["me"])],
    ):
        call_counts["get_user_me"] += 1
        return {
            "user_me": f"user_me_{call_counts['get_user_me']}",
            "current_user": current_user,
        }

    def get_user_items(
        user_me: Annotated[dict, Depends(get_user_me)],
    ):
        call_counts["get_user_items"] += 1
        return {
            "user_items": f"user_items_{call_counts['get_user_items']}",
            "user_me": user_me,
        }

    app = FastAPI()

    @app.get("/")
    def path_operation(
        user_me: Annotated[dict, Depends(get_user_me)],
        user_items: Annotated[dict, Security(get_user_items, scopes=["items"])],
    ):
        return {
            "user_me": user_me,
            "user_items": user_items,
        }

    return app


@pytest.fixture(name="client")
def client_fixture(app: FastAPI):
    return TestClient(app)


def test_security_scopes_sub_dependency_caching(
    client: TestClient, call_counts: dict[str, int]
):
    response = client.get("/")

    assert response.status_code == 200
    assert call_counts["get_db_session"] == 1
    assert call_counts["get_current_user"] == 2
    assert call_counts["get_user_me"] == 2
    assert call_counts["get_user_items"] == 1
    assert response.json() == {
        "user_me": {
            "user_me": "user_me_1",
            "current_user": {
                "user": "user_1",
                "scopes": ["me"],
                "db_session": "db_session_1",
            },
        },
        "user_items": {
            "user_items": "user_items_1",
            "user_me": {
                "user_me": "user_me_2",
                "current_user": {
                    "user": "user_2",
                    "scopes": ["items", "me"],
                    "db_session": "db_session_1",
                },
            },
        },
    }

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_security_scopes_sub_dependency.py do?
test_security_scopes_sub_dependency.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in test_security_scopes_sub_dependency.py?
test_security_scopes_sub_dependency.py defines 4 function(s): app_fixture, call_counts_fixture, client_fixture, test_security_scopes_sub_dependency_caching.
What does test_security_scopes_sub_dependency.py depend on?
test_security_scopes_sub_dependency.py imports 5 module(s): __init__.py, __init__.py, pytest, testclient.py, typing.
Where is test_security_scopes_sub_dependency.py in the architecture?
test_security_scopes_sub_dependency.py is located at tests/test_security_scopes_sub_dependency.py (domain: FastAPI, subdomain: Responses, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free