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