Coverage for tests/test_tutorial/test_request_form_models/test_tutorial001_an_py39.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-06 16:22 +0000

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from tests.utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="client") 1eabcd

9def get_client(): 1eabcd

10 from docs_src.request_form_models.tutorial001_an_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17def test_post_body_form(client: TestClient): 1eabcd

18 response = client.post("/login/", data={"username": "Foo", "password": "secret"}) 1fghi

19 assert response.status_code == 200 1fghi

20 assert response.json() == {"username": "Foo", "password": "secret"} 1fghi

21 

22 

23@needs_py39 1eabcd

24def test_post_body_form_no_password(client: TestClient): 1eabcd

25 response = client.post("/login/", data={"username": "Foo"}) 1jklm

26 assert response.status_code == 422 1jklm

27 assert response.json() == IsDict( 1jklm

28 { 

29 "detail": [ 

30 { 

31 "type": "missing", 

32 "loc": ["body", "password"], 

33 "msg": "Field required", 

34 "input": {"username": "Foo"}, 

35 } 

36 ] 

37 } 

38 ) | IsDict( 

39 # TODO: remove when deprecating Pydantic v1 

40 { 

41 "detail": [ 

42 { 

43 "loc": ["body", "password"], 

44 "msg": "field required", 

45 "type": "value_error.missing", 

46 } 

47 ] 

48 } 

49 ) 

50 

51 

52@needs_py39 1eabcd

53def test_post_body_form_no_username(client: TestClient): 1eabcd

54 response = client.post("/login/", data={"password": "secret"}) 1nopq

55 assert response.status_code == 422 1nopq

56 assert response.json() == IsDict( 1nopq

57 { 

58 "detail": [ 

59 { 

60 "type": "missing", 

61 "loc": ["body", "username"], 

62 "msg": "Field required", 

63 "input": {"password": "secret"}, 

64 } 

65 ] 

66 } 

67 ) | IsDict( 

68 # TODO: remove when deprecating Pydantic v1 

69 { 

70 "detail": [ 

71 { 

72 "loc": ["body", "username"], 

73 "msg": "field required", 

74 "type": "value_error.missing", 

75 } 

76 ] 

77 } 

78 ) 

79 

80 

81@needs_py39 1eabcd

82def test_post_body_form_no_data(client: TestClient): 1eabcd

83 response = client.post("/login/") 1rstu

84 assert response.status_code == 422 1rstu

85 assert response.json() == IsDict( 1rstu

86 { 

87 "detail": [ 

88 { 

89 "type": "missing", 

90 "loc": ["body", "username"], 

91 "msg": "Field required", 

92 "input": {}, 

93 }, 

94 { 

95 "type": "missing", 

96 "loc": ["body", "password"], 

97 "msg": "Field required", 

98 "input": {}, 

99 }, 

100 ] 

101 } 

102 ) | IsDict( 

103 # TODO: remove when deprecating Pydantic v1 

104 { 

105 "detail": [ 

106 { 

107 "loc": ["body", "username"], 

108 "msg": "field required", 

109 "type": "value_error.missing", 

110 }, 

111 { 

112 "loc": ["body", "password"], 

113 "msg": "field required", 

114 "type": "value_error.missing", 

115 }, 

116 ] 

117 } 

118 ) 

119 

120 

121@needs_py39 1eabcd

122def test_post_body_json(client: TestClient): 1eabcd

123 response = client.post("/login/", json={"username": "Foo", "password": "secret"}) 1vwxy

124 assert response.status_code == 422, response.text 1vwxy

125 assert response.json() == IsDict( 1vwxy

126 { 

127 "detail": [ 

128 { 

129 "type": "missing", 

130 "loc": ["body", "username"], 

131 "msg": "Field required", 

132 "input": {}, 

133 }, 

134 { 

135 "type": "missing", 

136 "loc": ["body", "password"], 

137 "msg": "Field required", 

138 "input": {}, 

139 }, 

140 ] 

141 } 

142 ) | IsDict( 

143 # TODO: remove when deprecating Pydantic v1 

144 { 

145 "detail": [ 

146 { 

147 "loc": ["body", "username"], 

148 "msg": "field required", 

149 "type": "value_error.missing", 

150 }, 

151 { 

152 "loc": ["body", "password"], 

153 "msg": "field required", 

154 "type": "value_error.missing", 

155 }, 

156 ] 

157 } 

158 ) 

159 

160 

161@needs_py39 1eabcd

162def test_openapi_schema(client: TestClient): 1eabcd

163 response = client.get("/openapi.json") 1zABC

164 assert response.status_code == 200, response.text 1zABC

165 assert response.json() == { 1zABC

166 "openapi": "3.1.0", 

167 "info": {"title": "FastAPI", "version": "0.1.0"}, 

168 "paths": { 

169 "/login/": { 

170 "post": { 

171 "responses": { 

172 "200": { 

173 "description": "Successful Response", 

174 "content": {"application/json": {"schema": {}}}, 

175 }, 

176 "422": { 

177 "description": "Validation Error", 

178 "content": { 

179 "application/json": { 

180 "schema": { 

181 "$ref": "#/components/schemas/HTTPValidationError" 

182 } 

183 } 

184 }, 

185 }, 

186 }, 

187 "summary": "Login", 

188 "operationId": "login_login__post", 

189 "requestBody": { 

190 "content": { 

191 "application/x-www-form-urlencoded": { 

192 "schema": {"$ref": "#/components/schemas/FormData"} 

193 } 

194 }, 

195 "required": True, 

196 }, 

197 } 

198 } 

199 }, 

200 "components": { 

201 "schemas": { 

202 "FormData": { 

203 "properties": { 

204 "username": {"type": "string", "title": "Username"}, 

205 "password": {"type": "string", "title": "Password"}, 

206 }, 

207 "type": "object", 

208 "required": ["username", "password"], 

209 "title": "FormData", 

210 }, 

211 "ValidationError": { 

212 "title": "ValidationError", 

213 "required": ["loc", "msg", "type"], 

214 "type": "object", 

215 "properties": { 

216 "loc": { 

217 "title": "Location", 

218 "type": "array", 

219 "items": { 

220 "anyOf": [{"type": "string"}, {"type": "integer"}] 

221 }, 

222 }, 

223 "msg": {"title": "Message", "type": "string"}, 

224 "type": {"title": "Error Type", "type": "string"}, 

225 }, 

226 }, 

227 "HTTPValidationError": { 

228 "title": "HTTPValidationError", 

229 "type": "object", 

230 "properties": { 

231 "detail": { 

232 "title": "Detail", 

233 "type": "array", 

234 "items": {"$ref": "#/components/schemas/ValidationError"}, 

235 } 

236 }, 

237 }, 

238 } 

239 }, 

240 }