test_dependency_paramless.py — fastapi Source File
Architecture documentation for test_dependency_paramless.py, a python file in the fastapi codebase. 4 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR d2d88355_3702_eb8f_f242_b3e87e712831["test_dependency_paramless.py"] 0dda2280_3359_8460_301c_e98c77e78185["typing"] d2d88355_3702_eb8f_f242_b3e87e712831 --> 0dda2280_3359_8460_301c_e98c77e78185 534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"] d2d88355_3702_eb8f_f242_b3e87e712831 --> 534f6e44_61b8_3c38_8b89_6934a6df9802 35c4ea20_c454_5afd_6cda_f0818bbcc650["__init__.py"] d2d88355_3702_eb8f_f242_b3e87e712831 --> 35c4ea20_c454_5afd_6cda_f0818bbcc650 a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"] d2d88355_3702_eb8f_f242_b3e87e712831 --> a7c04dee_ee23_5891_b185_47ff6bed036d style d2d88355_3702_eb8f_f242_b3e87e712831 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
from typing import Annotated, Union
from fastapi import FastAPI, HTTPException, Security
from fastapi.security import (
OAuth2PasswordBearer,
SecurityScopes,
)
from fastapi.testclient import TestClient
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def process_auth(
credentials: Annotated[Union[str, None], Security(oauth2_scheme)],
security_scopes: SecurityScopes,
):
# This is an incorrect way of using it, this is not checking if the scopes are
# provided by the token, only if the endpoint is requesting them, but the test
# here is just to check if FastAPI is indeed registering and passing the scopes
# correctly when using Security with parameterless dependencies.
if "a" not in security_scopes.scopes or "b" not in security_scopes.scopes:
raise HTTPException(detail="a or b not in scopes", status_code=401)
return {"token": credentials, "scopes": security_scopes.scopes}
@app.get("/get-credentials")
def get_credentials(
credentials: Annotated[dict, Security(process_auth, scopes=["a", "b"])],
):
return credentials
@app.get(
"/parameterless-with-scopes",
dependencies=[Security(process_auth, scopes=["a", "b"])],
)
def get_parameterless_with_scopes():
return {"status": "ok"}
@app.get(
"/parameterless-without-scopes",
dependencies=[Security(process_auth)],
)
def get_parameterless_without_scopes():
return {"status": "ok"}
client = TestClient(app)
def test_get_credentials():
response = client.get("/get-credentials", headers={"authorization": "Bearer token"})
assert response.status_code == 200, response.text
assert response.json() == {"token": "token", "scopes": ["a", "b"]}
def test_parameterless_with_scopes():
response = client.get(
"/parameterless-with-scopes", headers={"authorization": "Bearer token"}
)
assert response.status_code == 200, response.text
assert response.json() == {"status": "ok"}
def test_parameterless_without_scopes():
response = client.get(
"/parameterless-without-scopes", headers={"authorization": "Bearer token"}
)
assert response.status_code == 401, response.text
assert response.json() == {"detail": "a or b not in scopes"}
def test_call_get_parameterless_without_scopes_for_coverage():
assert get_parameterless_without_scopes() == {"status": "ok"}
Domain
Subdomains
Functions
Dependencies
Source
Frequently Asked Questions
What does test_dependency_paramless.py do?
test_dependency_paramless.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_dependency_paramless.py?
test_dependency_paramless.py defines 8 function(s): get_credentials, get_parameterless_with_scopes, get_parameterless_without_scopes, process_auth, test_call_get_parameterless_without_scopes_for_coverage, test_get_credentials, test_parameterless_with_scopes, test_parameterless_without_scopes.
What does test_dependency_paramless.py depend on?
test_dependency_paramless.py imports 4 module(s): __init__.py, __init__.py, testclient.py, typing.
Where is test_dependency_paramless.py in the architecture?
test_dependency_paramless.py is located at tests/test_dependency_paramless.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