Coverage for tests / test_tutorial / test_request_forms / test_tutorial001.py: 100%
33 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
8@pytest.fixture( 1abdc
9 name="client",
10 params=[
11 "tutorial001_py310",
12 "tutorial001_an_py310",
13 ],
14)
15def get_client(request: pytest.FixtureRequest): 1abdc
16 mod = importlib.import_module(f"docs_src.request_forms.{request.param}") 1abc
18 client = TestClient(mod.app) 1abc
19 return client 1abc
22def test_post_body_form(client: TestClient): 1abdc
23 response = client.post("/login/", data={"username": "Foo", "password": "secret"}) 1efg
24 assert response.status_code == 200 1efg
25 assert response.json() == {"username": "Foo"} 1efg
28def test_post_body_form_no_password(client: TestClient): 1abdc
29 response = client.post("/login/", data={"username": "Foo"}) 1hij
30 assert response.status_code == 422 1hij
31 assert response.json() == { 1hij
32 "detail": [
33 {
34 "type": "missing",
35 "loc": ["body", "password"],
36 "msg": "Field required",
37 "input": None,
38 }
39 ]
40 }
43def test_post_body_form_no_username(client: TestClient): 1abdc
44 response = client.post("/login/", data={"password": "secret"}) 1klm
45 assert response.status_code == 422 1klm
46 assert response.json() == { 1klm
47 "detail": [
48 {
49 "type": "missing",
50 "loc": ["body", "username"],
51 "msg": "Field required",
52 "input": None,
53 }
54 ]
55 }
58def test_post_body_form_no_data(client: TestClient): 1abdc
59 response = client.post("/login/") 1nop
60 assert response.status_code == 422 1nop
61 assert response.json() == { 1nop
62 "detail": [
63 {
64 "type": "missing",
65 "loc": ["body", "username"],
66 "msg": "Field required",
67 "input": None,
68 },
69 {
70 "type": "missing",
71 "loc": ["body", "password"],
72 "msg": "Field required",
73 "input": None,
74 },
75 ]
76 }
79def test_post_body_json(client: TestClient): 1abdc
80 response = client.post("/login/", json={"username": "Foo", "password": "secret"}) 1qrs
81 assert response.status_code == 422, response.text 1qrs
82 assert response.json() == { 1qrs
83 "detail": [
84 {
85 "type": "missing",
86 "loc": ["body", "username"],
87 "msg": "Field required",
88 "input": None,
89 },
90 {
91 "type": "missing",
92 "loc": ["body", "password"],
93 "msg": "Field required",
94 "input": None,
95 },
96 ]
97 }
100def test_openapi_schema(client: TestClient): 1abdc
101 response = client.get("/openapi.json") 1tuv
102 assert response.status_code == 200, response.text 1tuv
103 assert response.json() == snapshot( 1tuv
104 {
105 "openapi": "3.1.0",
106 "info": {"title": "FastAPI", "version": "0.1.0"},
107 "paths": {
108 "/login/": {
109 "post": {
110 "responses": {
111 "200": {
112 "description": "Successful Response",
113 "content": {"application/json": {"schema": {}}},
114 },
115 "422": {
116 "description": "Validation Error",
117 "content": {
118 "application/json": {
119 "schema": {
120 "$ref": "#/components/schemas/HTTPValidationError"
121 }
122 }
123 },
124 },
125 },
126 "summary": "Login",
127 "operationId": "login_login__post",
128 "requestBody": {
129 "content": {
130 "application/x-www-form-urlencoded": {
131 "schema": {
132 "$ref": "#/components/schemas/Body_login_login__post"
133 }
134 }
135 },
136 "required": True,
137 },
138 }
139 }
140 },
141 "components": {
142 "schemas": {
143 "Body_login_login__post": {
144 "title": "Body_login_login__post",
145 "required": ["username", "password"],
146 "type": "object",
147 "properties": {
148 "username": {"title": "Username", "type": "string"},
149 "password": {"title": "Password", "type": "string"},
150 },
151 },
152 "ValidationError": {
153 "title": "ValidationError",
154 "required": ["loc", "msg", "type"],
155 "type": "object",
156 "properties": {
157 "loc": {
158 "title": "Location",
159 "type": "array",
160 "items": {
161 "anyOf": [{"type": "string"}, {"type": "integer"}]
162 },
163 },
164 "msg": {"title": "Message", "type": "string"},
165 "type": {"title": "Error Type", "type": "string"},
166 "input": {"title": "Input"},
167 "ctx": {"title": "Context", "type": "object"},
168 },
169 },
170 "HTTPValidationError": {
171 "title": "HTTPValidationError",
172 "type": "object",
173 "properties": {
174 "detail": {
175 "title": "Detail",
176 "type": "array",
177 "items": {
178 "$ref": "#/components/schemas/ValidationError"
179 },
180 }
181 },
182 },
183 }
184 },
185 }
186 )