Coverage for tests/test_tutorial/test_dependencies/test_tutorial012.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import importlib 1abcdefg

2 

3import pytest 1abcdefg

4from dirty_equals import IsDict 1abcdefg

5from fastapi.testclient import TestClient 1abcdefg

6 

7from ...utils import needs_py39 1abcdefg

8 

9 

10@pytest.fixture( 1abcdefg

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): 1abcdefg

19 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abcdefg

20 

21 client = TestClient(mod.app) 1abcdefg

22 return client 1abcdefg

23 

24 

25def test_get_no_headers_items(client: TestClient): 1abcdefg

26 response = client.get("/items/") 1hijklmn

27 assert response.status_code == 422, response.text 1hijklmn

28 assert response.json() == IsDict( 1hijklmn

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 ) 

62 

63 

64def test_get_no_headers_users(client: TestClient): 1abcdefg

65 response = client.get("/users/") 1opqrstu

66 assert response.status_code == 422, response.text 1opqrstu

67 assert response.json() == IsDict( 1opqrstu

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 ) 

101 

102 

103def test_get_invalid_one_header_items(client: TestClient): 1abcdefg

104 response = client.get("/items/", headers={"X-Token": "invalid"}) 1vwxyzAB

105 assert response.status_code == 400, response.text 1vwxyzAB

106 assert response.json() == {"detail": "X-Token header invalid"} 1vwxyzAB

107 

108 

109def test_get_invalid_one_users(client: TestClient): 1abcdefg

110 response = client.get("/users/", headers={"X-Token": "invalid"}) 1CDEFGHI

111 assert response.status_code == 400, response.text 1CDEFGHI

112 assert response.json() == {"detail": "X-Token header invalid"} 1CDEFGHI

113 

114 

115def test_get_invalid_second_header_items(client: TestClient): 1abcdefg

116 response = client.get( 1JKLMNOP

117 "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} 

118 ) 

119 assert response.status_code == 400, response.text 1JKLMNOP

120 assert response.json() == {"detail": "X-Key header invalid"} 1JKLMNOP

121 

122 

123def test_get_invalid_second_header_users(client: TestClient): 1abcdefg

124 response = client.get( 1QRSTUVW

125 "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} 

126 ) 

127 assert response.status_code == 400, response.text 1QRSTUVW

128 assert response.json() == {"detail": "X-Key header invalid"} 1QRSTUVW

129 

130 

131def test_get_valid_headers_items(client: TestClient): 1abcdefg

132 response = client.get( 1XYZ0123

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 1XYZ0123

140 assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}] 1XYZ0123

141 

142 

143def test_get_valid_headers_users(client: TestClient): 1abcdefg

144 response = client.get( 1456789!

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 1456789!

152 assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] 1456789!

153 

154 

155def test_openapi_schema(client: TestClient): 1abcdefg

156 response = client.get("/openapi.json") 1#$%'()*

157 assert response.status_code == 200, response.text 1#$%'()*

158 assert response.json() == { 1#$%'()*

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 }