diff --git a/zerver/tests/test_muting_users.py b/zerver/tests/test_muting_users.py index 26f0330f51..c06f740348 100644 --- a/zerver/tests/test_muting_users.py +++ b/zerver/tests/test_muting_users.py @@ -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) diff --git a/zerver/views/muting.py b/zerver/views/muting.py index b9d9989a4b..dd3f05609e 100644 --- a/zerver/views/muting.py +++ b/zerver/views/muting.py @@ -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)