test_helpers: Remove unnecessary HostRequestMock.body override.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-09-08 11:29:12 -07:00 committed by Anders Kaseorg
parent b4a2e71b6b
commit e245f51974
4 changed files with 14 additions and 22 deletions

View File

@ -372,14 +372,6 @@ class HostRequestMock(HttpRequest):
), ),
) )
@property
def body(self) -> bytes:
return super().body
@body.setter
def body(self, val: bytes) -> None:
self._body = val
def get_host(self) -> str: def get_host(self) -> str:
return self.host return self.host

View File

@ -200,7 +200,7 @@ class DecoratorTestCase(ZulipTestCase):
request.POST["api_key"] = webhook_bot_api_key request.POST["api_key"] = webhook_bot_api_key
with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log: with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log:
with self.assertRaisesRegex(Exception, "raised by webhook function"): with self.assertRaisesRegex(Exception, "raised by webhook function"):
request.body = b"{}" request._body = b"{}"
request.content_type = "application/json" request.content_type = "application/json"
my_webhook_raises_exception(request) my_webhook_raises_exception(request)
@ -212,7 +212,7 @@ class DecoratorTestCase(ZulipTestCase):
request.POST["api_key"] = webhook_bot_api_key request.POST["api_key"] = webhook_bot_api_key
with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log: with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log:
with self.assertRaisesRegex(Exception, "raised by webhook function"): with self.assertRaisesRegex(Exception, "raised by webhook function"):
request.body = b"notjson" request._body = b"notjson"
request.content_type = "text/plain" request.content_type = "text/plain"
my_webhook_raises_exception(request) my_webhook_raises_exception(request)
@ -224,7 +224,7 @@ class DecoratorTestCase(ZulipTestCase):
request.POST["api_key"] = webhook_bot_api_key request.POST["api_key"] = webhook_bot_api_key
with self.assertLogs("zulip.zerver.webhooks", level="ERROR") as log: with self.assertLogs("zulip.zerver.webhooks", level="ERROR") as log:
with self.assertRaisesRegex(Exception, "raised by webhook function"): with self.assertRaisesRegex(Exception, "raised by webhook function"):
request.body = b"invalidjson" request._body = b"invalidjson"
request.content_type = "application/json" request.content_type = "application/json"
request.META["HTTP_X_CUSTOM_HEADER"] = "custom_value" request.META["HTTP_X_CUSTOM_HEADER"] = "custom_value"
my_webhook_raises_exception(request) my_webhook_raises_exception(request)
@ -242,7 +242,7 @@ class DecoratorTestCase(ZulipTestCase):
) )
with self.assertLogs("zulip.zerver.webhooks.unsupported", level="ERROR") as log: with self.assertLogs("zulip.zerver.webhooks.unsupported", level="ERROR") as log:
with self.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg): with self.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg):
request.body = b"invalidjson" request._body = b"invalidjson"
request.content_type = "application/json" request.content_type = "application/json"
request.META["HTTP_X_CUSTOM_HEADER"] = "custom_value" request.META["HTTP_X_CUSTOM_HEADER"] = "custom_value"
my_webhook_raises_exception_unsupported_event(request) my_webhook_raises_exception_unsupported_event(request)
@ -381,7 +381,7 @@ class DecoratorLoggingTestCase(ZulipTestCase):
request.method = "POST" request.method = "POST"
request.host = "zulip.testserver" request.host = "zulip.testserver"
request.body = b"{}" request._body = b"{}"
request.content_type = "text/plain" request.content_type = "text/plain"
with self.assertLogs("zulip.zerver.webhooks") as logger: with self.assertLogs("zulip.zerver.webhooks") as logger:
@ -404,7 +404,7 @@ class DecoratorLoggingTestCase(ZulipTestCase):
request.method = "POST" request.method = "POST"
request.host = "zulip.testserver" request.host = "zulip.testserver"
request.body = b"{}" request._body = b"{}"
request.content_type = "text/plain" request.content_type = "text/plain"
with mock.patch( with mock.patch(
@ -432,7 +432,7 @@ class DecoratorLoggingTestCase(ZulipTestCase):
request.method = "POST" request.method = "POST"
request.host = "zulip.testserver" request.host = "zulip.testserver"
request.body = b"{}" request._body = b"{}"
request.content_type = "application/json" request.content_type = "application/json"
with mock.patch("zerver.decorator.webhook_logger.exception") as mock_exception: with mock.patch("zerver.decorator.webhook_logger.exception") as mock_exception:

View File

@ -140,18 +140,18 @@ class REQTestCase(ZulipTestCase):
return json_response(data={"payload": payload}) return json_response(data={"payload": payload})
request = HostRequestMock() request = HostRequestMock()
request.body = b"\xde\xad\xbe\xef" request._body = b"\xde\xad\xbe\xef"
with self.assertRaises(JsonableError) as cm: with self.assertRaises(JsonableError) as cm:
get_payload(request) get_payload(request)
self.assertEqual(str(cm.exception), "Malformed payload") self.assertEqual(str(cm.exception), "Malformed payload")
request = HostRequestMock() request = HostRequestMock()
request.body = b"notjson" request._body = b"notjson"
with self.assertRaises(JsonableError) as cm: with self.assertRaises(JsonableError) as cm:
get_payload(request) get_payload(request)
self.assertEqual(str(cm.exception), "Malformed JSON") self.assertEqual(str(cm.exception), "Malformed JSON")
request.body = b'{"a": "b"}' request._body = b'{"a": "b"}'
self.assertEqual(orjson.loads(get_payload(request).content).get("payload"), {"a": "b"}) self.assertEqual(orjson.loads(get_payload(request).content).get("payload"), {"a": "b"})

View File

@ -262,26 +262,26 @@ class TestEndpoint(ZulipTestCase):
# Set the body manually so that we can pass in something unusual # Set the body manually so that we can pass in something unusual
request = HostRequestMock() 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)
# Test for the rare case when both body and GET are used # Test for the rare case when both body and GET are used
request = HostRequestMock() request = HostRequestMock()
request.GET.update({"non_body": "15"}) request.GET.update({"non_body": "15"})
request.body = orjson.dumps({"totame": {"status": True}}) request._body = orjson.dumps({"totame": {"status": True}})
result = call_endpoint(webhook, request) result = call_endpoint(webhook, request)
self.assertDictEqual(result, {"status": True, "foo": 15}) self.assertDictEqual(result, {"status": True, "foo": 15})
with self.assertRaisesMessage(JsonableError, "Malformed JSON"): with self.assertRaisesMessage(JsonableError, "Malformed JSON"):
request = HostRequestMock() request = HostRequestMock()
request.body = b"{malformed_json" request._body = b"{malformed_json"
call_endpoint(webhook, request) call_endpoint(webhook, request)
with self.assertRaisesMessage(JsonableError, "Malformed payload"): with self.assertRaisesMessage(JsonableError, "Malformed payload"):
request = HostRequestMock() request = HostRequestMock()
# This body triggers UnicodeDecodeError # This body triggers UnicodeDecodeError
request.body = b"\x81" request._body = b"\x81"
call_endpoint(webhook, request) call_endpoint(webhook, request)
def test_path_only(self) -> None: def test_path_only(self) -> None: