mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	alert_word tests: Use hamlet for all tests.
The tests had a bunch of different ways to create users; now we are consistent. (This is a bit of a prep step, too, to allow us to easily clean Hamlet's existing words before each test.)
This commit is contained in:
		@@ -10,16 +10,21 @@ from zerver.models import UserProfile
 | 
			
		||||
class AlertWordTests(ZulipTestCase):
 | 
			
		||||
    interesting_alert_word_list = ['alert', 'multi-word word', '☃']
 | 
			
		||||
 | 
			
		||||
    def get_user(self) -> UserProfile:
 | 
			
		||||
        # One nice thing about Hamlet is that he is
 | 
			
		||||
        # already subscribed to Denmark.
 | 
			
		||||
        return self.example_user('hamlet')
 | 
			
		||||
 | 
			
		||||
    def test_internal_endpoint(self) -> None:
 | 
			
		||||
        user_name = "cordelia"
 | 
			
		||||
        self.login(user_name)
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        self.login_user(user)
 | 
			
		||||
 | 
			
		||||
        params = {
 | 
			
		||||
            'alert_words': ujson.dumps(['milk', 'cookies']),
 | 
			
		||||
        }
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words', params)
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        user = self.example_user(user_name)
 | 
			
		||||
 | 
			
		||||
        words = user_alert_words(user)
 | 
			
		||||
        self.assertEqual(set(words), {'milk', 'cookies'})
 | 
			
		||||
 | 
			
		||||
@@ -27,7 +32,7 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        """
 | 
			
		||||
        Users start out with no alert words.
 | 
			
		||||
        """
 | 
			
		||||
        user = self.example_user('cordelia')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        words = user_alert_words(user)
 | 
			
		||||
        self.assertEqual(words, [])
 | 
			
		||||
 | 
			
		||||
@@ -37,7 +42,7 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
 | 
			
		||||
        Also verifies the cache-flushing behavior.
 | 
			
		||||
        """
 | 
			
		||||
        user = self.example_user('cordelia')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        realm_alert_words = alert_words_in_realm(user.realm)
 | 
			
		||||
        self.assert_length(realm_alert_words.get(user.id, []), 0)
 | 
			
		||||
 | 
			
		||||
@@ -68,7 +73,7 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        Removing alert words works via do_remove_alert_words, even
 | 
			
		||||
        for multi-word and non-ascii words.
 | 
			
		||||
        """
 | 
			
		||||
        user = self.example_user('cordelia')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
 | 
			
		||||
        expected_remaining_alerts = set(self.interesting_alert_word_list)
 | 
			
		||||
        do_add_alert_words(user, self.interesting_alert_word_list)
 | 
			
		||||
@@ -86,7 +91,7 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        alert_words_in_realm. Alerts added for one user do not impact other
 | 
			
		||||
        users.
 | 
			
		||||
        """
 | 
			
		||||
        user1 = self.example_user('cordelia')
 | 
			
		||||
        user1 = self.get_user()
 | 
			
		||||
 | 
			
		||||
        do_add_alert_words(user1, self.interesting_alert_word_list)
 | 
			
		||||
 | 
			
		||||
@@ -101,23 +106,25 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        self.assertEqual(set(realm_words[user2.id]), {'another'})
 | 
			
		||||
 | 
			
		||||
    def test_json_list_default(self) -> None:
 | 
			
		||||
        self.login('hamlet')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        self.login_user(user)
 | 
			
		||||
 | 
			
		||||
        result = self.client_get('/json/users/me/alert_words')
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(result.json()['alert_words'], [])
 | 
			
		||||
 | 
			
		||||
    def test_json_list_nonempty(self) -> None:
 | 
			
		||||
        hamlet = self.example_user('hamlet')
 | 
			
		||||
        do_add_alert_words(hamlet, ['one', 'two', 'three'])
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        do_add_alert_words(user, ['one', 'two', 'three'])
 | 
			
		||||
 | 
			
		||||
        self.login('hamlet')
 | 
			
		||||
        self.login_user(user)
 | 
			
		||||
        result = self.client_get('/json/users/me/alert_words')
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(set(result.json()['alert_words']), {'one', 'two', 'three'})
 | 
			
		||||
 | 
			
		||||
    def test_json_list_add(self) -> None:
 | 
			
		||||
        self.login('hamlet')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        self.login_user(user)
 | 
			
		||||
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words',
 | 
			
		||||
                                  {'alert_words': ujson.dumps(['one ', '\n two', 'three'])})
 | 
			
		||||
@@ -125,7 +132,8 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        self.assertEqual(set(result.json()['alert_words']), {'one', 'two', 'three'})
 | 
			
		||||
 | 
			
		||||
    def test_json_list_remove(self) -> None:
 | 
			
		||||
        self.login('hamlet')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        self.login_user(user)
 | 
			
		||||
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words',
 | 
			
		||||
                                  {'alert_words': ujson.dumps(['one', 'two', 'three'])})
 | 
			
		||||
@@ -137,15 +145,15 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(set(result.json()['alert_words']), {'two', 'three'})
 | 
			
		||||
 | 
			
		||||
    def message_does_alert(self, user_profile: UserProfile, message: str) -> bool:
 | 
			
		||||
        """Send a bunch of messages as othello, so Hamlet is notified"""
 | 
			
		||||
    def message_does_alert(self, user: UserProfile, message: str) -> bool:
 | 
			
		||||
        """Send a bunch of messages as othello, so our user is notified"""
 | 
			
		||||
        self.send_stream_message(self.example_user("othello"), "Denmark", message)
 | 
			
		||||
        user_message = most_recent_usermessage(user_profile)
 | 
			
		||||
        user_message = most_recent_usermessage(user)
 | 
			
		||||
        return 'has_alert_word' in user_message.flags_list()
 | 
			
		||||
 | 
			
		||||
    def test_alert_flags(self) -> None:
 | 
			
		||||
        self.login('hamlet')
 | 
			
		||||
        user_profile_hamlet = self.example_user('hamlet')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        self.login_user(user)
 | 
			
		||||
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words',
 | 
			
		||||
                                  {'alert_words': ujson.dumps(['one', 'two', 'three'])})
 | 
			
		||||
@@ -153,38 +161,38 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        self.assertEqual(set(result.json()['alert_words']), {'one', 'two', 'three'})
 | 
			
		||||
 | 
			
		||||
        # Alerts in the middle of messages work.
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user_profile_hamlet, "Normal alert one time"))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "Normal alert one time"))
 | 
			
		||||
        # Alerts at the end of messages work.
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user_profile_hamlet, "Normal alert one"))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "Normal alert one"))
 | 
			
		||||
        # Alerts at the beginning of messages work.
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user_profile_hamlet, "two normal alerts"))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "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."))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "This one? should alert"))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "Definitely time for three."))
 | 
			
		||||
        # Multiple alerts in a message work.
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user_profile_hamlet, "One two three o'clock"))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "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"))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "One o'clock"))
 | 
			
		||||
        self.assertTrue(self.message_does_alert(user, "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"))
 | 
			
		||||
        self.assertFalse(self.message_does_alert(user, "Don't alert on http://t.co/one/ urls"))
 | 
			
		||||
        self.assertFalse(self.message_does_alert(user, "Don't alert on http://t.co/one urls"))
 | 
			
		||||
 | 
			
		||||
    def test_update_alert_words(self) -> None:
 | 
			
		||||
        user_profile = self.example_user('hamlet')
 | 
			
		||||
        user = self.get_user()
 | 
			
		||||
        self.login_user(user)
 | 
			
		||||
 | 
			
		||||
        self.login_user(user_profile)
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words',
 | 
			
		||||
                                  {'alert_words': ujson.dumps(['ALERT'])})
 | 
			
		||||
 | 
			
		||||
        content = 'this is an ALERT for you'
 | 
			
		||||
        self.send_stream_message(user_profile, "Denmark", content)
 | 
			
		||||
        self.send_stream_message(user, "Denmark", content)
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
 | 
			
		||||
        original_message = most_recent_message(user_profile)
 | 
			
		||||
        original_message = most_recent_message(user)
 | 
			
		||||
 | 
			
		||||
        user_message = most_recent_usermessage(user_profile)
 | 
			
		||||
        user_message = most_recent_usermessage(user)
 | 
			
		||||
        self.assertIn('has_alert_word', user_message.flags_list())
 | 
			
		||||
 | 
			
		||||
        result = self.client_patch("/json/messages/" + str(original_message.id), {
 | 
			
		||||
@@ -193,7 +201,7 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        })
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
 | 
			
		||||
        user_message = most_recent_usermessage(user_profile)
 | 
			
		||||
        user_message = most_recent_usermessage(user)
 | 
			
		||||
        self.assertEqual(user_message.message.content, 'new ALERT for you')
 | 
			
		||||
        self.assertIn('has_alert_word', user_message.flags_list())
 | 
			
		||||
 | 
			
		||||
@@ -203,6 +211,6 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        })
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
 | 
			
		||||
        user_message = most_recent_usermessage(user_profile)
 | 
			
		||||
        user_message = most_recent_usermessage(user)
 | 
			
		||||
        self.assertEqual(user_message.message.content, 'sorry false alarm')
 | 
			
		||||
        self.assertNotIn('has_alert_word', user_message.flags_list())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user