1 changed files with 74 additions and 0 deletions
@ -0,0 +1,74 @@ |
|||||
|
import importlib |
||||
|
|
||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from ...utils import needs_py39, needs_py310, needs_pydanticv2 |
||||
|
|
||||
|
skip_for_pydantic_v1 = needs_pydanticv2 |
||||
|
|
||||
|
|
||||
|
@pytest.fixture( |
||||
|
name="client", |
||||
|
params=[ |
||||
|
pytest.param("tutorial002"), |
||||
|
pytest.param("tutorial002_py310", marks=needs_py310), |
||||
|
pytest.param("tutorial002_an"), |
||||
|
pytest.param("tutorial002_an_py39", marks=needs_py39), |
||||
|
pytest.param("tutorial002_an_py310", marks=needs_py310), |
||||
|
], |
||||
|
) |
||||
|
def get_client(request: pytest.FixtureRequest): |
||||
|
mod = importlib.import_module(f"docs_src.security.{request.param}") |
||||
|
client = TestClient(mod.app) |
||||
|
return client |
||||
|
|
||||
|
|
||||
|
def test_no_token(client: TestClient): |
||||
|
response = client.get("/users/me") |
||||
|
assert response.status_code == 401, response.text |
||||
|
assert response.json() == {"detail": "Not authenticated"} |
||||
|
assert response.headers["WWW-Authenticate"] == "Bearer" |
||||
|
|
||||
|
|
||||
|
def test_token(client: TestClient): |
||||
|
response = client.get("/users/me", headers={"Authorization": "Bearer testtoken"}) |
||||
|
assert response.status_code == 200, response.text |
||||
|
assert response.json() == { |
||||
|
"username": "testtokenfakedecoded", |
||||
|
"email": "[email protected]", |
||||
|
"full_name": "John Doe", |
||||
|
"disabled": None, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(client: TestClient): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200, response.text |
||||
|
assert response.json() == { |
||||
|
"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 Users Me", |
||||
|
"operationId": "read_users_me_users_me_get", |
||||
|
"security": [{"OAuth2PasswordBearer": []}], |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
"components": { |
||||
|
"securitySchemes": { |
||||
|
"OAuth2PasswordBearer": { |
||||
|
"type": "oauth2", |
||||
|
"flows": {"password": {"scopes": {}, "tokenUrl": "token"}}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
Loading…
Reference in new issue