2017-03-08 12:46:05 +01:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-10-23 19:39:12 +02:00
|
|
|
from zerver.lib.stream_topic import StreamTopicTarget
|
|
|
|
|
|
|
|
from zerver.models import (
|
|
|
|
get_stream,
|
2017-10-28 20:26:11 +02:00
|
|
|
get_stream_recipient,
|
2017-10-23 19:39:12 +02:00
|
|
|
UserProfile,
|
|
|
|
)
|
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
from zerver.lib.topic_mutes import (
|
|
|
|
add_topic_mute,
|
|
|
|
get_topic_mutes,
|
2018-12-24 17:04:27 +01:00
|
|
|
remove_topic_mute,
|
2017-08-30 02:19:34 +02:00
|
|
|
topic_is_muted,
|
|
|
|
)
|
2017-03-08 12:46:05 +01:00
|
|
|
|
|
|
|
class MutedTopicsTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_ids_muting_topic(self) -> None:
|
2017-10-23 19:39:12 +02:00
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
cordelia = self.example_user('cordelia')
|
|
|
|
realm = hamlet.realm
|
|
|
|
stream = get_stream(u'Verona', realm)
|
2017-10-28 20:26:11 +02:00
|
|
|
recipient = get_stream_recipient(stream.id)
|
2017-10-23 19:39:12 +02:00
|
|
|
topic_name = 'teST topic'
|
|
|
|
|
|
|
|
stream_topic_target = StreamTopicTarget(
|
|
|
|
stream_id=stream.id,
|
|
|
|
topic_name=topic_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
user_ids = stream_topic_target.user_ids_muting_topic()
|
|
|
|
self.assertEqual(user_ids, set())
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def mute_user(user: UserProfile) -> None:
|
2017-10-23 19:39:12 +02:00
|
|
|
add_topic_mute(
|
|
|
|
user_profile=user,
|
|
|
|
stream_id=stream.id,
|
|
|
|
recipient_id=recipient.id,
|
|
|
|
topic_name='test TOPIC',
|
|
|
|
)
|
|
|
|
|
|
|
|
mute_user(hamlet)
|
|
|
|
user_ids = stream_topic_target.user_ids_muting_topic()
|
|
|
|
self.assertEqual(user_ids, {hamlet.id})
|
|
|
|
|
|
|
|
mute_user(cordelia)
|
|
|
|
user_ids = stream_topic_target.user_ids_muting_topic()
|
|
|
|
self.assertEqual(user_ids, {hamlet.id, cordelia.id})
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_muted_topic(self) -> None:
|
2018-12-24 17:04:27 +01:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
self.login(user.email)
|
|
|
|
|
|
|
|
stream = get_stream('Verona', user.realm)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
|
|
|
url = '/api/v1/users/me/subscriptions/muted_topics'
|
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
payloads = [
|
|
|
|
{'stream': stream.name, 'topic': 'Verona3', 'op': 'add'},
|
|
|
|
{'stream_id': stream.id, 'topic': 'Verona3', 'op': 'add'},
|
|
|
|
]
|
|
|
|
|
|
|
|
for data in payloads:
|
|
|
|
result = self.api_patch(user.email, url, data)
|
|
|
|
self.assert_json_success(result)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assertIn([stream.name, 'Verona3'], get_topic_mutes(user))
|
|
|
|
|
|
|
|
self.assertTrue(topic_is_muted(user, stream.id, 'Verona3'))
|
|
|
|
self.assertTrue(topic_is_muted(user, stream.id, 'verona3'))
|
|
|
|
|
|
|
|
remove_topic_mute(
|
|
|
|
user_profile=user,
|
|
|
|
stream_id=stream.id,
|
|
|
|
topic_name='Verona3',
|
|
|
|
)
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_remove_muted_topic(self) -> None:
|
2018-12-24 17:04:27 +01:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
|
|
|
realm = user.realm
|
2017-03-13 22:07:00 +01:00
|
|
|
self.login(email)
|
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
stream = get_stream(u'Verona', realm)
|
2017-10-28 20:26:11 +02:00
|
|
|
recipient = get_stream_recipient(stream.id)
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2017-03-13 22:07:00 +01:00
|
|
|
url = '/api/v1/users/me/subscriptions/muted_topics'
|
2018-12-24 17:04:27 +01:00
|
|
|
payloads = [
|
|
|
|
{'stream': stream.name, 'topic': 'vERONA3', 'op': 'remove'},
|
|
|
|
{'stream_id': stream.id, 'topic': 'vEroNA3', 'op': 'remove'},
|
|
|
|
]
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
for data in payloads:
|
|
|
|
add_topic_mute(
|
|
|
|
user_profile=user,
|
|
|
|
stream_id=stream.id,
|
|
|
|
recipient_id=recipient.id,
|
|
|
|
topic_name='Verona3',
|
|
|
|
)
|
|
|
|
self.assertIn([stream.name, 'Verona3'], get_topic_mutes(user))
|
|
|
|
|
|
|
|
result = self.api_patch(email, url, data)
|
|
|
|
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertNotIn([stream.name, 'Verona3'], get_topic_mutes(user))
|
|
|
|
self.assertFalse(topic_is_muted(user, stream.id, 'verona3'))
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_muted_topic_add_invalid(self) -> None:
|
2018-12-24 17:04:27 +01:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
|
|
|
realm = user.realm
|
2017-03-13 22:07:00 +01:00
|
|
|
self.login(email)
|
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
stream = get_stream('Verona', realm)
|
2017-10-28 20:26:11 +02:00
|
|
|
recipient = get_stream_recipient(stream.id)
|
2017-08-30 02:19:34 +02:00
|
|
|
add_topic_mute(
|
2018-12-24 17:04:27 +01:00
|
|
|
user_profile=user,
|
2017-08-30 02:19:34 +02:00
|
|
|
stream_id=stream.id,
|
|
|
|
recipient_id=recipient.id,
|
|
|
|
topic_name=u'Verona3',
|
|
|
|
)
|
|
|
|
|
2017-03-13 22:07:00 +01:00
|
|
|
url = '/api/v1/users/me/subscriptions/muted_topics'
|
2018-12-24 17:04:27 +01:00
|
|
|
|
|
|
|
data = {'stream': stream.name, 'topic': 'Verona3', 'op': 'add'} # type: Dict[str, Any]
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_patch(email, url, data)
|
2017-03-13 22:07:00 +01:00
|
|
|
self.assert_json_error(result, "Topic already muted")
|
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
data = {'stream_id': 999999999, 'topic': 'Verona3', 'op': 'add'}
|
|
|
|
result = self.api_patch(email, url, data)
|
|
|
|
self.assert_json_error(result, "Invalid stream id")
|
|
|
|
|
|
|
|
data = {'topic': 'Verona3', 'op': 'add'}
|
|
|
|
result = self.api_patch(email, url, data)
|
|
|
|
self.assert_json_error(result, "Please supply 'stream'.")
|
|
|
|
|
|
|
|
data = {'stream': stream.name, 'stream_id': stream.id, 'topic': 'Verona3', 'op': 'add'}
|
|
|
|
result = self.api_patch(email, url, data)
|
|
|
|
self.assert_json_error(result, "Please choose one: 'stream' or 'stream_id'.")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_muted_topic_remove_invalid(self) -> None:
|
2018-12-24 17:04:27 +01:00
|
|
|
user = self.example_user('hamlet')
|
|
|
|
email = user.email
|
|
|
|
realm = user.realm
|
2017-03-13 22:07:00 +01:00
|
|
|
self.login(email)
|
2018-12-24 17:04:27 +01:00
|
|
|
stream = get_stream('Verona', realm)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
|
|
|
url = '/api/v1/users/me/subscriptions/muted_topics'
|
2018-12-24 17:04:27 +01:00
|
|
|
data = {'stream': 'BOGUS', 'topic': 'Verona3', 'op': 'remove'} # type: Dict[str, Any]
|
|
|
|
result = self.api_patch(email, url, data)
|
|
|
|
self.assert_json_error(result, "Topic is not muted")
|
|
|
|
|
|
|
|
data = {'stream': stream.name, 'topic': 'BOGUS', 'op': 'remove'}
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_patch(email, url, data)
|
2018-08-27 22:13:56 +02:00
|
|
|
self.assert_json_error(result, "Topic is not muted")
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
data = {'stream_id': 999999999, 'topic': 'BOGUS', 'op': 'remove'}
|
2017-12-14 19:02:31 +01:00
|
|
|
result = self.api_patch(email, url, data)
|
2018-08-27 22:13:56 +02:00
|
|
|
self.assert_json_error(result, "Topic is not muted")
|
2018-12-24 17:04:27 +01:00
|
|
|
|
|
|
|
data = {'topic': 'Verona3', 'op': 'remove'}
|
|
|
|
result = self.api_patch(email, url, data)
|
|
|
|
self.assert_json_error(result, "Please supply 'stream'.")
|
|
|
|
|
|
|
|
data = {'stream': stream.name, 'stream_id': stream.id, 'topic': 'Verona3', 'op': 'remove'}
|
|
|
|
result = self.api_patch(email, url, data)
|
|
|
|
self.assert_json_error(result, "Please choose one: 'stream' or 'stream_id'.")
|