| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture( |
| 9 |
name="client", |
| 10 |
params=[ |
| 11 |
"tutorial001_py310", |
| 12 |
"tutorial001_an_py310", |
| 13 |
], |
| 14 |
) |
| 15 |
def get_client(request: pytest.FixtureRequest): |
| 16 |
mod = importlib.import_module(f"docs_src.request_form_models.{request.param}") |
| 17 |
|
| 18 |
client = TestClient(mod.app) |
| 19 |
return client |
| 20 |
|
| 21 |
|
| 22 |
def test_post_body_form(client: TestClient): |
| 23 |
response = client.post("/login/", data={"username": "Foo", "password": "secret"}) |
| 24 |
assert response.status_code == 200 |
| 25 |
assert response.json() == {"username": "Foo", "password": "secret"} |
| 26 |
|
| 27 |
|
| 28 |
def test_post_body_form_no_password(client: TestClient): |
| 29 |
response = client.post("/login/", data={"username": "Foo"}) |
| 30 |
assert response.status_code == 422 |
| 31 |
assert response.json() == { |
| 32 |
"detail": [ |
| 33 |
{ |
| 34 |
"type": "missing", |
| 35 |
"loc": ["body", "password"], |
| 36 |
"msg": "Field required", |
| 37 |
"input": {"username": "Foo"}, |
| 38 |
} |
| 39 |
] |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
def test_post_body_form_no_username(client: TestClient): |
| 44 |
response = client.post("/login/", data={"password": "secret"}) |
| 45 |
assert response.status_code == 422 |
| 46 |
assert response.json() == { |
| 47 |
"detail": [ |
| 48 |
{ |
| 49 |
"type": "missing", |
| 50 |
"loc": ["body", "username"], |
| 51 |
"msg": "Field required", |
| 52 |
"input": {"password": "secret"}, |
| 53 |
} |
| 54 |
] |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
def test_post_body_form_no_data(client: TestClient): |
| 59 |
response = client.post("/login/") |
| 60 |
assert response.status_code == 422 |
| 61 |
assert response.json() == { |
| 62 |
"detail": [ |
| 63 |
{ |
| 64 |
"type": "missing", |
| 65 |
"loc": ["body", "username"], |
| 66 |
"msg": "Field required", |
| 67 |
"input": {}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
"type": "missing", |
| 71 |
"loc": ["body", "password"], |
| 72 |
"msg": "Field required", |
| 73 |
"input": {}, |
| 74 |
}, |
| 75 |
] |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
def test_post_body_json(client: TestClient): |
| 80 |
response = client.post("/login/", json={"username": "Foo", "password": "secret"}) |
| 81 |
assert response.status_code == 422, response.text |
| 82 |
assert response.json() == { |
| 83 |
"detail": [ |
| 84 |
{ |
| 85 |
"type": "missing", |
| 86 |
"loc": ["body", "username"], |
| 87 |
"msg": "Field required", |
| 88 |
"input": {}, |
| 89 |
}, |
| 90 |
{ |
| 91 |
"type": "missing", |
| 92 |
"loc": ["body", "password"], |
| 93 |
"msg": "Field required", |
| 94 |
"input": {}, |
| 95 |
}, |
| 96 |
] |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
def test_openapi_schema(client: TestClient): |
| 101 |
response = client.get("/openapi.json") |
| 102 |
assert response.status_code == 200, response.text |
| 103 |
assert response.json() == snapshot( |
| 104 |
{ |
| 105 |
"openapi": "3.1.0", |
| 106 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 107 |
"paths": { |
| 108 |
"/login/": { |
| 109 |
"post": { |
| 110 |
"responses": { |
| 111 |
"200": { |
| 112 |
"description": "Successful Response", |
| 113 |
"content": {"application/json": {"schema": {}}}, |
| 114 |
}, |
| 115 |
"422": { |
| 116 |
"description": "Validation Error", |
| 117 |
"content": { |
| 118 |
"application/json": { |
| 119 |
"schema": { |
| 120 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 121 |
} |
| 122 |
} |
| 123 |
}, |
| 124 |
}, |
| 125 |
}, |
| 126 |
"summary": "Login", |
| 127 |
"operationId": "login_login__post", |
| 128 |
"requestBody": { |
| 129 |
"content": { |
| 130 |
"application/x-www-form-urlencoded": { |
| 131 |
"schema": {"$ref": "#/components/schemas/FormData"} |
| 132 |
} |
| 133 |
}, |
| 134 |
"required": True, |
| 135 |
}, |
| 136 |
} |
| 137 |
} |
| 138 |
}, |
| 139 |
"components": { |
| 140 |
"schemas": { |
| 141 |
"FormData": { |
| 142 |
"properties": { |
| 143 |
"username": {"type": "string", "title": "Username"}, |
| 144 |
"password": {"type": "string", "title": "Password"}, |
| 145 |
}, |
| 146 |
"type": "object", |
| 147 |
"required": ["username", "password"], |
| 148 |
"title": "FormData", |
| 149 |
}, |
| 150 |
"ValidationError": { |
| 151 |
"title": "ValidationError", |
| 152 |
"required": ["loc", "msg", "type"], |
| 153 |
"type": "object", |
| 154 |
"properties": { |
| 155 |
"loc": { |
| 156 |
"title": "Location", |
| 157 |
"type": "array", |
| 158 |
"items": { |
| 159 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 160 |
}, |
| 161 |
}, |
| 162 |
"msg": {"title": "Message", "type": "string"}, |
| 163 |
"type": {"title": "Error Type", "type": "string"}, |
| 164 |
"input": {"title": "Input"}, |
| 165 |
"ctx": {"title": "Context", "type": "object"}, |
| 166 |
}, |
| 167 |
}, |
| 168 |
"HTTPValidationError": { |
| 169 |
"title": "HTTPValidationError", |
| 170 |
"type": "object", |
| 171 |
"properties": { |
| 172 |
"detail": { |
| 173 |
"title": "Detail", |
| 174 |
"type": "array", |
| 175 |
"items": { |
| 176 |
"$ref": "#/components/schemas/ValidationError" |
| 177 |
}, |
| 178 |
} |
| 179 |
}, |
| 180 |
}, |
| 181 |
} |
| 182 |
}, |
| 183 |
} |
| 184 |
) |