Coverage for tests / test_tutorial / test_dependencies / test_tutorial012.py: 100%
45 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 pytest.param("tutorial012_py310"),
12 pytest.param("tutorial012_an_py310"),
13 ],
14)
15def get_client(request: pytest.FixtureRequest): 1abdc
16 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abc
18 client = TestClient(mod.app) 1abc
19 return client 1abc
22def test_get_no_headers_items(client: TestClient): 1abdc
23 response = client.get("/items/") 1efg
24 assert response.status_code == 422, response.text 1efg
25 assert response.json() == { 1efg
26 "detail": [
27 {
28 "type": "missing",
29 "loc": ["header", "x-token"],
30 "msg": "Field required",
31 "input": None,
32 },
33 {
34 "type": "missing",
35 "loc": ["header", "x-key"],
36 "msg": "Field required",
37 "input": None,
38 },
39 ]
40 }
43def test_get_no_headers_users(client: TestClient): 1abdc
44 response = client.get("/users/") 1hij
45 assert response.status_code == 422, response.text 1hij
46 assert response.json() == { 1hij
47 "detail": [
48 {
49 "type": "missing",
50 "loc": ["header", "x-token"],
51 "msg": "Field required",
52 "input": None,
53 },
54 {
55 "type": "missing",
56 "loc": ["header", "x-key"],
57 "msg": "Field required",
58 "input": None,
59 },
60 ]
61 }
64def test_get_invalid_one_header_items(client: TestClient): 1abdc
65 response = client.get("/items/", headers={"X-Token": "invalid"}) 1klm
66 assert response.status_code == 400, response.text 1klm
67 assert response.json() == {"detail": "X-Token header invalid"} 1klm
70def test_get_invalid_one_users(client: TestClient): 1abdc
71 response = client.get("/users/", headers={"X-Token": "invalid"}) 1nop
72 assert response.status_code == 400, response.text 1nop
73 assert response.json() == {"detail": "X-Token header invalid"} 1nop
76def test_get_invalid_second_header_items(client: TestClient): 1abdc
77 response = client.get( 1qrs
78 "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
79 )
80 assert response.status_code == 400, response.text 1qrs
81 assert response.json() == {"detail": "X-Key header invalid"} 1qrs
84def test_get_invalid_second_header_users(client: TestClient): 1abdc
85 response = client.get( 1tuv
86 "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
87 )
88 assert response.status_code == 400, response.text 1tuv
89 assert response.json() == {"detail": "X-Key header invalid"} 1tuv
92def test_get_valid_headers_items(client: TestClient): 1abdc
93 response = client.get( 1wxy
94 "/items/",
95 headers={
96 "X-Token": "fake-super-secret-token",
97 "X-Key": "fake-super-secret-key",
98 },
99 )
100 assert response.status_code == 200, response.text 1wxy
101 assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}] 1wxy
104def test_get_valid_headers_users(client: TestClient): 1abdc
105 response = client.get( 1zAB
106 "/users/",
107 headers={
108 "X-Token": "fake-super-secret-token",
109 "X-Key": "fake-super-secret-key",
110 },
111 )
112 assert response.status_code == 200, response.text 1zAB
113 assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] 1zAB
116def test_openapi_schema(client: TestClient): 1abdc
117 response = client.get("/openapi.json") 1CDE
118 assert response.status_code == 200, response.text 1CDE
119 assert response.json() == snapshot( 1CDE
120 {
121 "openapi": "3.1.0",
122 "info": {"title": "FastAPI", "version": "0.1.0"},
123 "paths": {
124 "/items/": {
125 "get": {
126 "summary": "Read Items",
127 "operationId": "read_items_items__get",
128 "parameters": [
129 {
130 "required": True,
131 "schema": {"title": "X-Token", "type": "string"},
132 "name": "x-token",
133 "in": "header",
134 },
135 {
136 "required": True,
137 "schema": {"title": "X-Key", "type": "string"},
138 "name": "x-key",
139 "in": "header",
140 },
141 ],
142 "responses": {
143 "200": {
144 "description": "Successful Response",
145 "content": {"application/json": {"schema": {}}},
146 },
147 "422": {
148 "description": "Validation Error",
149 "content": {
150 "application/json": {
151 "schema": {
152 "$ref": "#/components/schemas/HTTPValidationError"
153 }
154 }
155 },
156 },
157 },
158 }
159 },
160 "/users/": {
161 "get": {
162 "summary": "Read Users",
163 "operationId": "read_users_users__get",
164 "parameters": [
165 {
166 "required": True,
167 "schema": {"title": "X-Token", "type": "string"},
168 "name": "x-token",
169 "in": "header",
170 },
171 {
172 "required": True,
173 "schema": {"title": "X-Key", "type": "string"},
174 "name": "x-key",
175 "in": "header",
176 },
177 ],
178 "responses": {
179 "200": {
180 "description": "Successful Response",
181 "content": {"application/json": {"schema": {}}},
182 },
183 "422": {
184 "description": "Validation Error",
185 "content": {
186 "application/json": {
187 "schema": {
188 "$ref": "#/components/schemas/HTTPValidationError"
189 }
190 }
191 },
192 },
193 },
194 }
195 },
196 },
197 "components": {
198 "schemas": {
199 "HTTPValidationError": {
200 "title": "HTTPValidationError",
201 "type": "object",
202 "properties": {
203 "detail": {
204 "title": "Detail",
205 "type": "array",
206 "items": {
207 "$ref": "#/components/schemas/ValidationError"
208 },
209 }
210 },
211 },
212 "ValidationError": {
213 "title": "ValidationError",
214 "required": ["loc", "msg", "type"],
215 "type": "object",
216 "properties": {
217 "loc": {
218 "title": "Location",
219 "type": "array",
220 "items": {
221 "anyOf": [{"type": "string"}, {"type": "integer"}]
222 },
223 },
224 "msg": {"title": "Message", "type": "string"},
225 "type": {"title": "Error Type", "type": "string"},
226 "input": {"title": "Input"},
227 "ctx": {"title": "Context", "type": "object"},
228 },
229 },
230 }
231 },
232 }
233 )