def test_standardize_headers(self) -> None: self.assertEqual(standardize_headers({}), {}) raw_headers = {"Content-Type": "text/plain", "X-Event-Type": "ping"} djangoified_headers = standardize_headers(raw_headers) expected_djangoified_headers = {"CONTENT_TYPE": "text/plain", "HTTP_X_EVENT_TYPE": "ping"} self.assertEqual(djangoified_headers, expected_djangoified_headers)
def send_and_test_private_message( self, fixture_name: str, expected_message: str, content_type: str = "application/json", **kwargs: Any, ) -> Message: """ For the rare cases that you are testing a webhook that sends private messages, use this function. Most webhooks send to streams, and you will want to look at check_webhook. """ payload = self.get_payload(fixture_name) kwargs['content_type'] = content_type if self.FIXTURE_DIR_NAME is not None: headers = get_fixture_http_headers(self.FIXTURE_DIR_NAME, fixture_name) headers = standardize_headers(headers) kwargs.update(headers) # The sender profile shouldn't be passed any further in kwargs, so we pop it. sender = kwargs.pop('sender', self.test_user) msg = self.send_webhook_payload( sender, self.url, payload, **kwargs, ) self.assertEqual(msg.content, expected_message) return msg
def send_and_test_private_message( self, fixture_name: str, expected_topic: Optional[str] = None, expected_message: Optional[str] = None, content_type: Optional[str] = "application/json", **kwargs: Any, ) -> Message: payload = self.get_body(fixture_name) if content_type is not None: kwargs['content_type'] = content_type if self.FIXTURE_DIR_NAME is not None: headers = get_fixture_http_headers(self.FIXTURE_DIR_NAME, fixture_name) headers = standardize_headers(headers) kwargs.update(headers) # The sender profile shouldn't be passed any further in kwargs, so we pop it. sender = kwargs.pop('sender', self.test_user) msg = self.send_json_payload(sender, self.url, payload, stream_name=None, **kwargs) self.do_test_message(msg, expected_message) return msg
def parse_headers(self, custom_headers: Union[None, str]) -> Union[None, Dict[str, str]]: if not custom_headers: return {} try: custom_headers_dict = ujson.loads(custom_headers) except ValueError as ve: raise CommandError('Encountered an error while attempting to parse custom headers: {}\n' 'Note: all strings must be enclosed within "" instead of \'\''.format(ve)) return standardize_headers(custom_headers_dict)
def parse_headers(self, custom_headers: Union[None, str]) -> Union[None, Dict[str, str]]: if not custom_headers: return {} try: custom_headers_dict = orjson.loads(custom_headers) except orjson.JSONDecodeError as ve: raise CommandError( "Encountered an error while attempting to parse custom headers: {}\n" "Note: all strings must be enclosed within \"\" instead of ''".format(ve) ) return standardize_headers(custom_headers_dict)
def check_webhook( self, fixture_name: str, expected_topic: str, expected_message: str, content_type: Optional[str] = "application/json", **kwargs: Any, ) -> None: """ check_webhook is the main way to test "normal" webhooks that work by receiving a payload from a third party and then writing some message to a Zulip stream. We use `fixture_name` to find the payload data in of our test fixtures. Then we verify that a message gets sent to a stream: self.STREAM_NAME: stream name expected_topic: topic expected_message: content We simulate the delivery of the payload with `content_type`, and you can pass other headers via `kwargs`. For the rare cases of webhooks actually sending private messages, see send_and_test_private_message. """ assert self.STREAM_NAME is not None self.subscribe(self.test_user, self.STREAM_NAME) payload = self.get_payload(fixture_name) if content_type is not None: kwargs['content_type'] = content_type if self.FIXTURE_DIR_NAME is not None: headers = get_fixture_http_headers(self.FIXTURE_DIR_NAME, fixture_name) headers = standardize_headers(headers) kwargs.update(headers) msg = self.send_webhook_payload( self.test_user, self.url, payload, **kwargs, ) self.assert_stream_message( message=msg, stream_name=self.STREAM_NAME, topic_name=expected_topic, content=expected_message, )
def send_webhook_fixture_message(url: str=REQ(), body: str=REQ(), is_json: bool=REQ(), custom_headers: Dict[str, Any]=REQ()) -> HttpResponse: client = Client() realm = get_realm("zulip") standardized_headers = standardize_headers(custom_headers) http_host = standardized_headers.pop("HTTP_HOST", realm.host) if is_json: content_type = standardized_headers.pop("HTTP_CONTENT_TYPE", "application/json") else: content_type = standardized_headers.pop("HTTP_CONTENT_TYPE", "text/plain") return client.post(url, body, content_type=content_type, HTTP_HOST=http_host, **standardized_headers)
def send_and_test_stream_message(self, fixture_name: str, expected_topic: Optional[str]=None, expected_message: Optional[str]=None, content_type: Optional[str]="application/json", **kwargs: Any) -> Message: payload = self.get_body(fixture_name) if content_type is not None: kwargs['content_type'] = content_type headers = get_fixture_http_headers(self.FIXTURE_DIR_NAME, fixture_name) headers = standardize_headers(headers) kwargs.update(headers) msg = self.send_json_payload(self.test_user, self.url, payload, self.STREAM_NAME, **kwargs) self.do_test_topic(msg, expected_topic) self.do_test_message(msg, expected_message) return msg
def send_webhook_fixture_message(url: str=REQ(), body: str=REQ(), is_json: bool=REQ(), custom_headers_str: str=REQ()) -> HttpResponse: client = Client() realm = get_realm("zulip") try: custom_headers = ujson.loads(custom_headers_str) except ValueError as ve: return json_error("Custom HTTP headers are not in a valid JSON format. {}".format(ve)) # nolint standardized_headers = standardize_headers(custom_headers) http_host = standardized_headers.pop("HTTP_HOST", realm.host) if is_json: content_type = standardized_headers.pop("HTTP_CONTENT_TYPE", "application/json") else: content_type = standardized_headers.pop("HTTP_CONTENT_TYPE", "text/plain") return client.post(url, body, content_type=content_type, HTTP_HOST=http_host, **standardized_headers)