Coverage for tests/test_tutorial/test_request_form_models/test_tutorial001.py: 100%
32 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
1import pytest 1abcde
2from dirty_equals import IsDict 1abcde
3from fastapi.testclient import TestClient 1abcde
6@pytest.fixture(name="client") 1abcde
7def get_client(): 1abcde
8 from docs_src.request_form_models.tutorial001 import app 1abcde
10 client = TestClient(app) 1abcde
11 return client 1abcde
14def test_post_body_form(client: TestClient): 1abcde
15 response = client.post("/login/", data={"username": "Foo", "password": "secret"}) 1fghij
16 assert response.status_code == 200 1fghij
17 assert response.json() == {"username": "Foo", "password": "secret"} 1fghij
20def test_post_body_form_no_password(client: TestClient): 1abcde
21 response = client.post("/login/", data={"username": "Foo"}) 1klmno
22 assert response.status_code == 422 1klmno
23 assert response.json() == IsDict( 1klmno
24 {
25 "detail": [
26 {
27 "type": "missing",
28 "loc": ["body", "password"],
29 "msg": "Field required",
30 "input": {"username": "Foo"},
31 }
32 ]
33 }
34 ) | IsDict(
35 # TODO: remove when deprecating Pydantic v1
36 {
37 "detail": [
38 {
39 "loc": ["body", "password"],
40 "msg": "field required",
41 "type": "value_error.missing",
42 }
43 ]
44 }
45 )
48def test_post_body_form_no_username(client: TestClient): 1abcde
49 response = client.post("/login/", data={"password": "secret"}) 1pqrst
50 assert response.status_code == 422 1pqrst
51 assert response.json() == IsDict( 1pqrst
52 {
53 "detail": [
54 {
55 "type": "missing",
56 "loc": ["body", "username"],
57 "msg": "Field required",
58 "input": {"password": "secret"},
59 }
60 ]
61 }
62 ) | IsDict(
63 # TODO: remove when deprecating Pydantic v1
64 {
65 "detail": [
66 {
67 "loc": ["body", "username"],
68 "msg": "field required",
69 "type": "value_error.missing",
70 }
71 ]
72 }
73 )
76def test_post_body_form_no_data(client: TestClient): 1abcde
77 response = client.post("/login/") 1uvwxy
78 assert response.status_code == 422 1uvwxy
79 assert response.json() == IsDict( 1uvwxy
80 {
81 "detail": [
82 {
83 "type": "missing",
84 "loc": ["body", "username"],
85 "msg": "Field required",
86 "input": {},
87 },
88 {
89 "type": "missing",
90 "loc": ["body", "password"],
91 "msg": "Field required",
92 "input": {},
93 },
94 ]
95 }
96 ) | IsDict(
97 # TODO: remove when deprecating Pydantic v1
98 {
99 "detail": [
100 {
101 "loc": ["body", "username"],
102 "msg": "field required",
103 "type": "value_error.missing",
104 },
105 {
106 "loc": ["body", "password"],
107 "msg": "field required",
108 "type": "value_error.missing",
109 },
110 ]
111 }
112 )
115def test_post_body_json(client: TestClient): 1abcde
116 response = client.post("/login/", json={"username": "Foo", "password": "secret"}) 1zABCD
117 assert response.status_code == 422, response.text 1zABCD
118 assert response.json() == IsDict( 1zABCD
119 {
120 "detail": [
121 {
122 "type": "missing",
123 "loc": ["body", "username"],
124 "msg": "Field required",
125 "input": {},
126 },
127 {
128 "type": "missing",
129 "loc": ["body", "password"],
130 "msg": "Field required",
131 "input": {},
132 },
133 ]
134 }
135 ) | IsDict(
136 # TODO: remove when deprecating Pydantic v1
137 {
138 "detail": [
139 {
140 "loc": ["body", "username"],
141 "msg": "field required",
142 "type": "value_error.missing",
143 },
144 {
145 "loc": ["body", "password"],
146 "msg": "field required",
147 "type": "value_error.missing",
148 },
149 ]
150 }
151 )
154def test_openapi_schema(client: TestClient): 1abcde
155 response = client.get("/openapi.json") 1EFGHI
156 assert response.status_code == 200, response.text 1EFGHI
157 assert response.json() == { 1EFGHI
158 "openapi": "3.1.0",
159 "info": {"title": "FastAPI", "version": "0.1.0"},
160 "paths": {
161 "/login/": {
162 "post": {
163 "responses": {
164 "200": {
165 "description": "Successful Response",
166 "content": {"application/json": {"schema": {}}},
167 },
168 "422": {
169 "description": "Validation Error",
170 "content": {
171 "application/json": {
172 "schema": {
173 "$ref": "#/components/schemas/HTTPValidationError"
174 }
175 }
176 },
177 },
178 },
179 "summary": "Login",
180 "operationId": "login_login__post",
181 "requestBody": {
182 "content": {
183 "application/x-www-form-urlencoded": {
184 "schema": {"$ref": "#/components/schemas/FormData"}
185 }
186 },
187 "required": True,
188 },
189 }
190 }
191 },
192 "components": {
193 "schemas": {
194 "FormData": {
195 "properties": {
196 "username": {"type": "string", "title": "Username"},
197 "password": {"type": "string", "title": "Password"},
198 },
199 "type": "object",
200 "required": ["username", "password"],
201 "title": "FormData",
202 },
203 "ValidationError": {
204 "title": "ValidationError",
205 "required": ["loc", "msg", "type"],
206 "type": "object",
207 "properties": {
208 "loc": {
209 "title": "Location",
210 "type": "array",
211 "items": {
212 "anyOf": [{"type": "string"}, {"type": "integer"}]
213 },
214 },
215 "msg": {"title": "Message", "type": "string"},
216 "type": {"title": "Error Type", "type": "string"},
217 },
218 },
219 "HTTPValidationError": {
220 "title": "HTTPValidationError",
221 "type": "object",
222 "properties": {
223 "detail": {
224 "title": "Detail",
225 "type": "array",
226 "items": {"$ref": "#/components/schemas/ValidationError"},
227 }
228 },
229 },
230 }
231 },
232 }