Home / File/ test_security_api_key_header_optional.py — fastapi Source File

test_security_api_key_header_optional.py — fastapi Source File

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

File python FastAPI Responses 6 imports 5 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  c4979cd9_80cd_7995_c6ff_bb63eb1455c8["test_security_api_key_header_optional.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  c4979cd9_80cd_7995_c6ff_bb63eb1455c8 --> 0dda2280_3359_8460_301c_e98c77e78185
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  c4979cd9_80cd_7995_c6ff_bb63eb1455c8 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  35c4ea20_c454_5afd_6cda_f0818bbcc650["__init__.py"]
  c4979cd9_80cd_7995_c6ff_bb63eb1455c8 --> 35c4ea20_c454_5afd_6cda_f0818bbcc650
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  c4979cd9_80cd_7995_c6ff_bb63eb1455c8 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  a7f4e7b0_9725_db90_5cbc_7ca8211b323a["inline_snapshot"]
  c4979cd9_80cd_7995_c6ff_bb63eb1455c8 --> a7f4e7b0_9725_db90_5cbc_7ca8211b323a
  6913fbd4_39df_d14b_44bb_522e99b65b90["pydantic"]
  c4979cd9_80cd_7995_c6ff_bb63eb1455c8 --> 6913fbd4_39df_d14b_44bb_522e99b65b90
  style c4979cd9_80cd_7995_c6ff_bb63eb1455c8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Optional

from fastapi import Depends, FastAPI, Security
from fastapi.security import APIKeyHeader
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from pydantic import BaseModel

app = FastAPI()

api_key = APIKeyHeader(name="key", auto_error=False)


class User(BaseModel):
    username: str


def get_current_user(oauth_header: Optional[str] = Security(api_key)):
    if oauth_header is None:
        return None
    user = User(username=oauth_header)
    return user


@app.get("/users/me")
def read_current_user(current_user: Optional[User] = Depends(get_current_user)):
    if current_user is None:
        return {"msg": "Create an account first"}
    return current_user


client = TestClient(app)


def test_security_api_key():
    response = client.get("/users/me", headers={"key": "secret"})
    assert response.status_code == 200, response.text
    assert response.json() == {"username": "secret"}


def test_security_api_key_no_key():
    response = client.get("/users/me")
    assert response.status_code == 200, response.text
    assert response.json() == {"msg": "Create an account first"}


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": {
                "/users/me": {
                    "get": {
                        "responses": {
                            "200": {
                                "description": "Successful Response",
                                "content": {"application/json": {"schema": {}}},
                            }
                        },
                        "summary": "Read Current User",
                        "operationId": "read_current_user_users_me_get",
                        "security": [{"APIKeyHeader": []}],
                    }
                }
            },
            "components": {
                "securitySchemes": {
                    "APIKeyHeader": {"type": "apiKey", "name": "key", "in": "header"}
                }
            },
        }
    )

Domain

Subdomains

Classes

Dependencies

Frequently Asked Questions

What does test_security_api_key_header_optional.py do?
test_security_api_key_header_optional.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_api_key_header_optional.py?
test_security_api_key_header_optional.py defines 5 function(s): get_current_user, read_current_user, test_openapi_schema, test_security_api_key, test_security_api_key_no_key.
What does test_security_api_key_header_optional.py depend on?
test_security_api_key_header_optional.py imports 6 module(s): __init__.py, __init__.py, inline_snapshot, pydantic, testclient.py, typing.
Where is test_security_api_key_header_optional.py in the architecture?
test_security_api_key_header_optional.py is located at tests/test_security_api_key_header_optional.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