Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
request: Refactor HostRequestMock to subclass HttpRequest.
PIG20875cde8ebcfcf
diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.pyindex 2bc06eff7f2..2b907e75420 100644--- a/zerver/lib/test_helpers.py+++ b/zerver/lib/test_helpers.py@@ -30,7 +30,8 @@ import orjsonfrom boto3.resources.base import ServiceResourcefrom django.conf import settingsfrom django.db.migrations.state import StateApps-from django.http import HttpResponse, HttpResponseRedirect+from django.http import HttpRequest, HttpResponse, HttpResponseRedirect+from django.http.request import QueryDictfrom django.test import override_settingsfrom django.urls import URLResolverfrom moto import mock_s3@@ -280,7 +281,7 @@ class DummyHandler(AsyncDjangoHandler):allocate_handler_id(self)-class HostRequestMock:+class HostRequestMock(HttpRequest):"""A mock request object where get_host() works. Useful for testingroutes that use Zulip's subdomains feature"""@@ -290,9 +291,10 @@ class HostRequestMock:user_profile: Optional[UserProfile] = None,host: str = settings.EXTERNAL_HOST,client_name: Optional[str] = None,+ meta_data: Optional[Dict[str, Any]] = None,) -> None:self.host = host- self.GET: Dict[str, Any] = {}+ self.GET = QueryDict(mutable=True)self.method = ""if client_name is not None:self.client = get_client(client_name)@@ -301,20 +303,31 @@ class HostRequestMock:# though of course the HTTP API would do so. Ideally, we'd# get rid of this abstraction entirely and just use the HTTP# API directly, but while it exists, we need this code- self.POST: Dict[str, Any] = {}+ self.POST = QueryDict(mutable=True)for key in post_data:self.POST[key] = str(post_data[key])self.method = "POST"self._tornado_handler = DummyHandler()self._log_data: Dict[str, Any] = {}- self.META = {"PATH_INFO": "test"}+ if meta_data is None:+ self.META = {"PATH_INFO": "test"}+ else:+ self.META = meta_dataself.path = ""self.user = user_profile- self.body = ""+ self._body = b""self.content_type = ""self.client_name = ""+ @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.hostdiff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 9fd4beedd7f..3234863c28d 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -264,12 +264,12 @@ class DecoratorTestCase(ZulipTestCase):return payloadrequest = HostRequestMock()- request.body = "notjson"+ request.body = b"notjson"with self.assertRaises(JsonableError) as cm:get_payload(request)self.assertEqual(str(cm.exception), "Malformed JSON")- request.body = '{"a": "b"}'+ request.body = b'{"a": "b"}'self.assertEqual(get_payload(request), {"a": "b"})def logger_output(self, output_string: str, type: str, logger: str) -> str:@@ -340,7 +340,7 @@ class DecoratorTestCase(ZulipTestCase):with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log:with self.assertRaisesRegex(Exception, "raised by webhook function"):- request.body = "{}"+ request.body = b"{}"request.content_type = "application/json"my_webhook_raises_exception(request)@@ -349,7 +349,7 @@ class DecoratorTestCase(ZulipTestCase):with self.assertLogs("zulip.zerver.webhooks", level="INFO") as log:with self.assertRaisesRegex(Exception, "raised by webhook function"):- request.body = "notjson"+ request.body = b"notjson"request.content_type = "text/plain"my_webhook_raises_exception(request)@@ -358,7 +358,7 @@ class DecoratorTestCase(ZulipTestCase):# exception raised in the webhook function should be re-raisedwith self.assertLogs("zulip.zerver.webhooks", level="ERROR") as log:with self.assertRaisesRegex(Exception, "raised by webhook function"):- request.body = "invalidjson"+ request.body = b"invalidjson"request.content_type = "application/json"request.META["HTTP_X_CUSTOM_HEADER"] = "custom_value"my_webhook_raises_exception(request)@@ -371,7 +371,7 @@ class DecoratorTestCase(ZulipTestCase):exception_msg = "The 'test_event' event isn't currently supported by the ClientName webhook"with self.assertLogs("zulip.zerver.webhooks.unsupported", level="ERROR") as log:with self.assertRaisesRegex(UnsupportedWebhookEventType, exception_msg):- request.body = "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)@@ -487,7 +487,7 @@ class DecoratorLoggingTestCase(ZulipTestCase):request.method = "POST"request.host = "zulip.testserver"- request.body = "{}"+ request.body = b"{}"request.content_type = "text/plain"with mock.patch("zerver.decorator.webhook_logger.exception") as mock_exception:@@ -508,7 +508,7 @@ class DecoratorLoggingTestCase(ZulipTestCase):request.method = "POST"request.host = "zulip.testserver"- request.body = "{}"+ request.body = b"{}"request.content_type = "text/plain"with mock.patch(@@ -534,7 +534,7 @@ class DecoratorLoggingTestCase(ZulipTestCase):request.method = "POST"request.host = "zulip.testserver"- request.body = "{}"+ request.body = b"{}"request.content_type = "application/json"with mock.patch("zerver.decorator.webhook_logger.exception") as mock_exception: