diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py index dbd192b576..29a876041d 100644 --- a/zerver/lib/test_helpers.py +++ b/zerver/lib/test_helpers.py @@ -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: return self.host diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py index d81d707016..be470db21c 100644 --- a/zerver/tests/test_decorators.py +++ b/zerver/tests/test_decorators.py @@ -200,7 +200,7 @@ class DecoratorTestCase(ZulipTestCase): request.POST["api_key"] = webhook_bot_api_key with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log: with self.assertRaisesRegex(Exception, "raised by webhook function"): - request.body = b"{}" + request._body = b"{}" request.content_type = "application/json" my_webhook_raises_exception(request) @@ -212,7 +212,7 @@ class DecoratorTestCase(ZulipTestCase): request.POST["api_key"] = webhook_bot_api_key with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log: with self.assertRaisesRegex(Exception, "raised by webhook function"): - request.body = b"notjson" + request._body = b"notjson" request.content_type = "text/plain" my_webhook_raises_exception(request) @@ -224,7 +224,7 @@ class DecoratorTestCase(ZulipTestCase): request.POST["api_key"] = webhook_bot_api_key with self.assertLogs("zulip.zerver.webhooks", level="ERROR") as log: with self.assertRaisesRegex(Exception, "raised by webhook function"): - request.body = b"invalidjson" + request._body = b"invalidjson" request.content_type = "application/json" request.META["HTTP_X_CUSTOM_HEADER"] = "custom_value" 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.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg): - request.body = b"invalidjson" + request._body = b"invalidjson" request.content_type = "application/json" request.META["HTTP_X_CUSTOM_HEADER"] = "custom_value" my_webhook_raises_exception_unsupported_event(request) @@ -381,7 +381,7 @@ class DecoratorLoggingTestCase(ZulipTestCase): request.method = "POST" request.host = "zulip.testserver" - request.body = b"{}" + request._body = b"{}" request.content_type = "text/plain" with self.assertLogs("zulip.zerver.webhooks") as logger: @@ -404,7 +404,7 @@ class DecoratorLoggingTestCase(ZulipTestCase): request.method = "POST" request.host = "zulip.testserver" - request.body = b"{}" + request._body = b"{}" request.content_type = "text/plain" with mock.patch( @@ -432,7 +432,7 @@ class DecoratorLoggingTestCase(ZulipTestCase): request.method = "POST" request.host = "zulip.testserver" - request.body = b"{}" + request._body = b"{}" request.content_type = "application/json" with mock.patch("zerver.decorator.webhook_logger.exception") as mock_exception: diff --git a/zerver/tests/test_has_request_variables.py b/zerver/tests/test_has_request_variables.py index cb2b51454a..47692357a1 100644 --- a/zerver/tests/test_has_request_variables.py +++ b/zerver/tests/test_has_request_variables.py @@ -140,18 +140,18 @@ class REQTestCase(ZulipTestCase): return json_response(data={"payload": payload}) request = HostRequestMock() - request.body = b"\xde\xad\xbe\xef" + request._body = b"\xde\xad\xbe\xef" with self.assertRaises(JsonableError) as cm: get_payload(request) self.assertEqual(str(cm.exception), "Malformed payload") request = HostRequestMock() - request.body = b"notjson" + request._body = b"notjson" with self.assertRaises(JsonableError) as cm: get_payload(request) 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"}) diff --git a/zerver/tests/test_typed_endpoint.py b/zerver/tests/test_typed_endpoint.py index c89007fe5c..d902482f3e 100644 --- a/zerver/tests/test_typed_endpoint.py +++ b/zerver/tests/test_typed_endpoint.py @@ -262,26 +262,26 @@ class TestEndpoint(ZulipTestCase): # 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"): result = call_endpoint(webhook, request) # Test for the rare case when both body and GET are used request = HostRequestMock() 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) self.assertDictEqual(result, {"status": True, "foo": 15}) with self.assertRaisesMessage(JsonableError, "Malformed JSON"): request = HostRequestMock() - request.body = b"{malformed_json" + request._body = b"{malformed_json" call_endpoint(webhook, request) with self.assertRaisesMessage(JsonableError, "Malformed payload"): request = HostRequestMock() # This body triggers UnicodeDecodeError - request.body = b"\x81" + request._body = b"\x81" call_endpoint(webhook, request) def test_path_only(self) -> None: