Files
upstream-zulip/zerver/tests/test_messages.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

155 lines
6.6 KiB
Python

from datetime import timedelta
from django.utils.timezone import now as timezone_now
from zerver.actions.message_send import get_active_presence_idle_user_ids
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import Message, UserPresence, UserProfile
from zerver.models.recipients import (
bulk_get_direct_message_group_user_ids,
get_direct_message_group,
get_direct_message_group_user_ids,
get_or_create_direct_message_group,
)
class MissedMessageTest(ZulipTestCase):
def test_presence_idle_user_ids(self) -> None:
UserPresence.objects.all().delete()
sender = self.example_user("cordelia")
realm = sender.realm
hamlet = self.example_user("hamlet")
othello = self.example_user("othello")
hamlet_notifications_data = self.create_user_notifications_data_object(user_id=hamlet.id)
othello_notifications_data = self.create_user_notifications_data_object(user_id=othello.id)
def assert_active_presence_idle_user_ids(user_ids: list[int]) -> None:
presence_idle_user_ids = get_active_presence_idle_user_ids(
realm=realm,
sender_id=sender.id,
user_notifications_data_list=[
hamlet_notifications_data,
othello_notifications_data,
],
)
self.assertEqual(sorted(user_ids), sorted(presence_idle_user_ids))
def set_presence(user: UserProfile, client_name: str, ago: int) -> None:
when = timezone_now() - timedelta(seconds=ago)
UserPresence.objects.update_or_create(
user_profile_id=user.id,
defaults=dict(
realm_id=user.realm_id, last_active_time=when, last_connected_time=when
),
)
hamlet_notifications_data.dm_push_notify = True
othello_notifications_data.dm_push_notify = True
assert_active_presence_idle_user_ids([hamlet.id, othello.id])
# We have already thoroughly tested the `is_notifiable` function elsewhere,
# so we needn't test all cases here. This test exists mainly to avoid a bug
# which existed earlier with `get_active_presence_idle_user_ids` only looking
# at `private_message` and the `mentioned` flag, not stream level notifications.
# Simulate Hamlet has turned on notifications for the stream, and test that he's
# in the list.
hamlet_notifications_data.dm_push_notify = False
othello_notifications_data.dm_push_notify = False
hamlet_notifications_data.stream_email_notify = True
assert_active_presence_idle_user_ids([hamlet.id])
# Hamlet is idle (and is supposed to receive stream notifications), so he should be in the list.
set_presence(hamlet, "iPhone", ago=5000)
assert_active_presence_idle_user_ids([hamlet.id])
# If Hamlet is active, don't include him in the `presence_idle` list.
set_presence(hamlet, "website", ago=15)
assert_active_presence_idle_user_ids([])
# Hamlet is active now, so only Othello should be in the list for a group
# direct message.
hamlet_notifications_data.stream_email_notify = False
hamlet_notifications_data.dm_push_notify = False
othello_notifications_data.dm_push_notify = True
assert_active_presence_idle_user_ids([othello.id])
class TestBulkGetDirectMessageGroupUserIds(ZulipTestCase):
def test_bulk_get_direct_message_group_user_ids(self) -> None:
hamlet = self.example_user("hamlet")
cordelia = self.example_user("cordelia")
othello = self.example_user("othello")
iago = self.example_user("iago")
message_ids = [
self.send_group_direct_message(hamlet, [cordelia, othello], "test"),
self.send_group_direct_message(cordelia, [hamlet, othello, iago], "test"),
]
messages = Message.objects.filter(id__in=message_ids).order_by("id")
first_direct_message_group_recipient = messages[0].recipient
first_direct_message_group_user_ids = set(
get_direct_message_group_user_ids(first_direct_message_group_recipient)
)
first_direct_message_group = get_or_create_direct_message_group(
list(first_direct_message_group_user_ids)
)
second_direct_message_group_recipient = messages[1].recipient
second_direct_message_group_user_ids = set(
get_direct_message_group_user_ids(second_direct_message_group_recipient)
)
second_direct_message_group = get_or_create_direct_message_group(
list(second_direct_message_group_user_ids)
)
direct_message_group_user_ids = bulk_get_direct_message_group_user_ids(
[first_direct_message_group_recipient.id, second_direct_message_group_recipient.id]
)
self.assertEqual(
direct_message_group_user_ids[first_direct_message_group_recipient.id],
first_direct_message_group_user_ids,
)
self.assertEqual(
first_direct_message_group.group_size, len(first_direct_message_group_user_ids)
)
self.assertEqual(
direct_message_group_user_ids[second_direct_message_group_recipient.id],
second_direct_message_group_user_ids,
)
self.assertEqual(
second_direct_message_group.group_size, len(second_direct_message_group_user_ids)
)
def test_bulk_get_direct_message_group_user_ids_empty_list(self) -> None:
self.assertEqual(bulk_get_direct_message_group_user_ids([]), {})
class TestGetDirectMessageGroup(ZulipTestCase):
def test_get_direct_message_group_nonexistent(self) -> None:
"""Test that get_direct_message_group returns None when the group doesn't exist."""
hamlet = self.example_user("hamlet")
cordelia = self.example_user("cordelia")
# These users haven't had any group DM, so no DirectMessageGroup exists
result = get_direct_message_group([hamlet.id, cordelia.id])
self.assertIsNone(result)
def test_get_direct_message_group_exists(self) -> None:
"""Test that get_direct_message_group returns the group when it exists."""
hamlet = self.example_user("hamlet")
cordelia = self.example_user("cordelia")
othello = self.example_user("othello")
# Create a group DM which will create the DirectMessageGroup
self.send_group_direct_message(hamlet, [cordelia, othello], "test")
# Now the DirectMessageGroup should exist
result = get_direct_message_group([hamlet.id, cordelia.id, othello.id])
assert result is not None
self.assertEqual(result.group_size, 3)