Coverage for tests/test_tutorial/test_request_form_models/test_tutorial002_pv1_an.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_pydanticv1 1abcde
7@pytest.fixture(name="client") 1abcde
8def get_client(): 1abcde
9 from docs_src.request_form_models.tutorial002_pv1_an import app 1abcde
11 client = TestClient(app) 1abcde
12 return client 1abcde
15# TODO: remove when deprecating Pydantic v1
16@needs_pydanticv1 1abcde
17def test_post_body_form(client: TestClient): 1abcde
18 response = client.post("/login/", data={"username": "Foo", "password": "secret"}) 1fghij
19 assert response.status_code == 200 1fghij
20 assert response.json() == {"username": "Foo", "password": "secret"} 1fghij
23# TODO: remove when deprecating Pydantic v1
24@needs_pydanticv1 1abcde
25def test_post_body_extra_form(client: TestClient): 1abcde
26 response = client.post( 1klmno
27 "/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
28 )
29 assert response.status_code == 422 1klmno
30 assert response.json() == { 1klmno
31 "detail": [
32 {
33 "type": "value_error.extra",
34 "loc": ["body", "extra"],
35 "msg": "extra fields not permitted",
36 }
37 ]
38 }
41# TODO: remove when deprecating Pydantic v1
42@needs_pydanticv1 1abcde
43def test_post_body_form_no_password(client: TestClient): 1abcde
44 response = client.post("/login/", data={"username": "Foo"}) 1pqrst
45 assert response.status_code == 422 1pqrst
46 assert response.json() == { 1pqrst
47 "detail": [
48 {
49 "type": "value_error.missing",
50 "loc": ["body", "password"],
51 "msg": "field required",
52 }
53 ]
54 }
57# TODO: remove when deprecating Pydantic v1
58@needs_pydanticv1 1abcde
59def test_post_body_form_no_username(client: TestClient): 1abcde
60 response = client.post("/login/", data={"password": "secret"}) 1uvwxy
61 assert response.status_code == 422 1uvwxy
62 assert response.json() == { 1uvwxy
63 "detail": [
64 {
65 "type": "value_error.missing",
66 "loc": ["body", "username"],
67 "msg": "field required",
68 }
69 ]
70 }
73# TODO: remove when deprecating Pydantic v1
74@needs_pydanticv1 1abcde
75def test_post_body_form_no_data(client: TestClient): 1abcde
76 response = client.post("/login/") 1zABCD
77 assert response.status_code == 422 1zABCD
78 assert response.json() == { 1zABCD
79 "detail": [
80 {
81 "type": "value_error.missing",
82 "loc": ["body", "username"],
83 "msg": "field required",
84 },
85 {
86 "type": "value_error.missing",
87 "loc": ["body", "password"],
88 "msg": "field required",
89 },
90 ]
91 }
94# TODO: remove when deprecating Pydantic v1
95@needs_pydanticv1 1abcde
96def test_post_body_json(client: TestClient): 1abcde
97 response = client.post("/login/", json={"username": "Foo", "password": "secret"}) 1EFGHI
98 assert response.status_code == 422, response.text 1EFGHI
99 assert response.json() == { 1EFGHI
100 "detail": [
101 {
102 "type": "value_error.missing",
103 "loc": ["body", "username"],
104 "msg": "field required",
105 },
106 {
107 "type": "value_error.missing",
108 "loc": ["body", "password"],
109 "msg": "field required",
110 },
111 ]
112 }
115# TODO: remove when deprecating Pydantic v1
116@needs_pydanticv1 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 }