Coverage for tests/test_tutorial/test_request_form_models/test_tutorial002.py: 100%
43 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 fastapi.testclient import TestClient 1abcde
4from tests.utils import needs_pydanticv2 1abcde
7@pytest.fixture(name="client") 1abcde
8def get_client(): 1abcde
9 from docs_src.request_form_models.tutorial002 import app 1abcde
11 client = TestClient(app) 1abcde
12 return client 1abcde
15@needs_pydanticv2 1abcde
16def test_post_body_form(client: TestClient): 1abcde
17 response = client.post("/login/", data={"username": "Foo", "password": "secret"}) 1fghij
18 assert response.status_code == 200 1fghij
19 assert response.json() == {"username": "Foo", "password": "secret"} 1fghij
22@needs_pydanticv2 1abcde
23def test_post_body_extra_form(client: TestClient): 1abcde
24 response = client.post( 1klmno
25 "/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
26 )
27 assert response.status_code == 422 1klmno
28 assert response.json() == { 1klmno
29 "detail": [
30 {
31 "type": "extra_forbidden",
32 "loc": ["body", "extra"],
33 "msg": "Extra inputs are not permitted",
34 "input": "extra",
35 }
36 ]
37 }
40@needs_pydanticv2 1abcde
41def test_post_body_form_no_password(client: TestClient): 1abcde
42 response = client.post("/login/", data={"username": "Foo"}) 1pqrst
43 assert response.status_code == 422 1pqrst
44 assert response.json() == { 1pqrst
45 "detail": [
46 {
47 "type": "missing",
48 "loc": ["body", "password"],
49 "msg": "Field required",
50 "input": {"username": "Foo"},
51 }
52 ]
53 }
56@needs_pydanticv2 1abcde
57def test_post_body_form_no_username(client: TestClient): 1abcde
58 response = client.post("/login/", data={"password": "secret"}) 1uvwxy
59 assert response.status_code == 422 1uvwxy
60 assert response.json() == { 1uvwxy
61 "detail": [
62 {
63 "type": "missing",
64 "loc": ["body", "username"],
65 "msg": "Field required",
66 "input": {"password": "secret"},
67 }
68 ]
69 }
72@needs_pydanticv2 1abcde
73def test_post_body_form_no_data(client: TestClient): 1abcde
74 response = client.post("/login/") 1zABCD
75 assert response.status_code == 422 1zABCD
76 assert response.json() == { 1zABCD
77 "detail": [
78 {
79 "type": "missing",
80 "loc": ["body", "username"],
81 "msg": "Field required",
82 "input": {},
83 },
84 {
85 "type": "missing",
86 "loc": ["body", "password"],
87 "msg": "Field required",
88 "input": {},
89 },
90 ]
91 }
94@needs_pydanticv2 1abcde
95def test_post_body_json(client: TestClient): 1abcde
96 response = client.post("/login/", json={"username": "Foo", "password": "secret"}) 1EFGHI
97 assert response.status_code == 422, response.text 1EFGHI
98 assert response.json() == { 1EFGHI
99 "detail": [
100 {
101 "type": "missing",
102 "loc": ["body", "username"],
103 "msg": "Field required",
104 "input": {},
105 },
106 {
107 "type": "missing",
108 "loc": ["body", "password"],
109 "msg": "Field required",
110 "input": {},
111 },
112 ]
113 }
116@needs_pydanticv2 1abcde
117def test_openapi_schema(client: TestClient): 1abcde
118 response = client.get("/openapi.json") 1JKLMN
119 assert response.status_code == 200, response.text 1JKLMN
120 assert response.json() == { 1JKLMN
121 "openapi": "3.1.0",
122 "info": {"title": "FastAPI", "version": "0.1.0"},
123 "paths": {
124 "/login/": {
125 "post": {
126 "responses": {
127 "200": {
128 "description": "Successful Response",
129 "content": {"application/json": {"schema": {}}},
130 },
131 "422": {
132 "description": "Validation Error",
133 "content": {
134 "application/json": {
135 "schema": {
136 "$ref": "#/components/schemas/HTTPValidationError"
137 }
138 }
139 },
140 },
141 },
142 "summary": "Login",
143 "operationId": "login_login__post",
144 "requestBody": {
145 "content": {
146 "application/x-www-form-urlencoded": {
147 "schema": {"$ref": "#/components/schemas/FormData"}
148 }
149 },
150 "required": True,
151 },
152 }
153 }
154 },
155 "components": {
156 "schemas": {
157 "FormData": {
158 "properties": {
159 "username": {"type": "string", "title": "Username"},
160 "password": {"type": "string", "title": "Password"},
161 },
162 "additionalProperties": False,
163 "type": "object",
164 "required": ["username", "password"],
165 "title": "FormData",
166 },
167 "ValidationError": {
168 "title": "ValidationError",
169 "required": ["loc", "msg", "type"],
170 "type": "object",
171 "properties": {
172 "loc": {
173 "title": "Location",
174 "type": "array",
175 "items": {
176 "anyOf": [{"type": "string"}, {"type": "integer"}]
177 },
178 },
179 "msg": {"title": "Message", "type": "string"},
180 "type": {"title": "Error Type", "type": "string"},
181 },
182 },
183 "HTTPValidationError": {
184 "title": "HTTPValidationError",
185 "type": "object",
186 "properties": {
187 "detail": {
188 "title": "Detail",
189 "type": "array",
190 "items": {"$ref": "#/components/schemas/ValidationError"},
191 }
192 },
193 },
194 }
195 },
196 }