Skip to content

Commit 20d791d

Browse files
model/test_model/conftest :Notify when a message with alert word is
received received
1 parent d8bae88 commit 20d791d

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ def stream_msg_template() -> Message:
464464
return msg_template
465465

466466

467+
@pytest.fixture(params=["stream_msg_template"])
468+
def stream_msg_fixture(request: Any) -> Message:
469+
return request.getfixturevalue(request.param)
470+
471+
467472
@pytest.fixture
468473
def extra_stream_msg_template() -> Message:
469474
msg_template = msg_template_factory(

tests/model/test_model.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,99 @@ def test_notify_users_hides_PM_content_based_on_user_setting(
20482048
f"Test Organization Name:\nFoo Foo (to you{others})", expected_content
20492049
)
20502050

2051+
@pytest.mark.parametrize(
2052+
[
2053+
"user_id",
2054+
"flags",
2055+
"visual_notification_status",
2056+
"display_recipient",
2057+
"subject",
2058+
"is_stream_muted",
2059+
"is_topic_muted",
2060+
"no_of_times_notify_call_expected",
2061+
],
2062+
[
2063+
(
2064+
4444,
2065+
{"flags": ["has_alert_word"]},
2066+
True,
2067+
{"display_recipient": ["test_here"]},
2068+
{"subject": ["k1"]},
2069+
True,
2070+
True,
2071+
1,
2072+
),
2073+
(
2074+
4444,
2075+
{"flags": ["has_alert_word"]},
2076+
False,
2077+
{"display_recipient": ["test_here"]},
2078+
{"subject": ["k1"]},
2079+
True,
2080+
True,
2081+
0,
2082+
),
2083+
(
2084+
4444,
2085+
{"flags": ["has_alert_word"]},
2086+
False,
2087+
{"display_recipient": ["test_here"]},
2088+
{"subject": ["k1"]},
2089+
True,
2090+
False,
2091+
0,
2092+
),
2093+
(
2094+
4444,
2095+
{"flags": ["has_alert_word"]},
2096+
False,
2097+
{"display_recipient": ["test_here"]},
2098+
{"subject": ["k1"]},
2099+
False,
2100+
False,
2101+
1,
2102+
),
2103+
],
2104+
ids=[
2105+
"visual_notification is on -> no effect due to mute",
2106+
"visual_notification is off, and both topic and stream is muted",
2107+
"visual_notification is off, only topic is muted",
2108+
"visual_notification is off, neither topic or stream is muted",
2109+
],
2110+
)
2111+
def test_notify_user_with_alert_words(
2112+
self,
2113+
mocker,
2114+
model,
2115+
stream_msg_fixture,
2116+
user_id,
2117+
flags,
2118+
visual_notification_status,
2119+
display_recipient,
2120+
subject,
2121+
is_stream_muted,
2122+
is_topic_muted,
2123+
no_of_times_notify_call_expected,
2124+
):
2125+
stream_msg_fixture.update(flags)
2126+
self.controller.notify_enabled = True
2127+
stream_msg_fixture.update(display_recipient)
2128+
stream_msg_fixture.update(subject)
2129+
mocker.patch.object(
2130+
model,
2131+
"is_visual_notifications_enabled",
2132+
return_value=visual_notification_status,
2133+
)
2134+
mocker.patch.object(model, "is_muted_stream", return_value=is_stream_muted)
2135+
mocker.patch.object(model, "is_muted_topic", return_value=is_topic_muted)
2136+
notify = mocker.patch(MODULE + ".notify")
2137+
model.notify_user(stream_msg_fixture)
2138+
if no_of_times_notify_call_expected == 1:
2139+
assert notify.called
2140+
else:
2141+
assert not notify.called
2142+
# recipient = notify.call_args[0][0]
2143+
20512144
@pytest.mark.parametrize(
20522145
"event, expected_times_messages_rerendered, expected_index, topic_view_enabled",
20532146
[

zulipterminal/model.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,13 @@ def notify_user(self, message: Message) -> str:
14471447
set(message["flags"])
14481448
) or self.is_visual_notifications_enabled(stream_id):
14491449
recipient = "{display_recipient} -> {subject}".format(**message)
1450-
1450+
if "has_alert_word" in message["flags"]:
1451+
# Check if stream or topic is muted
1452+
topic = message.get("subject", "")
1453+
if not self.is_muted_stream(stream_id) and not self.is_muted_topic(
1454+
stream_id, topic
1455+
):
1456+
recipient = "{display_recipient} -> {subject}".format(**message)
14511457
if recipient:
14521458
if hidden_content:
14531459
text = content
@@ -1468,7 +1474,6 @@ def notify_user(self, message: Message) -> str:
14681474

14691475
spoiler_tag.unwrap()
14701476
text = soup.text
1471-
14721477
return notify(
14731478
f"{self.server_name}:\n"
14741479
f"{message['sender_full_name']} (to {recipient})",

0 commit comments

Comments
 (0)