Debian Codegraph @

tests/test_tutorial/test_request_forms/test_tutorial001.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7
8 @pytest.fixture(
9 name="client",
10 params=[
11 "tutorial001_py310",
12 "tutorial001_an_py310",
13 ],
14 )
15 def get_client(request: pytest.FixtureRequest):
16 mod = importlib.import_module(f"docs_src.request_forms.{request.param}")
17
18 client = TestClient(mod.app)
19 return client
20
21
22 def test_post_body_form(client: TestClient):
23 response = client.post("/login/", data={"username": "Foo", "password": "secret"})
24 assert response.status_code == 200
25 assert response.json() == {"username": "Foo"}
26
27
28 def test_post_body_form_no_password(client: TestClient):
29 response = client.post("/login/", data={"username": "Foo"})
30 assert response.status_code == 422
31 assert response.json() == {
32 "detail": [
33 {
34 "type": "missing",
35 "loc": ["body", "password"],
36 "msg": "Field required",
37 "input": None,
38 }
39 ]
40 }
41
42
43 def test_post_body_form_no_username(client: TestClient):
44 response = client.post("/login/", data={"password": "secret"})
45 assert response.status_code == 422
46 assert response.json() == {
47 "detail": [
48 {
49 "type": "missing",
50 "loc": ["body", "username"],
51 "msg": "Field required",
52 "input": None,
53 }
54 ]
55 }
56
57
58 def test_post_body_form_no_data(client: TestClient):
59 response = client.post("/login/")
60 assert response.status_code == 422
61 assert response.json() == {
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 }
77
78
79 def test_post_body_json(client: TestClient):
80 response = client.post("/login/", json={"username": "Foo", "password": "secret"})
81 assert response.status_code == 422, response.text
82 assert response.json() == {
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 }
98
99
100 def test_openapi_schema(client: TestClient):
101 response = client.get("/openapi.json")
102 assert response.status_code == 200, response.text
103 assert response.json() == snapshot(
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 )