muting: Handle the case of a race muting the same user twice.

This commit is contained in:
Alex Vandiver 2022-03-24 16:45:35 -07:00 committed by Tim Abbott
parent 781107308d
commit 141b0c4cec
2 changed files with 13 additions and 1 deletions

View File

@ -77,6 +77,14 @@ class MutedUsersTests(ZulipTestCase):
result = self.api_post(hamlet, url)
self.assert_json_error(result, "User already muted")
# Verify the error handling for the database level
# IntegrityError we'll get with a race between two processes
# trying to mute the user. To do this, we patch the
# get_mute_object function to always return None.
with mock.patch("zerver.views.muting.get_mute_object", return_value=None):
result = self.api_post(hamlet, url)
self.assert_json_error(result, "User already muted")
def _test_add_muted_user_valid_data(self, deactivate_user: bool = False) -> None:
hamlet = self.example_user("hamlet")
self.login_user(hamlet)

View File

@ -106,7 +106,11 @@ def mute_user(request: HttpRequest, user_profile: UserProfile, muted_user_id: in
if get_mute_object(user_profile, muted_user) is not None:
raise JsonableError(_("User already muted"))
do_mute_user(user_profile, muted_user, date_muted)
try:
do_mute_user(user_profile, muted_user, date_muted)
except IntegrityError:
raise JsonableError(_("User already muted"))
return json_success(request)