Coverage for tests/test_tutorial/test_dependencies/test_tutorial012_an.py: 100%
40 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
1from dirty_equals import IsDict 1abcde
2from fastapi.testclient import TestClient 1abcde
4from docs_src.dependencies.tutorial012_an import app 1abcde
6client = TestClient(app) 1abcde
9def test_get_no_headers_items(): 1abcde
10 response = client.get("/items/") 1abcde
11 assert response.status_code == 422, response.text 1abcde
12 assert response.json() == IsDict( 1abcde
13 {
14 "detail": [
15 {
16 "type": "missing",
17 "loc": ["header", "x-token"],
18 "msg": "Field required",
19 "input": None,
20 },
21 {
22 "type": "missing",
23 "loc": ["header", "x-key"],
24 "msg": "Field required",
25 "input": None,
26 },
27 ]
28 }
29 ) | IsDict(
30 # TODO: remove when deprecating Pydantic v1
31 {
32 "detail": [
33 {
34 "loc": ["header", "x-token"],
35 "msg": "field required",
36 "type": "value_error.missing",
37 },
38 {
39 "loc": ["header", "x-key"],
40 "msg": "field required",
41 "type": "value_error.missing",
42 },
43 ]
44 }
45 )
48def test_get_no_headers_users(): 1abcde
49 response = client.get("/users/") 1abcde
50 assert response.status_code == 422, response.text 1abcde
51 assert response.json() == IsDict( 1abcde
52 {
53 "detail": [
54 {
55 "type": "missing",
56 "loc": ["header", "x-token"],
57 "msg": "Field required",
58 "input": None,
59 },
60 {
61 "type": "missing",
62 "loc": ["header", "x-key"],
63 "msg": "Field required",
64 "input": None,
65 },
66 ]
67 }
68 ) | IsDict(
69 # TODO: remove when deprecating Pydantic v1
70 {
71 "detail": [
72 {
73 "loc": ["header", "x-token"],
74 "msg": "field required",
75 "type": "value_error.missing",
76 },
77 {
78 "loc": ["header", "x-key"],
79 "msg": "field required",
80 "type": "value_error.missing",
81 },
82 ]
83 }
84 )
87def test_get_invalid_one_header_items(): 1abcde
88 response = client.get("/items/", headers={"X-Token": "invalid"}) 1abcde
89 assert response.status_code == 400, response.text 1abcde
90 assert response.json() == {"detail": "X-Token header invalid"} 1abcde
93def test_get_invalid_one_users(): 1abcde
94 response = client.get("/users/", headers={"X-Token": "invalid"}) 1abcde
95 assert response.status_code == 400, response.text 1abcde
96 assert response.json() == {"detail": "X-Token header invalid"} 1abcde
99def test_get_invalid_second_header_items(): 1abcde
100 response = client.get( 1abcde
101 "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
102 )
103 assert response.status_code == 400, response.text 1abcde
104 assert response.json() == {"detail": "X-Key header invalid"} 1abcde
107def test_get_invalid_second_header_users(): 1abcde
108 response = client.get( 1abcde
109 "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
110 )
111 assert response.status_code == 400, response.text 1abcde
112 assert response.json() == {"detail": "X-Key header invalid"} 1abcde
115def test_get_valid_headers_items(): 1abcde
116 response = client.get( 1abcde
117 "/items/",
118 headers={
119 "X-Token": "fake-super-secret-token",
120 "X-Key": "fake-super-secret-key",
121 },
122 )
123 assert response.status_code == 200, response.text 1abcde
124 assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}] 1abcde
127def test_get_valid_headers_users(): 1abcde
128 response = client.get( 1abcde
129 "/users/",
130 headers={
131 "X-Token": "fake-super-secret-token",
132 "X-Key": "fake-super-secret-key",
133 },
134 )
135 assert response.status_code == 200, response.text 1abcde
136 assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] 1abcde
139def test_openapi_schema(): 1abcde
140 response = client.get("/openapi.json") 1abcde
141 assert response.status_code == 200, response.text 1abcde
142 assert response.json() == { 1abcde
143 "openapi": "3.1.0",
144 "info": {"title": "FastAPI", "version": "0.1.0"},
145 "paths": {
146 "/items/": {
147 "get": {
148 "summary": "Read Items",
149 "operationId": "read_items_items__get",
150 "parameters": [
151 {
152 "required": True,
153 "schema": {"title": "X-Token", "type": "string"},
154 "name": "x-token",
155 "in": "header",
156 },
157 {
158 "required": True,
159 "schema": {"title": "X-Key", "type": "string"},
160 "name": "x-key",
161 "in": "header",
162 },
163 ],
164 "responses": {
165 "200": {
166 "description": "Successful Response",
167 "content": {"application/json": {"schema": {}}},
168 },
169 "422": {
170 "description": "Validation Error",
171 "content": {
172 "application/json": {
173 "schema": {
174 "$ref": "#/components/schemas/HTTPValidationError"
175 }
176 }
177 },
178 },
179 },
180 }
181 },
182 "/users/": {
183 "get": {
184 "summary": "Read Users",
185 "operationId": "read_users_users__get",
186 "parameters": [
187 {
188 "required": True,
189 "schema": {"title": "X-Token", "type": "string"},
190 "name": "x-token",
191 "in": "header",
192 },
193 {
194 "required": True,
195 "schema": {"title": "X-Key", "type": "string"},
196 "name": "x-key",
197 "in": "header",
198 },
199 ],
200 "responses": {
201 "200": {
202 "description": "Successful Response",
203 "content": {"application/json": {"schema": {}}},
204 },
205 "422": {
206 "description": "Validation Error",
207 "content": {
208 "application/json": {
209 "schema": {
210 "$ref": "#/components/schemas/HTTPValidationError"
211 }
212 }
213 },
214 },
215 },
216 }
217 },
218 },
219 "components": {
220 "schemas": {
221 "HTTPValidationError": {
222 "title": "HTTPValidationError",
223 "type": "object",
224 "properties": {
225 "detail": {
226 "title": "Detail",
227 "type": "array",
228 "items": {"$ref": "#/components/schemas/ValidationError"},
229 }
230 },
231 },
232 "ValidationError": {
233 "title": "ValidationError",
234 "required": ["loc", "msg", "type"],
235 "type": "object",
236 "properties": {
237 "loc": {
238 "title": "Location",
239 "type": "array",
240 "items": {
241 "anyOf": [{"type": "string"}, {"type": "integer"}]
242 },
243 },
244 "msg": {"title": "Message", "type": "string"},
245 "type": {"title": "Error Type", "type": "string"},
246 },
247 },
248 }
249 },
250 }