Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stream: Show private channel for their channel admins.

Shubham Padiaaabf42c2ce9a
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index eeabaf29fcf..4cd9a8a715a 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -40,6 +40,10 @@ format used by the Zulip server that they are interacting with.
Previously, non-admin users who were channel admins or users
belonging to `can_add_subscribers_group` were not notified of events
for a private channel they were not subscribed to.
+* [`GET /events`](/api/get-events): If a user is a channel
+ administrator for a private channel they are not subscribed to. That
+ channel will now appear either in the `unsubscribed` or
+ `never_subscribed` list in subscription info.
**Feature level 348**
diff --git a/zerver/lib/stream_subscription.py b/zerver/lib/stream_subscription.py
index f04539b931d..48ff6c61339 100644
--- a/zerver/lib/stream_subscription.py
+++ b/zerver/lib/stream_subscription.py
@@ -8,6 +8,7 @@ from typing import Any
from django.db.models import Q, QuerySet
+from zerver.lib.user_groups import get_user_group_member_ids
from zerver.models import AlertWord, Realm, Recipient, Stream, Subscription, UserProfile, UserTopic
@@ -205,20 +206,30 @@ def bulk_get_subscriber_peer_info(
subscribed_ids = {}
private_peer_dict = {}
- private_stream_ids = {stream.id for stream in streams if stream.invite_only}
+ private_streams = {stream for stream in streams if stream.invite_only}
+ private_stream_ids = {stream.id for stream in private_streams}
public_stream_ids = {stream.id for stream in streams if not stream.invite_only}
stream_user_ids = get_user_ids_for_streams(private_stream_ids | public_stream_ids)
- if private_stream_ids:
+ if private_streams:
realm_admin_ids = {user.id for user in realm.get_admin_users_and_bots()}
- for stream_id in private_stream_ids:
+ for stream in private_streams:
# Realm admins can see all private stream
# subscribers.
- subscribed_user_ids = stream_user_ids.get(stream_id, set())
- subscribed_ids[stream_id] = subscribed_user_ids
- private_peer_dict[stream_id] = subscribed_user_ids | realm_admin_ids
+ subscribed_user_ids = stream_user_ids.get(stream.id, set())
+ subscribed_ids[stream.id] = subscribed_user_ids
+ channel_admin_ids = set(get_user_group_member_ids(stream.can_administer_channel_group))
+ can_add_subscribers_group_user_ids = set(
+ get_user_group_member_ids(stream.can_add_subscribers_group)
+ )
+ private_peer_dict[stream.id] = (
+ subscribed_user_ids
+ | realm_admin_ids
+ | channel_admin_ids
+ | can_add_subscribers_group_user_ids
+ )
for stream_id in public_stream_ids:
subscribed_user_ids = stream_user_ids.get(stream_id, set())
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index 93b55b64695..fe5afca5a49 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -539,6 +539,17 @@ def access_stream_for_delete_or_update_requiring_metadata_access(
return (stream, sub)
+def has_metadata_access_to_channel_via_groups(
+ user_recursive_group_ids: set[int],
+ can_administer_channel_group_id: int,
+ can_add_subscribers_group_id: int,
+) -> bool:
+ return (
+ can_administer_channel_group_id in user_recursive_group_ids
+ or can_add_subscribers_group_id in user_recursive_group_ids
+ )
+
+
def check_basic_stream_access(
user_profile: UserProfile,
stream: Stream,
diff --git a/zerver/lib/subscription_info.py b/zerver/lib/subscription_info.py
index ae925f024c9..494d8ac8ce9 100644
--- a/zerver/lib/subscription_info.py
+++ b/zerver/lib/subscription_info.py
@@ -21,6 +21,7 @@ from zerver.lib.streams import (
get_setting_values_for_group_settings,
get_stream_post_policy_value_based_on_group_setting,
get_web_public_streams_queryset,
+ has_metadata_access_to_channel_via_groups,
subscribed_to_stream,
)
from zerver.lib.timestamp import datetime_to_timestamp
@@ -33,6 +34,7 @@ from zerver.lib.types import (
SubscriptionInfo,
SubscriptionStreamDict,
)
+from zerver.lib.user_groups import get_recursive_membership_groups
from zerver.models import Realm, Stream, Subscription, UserProfile
from zerver.models.streams import get_all_streams
@@ -336,16 +338,31 @@ def validate_user_access_to_subscribers(user_profile: UserProfile | None, stream
* The stream is invite only, requesting_user is passed, and that user
does not subscribe to the stream.
"""
+ user_recursive_group_ids: set[int] = set()
+ # Optimization for the organization administrator code path. We
+ # don't explicitly grant realm admins this permission, but admins
+ # implicitly have the can_administer_channel_group permission for
+ # all channels. user_recursive_group_ids is used to check the
+ # membership of the current user in can_administer_channel_group
+ # which we don't need to calculate in case of a realm admin.
+ if user_profile and not user_profile.is_realm_admin:
+ user_recursive_group_ids = set(
+ get_recursive_membership_groups(user_profile).values_list("id", flat=True)
+ )
+
validate_user_access_to_subscribers_helper(
user_profile,
{
"realm_id": stream.realm_id,
"is_web_public": stream.is_web_public,
"invite_only": stream.invite_only,
+ "can_administer_channel_group_id": stream.can_administer_channel_group_id,
+ "can_add_subscribers_group_id": stream.can_add_subscribers_group_id,
},
# We use a lambda here so that we only compute whether the
# user is subscribed if we have to
lambda user_profile: subscribed_to_stream(user_profile, stream.id),
+ user_recursive_group_ids,
)
@@ -353,6 +370,7 @@ def validate_user_access_to_subscribers_helper(
user_profile: UserProfile | None,
stream_dict: Mapping[str, Any],
check_user_subscribed: Callable[[UserProfile], bool],
+ user_recursive_group_ids: set[int],
) -> None:
"""Helper for validate_user_access_to_subscribers that doesn't require
a full stream object. This function is a bit hard to read,
@@ -401,6 +419,13 @@ def validate_user_access_to_subscribers_helper(
if user_profile.is_realm_admin:
return
+ if has_metadata_access_to_channel_via_groups(
+ user_recursive_group_ids,
+ stream_dict["can_administer_channel_group_id"],
+ stream_dict["can_add_subscribers_group_id"],
+ ):
+ return
+
if stream_dict["invite_only"] and not check_user_subscribed(user_profile):
raise JsonableError(_("Unable to retrieve subscribers for private channel"))
@@ -415,6 +440,17 @@ def bulk_get_subscriber_user_ids(
is_subscribed: bool
check_user_subscribed = lambda user_profile: is_subscribed
+ user_recursive_group_ids = set()
+ # Optimization for the organization administrator code path. We
+ # don't explicitly grant realm admins this permission, but admins
+ # implicitly have the can_administer_channel_group permission for
+ # all channels. user_recursive_group_ids is used to check the
+ # membership of the current user in can_administer_channel_group
+ # which we don't need to calculate in case of a realm admin.
+ if not user_profile.is_realm_admin:
+ user_recursive_group_ids = set(
+ get_recursive_membership_groups(user_profile).values_list("id", flat=True)
+ )
for stream_dict in stream_dicts:
stream_id = stream_dict["id"]
is_subscribed = stream_id in subscribed_stream_ids
@@ -424,6 +460,7 @@ def bulk_get_subscriber_user_ids(
user_profile,
stream_dict,
check_user_subscribed,
+ user_recursive_group_ids,
)
except JsonableError:
continue
@@ -493,7 +530,11 @@ def get_subscribers_query(
def has_metadata_access_to_previously_subscribed_stream(
- user_profile: UserProfile, stream_dict: SubscriptionStreamDict
+ user_profile: UserProfile,
+ stream_dict: SubscriptionStreamDict,
+ user_recursive_group_ids: set[int],
+ can_administer_channel_group_id: int,
+ can_add_subscribers_group_id: int,
) -> bool:
if stream_dict["is_web_public"]:
return True
@@ -502,7 +543,9 @@ def has_metadata_access_to_previously_subscribed_stream(
return False
if stream_dict["invite_only"]:
- return user_profile.is_realm_admin
+ return user_profile.is_realm_admin or has_metadata_access_to_channel_via_groups(
+ user_recursive_group_ids, can_administer_channel_group_id, can_add_subscribers_group_id
+ )
return True
@@ -576,6 +619,17 @@ def gather_subscriptions_helper(
unsubscribed: list[SubscriptionStreamDict] = []
never_subscribed: list[NeverSubscribedStreamDict] = []
+ user_recursive_group_ids = set()
+ # Optimization for the organization administrator code path. We
+ # don't explicitly grant realm admins this permission, but admins
+ # implicitly have the can_administer_channel_group permission for
+ # all channels. user_recursive_group_ids is used to check the
+ # membership of the current user in can_administer_channel_group
+ # which we don't need to calculate in case of a realm admin.
+ if not user_profile.is_realm_admin:
+ user_recursive_group_ids = set(
+ get_recursive_membership_groups(user_profile).values_list("id", flat=True)
+ )
sub_unsub_stream_ids = set()
for sub_dict in sub_dicts:
stream_id = get_stream_id(sub_dict)
@@ -595,7 +649,15 @@ def gather_subscriptions_helper(
if is_active:
subscribed.append(stream_dict)
else:
- if has_metadata_access_to_previously_subscribed_stream(user_profile, stream_dict):
+ can_administer_channel_group_id = raw_stream_dict["can_administer_channel_group_id"]
+ can_add_subscribers_group_id = raw_stream_dict["can_add_subscribers_group_id"]
+ if has_metadata_access_to_previously_subscribed_stream(
+ user_profile,
+ stream_dict,
+ user_recursive_group_ids,
+ can_administer_channel_group_id,
+ can_add_subscribers_group_id,
+ ):
"""
User who are no longer subscribed to a stream that they don't have
metadata access to will not receive metadata related to this stream
@@ -621,7 +683,12 @@ def gather_subscriptions_helper(
for raw_stream_dict in never_subscribed_streams:
is_public = not raw_stream_dict["invite_only"]
- if is_public or user_profile.is_realm_admin:
+ can_administer_channel_group_id = raw_stream_dict["can_administer_channel_group_id"]
+ can_add_subscribers_group_id = raw_stream_dict["can_add_subscribers_group_id"]
+ has_metadata_access = has_metadata_access_to_channel_via_groups(
+ user_recursive_group_ids, can_administer_channel_group_id, can_add_subscribers_group_id
+ )
+ if is_public or user_profile.is_realm_admin or has_metadata_access:
slim_stream_dict = build_stream_dict_for_never_sub(
raw_stream_dict=raw_stream_dict,
recent_traffic=recent_traffic,
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index aab11cf8e51..7731c13cae3 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -15123,7 +15123,12 @@ paths:
Unlike `never_subscribed`, the user might have messages in their personal
message history that were sent to these channels.
- **Changes**: Removed `email_address` field from the dictionary
+ **Changes**: Prior to Zulip 10.0 (feature level 349), if a user was
+ in `can_administer_channel_group` of a channel that they had
+ unsubscribed from, but not an organization administrator, the channel
+ in question would not be part of this array.
+
+ Removed `email_address` field from the dictionary
in Zulip 8.0 (feature level 226).
Removed `role` field from the dictionary
@@ -15188,6 +15193,11 @@ paths:
Important for clients containing UI where one can browse channels to subscribe
to.
+
+ **Changes**: Prior to Zulip 10.0 (feature level 349), if a user was
+ in `can_administer_channel_group` of a channel that they never
+ subscribed to, but not an organization administrator, the channel
+ in question would not be part of this array.
unread_msgs:
type: object
description: |
diff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.py
index 0219f2d94d3..d684b943e3b 100644
--- a/zerver/tests/test_event_system.py
+++ b/zerver/tests/test_event_system.py
@@ -1218,7 +1218,7 @@ class FetchQueriesTest(ZulipTestCase):
realm = get_realm_with_settings(realm_id=user.realm_id)
with (
- self.assert_database_query_count(45),
+ self.assert_database_query_count(47),
mock.patch("zerver.lib.events.always_want") as want_mock,
):
fetch_initial_state_data(user, realm=realm)
@@ -1252,7 +1252,7 @@ class FetchQueriesTest(ZulipTestCase):
starred_messages=1,
stream=5,
stop_words=0,
- subscription=7,
+ subscription=9,
update_display_settings=0,
update_global_notifications=0,
update_message_flags=5,
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index f0bea675484..4be22005656 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -277,7 +277,7 @@ class HomeTest(ZulipTestCase):
# Verify succeeds once logged-in
with (
- self.assert_database_query_count(54),
+ self.assert_database_query_count(56),
patch("zerver.lib.cache.cache_set") as cache_mock,
):
result = self._get_home_page(stream="Denmark")
@@ -614,7 +614,7 @@ class HomeTest(ZulipTestCase):
self._get_home_page()
# Then for the second page load, measure the number of queries.
- with self.assert_database_query_count(49):
+ with self.assert_database_query_count(51):
result = self._get_home_page()
# Do a sanity check that our new streams were in the payload.
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index 514e37bc9ff..4b7ad5d923b 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -106,7 +106,7 @@ from zerver.lib.types import (
NeverSubscribedStreamDict,
SubscriptionInfo,
)
-from zerver.lib.user_groups import is_user_in_group
+from zerver.lib.user_groups import get_recursive_membership_groups, is_user_in_group
from zerver.models import (
Attachment,
DefaultStream,
@@ -311,7 +311,7 @@ class TestCreateStreams(ZulipTestCase):
"Private stream",
invite_only=True,
can_administer_channel_group=aaron_group,
- can_add_subscribers_group=prospero_group
+ can_add_subscribers_group=prospero_group,
)
self.assertEqual(events[0]["event"]["type"], "stream")
@@ -3247,7 +3247,7 @@ class StreamAdminTest(ZulipTestCase):
are on.
"""
result = self.attempt_unsubscribe_of_principal(
- query_count=14,
+ query_count=18,
target_users=[self.example_user("cordelia")],
is_realm_admin=True,
is_subbed=True,
@@ -3264,7 +3264,7 @@ class StreamAdminTest(ZulipTestCase):
streams you aren't on.
"""
result = self.attempt_unsubscribe_of_principal(
- query_count=14,
+ query_count=18,
target_users=[self.example_user("cordelia")],
is_realm_admin=True,
is_subbed=False,
@@ -5956,6 +5956,8 @@ class SubscriptionAPITest(ZulipTestCase):
user3 = self.example_user("hamlet")
user4 = self.example_user("iago")
user5 = self.example_user("AARON")
+ user6 = self.example_user("prospero")
+ user7 = self.example_user("shiva")
guest = self.example_user("polonius")
realm = user1.realm
@@ -5976,10 +5978,20 @@ class SubscriptionAPITest(ZulipTestCase):
self.subscribe(user2, "private_stream")
self.subscribe(user3, "private_stream")
+ user6_group = self.create_or_update_anonymous_group_for_setting([user6], [])
+ do_change_stream_group_based_setting(
+ private, "can_administer_channel_group", user6_group, acting_user=user6
+ )
+
+ user7_group = self.create_or_update_anonymous_group_for_setting([user7], [])
+ do_change_stream_group_based_setting(
+ private, "can_add_subscribers_group", user7_group, acting_user=user7
+ )
+
# Sends 3 peer-remove events, 2 unsubscribe events
# and 2 stream delete events for private streams.
with (
- self.assert_database_query_count(16),
+ self.assert_database_query_count(18),
self.assert_memcached_count(3),
self.capture_send_event_calls(expected_num_events=7) as events,
):
@@ -6005,6 +6017,8 @@ class SubscriptionAPITest(ZulipTestCase):
user3.id,
user4.id,
user5.id,
+ user6.id,
+ user7.id,
guest.id,
}
@@ -6021,9 +6035,20 @@ class SubscriptionAPITest(ZulipTestCase):
self.assertEqual(
notifications,
[
- ("private_stream", {user1.id, user2.id}, {user3.id, user4.id}),
- ("stream1", {user1.id, user2.id}, {user3.id, user4.id, user5.id}),
- ("stream2,stream3", {user2.id}, {user1.id, user3.id, user4.id, user5.id}),
+ # user6 and user7 have metadata access to the channel
+ # via `can_administer_channel_group` and
+ # `can_add_subscribers_group` respectively.
+ ("private_stream", {user1.id, user2.id}, {user3.id, user4.id, user6.id, user7.id}),
+ (
+ "stream1",
+ {user1.id, user2.id},
+ {user3.id, user4.id, user5.id, user6.id, user7.id},
+ ),
+ (
+ "stream2,stream3",
+ {user2.id},
+ {user1.id, user3.id, user4.id, user5.id, user6.id, user7.id},
+ ),
],
)
@@ -6468,11 +6493,18 @@ class SubscriptionAPITest(ZulipTestCase):
user_profile = self.example_user("othello")
realm_name = "no_othello_allowed"
realm = do_create_realm(realm_name, "Everyone but Othello is allowed")
+ nobody_group = NamedUserGroup.objects.get(
+ name="role:nobody", is_system_group=True, realm=realm
+ )
stream_dict = {
"name": "publicstream",
"description": "Public stream with public history",
"realm_id": realm.id,
+ "can_administer_channel_group_id": nobody_group.id,
}
+ user_recursive_group_ids = set(
+ get_recursive_membership_groups(user_profile).values_list("id", flat=True)
+ )
# For this test to work, othello can't be in the no_othello_here realm
self.assertNotEqual(
@@ -6481,12 +6513,17 @@ class SubscriptionAPITest(ZulipTestCase):
# This should result in missing user
with self.assertRaises(ValidationError):
- validate_user_access_to_subscribers_helper(None, stream_dict, lambda user_profile: True)
+ validate_user_access_to_subscribers_helper(
+ None, stream_dict, lambda user_profile: True, user_recursive_group_ids
+ )
# This should result in user not in realm
with self.assertRaises(ValidationError):
validate_user_access_to_subscribers_helper(
- user_profile, stream_dict, lambda user_profile: True
+ user_profile,
+ stream_dict,
+ lambda user_profile: True,
+ user_recursive_group_ids,
)
def test_subscriptions_query_count(self) -> None:
@@ -6510,7 +6547,7 @@ class SubscriptionAPITest(ZulipTestCase):
)
# Test creating private stream.
- with self.assert_database_query_count(48):
+ with self.assert_database_query_count(50):
self.subscribe_via_post(
self.test_user,
[new_streams[1]],
@@ -7066,7 +7103,7 @@ class GetSubscribersTest(ZulipTestCase):
for user in [cordelia, othello, polonius]:
self.assert_user_got_subscription_notification(user, msg)
- with self.assert_database_query_count(7):
+ with self.assert_database_query_count(9):
subscribed_streams, _ = gather_subscriptions(
self.user_profile, include_subscribers=True
)
@@ -7101,7 +7138,7 @@ class GetSubscribersTest(ZulipTestCase):
acting_user=None,
)
- with self.assert_database_query_count(7):
+ with self.assert_database_query_count(9):
subscribed_streams, _ = gather_subscriptions(
self.user_profile, include_subscribers=True
)
@@ -7184,7 +7221,7 @@ class GetSubscribersTest(ZulipTestCase):
stream, "can_send_message_group", setting_group, acting_user=desdemona
)
- with self.assert_database_query_count(7):
+ with self.assert_database_query_count(9):
subscribed_streams, _ = gather_subscriptions(hamlet, include_subscribers=True)
[stream_1_sub] = [sub for sub in subscribed_streams if sub["name"] == "stream_1"]
@@ -7244,6 +7281,10 @@ class GetSubscribersTest(ZulipTestCase):
"test_stream_web_public_2",
]
+ nobody_group = NamedUserGroup.objects.get(
+ name="role:nobody", is_system_group=True, realm=realm
+ )
+
def create_public_streams() -> None:
for stream_name in public_streams:
self.make_stream(stream_name, realm=realm)
@@ -7251,7 +7292,10 @@ class GetSubscribersTest(ZulipTestCase):
self.subscribe_via_post(
self.user_profile,
public_streams,
- dict(principals=orjson.dumps(users_to_subscribe).decode()),
+ dict(
+ principals=orjson.dumps(users_to_subscribe).decode(),
+ can_administer_channel_group=nobody_group.id,
+ ),
)
create_public_streams()
@@ -7263,7 +7307,10 @@ class GetSubscribersTest(ZulipTestCase):
ret = self.subscribe_via_post(
self.user_profile,
web_public_streams,
- dict(principals=orjson.dumps(users_to_subscribe).decode()),
+ dict(
+ principals=orjson.dumps(users_to_subscribe).decode(),
+ can_administer_channel_group=nobody_group.id,
+ ),
)
self.assert_json_success(ret)
@@ -7273,14 +7320,17 @@ class GetSubscribersTest(ZulipTestCase):
self.subscribe_via_post(
self.user_profile,
private_streams,
- dict(principals=orjson.dumps(users_to_subscribe).decode()),
+ dict(
+ principals=orjson.dumps(users_to_subscribe).decode(),
+ can_administer_channel_group=nobody_group.id,
+ ),
invite_only=True,
)
create_private_streams()
- def get_never_subscribed() -> list[NeverSubscribedStreamDict]:
- with self.assert_database_query_count(7):
+ def get_never_subscribed(query_count: int = 9) -> list[NeverSubscribedStreamDict]:
+ with self.assert_database_query_count(query_count):
sub_data = gather_subscriptions_helper(self.user_profile)
self.verify_sub_fields(sub_data)
never_subscribed = sub_data.never_subscribed
@@ -7299,10 +7349,10 @@ class GetSubscribersTest(ZulipTestCase):
self.assert_length(stream_dict["subscribers"], len(users_to_subscribe))
# Send private stream subscribers to all realm admins.
- def test_admin_case() -> None:
+ def test_realm_admin_case() -> None:
self.user_profile.role = UserProfile.ROLE_REALM_ADMINISTRATOR
# Test realm admins can get never subscribed private stream's subscribers.
- never_subscribed = get_never_subscribed()
+ never_subscribed = get_never_subscribed(7)
self.assertEqual(
len(never_subscribed),
@@ -7311,7 +7361,50 @@ class GetSubscribersTest(ZulipTestCase):
for stream_dict in never_subscribed:
self.assert_length(stream_dict["subscribers"], len(users_to_subscribe))
- test_admin_case()
+ test_realm_admin_case()
+
+ # Send private stream subscribers to all realm admins.
+ def test_channel_admin_case() -> None:
+ self.user_profile.role = UserProfile.ROLE_MEMBER
+ user_group = self.create_or_update_anonymous_group_for_setting([self.user_profile], [])
+ do_change_stream_group_based_setting(
+ get_stream("test_stream_invite_only_1", realm),
+ "can_administer_channel_group",
+ user_group,
+ acting_user=self.user_profile,
+ )
+ # Test channel admins can get never subscribed private stream's subscribers.
+ never_subscribed = get_never_subscribed()
+
+ self.assertEqual(
+ len(never_subscribed),
+ len(public_streams) + 1 + len(web_public_streams),
+ )
+ for stream_dict in never_subscribed:
+ self.assert_length(stream_dict["subscribers"], len(users_to_subscribe))
+
+ test_channel_admin_case()
+
+ def test_can_add_subscribers_case() -> None:
+ self.user_profile.role = UserProfile.ROLE_MEMBER
+ user_group = self.create_or_update_anonymous_group_for_setting([self.user_profile], [])
+ do_change_stream_group_based_setting(
+ get_stream("test_stream_invite_only_1", realm),
+ "can_add_subscribers_group",
+ user_group,
+ acting_user=self.user_profile,
+ )
+ # Test channel admins can get never subscribed private stream's subscribers.
+ never_subscribed = get_never_subscribed()
+
+ self.assertEqual(
+ len(never_subscribed),
+ len(public_streams) + 1 + len(web_public_streams),
+ )
+ for stream_dict in never_subscribed:
+ self.assert_length(stream_dict["subscribers"], len(users_to_subscribe))
+
+ test_can_add_subscribers_case()
def test_guest_user_case() -> None:
self.user_profile.role = UserProfile.ROLE_GUEST
@@ -7394,7 +7487,7 @@ class GetSubscribersTest(ZulipTestCase):
guest_user = self.example_user("polonius")
stream_name = "private_stream"
- self.make_stream(stream_name, realm=get_realm("zulip"), invite_only=True)
+ stream = self.make_stream(stream_name, realm=get_realm("zulip"), invite_only=True)
self.subscribe(admin_user, stream_name)
self.subscribe(non_admin_user, stream_name)
self.subscribe(guest_user, stream_name)
@@ -7417,6 +7510,22 @@ class GetSubscribersTest(ZulipTestCase):
unsubscribed_streams = sub_data.unsubscribed
self.assert_length(unsubscribed_streams, 0)
+ # Test channel admin gets previously subscribed private stream's subscribers.
+ non_admin_user_group = self.create_or_update_anonymous_group_for_setting(
+ [non_admin_user], []
+ )
+ do_change_stream_group_based_setting(
+ stream,
+ "can_administer_channel_group",
+ non_admin_user_group,
+ acting_user=admin_user,
+ )
+ sub_data = gather_subscriptions_helper(non_admin_user)
+ self.verify_sub_fields(sub_data)
+ unsubscribed_streams = sub_data.unsubscribed
+ self.assert_length(unsubscribed_streams, 1)
+ self.assert_length(unsubscribed_streams[0]["subscribers"], 1)
+
sub_data = gather_subscriptions_helper(guest_user)
self.verify_sub_fields(sub_data)
unsubscribed_streams = sub_data.unsubscribed
@@ -7477,7 +7586,7 @@ class GetSubscribersTest(ZulipTestCase):
subdomain="zephyr",
)
- with self.assert_database_query_count(6):
+ with self.assert_database_query_count(8):
subscribed_streams, _ = gather_subscriptions(mit_user_profile, include_subscribers=True)
self.assertGreaterEqual(len(subscribed_streams), 2)
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index 46d4ee0507a..13e038fb668 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -1021,7 +1021,7 @@ class QueryCountTest(ZulipTestCase):
prereg_user = PreregistrationUser.objects.get(email="[email protected]")
with (
- self.assert_database_query_count(85),
+ self.assert_database_query_count(93),
self.assert_memcached_count(19),
self.capture_send_event_calls(expected_num_events=10) as events,
):