mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	alert words: Return the updated list when adding/removing words.
POST and DELETE operations in /users/me/alert_words may leave the user's list of alert words in an unknown state: POSTing adds words to a list that the client may not know from the begining, and the same with DELETE. Replying with the current status of the alert words list is the best way of letting the client alter the list and knowing its contents after being updated with a single query. This is especially useful taking into account that POSTing words that were already present and DELETing non-existing words both produce a successful response. An extra test has been added to avoid leaving GET /users/me/alert_words too untested.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							3b21b17a35
						
					
				
				
					commit
					0e135b69f9
				
			@@ -105,14 +105,20 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(result.json()['alert_words'], [])
 | 
			
		||||
 | 
			
		||||
    def test_json_list_nonempty(self) -> None:
 | 
			
		||||
        hamlet = self.example_user('hamlet')
 | 
			
		||||
        add_user_alert_words(hamlet, ['one', 'two', 'three'])
 | 
			
		||||
 | 
			
		||||
        self.login(self.example_email('hamlet'))
 | 
			
		||||
        result = self.client_get('/json/users/me/alert_words')
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(result.json()['alert_words'], ['one', 'two', 'three'])
 | 
			
		||||
 | 
			
		||||
    def test_json_list_add(self) -> None:
 | 
			
		||||
        self.login(self.example_email("hamlet"))
 | 
			
		||||
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one ', '\n two', 'three'])})
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
 | 
			
		||||
        result = self.client_get('/json/users/me/alert_words')
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(result.json()['alert_words'], ['one', 'two', 'three'])
 | 
			
		||||
 | 
			
		||||
    def test_json_list_remove(self) -> None:
 | 
			
		||||
@@ -120,12 +126,10 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(result.json()['alert_words'], ['one', 'two', 'three'])
 | 
			
		||||
 | 
			
		||||
        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)
 | 
			
		||||
        self.assertEqual(result.json()['alert_words'], ['two', 'three'])
 | 
			
		||||
 | 
			
		||||
    def message_does_alert(self, user_profile: UserProfile, message: str) -> bool:
 | 
			
		||||
@@ -140,9 +144,6 @@ class AlertWordTests(ZulipTestCase):
 | 
			
		||||
 | 
			
		||||
        result = self.client_post('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
 | 
			
		||||
        result = self.client_get('/json/users/me/alert_words')
 | 
			
		||||
        self.assert_json_success(result)
 | 
			
		||||
        self.assertEqual(result.json()['alert_words'], ['one', 'two', 'three'])
 | 
			
		||||
 | 
			
		||||
        # Alerts in the middle of messages work.
 | 
			
		||||
 
 | 
			
		||||
@@ -23,11 +23,11 @@ def add_alert_words(request: HttpRequest, user_profile: UserProfile,
 | 
			
		||||
                    alert_words: List[str]=REQ(validator=check_list(check_string))
 | 
			
		||||
                    ) -> HttpResponse:
 | 
			
		||||
    do_add_alert_words(user_profile, clean_alert_words(alert_words))
 | 
			
		||||
    return json_success()
 | 
			
		||||
    return json_success({'alert_words': user_alert_words(user_profile)})
 | 
			
		||||
 | 
			
		||||
@has_request_variables
 | 
			
		||||
def remove_alert_words(request: HttpRequest, user_profile: UserProfile,
 | 
			
		||||
                       alert_words: List[str]=REQ(validator=check_list(check_string))
 | 
			
		||||
                       ) -> HttpResponse:
 | 
			
		||||
    do_remove_alert_words(user_profile, alert_words)
 | 
			
		||||
    return json_success()
 | 
			
		||||
    return json_success({'alert_words': user_alert_words(user_profile)})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user