Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
test_events: Extract SubscribeActionTest.
Steve Howellb4560948237b
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 0b3232be3dc..b6ef99aa600 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -2143,186 +2143,6 @@ class NormalActionsTest(BaseAction):])peer_add_schema_checker('events[1]', events[1])- @slow("Actually several tests combined together")- def test_subscribe_events(self) -> None:- self.do_test_subscribe_events(include_subscribers=True)-- @slow("Actually several tests combined together")- def test_subscribe_events_no_include_subscribers(self) -> None:- self.do_test_subscribe_events(include_subscribers=False)-- def do_test_subscribe_events(self, include_subscribers: bool) -> None:- subscription_fields = basic_stream_fields + [- ('audible_notifications', check_none_or(check_bool)),- ('color', check_string),- ('desktop_notifications', check_none_or(check_bool)),- ('email_address', check_string),- ('email_notifications', check_none_or(check_bool)),- ('in_home_view', check_bool),- ('is_muted', check_bool),- ('pin_to_top', check_bool),- ('push_notifications', check_none_or(check_bool)),- ('stream_weekly_traffic', check_none_or(check_int)),- ('wildcard_mentions_notify', check_none_or(check_bool)),- ]-- if include_subscribers:- subscription_fields.append(('subscribers', check_list(check_int)))- subscription_schema_checker = check_list(- check_dict_only(subscription_fields),- )- stream_create_schema_checker = check_events_dict([- ('type', equals('stream')),- ('op', equals('create')),- ('streams', check_list(check_dict_only(basic_stream_fields))),- ])- add_schema_checker = check_events_dict([- ('type', equals('subscription')),- ('op', equals('add')),- ('subscriptions', subscription_schema_checker),- ])- remove_schema_checker = check_events_dict([- ('type', equals('subscription')),- ('op', equals('remove')),- ('subscriptions', check_list(- check_dict_only([- ('name', equals('test_stream')),- ('stream_id', check_int),- ]),- )),- ])- peer_add_schema_checker = check_events_dict([- ('type', equals('subscription')),- ('op', equals('peer_add')),- ('user_id', check_int),- ('stream_id', check_int),- ])- peer_remove_schema_checker = check_events_dict([- ('type', equals('subscription')),- ('op', equals('peer_remove')),- ('user_id', check_int),- ('stream_id', check_int),- ])- stream_update_schema_checker = check_events_dict([- ('type', equals('stream')),- ('op', equals('update')),- ('property', equals('description')),- ('value', check_string),- ('rendered_description', check_string),- ('stream_id', check_int),- ('name', check_string),- ])- stream_update_invite_only_schema_checker = check_events_dict([- ('type', equals('stream')),- ('op', equals('update')),- ('property', equals('invite_only')),- ('stream_id', check_int),- ('name', check_string),- ('value', check_bool),- ('history_public_to_subscribers', check_bool),- ])- stream_update_stream_post_policy_schema_checker = check_events_dict([- ('type', equals('stream')),- ('op', equals('update')),- ('property', equals('stream_post_policy')),- ('stream_id', check_int),- ('name', check_string),- ('value', check_int_in(Stream.STREAM_POST_POLICY_TYPES)),- ])- stream_update_message_retention_days_schema_checker = check_events_dict([- ('type', equals('stream')),- ('op', equals('update')),- ('property', equals('message_retention_days')),- ('stream_id', check_int),- ('name', check_string),- ('value', check_none_or(check_int))- ])-- # Subscribe to a totally new stream, so it's just Hamlet on it- action: Callable[[], object] = lambda: self.subscribe(self.example_user("hamlet"), "test_stream")- events = self.verify_action(- action,- event_types=["subscription", "realm_user"],- include_subscribers=include_subscribers)- add_schema_checker('events[0]', events[0])-- # Add another user to that totally new stream- action = lambda: self.subscribe(self.example_user("othello"), "test_stream")- events = self.verify_action(- action,- include_subscribers=include_subscribers,- state_change_expected=include_subscribers)- peer_add_schema_checker('events[0]', events[0])-- stream = get_stream("test_stream", self.user_profile.realm)-- # Now remove the first user, to test the normal unsubscribe flow- action = lambda: bulk_remove_subscriptions(- [self.example_user('othello')],- [stream],- get_client("website"))- events = self.verify_action(- action,- include_subscribers=include_subscribers,- state_change_expected=include_subscribers)- peer_remove_schema_checker('events[0]', events[0])-- # Now remove the second user, to test the 'vacate' event flow- action = lambda: bulk_remove_subscriptions(- [self.example_user('hamlet')],- [stream],- get_client("website"))- events = self.verify_action(- action,- include_subscribers=include_subscribers,- num_events=3)- remove_schema_checker('events[0]', events[0])-- # Now resubscribe a user, to make sure that works on a vacated stream- action = lambda: self.subscribe(self.example_user("hamlet"), "test_stream")- events = self.verify_action(- action,- include_subscribers=include_subscribers,- num_events=2)- add_schema_checker('events[1]', events[1])-- action = lambda: do_change_stream_description(stream, 'new description')- events = self.verify_action(- action,- include_subscribers=include_subscribers)- stream_update_schema_checker('events[0]', events[0])-- # Update stream privacy- action = lambda: do_change_stream_invite_only(stream, True, history_public_to_subscribers=True)- events = self.verify_action(- action,- include_subscribers=include_subscribers)- stream_update_invite_only_schema_checker('events[0]', events[0])-- # Update stream stream_post_policy property- action = lambda: do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS)- events = self.verify_action(- action,- include_subscribers=include_subscribers, num_events=2)- stream_update_stream_post_policy_schema_checker('events[0]', events[0])-- action = lambda: do_change_stream_message_retention_days(stream, -1)- events = self.verify_action(- action,- include_subscribers=include_subscribers, num_events=1)- stream_update_message_retention_days_schema_checker('events[0]', events[0])-- # Subscribe to a totally new invite-only stream, so it's just Hamlet on it- stream = self.make_stream("private", self.user_profile.realm, invite_only=True)- user_profile = self.example_user('hamlet')- action = lambda: bulk_add_subscriptions([stream], [user_profile])- events = self.verify_action(- action,- include_subscribers=include_subscribers,- num_events=2)- stream_create_schema_checker('events[0]', events[0])- add_schema_checker('events[1]', events[1])-def test_do_delete_message_stream(self) -> None:schema_checker = check_events_dict([('type', equals('delete_message')),@@ -2766,3 +2586,184 @@ class UserDisplayActionTest(BaseAction):def test_set_user_display_settings(self) -> None:for prop in UserProfile.property_types:self.do_set_user_display_settings_test(prop)++class SubscribeActionTest(BaseAction):+ @slow("Actually several tests combined together")+ def test_subscribe_events(self) -> None:+ self.do_test_subscribe_events(include_subscribers=True)++ @slow("Actually several tests combined together")+ def test_subscribe_events_no_include_subscribers(self) -> None:+ self.do_test_subscribe_events(include_subscribers=False)++ def do_test_subscribe_events(self, include_subscribers: bool) -> None:+ subscription_fields = basic_stream_fields + [+ ('audible_notifications', check_none_or(check_bool)),+ ('color', check_string),+ ('desktop_notifications', check_none_or(check_bool)),+ ('email_address', check_string),+ ('email_notifications', check_none_or(check_bool)),+ ('in_home_view', check_bool),+ ('is_muted', check_bool),+ ('pin_to_top', check_bool),+ ('push_notifications', check_none_or(check_bool)),+ ('stream_weekly_traffic', check_none_or(check_int)),+ ('wildcard_mentions_notify', check_none_or(check_bool)),+ ]++ if include_subscribers:+ subscription_fields.append(('subscribers', check_list(check_int)))+ subscription_schema_checker = check_list(+ check_dict_only(subscription_fields),+ )+ stream_create_schema_checker = check_events_dict([+ ('type', equals('stream')),+ ('op', equals('create')),+ ('streams', check_list(check_dict_only(basic_stream_fields))),+ ])+ add_schema_checker = check_events_dict([+ ('type', equals('subscription')),+ ('op', equals('add')),+ ('subscriptions', subscription_schema_checker),+ ])+ remove_schema_checker = check_events_dict([+ ('type', equals('subscription')),+ ('op', equals('remove')),+ ('subscriptions', check_list(+ check_dict_only([+ ('name', equals('test_stream')),+ ('stream_id', check_int),+ ]),+ )),+ ])+ peer_add_schema_checker = check_events_dict([+ ('type', equals('subscription')),+ ('op', equals('peer_add')),+ ('user_id', check_int),+ ('stream_id', check_int),+ ])+ peer_remove_schema_checker = check_events_dict([+ ('type', equals('subscription')),+ ('op', equals('peer_remove')),+ ('user_id', check_int),+ ('stream_id', check_int),+ ])+ stream_update_schema_checker = check_events_dict([+ ('type', equals('stream')),+ ('op', equals('update')),+ ('property', equals('description')),+ ('value', check_string),+ ('rendered_description', check_string),+ ('stream_id', check_int),+ ('name', check_string),+ ])+ stream_update_invite_only_schema_checker = check_events_dict([+ ('type', equals('stream')),+ ('op', equals('update')),+ ('property', equals('invite_only')),+ ('stream_id', check_int),+ ('name', check_string),+ ('value', check_bool),+ ('history_public_to_subscribers', check_bool),+ ])+ stream_update_stream_post_policy_schema_checker = check_events_dict([+ ('type', equals('stream')),+ ('op', equals('update')),+ ('property', equals('stream_post_policy')),+ ('stream_id', check_int),+ ('name', check_string),+ ('value', check_int_in(Stream.STREAM_POST_POLICY_TYPES)),+ ])+ stream_update_message_retention_days_schema_checker = check_events_dict([+ ('type', equals('stream')),+ ('op', equals('update')),+ ('property', equals('message_retention_days')),+ ('stream_id', check_int),+ ('name', check_string),+ ('value', check_none_or(check_int))+ ])++ # Subscribe to a totally new stream, so it's just Hamlet on it+ action: Callable[[], object] = lambda: self.subscribe(self.example_user("hamlet"), "test_stream")+ events = self.verify_action(+ action,+ event_types=["subscription", "realm_user"],+ include_subscribers=include_subscribers)+ add_schema_checker('events[0]', events[0])++ # Add another user to that totally new stream+ action = lambda: self.subscribe(self.example_user("othello"), "test_stream")+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers,+ state_change_expected=include_subscribers)+ peer_add_schema_checker('events[0]', events[0])++ stream = get_stream("test_stream", self.user_profile.realm)++ # Now remove the first user, to test the normal unsubscribe flow+ action = lambda: bulk_remove_subscriptions(+ [self.example_user('othello')],+ [stream],+ get_client("website"))+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers,+ state_change_expected=include_subscribers)+ peer_remove_schema_checker('events[0]', events[0])++ # Now remove the second user, to test the 'vacate' event flow+ action = lambda: bulk_remove_subscriptions(+ [self.example_user('hamlet')],+ [stream],+ get_client("website"))+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers,+ num_events=3)+ remove_schema_checker('events[0]', events[0])++ # Now resubscribe a user, to make sure that works on a vacated stream+ action = lambda: self.subscribe(self.example_user("hamlet"), "test_stream")+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers,+ num_events=2)+ add_schema_checker('events[1]', events[1])++ action = lambda: do_change_stream_description(stream, 'new description')+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers)+ stream_update_schema_checker('events[0]', events[0])++ # Update stream privacy+ action = lambda: do_change_stream_invite_only(stream, True, history_public_to_subscribers=True)+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers)+ stream_update_invite_only_schema_checker('events[0]', events[0])++ # Update stream stream_post_policy property+ action = lambda: do_change_stream_post_policy(stream, Stream.STREAM_POST_POLICY_ADMINS)+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers, num_events=2)+ stream_update_stream_post_policy_schema_checker('events[0]', events[0])++ action = lambda: do_change_stream_message_retention_days(stream, -1)+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers, num_events=1)+ stream_update_message_retention_days_schema_checker('events[0]', events[0])++ # Subscribe to a totally new invite-only stream, so it's just Hamlet on it+ stream = self.make_stream("private", self.user_profile.realm, invite_only=True)+ user_profile = self.example_user('hamlet')+ action = lambda: bulk_add_subscriptions([stream], [user_profile])+ events = self.verify_action(+ action,+ include_subscribers=include_subscribers,+ num_events=2)+ stream_create_schema_checker('events[0]', events[0])+ add_schema_checker('events[1]', events[1])