mirror of https://github.com/tiangolo/fastapi.git
✨ Add support for forbidding extra form fields with Pydantic models (#12134)
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
This commit is contained in:
parent
1b06b53267
commit
4633b1bca9
|
|
@ -1,6 +1,6 @@
|
||||||
# Form Models
|
# Form Models
|
||||||
|
|
||||||
You can use Pydantic models to declare form fields in FastAPI.
|
You can use **Pydantic models** to declare **form fields** in FastAPI.
|
||||||
|
|
||||||
/// info
|
/// info
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ This is supported since FastAPI version `0.113.0`. 🤓
|
||||||
|
|
||||||
## Pydantic Models for Forms
|
## Pydantic Models for Forms
|
||||||
|
|
||||||
You just need to declare a Pydantic model with the fields you want to receive as form fields, and then declare the parameter as `Form`:
|
You just need to declare a **Pydantic model** with the fields you want to receive as **form fields**, and then declare the parameter as `Form`:
|
||||||
|
|
||||||
//// tab | Python 3.9+
|
//// tab | Python 3.9+
|
||||||
|
|
||||||
|
|
@ -54,7 +54,7 @@ Prefer to use the `Annotated` version if possible.
|
||||||
|
|
||||||
////
|
////
|
||||||
|
|
||||||
FastAPI will extract the data for each field from the form data in the request and give you the Pydantic model you defined.
|
**FastAPI** will **extract** the data for **each field** from the **form data** in the request and give you the Pydantic model you defined.
|
||||||
|
|
||||||
## Check the Docs
|
## Check the Docs
|
||||||
|
|
||||||
|
|
@ -63,3 +63,72 @@ You can verify it in the docs UI at `/docs`:
|
||||||
<div class="screenshot">
|
<div class="screenshot">
|
||||||
<img src="/img/tutorial/request-form-models/image01.png">
|
<img src="/img/tutorial/request-form-models/image01.png">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
## Restrict Extra Form Fields
|
||||||
|
|
||||||
|
In some special use cases (probably not very common), you might want to **restrict** the form fields to only those declared in the Pydantic model. And **forbid** any **extra** fields.
|
||||||
|
|
||||||
|
/// note
|
||||||
|
|
||||||
|
This is supported since FastAPI version `0.114.0`. 🤓
|
||||||
|
|
||||||
|
///
|
||||||
|
|
||||||
|
You can use Pydantic's model configuration to `forbid` any `extra` fields:
|
||||||
|
|
||||||
|
//// tab | Python 3.9+
|
||||||
|
|
||||||
|
```Python hl_lines="12"
|
||||||
|
{!> ../../../docs_src/request_form_models/tutorial002_an_py39.py!}
|
||||||
|
```
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
//// tab | Python 3.8+
|
||||||
|
|
||||||
|
```Python hl_lines="11"
|
||||||
|
{!> ../../../docs_src/request_form_models/tutorial002_an.py!}
|
||||||
|
```
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
//// tab | Python 3.8+ non-Annotated
|
||||||
|
|
||||||
|
/// tip
|
||||||
|
|
||||||
|
Prefer to use the `Annotated` version if possible.
|
||||||
|
|
||||||
|
///
|
||||||
|
|
||||||
|
```Python hl_lines="10"
|
||||||
|
{!> ../../../docs_src/request_form_models/tutorial002.py!}
|
||||||
|
```
|
||||||
|
|
||||||
|
////
|
||||||
|
|
||||||
|
If a client tries to send some extra data, they will receive an **error** response.
|
||||||
|
|
||||||
|
For example, if the client tries to send the form fields:
|
||||||
|
|
||||||
|
* `username`: `Rick`
|
||||||
|
* `password`: `Portal Gun`
|
||||||
|
* `extra`: `Mr. Poopybutthole`
|
||||||
|
|
||||||
|
They will receive an error response telling them that the field `extra` is not allowed:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "extra_forbidden",
|
||||||
|
"loc": ["body", "extra"],
|
||||||
|
"msg": "Extra inputs are not permitted",
|
||||||
|
"input": "Mr. Poopybutthole"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
You can use Pydantic models to declare form fields in FastAPI. 😎
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
from fastapi import FastAPI, Form
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class FormData(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
model_config = {"extra": "forbid"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/login/")
|
||||||
|
async def login(data: FormData = Form()):
|
||||||
|
return data
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
from fastapi import FastAPI, Form
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class FormData(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
model_config = {"extra": "forbid"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/login/")
|
||||||
|
async def login(data: Annotated[FormData, Form()]):
|
||||||
|
return data
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import FastAPI, Form
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class FormData(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
model_config = {"extra": "forbid"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/login/")
|
||||||
|
async def login(data: Annotated[FormData, Form()]):
|
||||||
|
return data
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
from fastapi import FastAPI, Form
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class FormData(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
extra = "forbid"
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/login/")
|
||||||
|
async def login(data: FormData = Form()):
|
||||||
|
return data
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
from fastapi import FastAPI, Form
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class FormData(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
extra = "forbid"
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/login/")
|
||||||
|
async def login(data: Annotated[FormData, Form()]):
|
||||||
|
return data
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import FastAPI, Form
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class FormData(BaseModel):
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
extra = "forbid"
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/login/")
|
||||||
|
async def login(data: Annotated[FormData, Form()]):
|
||||||
|
return data
|
||||||
|
|
@ -789,6 +789,9 @@ async def _extract_form_body(
|
||||||
value = serialize_sequence_value(field=field, value=results)
|
value = serialize_sequence_value(field=field, value=results)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
values[field.name] = value
|
values[field.name] = value
|
||||||
|
for key, value in received_body.items():
|
||||||
|
if key not in values:
|
||||||
|
values[key] = value
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,196 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from tests.utils import needs_pydanticv2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def get_client():
|
||||||
|
from docs_src.request_form_models.tutorial002 import app
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"username": "Foo", "password": "secret"}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_extra_form(client: TestClient):
|
||||||
|
response = client.post(
|
||||||
|
"/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
|
||||||
|
)
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "extra_forbidden",
|
||||||
|
"loc": ["body", "extra"],
|
||||||
|
"msg": "Extra inputs are not permitted",
|
||||||
|
"input": "extra",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form_no_password(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {"username": "Foo"},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form_no_username(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"password": "secret"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {"password": "secret"},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form_no_data(client: TestClient):
|
||||||
|
response = client.post("/login/")
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_json(client: TestClient):
|
||||||
|
response = client.post("/login/", json={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 422, response.text
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
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": {
|
||||||
|
"/login/": {
|
||||||
|
"post": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Login",
|
||||||
|
"operationId": "login_login__post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/x-www-form-urlencoded": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/FormData"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"FormData": {
|
||||||
|
"properties": {
|
||||||
|
"username": {"type": "string", "title": "Username"},
|
||||||
|
"password": {"type": "string", "title": "Password"},
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
"type": "object",
|
||||||
|
"required": ["username", "password"],
|
||||||
|
"title": "FormData",
|
||||||
|
},
|
||||||
|
"ValidationError": {
|
||||||
|
"title": "ValidationError",
|
||||||
|
"required": ["loc", "msg", "type"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"loc": {
|
||||||
|
"title": "Location",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"msg": {"title": "Message", "type": "string"},
|
||||||
|
"type": {"title": "Error Type", "type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,196 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from tests.utils import needs_pydanticv2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def get_client():
|
||||||
|
from docs_src.request_form_models.tutorial002_an import app
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"username": "Foo", "password": "secret"}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_extra_form(client: TestClient):
|
||||||
|
response = client.post(
|
||||||
|
"/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
|
||||||
|
)
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "extra_forbidden",
|
||||||
|
"loc": ["body", "extra"],
|
||||||
|
"msg": "Extra inputs are not permitted",
|
||||||
|
"input": "extra",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form_no_password(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {"username": "Foo"},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form_no_username(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"password": "secret"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {"password": "secret"},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_form_no_data(client: TestClient):
|
||||||
|
response = client.post("/login/")
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_post_body_json(client: TestClient):
|
||||||
|
response = client.post("/login/", json={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 422, response.text
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
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": {
|
||||||
|
"/login/": {
|
||||||
|
"post": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Login",
|
||||||
|
"operationId": "login_login__post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/x-www-form-urlencoded": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/FormData"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"FormData": {
|
||||||
|
"properties": {
|
||||||
|
"username": {"type": "string", "title": "Username"},
|
||||||
|
"password": {"type": "string", "title": "Password"},
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
"type": "object",
|
||||||
|
"required": ["username", "password"],
|
||||||
|
"title": "FormData",
|
||||||
|
},
|
||||||
|
"ValidationError": {
|
||||||
|
"title": "ValidationError",
|
||||||
|
"required": ["loc", "msg", "type"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"loc": {
|
||||||
|
"title": "Location",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"msg": {"title": "Message", "type": "string"},
|
||||||
|
"type": {"title": "Error Type", "type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,203 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from tests.utils import needs_py39, needs_pydanticv2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def get_client():
|
||||||
|
from docs_src.request_form_models.tutorial002_an_py39 import app
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"username": "Foo", "password": "secret"}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_extra_form(client: TestClient):
|
||||||
|
response = client.post(
|
||||||
|
"/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
|
||||||
|
)
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "extra_forbidden",
|
||||||
|
"loc": ["body", "extra"],
|
||||||
|
"msg": "Extra inputs are not permitted",
|
||||||
|
"input": "extra",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form_no_password(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {"username": "Foo"},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form_no_username(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"password": "secret"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {"password": "secret"},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form_no_data(client: TestClient):
|
||||||
|
response = client.post("/login/")
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_json(client: TestClient):
|
||||||
|
response = client.post("/login/", json={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 422, response.text
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "Field required",
|
||||||
|
"input": {},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
@needs_py39
|
||||||
|
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": {
|
||||||
|
"/login/": {
|
||||||
|
"post": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Login",
|
||||||
|
"operationId": "login_login__post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/x-www-form-urlencoded": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/FormData"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"FormData": {
|
||||||
|
"properties": {
|
||||||
|
"username": {"type": "string", "title": "Username"},
|
||||||
|
"password": {"type": "string", "title": "Password"},
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
"type": "object",
|
||||||
|
"required": ["username", "password"],
|
||||||
|
"title": "FormData",
|
||||||
|
},
|
||||||
|
"ValidationError": {
|
||||||
|
"title": "ValidationError",
|
||||||
|
"required": ["loc", "msg", "type"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"loc": {
|
||||||
|
"title": "Location",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"msg": {"title": "Message", "type": "string"},
|
||||||
|
"type": {"title": "Error Type", "type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,189 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from tests.utils import needs_pydanticv1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def get_client():
|
||||||
|
from docs_src.request_form_models.tutorial002_pv1 import app
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"username": "Foo", "password": "secret"}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_extra_form(client: TestClient):
|
||||||
|
response = client.post(
|
||||||
|
"/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
|
||||||
|
)
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.extra",
|
||||||
|
"loc": ["body", "extra"],
|
||||||
|
"msg": "extra fields not permitted",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form_no_password(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form_no_username(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"password": "secret"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form_no_data(client: TestClient):
|
||||||
|
response = client.post("/login/")
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_json(client: TestClient):
|
||||||
|
response = client.post("/login/", json={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 422, response.text
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv1
|
||||||
|
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": {
|
||||||
|
"/login/": {
|
||||||
|
"post": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Login",
|
||||||
|
"operationId": "login_login__post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/x-www-form-urlencoded": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/FormData"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"FormData": {
|
||||||
|
"properties": {
|
||||||
|
"username": {"type": "string", "title": "Username"},
|
||||||
|
"password": {"type": "string", "title": "Password"},
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
"type": "object",
|
||||||
|
"required": ["username", "password"],
|
||||||
|
"title": "FormData",
|
||||||
|
},
|
||||||
|
"ValidationError": {
|
||||||
|
"title": "ValidationError",
|
||||||
|
"required": ["loc", "msg", "type"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"loc": {
|
||||||
|
"title": "Location",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"msg": {"title": "Message", "type": "string"},
|
||||||
|
"type": {"title": "Error Type", "type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,196 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from tests.utils import needs_pydanticv1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def get_client():
|
||||||
|
from docs_src.request_form_models.tutorial002_pv1_an import app
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"username": "Foo", "password": "secret"}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_extra_form(client: TestClient):
|
||||||
|
response = client.post(
|
||||||
|
"/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
|
||||||
|
)
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.extra",
|
||||||
|
"loc": ["body", "extra"],
|
||||||
|
"msg": "extra fields not permitted",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form_no_password(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form_no_username(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"password": "secret"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_form_no_data(client: TestClient):
|
||||||
|
response = client.post("/login/")
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
def test_post_body_json(client: TestClient):
|
||||||
|
response = client.post("/login/", json={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 422, response.text
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
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": {
|
||||||
|
"/login/": {
|
||||||
|
"post": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Login",
|
||||||
|
"operationId": "login_login__post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/x-www-form-urlencoded": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/FormData"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"FormData": {
|
||||||
|
"properties": {
|
||||||
|
"username": {"type": "string", "title": "Username"},
|
||||||
|
"password": {"type": "string", "title": "Password"},
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
"type": "object",
|
||||||
|
"required": ["username", "password"],
|
||||||
|
"title": "FormData",
|
||||||
|
},
|
||||||
|
"ValidationError": {
|
||||||
|
"title": "ValidationError",
|
||||||
|
"required": ["loc", "msg", "type"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"loc": {
|
||||||
|
"title": "Location",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"msg": {"title": "Message", "type": "string"},
|
||||||
|
"type": {"title": "Error Type", "type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,203 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from tests.utils import needs_py39, needs_pydanticv1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def get_client():
|
||||||
|
from docs_src.request_form_models.tutorial002_pv1_an_py39 import app
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"username": "Foo", "password": "secret"}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_extra_form(client: TestClient):
|
||||||
|
response = client.post(
|
||||||
|
"/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
|
||||||
|
)
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.extra",
|
||||||
|
"loc": ["body", "extra"],
|
||||||
|
"msg": "extra fields not permitted",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form_no_password(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"username": "Foo"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form_no_username(client: TestClient):
|
||||||
|
response = client.post("/login/", data={"password": "secret"})
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_form_no_data(client: TestClient):
|
||||||
|
response = client.post("/login/")
|
||||||
|
assert response.status_code == 422
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
@needs_py39
|
||||||
|
def test_post_body_json(client: TestClient):
|
||||||
|
response = client.post("/login/", json={"username": "Foo", "password": "secret"})
|
||||||
|
assert response.status_code == 422, response.text
|
||||||
|
assert response.json() == {
|
||||||
|
"detail": [
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "username"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "value_error.missing",
|
||||||
|
"loc": ["body", "password"],
|
||||||
|
"msg": "field required",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: remove when deprecating Pydantic v1
|
||||||
|
@needs_pydanticv1
|
||||||
|
@needs_py39
|
||||||
|
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": {
|
||||||
|
"/login/": {
|
||||||
|
"post": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "Login",
|
||||||
|
"operationId": "login_login__post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/x-www-form-urlencoded": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/FormData"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"FormData": {
|
||||||
|
"properties": {
|
||||||
|
"username": {"type": "string", "title": "Username"},
|
||||||
|
"password": {"type": "string", "title": "Password"},
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
"type": "object",
|
||||||
|
"required": ["username", "password"],
|
||||||
|
"title": "FormData",
|
||||||
|
},
|
||||||
|
"ValidationError": {
|
||||||
|
"title": "ValidationError",
|
||||||
|
"required": ["loc", "msg", "type"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"loc": {
|
||||||
|
"title": "Location",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"msg": {"title": "Message", "type": "string"},
|
||||||
|
"type": {"title": "Error Type", "type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue