Coverage for tests/test_tutorial/test_request_forms/test_tutorial001_an.py: 100%
32 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 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_forms.tutorial001_an 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"}) 1abcde
16 assert response.status_code == 200 1abcde
17 assert response.json() == {"username": "Foo"} 1abcde
20def test_post_body_form_no_password(client: TestClient): 1abcde
21 response = client.post("/login/", data={"username": "Foo"}) 1abcde
22 assert response.status_code == 422 1abcde
23 assert response.json() == IsDict( 1abcde
24 {
25 "detail": [
26 {
27 "type": "missing",
28 "loc": ["body", "password"],
29 "msg": "Field required",
30 "input": None,
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"}) 1abcde
50 assert response.status_code == 422 1abcde
51 assert response.json() == IsDict( 1abcde
52 {
53 "detail": [
54 {
55 "type": "missing",
56 "loc": ["body", "username"],
57 "msg": "Field required",
58 "input": None,
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/") 1abcde
78 assert response.status_code == 422 1abcde
79 assert response.json() == IsDict( 1abcde
80 {
81 "detail": [
82 {
83 "type": "missing",
84 "loc": ["body", "username"],
85 "msg": "Field required",
86 "input": None,
87 },
88 {
89 "type": "missing",
90 "loc": ["body", "password"],
91 "msg": "Field required",
92 "input": None,
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"}) 1abcde
117 assert response.status_code == 422, response.text 1abcde
118 assert response.json() == IsDict( 1abcde
119 {
120 "detail": [
121 {
122 "type": "missing",
123 "loc": ["body", "username"],
124 "msg": "Field required",
125 "input": None,
126 },
127 {
128 "type": "missing",
129 "loc": ["body", "password"],
130 "msg": "Field required",
131 "input": None,
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") 1abcde
156 assert response.status_code == 200, response.text 1abcde
157 assert response.json() == { 1abcde
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": {
185 "$ref": "#/components/schemas/Body_login_login__post"
186 }
187 }
188 },
189 "required": True,
190 },
191 }
192 }
193 },
194 "components": {
195 "schemas": {
196 "Body_login_login__post": {
197 "title": "Body_login_login__post",
198 "required": ["username", "password"],
199 "type": "object",
200 "properties": {
201 "username": {"title": "Username", "type": "string"},
202 "password": {"title": "Password", "type": "string"},
203 },
204 },
205 "ValidationError": {
206 "title": "ValidationError",
207 "required": ["loc", "msg", "type"],
208 "type": "object",
209 "properties": {
210 "loc": {
211 "title": "Location",
212 "type": "array",
213 "items": {
214 "anyOf": [{"type": "string"}, {"type": "integer"}]
215 },
216 },
217 "msg": {"title": "Message", "type": "string"},
218 "type": {"title": "Error Type", "type": "string"},
219 },
220 },
221 "HTTPValidationError": {
222 "title": "HTTPValidationError",
223 "type": "object",
224 "properties": {
225 "detail": {
226 "title": "Detail",
227 "type": "array",
228 "items": {"$ref": "#/components/schemas/ValidationError"},
229 }
230 },
231 },
232 }
233 },
234 }