Skip to content

Commit 0173fa9

Browse files
PIG208freshpex
authored andcommitted
api: Rewrite argument type test for clarity.
We refactor HostRequestMock so that it now proper populates the request body given the post data, assuming that the request is JSON encoded.
1 parent a55595c commit 0173fa9

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

zerver/lib/test_helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def __init__(
358358
self.META = meta_data
359359
self.path = path
360360
self.user = user_profile or AnonymousUser()
361-
self._body = b""
361+
self._body = orjson.dumps(post_data)
362362
self.content_type = ""
363363

364364
RequestNotes.set_notes(

zerver/tests/test_typed_endpoint.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -239,37 +239,33 @@ def webhook(
239239
request: HttpRequest,
240240
*,
241241
body: WebhookPayload[WildValue],
242-
foo: Json[int],
243-
bar: Json[int] = 0,
242+
non_body: Json[int] = 0,
244243
) -> Dict[str, object]:
245244
status = body["totame"]["status"].tame(check_bool)
246-
return {"status": status, "foo": foo, "bar": bar}
247-
248-
# Simulate a paylaod that uses JSON encoding. We use the body setter to
249-
# overwrite the request body. The HostRequestMock initializer sets the
250-
# POST QueryDict, which is normally done by Django by parsing the body.
251-
data = {"foo": 15, "totame": {"status": True}}
252-
request = HostRequestMock(data)
253-
request.body = orjson.dumps(data)
245+
return {"status": status, "foo": non_body}
246+
247+
# A normal request that uses a JSON encoded body
248+
request = HostRequestMock({"non_body": 15, "totame": {"status": True}})
254249
result = call_endpoint(webhook, request)
255-
self.assertDictEqual(result, {"status": True, "foo": 15, "bar": 0})
250+
self.assertDictEqual(result, {"status": True, "foo": 15})
256251

252+
# Set the body manually so that we can pass in something unusual
253+
request = HostRequestMock()
257254
request.body = orjson.dumps([])
258255
with self.assertRaisesRegex(DjangoValidationError, "request is not a dict"):
259256
result = call_endpoint(webhook, request)
260257

261-
request.body = orjson.dumps(10)
262-
with self.assertRaisesRegex(DjangoValidationError, "request is not a dict"):
263-
result = call_endpoint(webhook, request)
264-
258+
# Test for the rare case when both body and GET are used
265259
request = HostRequestMock()
266-
request.GET.update({"foo": "15", "bar": "10"})
267-
request.body = orjson.dumps(data)
260+
request.GET.update({"non_body": "15"})
261+
request.body = orjson.dumps({"totame": {"status": True}})
268262
result = call_endpoint(webhook, request)
269-
self.assertDictEqual(result, {"status": True, "foo": 15, "bar": 10})
263+
self.assertDictEqual(result, {"status": True, "foo": 15})
270264

271265
with self.assertRaisesMessage(JsonableError, "Malformed JSON"):
272-
call_endpoint(webhook, HostRequestMock())
266+
request = HostRequestMock()
267+
request.body = b"{malformed_json"
268+
call_endpoint(webhook, request)
273269

274270
with self.assertRaisesMessage(JsonableError, "Malformed payload"):
275271
request = HostRequestMock()

0 commit comments

Comments
 (0)