tests: Use tornado_redirected_to_list instead of mocking send_event.

This commit is contained in:
Abhijeet Prasad Bodas 2021-05-27 19:55:23 +05:30 committed by Tim Abbott
parent 090f2d6664
commit c19b7b4db3
3 changed files with 14 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import time
from typing import Any, Callable, Dict, List, Optional
from typing import Any, Callable, Dict, List, Mapping, Optional
from unittest import mock
import orjson
@ -1123,18 +1123,19 @@ class TestGetRawUserDataSystemBotRealm(ZulipTestCase):
class TestUserPresenceUpdatesDisabled(ZulipTestCase):
def test_presence_events_diabled_on_larger_realm(self) -> None:
# First check that normally the mocked function gets called.
with mock.patch("zerver.lib.actions.send_event") as mock_send_event:
events: List[Mapping[str, Any]] = []
with self.tornado_redirected_to_list(events, expected_num_events=1):
do_update_user_presence(
self.example_user("cordelia"),
get_client("website"),
timezone_now(),
UserPresence.ACTIVE,
)
mock_send_event.assert_called_once()
# Now check that if the realm has more than the USER_LIMIT_FOR_SENDING_PRESENCE_UPDATE_EVENTS
# amount of active users, send_event doesn't get called.
with mock.patch("zerver.lib.actions.send_event") as mock_send_event:
events = []
with self.tornado_redirected_to_list(events, expected_num_events=0):
with self.settings(USER_LIMIT_FOR_SENDING_PRESENCE_UPDATE_EVENTS=1):
do_update_user_presence(
self.example_user("hamlet"),
@ -1142,4 +1143,3 @@ class TestUserPresenceUpdatesDisabled(ZulipTestCase):
timezone_now(),
UserPresence.ACTIVE,
)
mock_send_event.assert_not_called()

View File

@ -1,5 +1,5 @@
import datetime
from typing import Any, Optional, Set
from typing import Any, List, Mapping, Optional, Set
from unittest import mock
import orjson
@ -1660,14 +1660,14 @@ class StreamMessagesTest(ZulipTestCase):
)
def _send_stream_message(self, user: UserProfile, stream_name: str, content: str) -> Set[int]:
with mock.patch("zerver.lib.actions.send_event") as m:
events: List[Mapping[str, Any]] = []
with self.tornado_redirected_to_list(events):
self.send_stream_message(
user,
stream_name,
content=content,
)
self.assertEqual(m.call_count, 1)
users = m.call_args[0][2]
users = events[0]["users"]
user_ids = {u["id"] for u in users}
return user_ids

View File

@ -1,5 +1,4 @@
from typing import Any, Dict, List
from unittest import mock
from typing import Any, Dict, List, Mapping
from zerver.lib.message import MessageDict
from zerver.lib.test_classes import ZulipTestCase
@ -114,7 +113,8 @@ class TestBasics(ZulipTestCase):
msg_type="whatever",
content='{"name": "alice", "salary": 20}',
)
with mock.patch("zerver.lib.actions.send_event") as m:
events: List[Mapping[str, Any]] = []
with self.tornado_redirected_to_list(events):
result = self.client_post("/json/submessage", payload)
self.assert_json_success(result)
@ -129,10 +129,9 @@ class TestBasics(ZulipTestCase):
type="submessage",
)
self.assertEqual(m.call_count, 1)
data = m.call_args[0][1]
data = events[0]["event"]
self.assertEqual(data, expected_data)
users = m.call_args[0][2]
users = events[0]["users"]
self.assertIn(cordelia.id, users)
self.assertIn(hamlet.id, users)