mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 17:07:07 +00:00
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.
This commit is contained in:
committed by
Tim Abbott
parent
f4caf9dd79
commit
5a7b1065e5
@@ -358,7 +358,7 @@ class HostRequestMock(HttpRequest):
|
|||||||
self.META = meta_data
|
self.META = meta_data
|
||||||
self.path = path
|
self.path = path
|
||||||
self.user = user_profile or AnonymousUser()
|
self.user = user_profile or AnonymousUser()
|
||||||
self._body = b""
|
self._body = orjson.dumps(post_data)
|
||||||
self.content_type = ""
|
self.content_type = ""
|
||||||
|
|
||||||
RequestNotes.set_notes(
|
RequestNotes.set_notes(
|
||||||
|
|||||||
@@ -239,37 +239,33 @@ class TestEndpoint(ZulipTestCase):
|
|||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
*,
|
*,
|
||||||
body: WebhookPayload[WildValue],
|
body: WebhookPayload[WildValue],
|
||||||
foo: Json[int],
|
non_body: Json[int] = 0,
|
||||||
bar: Json[int] = 0,
|
|
||||||
) -> Dict[str, object]:
|
) -> Dict[str, object]:
|
||||||
status = body["totame"]["status"].tame(check_bool)
|
status = body["totame"]["status"].tame(check_bool)
|
||||||
return {"status": status, "foo": foo, "bar": bar}
|
return {"status": status, "foo": non_body}
|
||||||
|
|
||||||
# Simulate a paylaod that uses JSON encoding. We use the body setter to
|
# A normal request that uses a JSON encoded body
|
||||||
# overwrite the request body. The HostRequestMock initializer sets the
|
request = HostRequestMock({"non_body": 15, "totame": {"status": True}})
|
||||||
# POST QueryDict, which is normally done by Django by parsing the body.
|
|
||||||
data = {"foo": 15, "totame": {"status": True}}
|
|
||||||
request = HostRequestMock(data)
|
|
||||||
request.body = orjson.dumps(data)
|
|
||||||
result = call_endpoint(webhook, request)
|
result = call_endpoint(webhook, request)
|
||||||
self.assertDictEqual(result, {"status": True, "foo": 15, "bar": 0})
|
self.assertDictEqual(result, {"status": True, "foo": 15})
|
||||||
|
|
||||||
|
# Set the body manually so that we can pass in something unusual
|
||||||
|
request = HostRequestMock()
|
||||||
request.body = orjson.dumps([])
|
request.body = orjson.dumps([])
|
||||||
with self.assertRaisesRegex(DjangoValidationError, "request is not a dict"):
|
with self.assertRaisesRegex(DjangoValidationError, "request is not a dict"):
|
||||||
result = call_endpoint(webhook, request)
|
result = call_endpoint(webhook, request)
|
||||||
|
|
||||||
request.body = orjson.dumps(10)
|
# Test for the rare case when both body and GET are used
|
||||||
with self.assertRaisesRegex(DjangoValidationError, "request is not a dict"):
|
|
||||||
result = call_endpoint(webhook, request)
|
|
||||||
|
|
||||||
request = HostRequestMock()
|
request = HostRequestMock()
|
||||||
request.GET.update({"foo": "15", "bar": "10"})
|
request.GET.update({"non_body": "15"})
|
||||||
request.body = orjson.dumps(data)
|
request.body = orjson.dumps({"totame": {"status": True}})
|
||||||
result = call_endpoint(webhook, request)
|
result = call_endpoint(webhook, request)
|
||||||
self.assertDictEqual(result, {"status": True, "foo": 15, "bar": 10})
|
self.assertDictEqual(result, {"status": True, "foo": 15})
|
||||||
|
|
||||||
with self.assertRaisesMessage(JsonableError, "Malformed JSON"):
|
with self.assertRaisesMessage(JsonableError, "Malformed JSON"):
|
||||||
call_endpoint(webhook, HostRequestMock())
|
request = HostRequestMock()
|
||||||
|
request.body = b"{malformed_json"
|
||||||
|
call_endpoint(webhook, request)
|
||||||
|
|
||||||
with self.assertRaisesMessage(JsonableError, "Malformed payload"):
|
with self.assertRaisesMessage(JsonableError, "Malformed payload"):
|
||||||
request = HostRequestMock()
|
request = HostRequestMock()
|
||||||
|
|||||||
Reference in New Issue
Block a user