From 42a482759d020dab7380a0fa794d8d169c6259d4 Mon Sep 17 00:00:00 2001 From: Kenneth Rodrigues Date: Mon, 26 Aug 2024 21:23:32 +0530 Subject: [PATCH] message_send: Fix tests to use json encoded 'to'. OpenApi docs requires the `to` param to be Json encoded, but the tests were using raw strings. This commit fixes the tests. Despite OpenApi docs requiring the `to` param to be Json encoded, the endpoint still accepts the `to` param as a raw string. This is undesireable, but fixing it would render older bots that take advantage of this feature unusable. --- zerver/tests/test_decorators.py | 12 +++++----- zerver/tests/test_message_send.py | 40 +++++++++++++++---------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py index 1bf6c022cd..f374ee5c26 100644 --- a/zerver/tests/test_decorators.py +++ b/zerver/tests/test_decorators.py @@ -609,7 +609,7 @@ class DeactivatedRealmTest(ZulipTestCase): { "type": "private", "content": "Test message", - "to": self.example_email("othello"), + "to": orjson.dumps([self.example_email("othello")]).decode(), }, ) self.assert_json_error_contains(result, "Not logged in", status_code=401) @@ -626,7 +626,7 @@ class DeactivatedRealmTest(ZulipTestCase): { "type": "private", "content": "Test message", - "to": self.example_email("othello"), + "to": orjson.dumps([self.example_email("othello")]).decode(), }, ) self.assert_json_error_contains( @@ -639,7 +639,7 @@ class DeactivatedRealmTest(ZulipTestCase): { "type": "private", "content": "Test message", - "to": self.example_email("othello"), + "to": orjson.dumps([self.example_email("othello")]).decode(), }, ) self.assert_json_error_contains( @@ -765,7 +765,7 @@ class InactiveUserTest(ZulipTestCase): { "type": "private", "content": "Test message", - "to": self.example_email("othello"), + "to": orjson.dumps([self.example_email("othello")]).decode(), }, ) self.assert_json_error_contains(result, "Not logged in", status_code=401) @@ -780,7 +780,7 @@ class InactiveUserTest(ZulipTestCase): { "type": "private", "content": "Test message", - "to": self.example_email("othello"), + "to": orjson.dumps([self.example_email("othello")]).decode(), }, ) self.assert_json_error_contains(result, "Account is deactivated", status_code=401) @@ -791,7 +791,7 @@ class InactiveUserTest(ZulipTestCase): { "type": "private", "content": "Test message", - "to": self.example_email("othello"), + "to": orjson.dumps([self.example_email("othello")]).decode(), }, ) self.assert_json_error_contains(result, "Account is deactivated", status_code=401) diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py index f90e82b040..3cc18e9a8b 100644 --- a/zerver/tests/test_message_send.py +++ b/zerver/tests/test_message_send.py @@ -560,7 +560,7 @@ class MessagePOSTTest(ZulipTestCase): "/json/messages", { "type": "channel", - "to": "nonexistent_stream", + "to": orjson.dumps("nonexistent_stream").decode(), "content": "Test message", "topic": "Test topic", }, @@ -577,7 +577,7 @@ class MessagePOSTTest(ZulipTestCase): "/json/messages", { "type": "channel", - "to": """&<"'>""", + "to": orjson.dumps("""&<"'>""").decode(), "content": "Test message", "topic": "Test topic", }, @@ -760,7 +760,7 @@ class MessagePOSTTest(ZulipTestCase): { "type": "direct", "content": "Test message", - "to": "nonexistent", + "to": orjson.dumps("nonexistent").decode(), }, ) self.assert_json_error(result, "Invalid email 'nonexistent'") @@ -874,7 +874,7 @@ class MessagePOSTTest(ZulipTestCase): { "type": "invalid type", "content": "Test message", - "to": othello.email, + "to": orjson.dumps([othello.email]).decode(), }, ) self.assert_json_error(result, "Invalid type") @@ -887,7 +887,7 @@ class MessagePOSTTest(ZulipTestCase): othello = self.example_user("othello") result = self.client_post( "/json/messages", - {"type": "direct", "content": " ", "to": othello.email}, + {"type": "direct", "content": " ", "to": orjson.dumps([othello.email]).decode()}, ) self.assert_json_error(result, "Message must not be empty") @@ -900,7 +900,7 @@ class MessagePOSTTest(ZulipTestCase): "/json/messages", { "type": "channel", - "to": "Verona", + "to": orjson.dumps("Verona").decode(), "content": "Test message", "topic": "", }, @@ -914,7 +914,7 @@ class MessagePOSTTest(ZulipTestCase): self.login("hamlet") result = self.client_post( "/json/messages", - {"type": "channel", "to": "Verona", "content": "Test message"}, + {"type": "channel", "to": orjson.dumps("Verona").decode(), "content": "Test message"}, ) self.assert_json_error(result, "Missing topic") @@ -928,7 +928,7 @@ class MessagePOSTTest(ZulipTestCase): "/json/messages", { "type": "channel", - "to": "Verona", + "to": orjson.dumps("Verona").decode(), "topic": "Test\n\rTopic", "content": "Test message", }, @@ -940,7 +940,7 @@ class MessagePOSTTest(ZulipTestCase): "/json/messages", { "type": "channel", - "to": "Verona", + "to": orjson.dumps("Verona").decode(), "topic": "Test\ufffeTopic", "content": "Test message", }, @@ -956,7 +956,7 @@ class MessagePOSTTest(ZulipTestCase): "/json/messages", { "type": "invalid", - "to": "Verona", + "to": orjson.dumps("Verona").decode(), "content": "Test message", "topic": "Test topic", }, @@ -970,7 +970,7 @@ class MessagePOSTTest(ZulipTestCase): self.login("hamlet") result = self.client_post( "/json/messages", - {"type": "direct", "content": "Test content", "to": ""}, + {"type": "direct", "content": "Test content", "to": orjson.dumps("").decode()}, ) self.assert_json_error(result, "Message must have recipients") @@ -1025,7 +1025,7 @@ class MessagePOSTTest(ZulipTestCase): "sender": self.mit_email("sipbtest"), "content": "Test message", "client": "zephyr_mirror", - "to": self.mit_email("starnine"), + "to": orjson.dumps([self.mit_email("starnine")]).decode(), }, subdomain="zephyr", ) @@ -1043,7 +1043,7 @@ class MessagePOSTTest(ZulipTestCase): "sender": self.mit_email("sipbtest"), "content": "Test message", "client": "zephyr_mirror", - "to": self.mit_email("espuser"), + "to": orjson.dumps([self.mit_email("espuser")]).decode(), }, subdomain="zephyr", ) @@ -1090,7 +1090,7 @@ class MessagePOSTTest(ZulipTestCase): self.login("hamlet") post_data = { "type": "channel", - "to": "Verona", + "to": orjson.dumps("Verona").decode(), "content": " I like null bytes \x00 in my content", "topic": "Test topic", } @@ -1175,7 +1175,7 @@ class MessagePOSTTest(ZulipTestCase): "/json/messages", { "type": "channel", - "to": "Verona", + "to": orjson.dumps("Verona").decode(), "content": "Test message", "topic": "Test topic", "forged": "true", @@ -1191,7 +1191,7 @@ class MessagePOSTTest(ZulipTestCase): "type": "direct", "content": "Test message", "client": "zephyr_mirror", - "to": self.mit_email("starnine"), + "to": orjson.dumps([self.mit_email("starnine")]).decode(), }, subdomain="zephyr", ) @@ -1206,7 +1206,7 @@ class MessagePOSTTest(ZulipTestCase): "sender": self.mit_email("sipbtest"), "content": "Test message", "client": "zephyr_mirror", - "to": self.mit_email("starnine"), + "to": orjson.dumps([self.mit_email("starnine")]).decode(), }, subdomain="zephyr", ) @@ -1225,7 +1225,7 @@ class MessagePOSTTest(ZulipTestCase): "sender": self.mit_email("sipbtest"), "content": "Test message", "client": "zephyr_mirror", - "to": self.mit_email("starnine"), + "to": orjson.dumps([self.mit_email("starnine")]).decode(), }, subdomain="zephyr", ) @@ -1247,7 +1247,7 @@ class MessagePOSTTest(ZulipTestCase): "sender": self.mit_email("sipbtest"), "content": "Test message", "client": "zephyr_mirror", - "to": user.email, + "to": orjson.dumps([user.email]).decode(), }, subdomain="notzephyr", ) @@ -2298,7 +2298,7 @@ class StreamMessagesTest(ZulipTestCase): "/api/v1/messages", { "type": "channel", - "to": "Verona", + "to": orjson.dumps("Verona").decode(), "sender": self.mit_email("sipbtest"), "client": "zephyr_mirror", "topic": "announcement",