Coverage for tests/test_tutorial/test_dependencies/test_tutorial012.py: 100%
46 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-20 16:04 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-20 16:04 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py39 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial012",
14 "tutorial012_an",
15 pytest.param("tutorial012_an_py39", marks=needs_py39),
16 ],
17)
18def get_client(request: pytest.FixtureRequest): 1abcdef
19 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abcdef
21 client = TestClient(mod.app) 1abcdef
22 return client 1abcdef
25def test_get_no_headers_items(client: TestClient): 1abcdef
26 response = client.get("/items/") 1ghijkl
27 assert response.status_code == 422, response.text 1ghijkl
28 assert response.json() == IsDict( 1ghijkl
29 {
30 "detail": [
31 {
32 "type": "missing",
33 "loc": ["header", "x-token"],
34 "msg": "Field required",
35 "input": None,
36 },
37 {
38 "type": "missing",
39 "loc": ["header", "x-key"],
40 "msg": "Field required",
41 "input": None,
42 },
43 ]
44 }
45 ) | IsDict(
46 # TODO: remove when deprecating Pydantic v1
47 {
48 "detail": [
49 {
50 "loc": ["header", "x-token"],
51 "msg": "field required",
52 "type": "value_error.missing",
53 },
54 {
55 "loc": ["header", "x-key"],
56 "msg": "field required",
57 "type": "value_error.missing",
58 },
59 ]
60 }
61 )
64def test_get_no_headers_users(client: TestClient): 1abcdef
65 response = client.get("/users/") 1mnopqr
66 assert response.status_code == 422, response.text 1mnopqr
67 assert response.json() == IsDict( 1mnopqr
68 {
69 "detail": [
70 {
71 "type": "missing",
72 "loc": ["header", "x-token"],
73 "msg": "Field required",
74 "input": None,
75 },
76 {
77 "type": "missing",
78 "loc": ["header", "x-key"],
79 "msg": "Field required",
80 "input": None,
81 },
82 ]
83 }
84 ) | IsDict(
85 # TODO: remove when deprecating Pydantic v1
86 {
87 "detail": [
88 {
89 "loc": ["header", "x-token"],
90 "msg": "field required",
91 "type": "value_error.missing",
92 },
93 {
94 "loc": ["header", "x-key"],
95 "msg": "field required",
96 "type": "value_error.missing",
97 },
98 ]
99 }
100 )
103def test_get_invalid_one_header_items(client: TestClient): 1abcdef
104 response = client.get("/items/", headers={"X-Token": "invalid"}) 1stuvwx
105 assert response.status_code == 400, response.text 1stuvwx
106 assert response.json() == {"detail": "X-Token header invalid"} 1stuvwx
109def test_get_invalid_one_users(client: TestClient): 1abcdef
110 response = client.get("/users/", headers={"X-Token": "invalid"}) 1yzABCD
111 assert response.status_code == 400, response.text 1yzABCD
112 assert response.json() == {"detail": "X-Token header invalid"} 1yzABCD
115def test_get_invalid_second_header_items(client: TestClient): 1abcdef
116 response = client.get( 1EFGHIJ
117 "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
118 )
119 assert response.status_code == 400, response.text 1EFGHIJ
120 assert response.json() == {"detail": "X-Key header invalid"} 1EFGHIJ
123def test_get_invalid_second_header_users(client: TestClient): 1abcdef
124 response = client.get( 1KLMNOP
125 "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
126 )
127 assert response.status_code == 400, response.text 1KLMNOP
128 assert response.json() == {"detail": "X-Key header invalid"} 1KLMNOP
131def test_get_valid_headers_items(client: TestClient): 1abcdef
132 response = client.get( 1QRSTUV
133 "/items/",
134 headers={
135 "X-Token": "fake-super-secret-token",
136 "X-Key": "fake-super-secret-key",
137 },
138 )
139 assert response.status_code == 200, response.text 1QRSTUV
140 assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}] 1QRSTUV
143def test_get_valid_headers_users(client: TestClient): 1abcdef
144 response = client.get( 1WXYZ01
145 "/users/",
146 headers={
147 "X-Token": "fake-super-secret-token",
148 "X-Key": "fake-super-secret-key",
149 },
150 )
151 assert response.status_code == 200, response.text 1WXYZ01
152 assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] 1WXYZ01
155def test_openapi_schema(client: TestClient): 1abcdef
156 response = client.get("/openapi.json") 1234567
157 assert response.status_code == 200, response.text 1234567
158 assert response.json() == { 1234567
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 }