2016-09-14 15:39:41 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from zerver.lib.alert_words import (
|
|
|
|
add_user_alert_words,
|
|
|
|
alert_words_in_realm,
|
|
|
|
remove_user_alert_words,
|
|
|
|
user_alert_words,
|
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.lib.test_helpers import (
|
2016-09-14 18:25:44 +02:00
|
|
|
most_recent_message,
|
2016-09-14 15:39:41 +02:00
|
|
|
most_recent_usermessage,
|
2016-11-10 19:30:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import (
|
2016-09-14 15:39:41 +02:00
|
|
|
ZulipTestCase,
|
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.models import (
|
|
|
|
Recipient,
|
|
|
|
UserProfile,
|
|
|
|
)
|
|
|
|
|
2016-12-04 18:38:56 +01:00
|
|
|
from typing import Text
|
2016-09-14 15:39:41 +02:00
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
class AlertWordTests(ZulipTestCase):
|
|
|
|
interesting_alert_word_list = ['alert', 'multi-word word', u'☃']
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_internal_endpoint(self) -> None:
|
2017-05-23 20:57:59 +02:00
|
|
|
user_name = "cordelia"
|
|
|
|
email = self.example_email(user_name)
|
2016-09-14 15:39:41 +02:00
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'alert_words': ujson.dumps(['milk', 'cookies'])
|
|
|
|
}
|
2017-09-25 23:48:14 +02:00
|
|
|
result = self.client_post('/json/users/me/alert_words', params)
|
2016-09-14 15:39:41 +02:00
|
|
|
self.assert_json_success(result)
|
2017-05-23 20:57:59 +02:00
|
|
|
user = self.example_user(user_name)
|
2016-09-14 15:39:41 +02:00
|
|
|
words = user_alert_words(user)
|
|
|
|
self.assertEqual(words, ['milk', 'cookies'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_default_no_words(self) -> None:
|
2016-09-14 15:39:41 +02:00
|
|
|
"""
|
|
|
|
Users start out with no alert words.
|
|
|
|
"""
|
2017-05-07 19:39:30 +02:00
|
|
|
user = self.example_user('cordelia')
|
2016-09-14 15:39:41 +02:00
|
|
|
words = user_alert_words(user)
|
|
|
|
self.assertEqual(words, [])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_word(self) -> None:
|
2016-09-14 15:39:41 +02:00
|
|
|
"""
|
|
|
|
add_user_alert_words can add multiple alert words at once.
|
|
|
|
"""
|
2017-05-07 19:39:30 +02:00
|
|
|
user = self.example_user('cordelia')
|
2016-09-14 15:39:41 +02:00
|
|
|
|
|
|
|
# Add several words, including multi-word and non-ascii words.
|
|
|
|
add_user_alert_words(user, self.interesting_alert_word_list)
|
|
|
|
|
|
|
|
words = user_alert_words(user)
|
|
|
|
self.assertEqual(words, self.interesting_alert_word_list)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_remove_word(self) -> None:
|
2016-09-14 15:39:41 +02:00
|
|
|
"""
|
|
|
|
Removing alert words works via remove_user_alert_words, even
|
|
|
|
for multi-word and non-ascii words.
|
|
|
|
"""
|
2017-05-07 19:39:30 +02:00
|
|
|
user = self.example_user('cordelia')
|
2016-09-14 15:39:41 +02:00
|
|
|
|
|
|
|
add_user_alert_words(user, self.interesting_alert_word_list)
|
|
|
|
|
|
|
|
theoretical_remaining_alerts = self.interesting_alert_word_list[:]
|
|
|
|
|
|
|
|
for alert_word in self.interesting_alert_word_list:
|
|
|
|
remove_user_alert_words(user, alert_word)
|
|
|
|
theoretical_remaining_alerts.remove(alert_word)
|
|
|
|
actual_remaining_alerts = user_alert_words(user)
|
|
|
|
self.assertEqual(actual_remaining_alerts,
|
|
|
|
theoretical_remaining_alerts)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_realm_words(self) -> None:
|
2016-09-14 15:39:41 +02:00
|
|
|
"""
|
|
|
|
We can gather alert words for an entire realm via
|
|
|
|
alert_words_in_realm. Alerts added for one user do not impact other
|
|
|
|
users.
|
|
|
|
"""
|
2017-05-07 21:25:59 +02:00
|
|
|
user1 = self.example_user('cordelia')
|
2016-09-14 15:39:41 +02:00
|
|
|
|
|
|
|
add_user_alert_words(user1, self.interesting_alert_word_list)
|
|
|
|
|
2017-05-07 21:25:59 +02:00
|
|
|
user2 = self.example_user('othello')
|
2016-09-14 15:39:41 +02:00
|
|
|
add_user_alert_words(user2, ['another'])
|
|
|
|
|
|
|
|
realm_words = alert_words_in_realm(user2.realm)
|
|
|
|
self.assertEqual(len(realm_words), 2)
|
|
|
|
self.assertEqual(list(realm_words.keys()), [user1.id, user2.id])
|
|
|
|
self.assertEqual(realm_words[user1.id],
|
|
|
|
self.interesting_alert_word_list)
|
|
|
|
self.assertEqual(realm_words[user2.id], ['another'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_json_list_default(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-09-14 15:39:41 +02:00
|
|
|
|
|
|
|
result = self.client_get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:49:11 +02:00
|
|
|
self.assertEqual(result.json()['alert_words'], [])
|
2016-09-14 15:39:41 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_json_list_add(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-09-14 15:39:41 +02:00
|
|
|
|
2017-09-25 23:48:14 +02:00
|
|
|
result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one ', '\n two', 'three'])})
|
2016-09-14 15:39:41 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:49:11 +02:00
|
|
|
self.assertEqual(result.json()['alert_words'], ['one', 'two', 'three'])
|
2016-09-14 15:39:41 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_json_list_remove(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-09-14 15:39:41 +02:00
|
|
|
|
2017-09-25 23:48:14 +02:00
|
|
|
result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
|
2016-09-14 15:39:41 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_delete('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one'])})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:49:11 +02:00
|
|
|
self.assertEqual(result.json()['alert_words'], ['two', 'three'])
|
2016-09-14 15:39:41 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def message_does_alert(self, user_profile: UserProfile, message: Text) -> bool:
|
2016-09-14 15:39:41 +02:00
|
|
|
"""Send a bunch of messages as othello, so Hamlet is notified"""
|
2017-10-28 16:25:03 +02:00
|
|
|
self.send_stream_message(self.example_email("othello"), "Denmark", message)
|
2016-09-14 15:39:41 +02:00
|
|
|
user_message = most_recent_usermessage(user_profile)
|
|
|
|
return 'has_alert_word' in user_message.flags_list()
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_alert_flags(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile_hamlet = self.example_user('hamlet')
|
2016-09-14 15:39:41 +02:00
|
|
|
|
2017-09-25 23:48:14 +02:00
|
|
|
result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
|
2016-09-14 15:39:41 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_get('/json/users/me/alert_words')
|
|
|
|
self.assert_json_success(result)
|
2017-08-16 09:49:11 +02:00
|
|
|
self.assertEqual(result.json()['alert_words'], ['one', 'two', 'three'])
|
2016-09-14 15:39:41 +02:00
|
|
|
|
|
|
|
# Alerts in the middle of messages work.
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Normal alert one time"))
|
|
|
|
# Alerts at the end of messages work.
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Normal alert one"))
|
|
|
|
# Alerts at the beginning of messages work.
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "two normal alerts"))
|
|
|
|
# Alerts with surrounding punctuation work.
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "This one? should alert"))
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Definitely time for three."))
|
|
|
|
# Multiple alerts in a message work.
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "One two three o'clock"))
|
|
|
|
# Alerts are case-insensitive.
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "One o'clock"))
|
|
|
|
self.assertTrue(self.message_does_alert(user_profile_hamlet, "Case of ONE, won't stop me"))
|
|
|
|
|
|
|
|
# We don't cause alerts for matches in URLs.
|
|
|
|
self.assertFalse(self.message_does_alert(user_profile_hamlet, "Don't alert on http://t.co/one/ urls"))
|
|
|
|
self.assertFalse(self.message_does_alert(user_profile_hamlet, "Don't alert on http://t.co/one urls"))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_alert_words(self) -> None:
|
2017-05-07 21:25:59 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
me_email = user_profile.email
|
2016-09-14 18:25:44 +02:00
|
|
|
|
|
|
|
self.login(me_email)
|
2017-09-25 23:48:14 +02:00
|
|
|
result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['ALERT'])})
|
2016-09-14 18:25:44 +02:00
|
|
|
|
|
|
|
content = 'this is an ALERT for you'
|
2017-10-28 16:25:03 +02:00
|
|
|
self.send_stream_message(me_email, "Denmark", content)
|
2016-09-14 18:25:44 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
original_message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
user_message = most_recent_usermessage(user_profile)
|
|
|
|
self.assertIn('has_alert_word', user_message.flags_list())
|
|
|
|
|
2016-12-22 10:17:49 +01:00
|
|
|
result = self.client_patch("/json/messages/" + str(original_message.id), {
|
2016-09-14 18:25:44 +02:00
|
|
|
'message_id': original_message.id,
|
|
|
|
'content': 'new ALERT for you',
|
|
|
|
})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
user_message = most_recent_usermessage(user_profile)
|
|
|
|
self.assertEqual(user_message.message.content, 'new ALERT for you')
|
|
|
|
self.assertIn('has_alert_word', user_message.flags_list())
|
|
|
|
|
2016-12-22 10:17:49 +01:00
|
|
|
result = self.client_patch("/json/messages/" + str(original_message.id), {
|
2016-09-14 18:25:44 +02:00
|
|
|
'message_id': original_message.id,
|
|
|
|
'content': 'sorry false alarm',
|
|
|
|
})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
user_message = most_recent_usermessage(user_profile)
|
|
|
|
self.assertEqual(user_message.message.content, 'sorry false alarm')
|
|
|
|
self.assertNotIn('has_alert_word', user_message.flags_list())
|