Coverage for tests/test_tutorial/test_dependencies/test_tutorial012_an_py39.py: 100%
54 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import pytest 1eabcd
2from dirty_equals import IsDict 1eabcd
3from fastapi.testclient import TestClient 1eabcd
5from ...utils import needs_py39 1eabcd
8@pytest.fixture(name="client") 1eabcd
9def get_client(): 1eabcd
10 from docs_src.dependencies.tutorial012_an_py39 import app 1abcd
12 client = TestClient(app) 1abcd
13 return client 1abcd
16@needs_py39 1eabcd
17def test_get_no_headers_items(client: TestClient): 1eabcd
18 response = client.get("/items/") 1abcd
19 assert response.status_code == 422, response.text 1abcd
20 assert response.json() == IsDict( 1abcd
21 {
22 "detail": [
23 {
24 "type": "missing",
25 "loc": ["header", "x-token"],
26 "msg": "Field required",
27 "input": None,
28 },
29 {
30 "type": "missing",
31 "loc": ["header", "x-key"],
32 "msg": "Field required",
33 "input": None,
34 },
35 ]
36 }
37 ) | IsDict(
38 # TODO: remove when deprecating Pydantic v1
39 {
40 "detail": [
41 {
42 "loc": ["header", "x-token"],
43 "msg": "field required",
44 "type": "value_error.missing",
45 },
46 {
47 "loc": ["header", "x-key"],
48 "msg": "field required",
49 "type": "value_error.missing",
50 },
51 ]
52 }
53 )
56@needs_py39 1eabcd
57def test_get_no_headers_users(client: TestClient): 1eabcd
58 response = client.get("/users/") 1abcd
59 assert response.status_code == 422, response.text 1abcd
60 assert response.json() == IsDict( 1abcd
61 {
62 "detail": [
63 {
64 "type": "missing",
65 "loc": ["header", "x-token"],
66 "msg": "Field required",
67 "input": None,
68 },
69 {
70 "type": "missing",
71 "loc": ["header", "x-key"],
72 "msg": "Field required",
73 "input": None,
74 },
75 ]
76 }
77 ) | IsDict(
78 # TODO: remove when deprecating Pydantic v1
79 {
80 "detail": [
81 {
82 "loc": ["header", "x-token"],
83 "msg": "field required",
84 "type": "value_error.missing",
85 },
86 {
87 "loc": ["header", "x-key"],
88 "msg": "field required",
89 "type": "value_error.missing",
90 },
91 ]
92 }
93 )
96@needs_py39 1eabcd
97def test_get_invalid_one_header_items(client: TestClient): 1eabcd
98 response = client.get("/items/", headers={"X-Token": "invalid"}) 1abcd
99 assert response.status_code == 400, response.text 1abcd
100 assert response.json() == {"detail": "X-Token header invalid"} 1abcd
103@needs_py39 1eabcd
104def test_get_invalid_one_users(client: TestClient): 1eabcd
105 response = client.get("/users/", headers={"X-Token": "invalid"}) 1abcd
106 assert response.status_code == 400, response.text 1abcd
107 assert response.json() == {"detail": "X-Token header invalid"} 1abcd
110@needs_py39 1eabcd
111def test_get_invalid_second_header_items(client: TestClient): 1eabcd
112 response = client.get( 1abcd
113 "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
114 )
115 assert response.status_code == 400, response.text 1abcd
116 assert response.json() == {"detail": "X-Key header invalid"} 1abcd
119@needs_py39 1eabcd
120def test_get_invalid_second_header_users(client: TestClient): 1eabcd
121 response = client.get( 1abcd
122 "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
123 )
124 assert response.status_code == 400, response.text 1abcd
125 assert response.json() == {"detail": "X-Key header invalid"} 1abcd
128@needs_py39 1eabcd
129def test_get_valid_headers_items(client: TestClient): 1eabcd
130 response = client.get( 1abcd
131 "/items/",
132 headers={
133 "X-Token": "fake-super-secret-token",
134 "X-Key": "fake-super-secret-key",
135 },
136 )
137 assert response.status_code == 200, response.text 1abcd
138 assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}] 1abcd
141@needs_py39 1eabcd
142def test_get_valid_headers_users(client: TestClient): 1eabcd
143 response = client.get( 1abcd
144 "/users/",
145 headers={
146 "X-Token": "fake-super-secret-token",
147 "X-Key": "fake-super-secret-key",
148 },
149 )
150 assert response.status_code == 200, response.text 1abcd
151 assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] 1abcd
154@needs_py39 1eabcd
155def test_openapi_schema(client: TestClient): 1eabcd
156 response = client.get("/openapi.json") 1abcd
157 assert response.status_code == 200, response.text 1abcd
158 assert response.json() == { 1abcd
159 "openapi": "3.1.0",
160 "info": {"title": "FastAPI", "version": "0.1.0"},
161 "paths": {
162 "/items/": {
163 "get": {
164 "summary": "Read Items",
165 "operationId": "read_items_items__get",
166 "parameters": [
167 {
168 "required": True,
169 "schema": {"title": "X-Token", "type": "string"},
170 "name": "x-token",
171 "in": "header",
172 },
173 {
174 "required": True,
175 "schema": {"title": "X-Key", "type": "string"},
176 "name": "x-key",
177 "in": "header",
178 },
179 ],
180 "responses": {
181 "200": {
182 "description": "Successful Response",
183 "content": {"application/json": {"schema": {}}},
184 },
185 "422": {
186 "description": "Validation Error",
187 "content": {
188 "application/json": {
189 "schema": {
190 "$ref": "#/components/schemas/HTTPValidationError"
191 }
192 }
193 },
194 },
195 },
196 }
197 },
198 "/users/": {
199 "get": {
200 "summary": "Read Users",
201 "operationId": "read_users_users__get",
202 "parameters": [
203 {
204 "required": True,
205 "schema": {"title": "X-Token", "type": "string"},
206 "name": "x-token",
207 "in": "header",
208 },
209 {
210 "required": True,
211 "schema": {"title": "X-Key", "type": "string"},
212 "name": "x-key",
213 "in": "header",
214 },
215 ],
216 "responses": {
217 "200": {
218 "description": "Successful Response",
219 "content": {"application/json": {"schema": {}}},
220 },
221 "422": {
222 "description": "Validation Error",
223 "content": {
224 "application/json": {
225 "schema": {
226 "$ref": "#/components/schemas/HTTPValidationError"
227 }
228 }
229 },
230 },
231 },
232 }
233 },
234 },
235 "components": {
236 "schemas": {
237 "HTTPValidationError": {
238 "title": "HTTPValidationError",
239 "type": "object",
240 "properties": {
241 "detail": {
242 "title": "Detail",
243 "type": "array",
244 "items": {"$ref": "#/components/schemas/ValidationError"},
245 }
246 },
247 },
248 "ValidationError": {
249 "title": "ValidationError",
250 "required": ["loc", "msg", "type"],
251 "type": "object",
252 "properties": {
253 "loc": {
254 "title": "Location",
255 "type": "array",
256 "items": {
257 "anyOf": [{"type": "string"}, {"type": "integer"}]
258 },
259 },
260 "msg": {"title": "Message", "type": "string"},
261 "type": {"title": "Error Type", "type": "string"},
262 },
263 },
264 }
265 },
266 }