2017-03-08 12:46:05 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from mock import patch
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-08-30 02:19:34 +02:00
|
|
|
from zerver.models import get_realm, get_user, get_stream, get_recipient, Recipient
|
|
|
|
from zerver.lib.topic_mutes import (
|
|
|
|
add_topic_mute,
|
|
|
|
get_topic_mutes,
|
|
|
|
topic_is_muted,
|
|
|
|
)
|
2017-03-08 12:46:05 +01:00
|
|
|
|
|
|
|
class MutedTopicsTests(ZulipTestCase):
|
2017-03-13 22:07:00 +01:00
|
|
|
def test_add_muted_topic(self):
|
|
|
|
# type: () -> None
|
2017-05-24 02:42:31 +02:00
|
|
|
email = self.example_email('hamlet')
|
2017-03-13 22:07:00 +01:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
url = '/api/v1/users/me/subscriptions/muted_topics'
|
|
|
|
data = {'stream': 'Verona', 'topic': 'Verona3', 'op': 'add'}
|
|
|
|
result = self.client_patch(url, data, **self.api_auth(email))
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-08-30 18:14:41 +02:00
|
|
|
self.assertIn([u'Verona', u'Verona3'], get_topic_mutes(user))
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
stream = get_stream(u'Verona', user.realm)
|
|
|
|
self.assertTrue(topic_is_muted(user, stream, 'Verona3'))
|
|
|
|
self.assertTrue(topic_is_muted(user, stream, 'verona3'))
|
|
|
|
|
2017-03-13 22:07:00 +01:00
|
|
|
def test_remove_muted_topic(self):
|
|
|
|
# type: () -> None
|
2017-05-07 21:25:59 +02:00
|
|
|
self.user_profile = self.example_user('hamlet')
|
|
|
|
email = self.user_profile.email
|
2017-03-13 22:07:00 +01:00
|
|
|
self.login(email)
|
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
realm = self.user_profile.realm
|
|
|
|
stream = get_stream(u'Verona', realm)
|
|
|
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
|
|
|
add_topic_mute(
|
|
|
|
user_profile=self.user_profile,
|
|
|
|
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'
|
2017-08-30 02:19:34 +02:00
|
|
|
data = {'stream': 'Verona', 'topic': 'vERONA3', 'op': 'remove'}
|
2017-03-13 22:07:00 +01:00
|
|
|
result = self.client_patch(url, data, **self.api_auth(email))
|
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2017-05-24 02:42:31 +02:00
|
|
|
user = self.example_user('hamlet')
|
2017-08-30 18:14:41 +02:00
|
|
|
self.assertNotIn([[u'Verona', u'Verona3']], get_topic_mutes(user))
|
2017-03-13 22:07:00 +01:00
|
|
|
|
|
|
|
def test_muted_topic_add_invalid(self):
|
|
|
|
# type: () -> None
|
2017-05-07 21:25:59 +02:00
|
|
|
self.user_profile = self.example_user('hamlet')
|
|
|
|
email = self.user_profile.email
|
2017-03-13 22:07:00 +01:00
|
|
|
self.login(email)
|
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
realm = self.user_profile.realm
|
|
|
|
stream = get_stream(u'Verona', realm)
|
|
|
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
|
|
|
add_topic_mute(
|
|
|
|
user_profile=self.user_profile,
|
|
|
|
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'
|
|
|
|
data = {'stream': 'Verona', 'topic': 'Verona3', 'op': 'add'}
|
|
|
|
result = self.client_patch(url, data, **self.api_auth(email))
|
|
|
|
self.assert_json_error(result, "Topic already muted")
|
|
|
|
|
|
|
|
def test_muted_topic_remove_invalid(self):
|
|
|
|
# type: () -> None
|
2017-05-07 21:25:59 +02:00
|
|
|
self.user_profile = self.example_user('hamlet')
|
|
|
|
email = self.user_profile.email
|
2017-03-13 22:07:00 +01:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
url = '/api/v1/users/me/subscriptions/muted_topics'
|
2017-08-30 02:19:34 +02:00
|
|
|
data = {'stream': 'BOGUS', 'topic': 'Verona3', 'op': 'remove'}
|
|
|
|
result = self.client_patch(url, data, **self.api_auth(email))
|
|
|
|
self.assert_json_error(result, "Topic is not there in the muted_topics list")
|
|
|
|
|
|
|
|
data = {'stream': 'Verona', 'topic': 'BOGUS', 'op': 'remove'}
|
2017-03-13 22:07:00 +01:00
|
|
|
result = self.client_patch(url, data, **self.api_auth(email))
|
|
|
|
self.assert_json_error(result, "Topic is not there in the muted_topics list")
|