Coverage for tests/test_tutorial/test_request_form_models/test_tutorial002_pv1.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 import app 1abcde
11 client = TestClient(app) 1abcde
12 return client 1abcde
15@needs_pydanticv1 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_pydanticv1 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": "value_error.extra",
32 "loc": ["body", "extra"],
33 "msg": "extra fields not permitted",
34 }
35 ]
36 }
39@needs_pydanticv1 1abcde
40def test_post_body_form_no_password(client: TestClient): 1abcde
41 response = client.post("/login/", data={"username": "Foo"}) 1pqrst
42 assert response.status_code == 422 1pqrst
43 assert response.json() == { 1pqrst
44 "detail": [
45 {
46 "type": "value_error.missing",
47 "loc": ["body", "password"],
48 "msg": "field required",
49 }
50 ]
51 }
54@needs_pydanticv1 1abcde
55def test_post_body_form_no_username(client: TestClient): 1abcde
56 response = client.post("/login/", data={"password": "secret"}) 1uvwxy
57 assert response.status_code == 422 1uvwxy
58 assert response.json() == { 1uvwxy
59 "detail": [
60 {
61 "type": "value_error.missing",
62 "loc": ["body", "username"],
63 "msg": "field required",
64 }
65 ]
66 }
69@needs_pydanticv1 1abcde
70def test_post_body_form_no_data(client: TestClient): 1abcde
71 response = client.post("/login/") 1zABCD
72 assert response.status_code == 422 1zABCD
73 assert response.json() == { 1zABCD
74 "detail": [
75 {
76 "type": "value_error.missing",
77 "loc": ["body", "username"],
78 "msg": "field required",
79 },
80 {
81 "type": "value_error.missing",
82 "loc": ["body", "password"],
83 "msg": "field required",
84 },
85 ]
86 }
89@needs_pydanticv1 1abcde
90def test_post_body_json(client: TestClient): 1abcde
91 response = client.post("/login/", json={"username": "Foo", "password": "secret"}) 1EFGHI
92 assert response.status_code == 422, response.text 1EFGHI
93 assert response.json() == { 1EFGHI
94 "detail": [
95 {
96 "type": "value_error.missing",
97 "loc": ["body", "username"],
98 "msg": "field required",
99 },
100 {
101 "type": "value_error.missing",
102 "loc": ["body", "password"],
103 "msg": "field required",
104 },
105 ]
106 }
109@needs_pydanticv1 1abcde
110def test_openapi_schema(client: TestClient): 1abcde
111 response = client.get("/openapi.json") 1JKLMN
112 assert response.status_code == 200, response.text 1JKLMN
113 assert response.json() == { 1JKLMN
114 "openapi": "3.1.0",
115 "info": {"title": "FastAPI", "version": "0.1.0"},
116 "paths": {
117 "/login/": {
118 "post": {
119 "responses": {
120 "200": {
121 "description": "Successful Response",
122 "content": {"application/json": {"schema": {}}},
123 },
124 "422": {
125 "description": "Validation Error",
126 "content": {
127 "application/json": {
128 "schema": {
129 "$ref": "#/components/schemas/HTTPValidationError"
130 }
131 }
132 },
133 },
134 },
135 "summary": "Login",
136 "operationId": "login_login__post",
137 "requestBody": {
138 "content": {
139 "application/x-www-form-urlencoded": {
140 "schema": {"$ref": "#/components/schemas/FormData"}
141 }
142 },
143 "required": True,
144 },
145 }
146 }
147 },
148 "components": {
149 "schemas": {
150 "FormData": {
151 "properties": {
152 "username": {"type": "string", "title": "Username"},
153 "password": {"type": "string", "title": "Password"},
154 },
155 "additionalProperties": False,
156 "type": "object",
157 "required": ["username", "password"],
158 "title": "FormData",
159 },
160 "ValidationError": {
161 "title": "ValidationError",
162 "required": ["loc", "msg", "type"],
163 "type": "object",
164 "properties": {
165 "loc": {
166 "title": "Location",
167 "type": "array",
168 "items": {
169 "anyOf": [{"type": "string"}, {"type": "integer"}]
170 },
171 },
172 "msg": {"title": "Message", "type": "string"},
173 "type": {"title": "Error Type", "type": "string"},
174 },
175 },
176 "HTTPValidationError": {
177 "title": "HTTPValidationError",
178 "type": "object",
179 "properties": {
180 "detail": {
181 "title": "Detail",
182 "type": "array",
183 "items": {"$ref": "#/components/schemas/ValidationError"},
184 }
185 },
186 },
187 }
188 },
189 }