Skip to content

Commit

Permalink
model/test_model/conftest :Notify when a message with alert word is
Browse files Browse the repository at this point in the history
received

received
  • Loading branch information
Subhasish-Behera committed Feb 21, 2023
1 parent d8bae88 commit 20d791d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,11 @@ def stream_msg_template() -> Message:
return msg_template


@pytest.fixture(params=["stream_msg_template"])
def stream_msg_fixture(request: Any) -> Message:
return request.getfixturevalue(request.param)


@pytest.fixture
def extra_stream_msg_template() -> Message:
msg_template = msg_template_factory(
Expand Down
93 changes: 93 additions & 0 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,99 @@ def test_notify_users_hides_PM_content_based_on_user_setting(
f"Test Organization Name:\nFoo Foo (to you{others})", expected_content
)

@pytest.mark.parametrize(
[
"user_id",
"flags",
"visual_notification_status",
"display_recipient",
"subject",
"is_stream_muted",
"is_topic_muted",
"no_of_times_notify_call_expected",
],
[
(
4444,
{"flags": ["has_alert_word"]},
True,
{"display_recipient": ["test_here"]},
{"subject": ["k1"]},
True,
True,
1,
),
(
4444,
{"flags": ["has_alert_word"]},
False,
{"display_recipient": ["test_here"]},
{"subject": ["k1"]},
True,
True,
0,
),
(
4444,
{"flags": ["has_alert_word"]},
False,
{"display_recipient": ["test_here"]},
{"subject": ["k1"]},
True,
False,
0,
),
(
4444,
{"flags": ["has_alert_word"]},
False,
{"display_recipient": ["test_here"]},
{"subject": ["k1"]},
False,
False,
1,
),
],
ids=[
"visual_notification is on -> no effect due to mute",
"visual_notification is off, and both topic and stream is muted",
"visual_notification is off, only topic is muted",
"visual_notification is off, neither topic or stream is muted",
],
)
def test_notify_user_with_alert_words(
self,
mocker,
model,
stream_msg_fixture,
user_id,
flags,
visual_notification_status,
display_recipient,
subject,
is_stream_muted,
is_topic_muted,
no_of_times_notify_call_expected,
):
stream_msg_fixture.update(flags)
self.controller.notify_enabled = True
stream_msg_fixture.update(display_recipient)
stream_msg_fixture.update(subject)
mocker.patch.object(
model,
"is_visual_notifications_enabled",
return_value=visual_notification_status,
)
mocker.patch.object(model, "is_muted_stream", return_value=is_stream_muted)
mocker.patch.object(model, "is_muted_topic", return_value=is_topic_muted)
notify = mocker.patch(MODULE + ".notify")
model.notify_user(stream_msg_fixture)
if no_of_times_notify_call_expected == 1:
assert notify.called
else:
assert not notify.called
# recipient = notify.call_args[0][0]

@pytest.mark.parametrize(
"event, expected_times_messages_rerendered, expected_index, topic_view_enabled",
[
Expand Down
9 changes: 7 additions & 2 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,13 @@ def notify_user(self, message: Message) -> str:
set(message["flags"])
) or self.is_visual_notifications_enabled(stream_id):
recipient = "{display_recipient} -> {subject}".format(**message)

if "has_alert_word" in message["flags"]:
# Check if stream or topic is muted
topic = message.get("subject", "")
if not self.is_muted_stream(stream_id) and not self.is_muted_topic(
stream_id, topic
):
recipient = "{display_recipient} -> {subject}".format(**message)
if recipient:
if hidden_content:
text = content
Expand All @@ -1468,7 +1474,6 @@ def notify_user(self, message: Message) -> str:

spoiler_tag.unwrap()
text = soup.text

return notify(
f"{self.server_name}:\n"
f"{message['sender_full_name']} (to {recipient})",
Expand Down

0 comments on commit 20d791d

Please sign in to comment.