2020-06-11 00:54:34 +02:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2017-03-08 12:46:05 +01:00
|
|
|
from typing import Any, Dict
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2017-03-08 12:46:05 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-10-23 19:39:12 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.stream_topic import StreamTopicTarget
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
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,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import MutedTopic, UserProfile, get_stream
|
|
|
|
|
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')
|
2021-02-12 08:19:30 +01:00
|
|
|
cordelia = self.example_user('cordelia')
|
2017-10-23 19:39:12 +02:00
|
|
|
realm = hamlet.realm
|
2020-04-09 21:51:58 +02:00
|
|
|
stream = get_stream('Verona', realm)
|
2020-02-18 17:25:43 +01:00
|
|
|
recipient = stream.recipient
|
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',
|
2020-01-17 16:01:00 +01:00
|
|
|
date_muted=timezone_now(),
|
2017-10-23 19:39:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
mute_user(hamlet)
|
|
|
|
user_ids = stream_topic_target.user_ids_muting_topic()
|
|
|
|
self.assertEqual(user_ids, {hamlet.id})
|
2020-01-17 16:01:00 +01:00
|
|
|
hamlet_date_muted = MutedTopic.objects.filter(user_profile=hamlet)[0].date_muted
|
|
|
|
self.assertTrue(timezone_now() - hamlet_date_muted <= timedelta(seconds=100))
|
2017-10-23 19:39:12 +02:00
|
|
|
|
|
|
|
mute_user(cordelia)
|
|
|
|
user_ids = stream_topic_target.user_ids_muting_topic()
|
|
|
|
self.assertEqual(user_ids, {hamlet.id, cordelia.id})
|
2020-01-17 16:01:00 +01:00
|
|
|
cordelia_date_muted = MutedTopic.objects.filter(user_profile=cordelia)[0].date_muted
|
|
|
|
self.assertTrue(timezone_now() - cordelia_date_muted <= timedelta(seconds=100))
|
2017-10-23 19:39:12 +02:00
|
|
|
|
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')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2018-12-24 17:04:27 +01:00
|
|
|
|
|
|
|
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'},
|
|
|
|
]
|
|
|
|
|
2020-06-04 03:32:59 +02:00
|
|
|
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
|
2018-12-24 17:04:27 +01:00
|
|
|
for data in payloads:
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
|
|
|
'zerver.views.muting.timezone_now',
|
|
|
|
return_value=datetime(2020, 1, 1, tzinfo=timezone.utc),
|
|
|
|
):
|
2020-02-05 09:03:11 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
|
|
|
self.assert_json_success(result)
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2020-06-24 16:38:35 +02:00
|
|
|
self.assertIn((stream.name, 'Verona3', mock_date_muted), get_topic_mutes(user))
|
2018-12-24 17:04:27 +01:00
|
|
|
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')
|
|
|
|
realm = user.realm
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2020-04-09 21:51:58 +02:00
|
|
|
stream = get_stream('Verona', realm)
|
2020-02-18 17:25:43 +01:00
|
|
|
recipient = stream.recipient
|
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'},
|
|
|
|
]
|
2020-06-04 03:32:59 +02:00
|
|
|
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
|
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',
|
2020-06-04 03:32:59 +02:00
|
|
|
date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc),
|
2018-12-24 17:04:27 +01:00
|
|
|
)
|
2020-06-24 16:38:35 +02:00
|
|
|
self.assertIn((stream.name, 'Verona3', mock_date_muted), get_topic_mutes(user))
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2020-06-24 16:38:35 +02:00
|
|
|
self.assertNotIn((stream.name, 'Verona3', mock_date_muted), get_topic_mutes(user))
|
2018-12-24 17:04:27 +01:00
|
|
|
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')
|
|
|
|
realm = user.realm
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
stream = get_stream('Verona', realm)
|
2020-02-18 17:25:43 +01:00
|
|
|
recipient = stream.recipient
|
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,
|
2020-04-09 21:51:58 +02:00
|
|
|
topic_name='Verona3',
|
2020-01-17 16:01:00 +01:00
|
|
|
date_muted=timezone_now(),
|
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
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
data: Dict[str, Any] = {'stream': stream.name, 'topic': 'Verona3', 'op': 'add'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, 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'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Invalid stream id")
|
|
|
|
|
|
|
|
data = {'topic': 'Verona3', 'op': 'add'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Please supply 'stream'.")
|
|
|
|
|
|
|
|
data = {'stream': stream.name, 'stream_id': stream.id, 'topic': 'Verona3', 'op': 'add'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
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')
|
|
|
|
realm = user.realm
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
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'
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
data: Dict[str, Any] = {'stream': 'BOGUS', 'topic': 'Verona3', 'op': 'remove'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Topic is not muted")
|
|
|
|
|
|
|
|
data = {'stream': stream.name, 'topic': 'BOGUS', 'op': 'remove'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, 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'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, 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'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Please supply 'stream'.")
|
|
|
|
|
|
|
|
data = {'stream': stream.name, 'stream_id': stream.id, 'topic': 'Verona3', 'op': 'remove'}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Please choose one: 'stream' or 'stream_id'.")
|