Home / File/ test_top_level_security_scheme_in_openapi.py — fastapi Source File

test_top_level_security_scheme_in_openapi.py — fastapi Source File

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

File python FastAPI Responses 4 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  e3231cdc_0792_5877_3436_629083a05aa3["test_top_level_security_scheme_in_openapi.py"]
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  e3231cdc_0792_5877_3436_629083a05aa3 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  35c4ea20_c454_5afd_6cda_f0818bbcc650["__init__.py"]
  e3231cdc_0792_5877_3436_629083a05aa3 --> 35c4ea20_c454_5afd_6cda_f0818bbcc650
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  e3231cdc_0792_5877_3436_629083a05aa3 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  a7f4e7b0_9725_db90_5cbc_7ca8211b323a["inline_snapshot"]
  e3231cdc_0792_5877_3436_629083a05aa3 --> a7f4e7b0_9725_db90_5cbc_7ca8211b323a
  style e3231cdc_0792_5877_3436_629083a05aa3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

# Test security scheme at the top level, including OpenAPI
# Ref: https://github.com/fastapi/fastapi/discussions/14263
# Ref: https://github.com/fastapi/fastapi/issues/14271
from fastapi import Depends, FastAPI
from fastapi.security import HTTPBearer
from fastapi.testclient import TestClient
from inline_snapshot import snapshot

app = FastAPI()

bearer_scheme = HTTPBearer()


@app.get("/", dependencies=[Depends(bearer_scheme)])
async def get_root():
    return {"message": "Hello, World!"}


client = TestClient(app)


def test_get_root():
    response = client.get("/", headers={"Authorization": "Bearer token"})
    assert response.status_code == 200, response.text
    assert response.json() == {"message": "Hello, World!"}


def test_get_root_no_token():
    response = client.get("/")
    assert response.status_code == 401, response.text
    assert response.json() == {"detail": "Not authenticated"}


def test_openapi_schema():
    response = client.get("/openapi.json")
    assert response.status_code == 200, response.text
    assert response.json() == snapshot(
        {
            "openapi": "3.1.0",
            "info": {"title": "FastAPI", "version": "0.1.0"},
            "paths": {
                "/": {
                    "get": {
                        "summary": "Get Root",
                        "operationId": "get_root__get",
                        "responses": {
                            "200": {
                                "description": "Successful Response",
                                "content": {"application/json": {"schema": {}}},
                            }
                        },
                        "security": [{"HTTPBearer": []}],
                    }
                }
            },
            "components": {
                "securitySchemes": {"HTTPBearer": {"type": "http", "scheme": "bearer"}}
            },
        }
    )

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_top_level_security_scheme_in_openapi.py do?
test_top_level_security_scheme_in_openapi.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_top_level_security_scheme_in_openapi.py?
test_top_level_security_scheme_in_openapi.py defines 4 function(s): get_root, test_get_root, test_get_root_no_token, test_openapi_schema.
What does test_top_level_security_scheme_in_openapi.py depend on?
test_top_level_security_scheme_in_openapi.py imports 4 module(s): __init__.py, __init__.py, inline_snapshot, testclient.py.
Where is test_top_level_security_scheme_in_openapi.py in the architecture?
test_top_level_security_scheme_in_openapi.py is located at tests/test_top_level_security_scheme_in_openapi.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