Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
refactor: Make acting_user a mandatory kwarg for do_deactivate_user.
shanukunc95061e9b953
diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.pyindex e90453ce654..cb498c2720b 100644--- a/analytics/tests/test_counts.py+++ b/analytics/tests/test_counts.py@@ -1322,7 +1322,7 @@ class TestLoggingCountStats(AnalyticsTestCase):"value__sum"],)- do_deactivate_user(user)+ do_deactivate_user(user, acting_user=None)self.assertEqual(0,RealmCount.objects.filter(property=property, subgroup=False).aggregate(Sum("value"))[@@ -1336,7 +1336,7 @@ class TestLoggingCountStats(AnalyticsTestCase):"value__sum"],)- do_deactivate_user(user)+ do_deactivate_user(user, acting_user=None)self.assertEqual(0,RealmCount.objects.filter(property=property, subgroup=False).aggregate(Sum("value"))[@@ -1662,7 +1662,7 @@ class TestActiveUsersAudit(AnalyticsTestCase):user4 = do_create_user("email4", "password", self.default_realm, "full_name", acting_user=None)- do_deactivate_user(user2)+ do_deactivate_user(user2, acting_user=None)do_activate_user(user3, acting_user=None)do_reactivate_user(user4, acting_user=None)end_time = floor_to_day(timezone_now()) + self.DAY@@ -1785,7 +1785,7 @@ class TestRealmActiveHumans(AnalyticsTestCase):time_zero = floor_to_day(timezone_now()) + self.DAYupdate_user_activity_interval(user1, time_zero)update_user_activity_interval(user2, time_zero)- do_deactivate_user(user2)+ do_deactivate_user(user2, acting_user=None)for property in ["active_users_audit:is_bot:day","15day_actives::day",diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 86f591816ae..27d37fb1af5 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -1531,7 +1531,7 @@ class StripeTest(StripeTestCase):self.assertEqual(get_latest_seat_count(realm), initial_count + 1)# Test that inactive users aren't counted- do_deactivate_user(user2)+ do_deactivate_user(user2, acting_user=None)self.assertEqual(get_latest_seat_count(realm), initial_count)# Test guests@@ -2719,7 +2719,7 @@ class LicenseLedgerTest(StripeTestCase):def test_user_changes(self) -> None:self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token")user = do_create_user("email", "password", get_realm("zulip"), "name", acting_user=None)- do_deactivate_user(user)+ do_deactivate_user(user, acting_user=None)do_reactivate_user(user, acting_user=None)# Not a proper use of do_activate_user, but fine for this testdo_activate_user(user, acting_user=None)diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex d8e50a918e1..c74a7dbf905 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -1090,7 +1090,7 @@ def do_delete_user(user_profile: UserProfile) -> None:if user_profile.realm.is_zephyr_mirror_realm:raise AssertionError("Deleting zephyr mirror users is not supported")- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)subscribed_huddle_recipient_ids = set(Subscription.objects.filter(@@ -1122,7 +1122,7 @@ def do_delete_user(user_profile: UserProfile) -> None:def do_deactivate_user(- user_profile: UserProfile, acting_user: Optional[UserProfile] = None, _cascade: bool = True+ user_profile: UserProfile, _cascade: bool = True, *, acting_user: Optional[UserProfile]) -> None:if not user_profile.is_active:return@@ -1184,7 +1184,7 @@ def do_deactivate_user(is_bot=True, is_active=True, bot_owner=user_profile)for profile in bot_profiles:- do_deactivate_user(profile, acting_user=acting_user, _cascade=False)+ do_deactivate_user(profile, _cascade=False, acting_user=acting_user)def do_deactivate_stream(diff --git a/zerver/management/commands/deactivate_user.py b/zerver/management/commands/deactivate_user.pyindex 4f7a7d136d5..f2be870d0e0 100644--- a/zerver/management/commands/deactivate_user.py+++ b/zerver/management/commands/deactivate_user.py@@ -45,5 +45,5 @@ class Command(ZulipBaseCommand):if not options["for_real"]:raise CommandError("This was a dry run. Pass -f to actually deactivate.")- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)print("Sessions deleted, user deactivated.")diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.pyindex 6a00ce53350..ce17e27a9d9 100644--- a/zerver/tests/test_auth_backends.py+++ b/zerver/tests/test_auth_backends.py@@ -162,7 +162,7 @@ class AuthBackendTest(ZulipTestCase):self.assertEqual(user_profile, result)# Verify auth fails with a deactivated user- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)result = backend.authenticate(**good_kwargs)if isinstance(backend, SocialAuthMixin):# Returns a redirect to login page with an error.@@ -1067,7 +1067,7 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase):def test_social_auth_deactivated_user(self) -> None:user_profile = self.example_user("hamlet")- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)account_data_dict = self.get_account_data_dict(email=self.email, name=self.name)# We expect to go through the "choose email" screen here,# because there won't be an existing user account we can@@ -3812,7 +3812,7 @@ class FetchAPIKeyTest(ZulipTestCase):self.assert_json_success(result)def test_inactive_user(self) -> None:- do_deactivate_user(self.user_profile)+ do_deactivate_user(self.user_profile, acting_user=None)result = self.client_post("/api/v1/fetch_api_key",dict(username=self.email, password=initial_password(self.email)),@@ -3853,7 +3853,7 @@ class DevFetchAPIKeyTest(ZulipTestCase):self.assert_json_error_contains(result, "This user is not registered.", 403)def test_inactive_user(self) -> None:- do_deactivate_user(self.user_profile)+ do_deactivate_user(self.user_profile, acting_user=None)result = self.client_post("/api/v1/dev_fetch_api_key", dict(username=self.email))self.assert_json_error_contains(result, "Your account has been disabled", 403)@@ -5181,7 +5181,7 @@ class TestLDAP(ZulipLDAPTestCase):@override_settings(AUTHENTICATION_BACKENDS=("zproject.backends.ZulipLDAPAuthBackend",))def test_login_failure_due_to_deactivated_user(self) -> None:user_profile = self.example_user("hamlet")- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)with self.settings(LDAP_APPEND_DOMAIN="zulip.com"):user_profile = self.backend.authenticate(request=mock.MagicMock(),@@ -5364,7 +5364,7 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase):)def test_reactivate_user(self) -> None:- do_deactivate_user(self.example_user("hamlet"))+ do_deactivate_user(self.example_user("hamlet"), acting_user=None)with self.settings(AUTH_LDAP_USER_ATTR_MAP={"full_name": "cn", "userAccountControl": "userAccountControl"}@@ -5411,7 +5411,7 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase):)self.assertFalse(result)- do_deactivate_user(othello)+ do_deactivate_user(othello, acting_user=None)mock_logger = mock.MagicMock()result = sync_user_from_ldap(othello, mock_logger)# In this case the logger shouldn't be used.diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex 4e9f7bcb6fc..f74e1c3aa44 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -777,7 +777,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):bot_email = "[email protected]"bot_user = get_user(bot_email, bot_realm)- do_deactivate_user(bot_user)+ do_deactivate_user(bot_user, acting_user=None)# A regular user cannot reactivate a generic botself.assert_num_bots_equal(0)@@ -963,7 +963,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assert_num_bots_equal(1)target_user_profile = self.example_user("othello")- do_deactivate_user(target_user_profile)+ do_deactivate_user(target_user_profile, acting_user=None)target_user_profile = self.example_user("othello")self.assertFalse(target_user_profile.is_active)bot_info = {diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 17aeefaf204..528b792a10e 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -1184,7 +1184,7 @@ class InactiveUserTest(ZulipTestCase):"""user_profile = self.example_user("hamlet")self.login_user(user_profile)- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)result = self.client_post("/json/messages",@@ -1249,7 +1249,7 @@ class InactiveUserTest(ZulipTestCase):"""user_profile = self.example_user("hamlet")- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)result = self.login_with_return(self.example_email("hamlet"))self.assert_in_response("Your account is no longer active.", result)@@ -1278,7 +1278,7 @@ class InactiveUserTest(ZulipTestCase):self.assertTrue(form.is_valid())# Test a mirror-dummy deactivated user.- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)user_profile.save()form = OurAuthenticationForm(request, payload)@@ -1301,7 +1301,7 @@ class InactiveUserTest(ZulipTestCase):"""user_profile = self.example_user("hamlet")- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)api_key = get_api_key(user_profile)url = f"/api/v1/external/jira?api_key={api_key}&stream=jira_custom"diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex f97f24ff27b..a719899b837 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -1041,7 +1041,7 @@ class TestMissedMessageEmailMessages(ZulipTestCase):mm_address = create_missed_message_address(user_profile, message)- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)incoming_valid_message = EmailMessage()incoming_valid_message.set_content("TestMissedMessageEmailMessages Body")diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 1e5e7002c0c..3737753267b 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -1480,14 +1480,14 @@ class NormalActionsTest(BaseAction):def test_do_deactivate_user(self) -> None:bot = self.create_bot("test")- action = lambda: do_deactivate_user(bot)+ action = lambda: do_deactivate_user(bot, acting_user=None)events = self.verify_action(action, num_events=2)check_realm_user_remove("events[0]", events[0])check_realm_bot_remove("events[1]", events[1])def test_do_reactivate_user(self) -> None:bot = self.create_bot("test")- do_deactivate_user(bot)+ do_deactivate_user(bot, acting_user=None)action = lambda: do_reactivate_user(bot, acting_user=None)events = self.verify_action(action, num_events=2)check_realm_bot_add("events[1]", events[1])diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex d62399309ed..84a6a2345f9 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -1697,7 +1697,7 @@ class GetOldMessagesTest(ZulipTestCase):[self.example_user("iago"), self.example_user("aaron")],)aaron = self.example_user("aaron")- do_deactivate_user(aaron)+ do_deactivate_user(aaron, acting_user=None)self.assertFalse(aaron.is_active)personals = [diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex fb52f6c39c9..de865436a93 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -546,7 +546,7 @@ class MessagePOSTTest(ZulipTestCase):"""othello = self.example_user("othello")cordelia = self.example_user("cordelia")- do_deactivate_user(othello)+ do_deactivate_user(othello, acting_user=None)self.login("hamlet")result = self.client_post(diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.pyindex 466264d40bf..b58ea78496a 100644--- a/zerver/tests/test_presence.py+++ b/zerver/tests/test_presence.py@@ -468,7 +468,7 @@ class SingleUserPresenceTests(ZulipTestCase):result = self.client_get(f"/json/users/{cordelia.id}/presence")self.assert_json_error(result, f"No presence data for {cordelia.id}")- do_deactivate_user(self.example_user("cordelia"))+ do_deactivate_user(self.example_user("cordelia"), acting_user=None)result = self.client_get("/json/users/[email protected]/presence")self.assert_json_error(result, "No such user")diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 69270f65cbf..0419e73e4bf 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -443,7 +443,7 @@ class PasswordResetTest(ZulipTestCase):def test_password_reset_for_deactivated_user(self) -> None:user_profile = self.example_user("hamlet")email = user_profile.delivery_email- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)# start the password reset process by supplying an email addressresult = self.client_post("/accounts/password/reset/", {"email": email})@@ -654,7 +654,7 @@ class LoginTest(ZulipTestCase):def test_login_deactivated_user(self) -> None:user_profile = self.example_user("hamlet")- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)result = self.login_with_return(self.example_email("hamlet"), "xxx")self.assertEqual(result.status_code, 200)self.assert_in_response("Your account is no longer active.", result)@@ -4768,7 +4768,7 @@ class TestFindMyTeam(ZulipTestCase):self.assertEqual(len(outbox), 1)def test_find_team_deactivated_user(self) -> None:- do_deactivate_user(self.example_user("hamlet"))+ do_deactivate_user(self.example_user("hamlet"), acting_user=None)data = {"emails": self.example_email("hamlet")}result = self.client_post("/accounts/find/", data)self.assertEqual(result.status_code, 302)diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 23fa42c46c7..7a0b86b2e22 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -3922,7 +3922,7 @@ class SubscriptionAPITest(ZulipTestCase):)self.common_subscribe_to_streams(self.test_user, "Verona", post_data)- do_deactivate_user(target_profile)+ do_deactivate_user(target_profile, acting_user=None)result = self.common_subscribe_to_streams(self.test_user, "Denmark", post_data, allow_fail=True)diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 86f979efc44..02e1b4be6fb 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -405,7 +405,7 @@ class PermissionTest(ZulipTestCase):# Can only access deactivated users if allow_deactivated is passedhamlet = self.example_user("hamlet")- do_deactivate_user(hamlet)+ do_deactivate_user(hamlet, acting_user=None)with self.assertRaises(JsonableError):access_user_by_id(iago, hamlet.id, for_admin=False)with self.assertRaises(JsonableError):@@ -1158,7 +1158,7 @@ class UserProfileTest(ZulipTestCase):# We verify that get_accounts_for_email don't return deactivated users accountsuser = self.example_user("hamlet")- do_deactivate_user(user)+ do_deactivate_user(user, acting_user=None)email = self.example_email("hamlet")accounts = get_accounts_for_email(email)with self.assertRaises(AssertionError):@@ -1326,7 +1326,7 @@ class UserProfileTest(ZulipTestCase):class ActivateTest(ZulipTestCase):def test_basics(self) -> None:user = self.example_user("hamlet")- do_deactivate_user(user)+ do_deactivate_user(user, acting_user=None)self.assertFalse(user.is_active)do_reactivate_user(user, acting_user=None)self.assertTrue(user.is_active)@@ -1407,7 +1407,7 @@ class ActivateTest(ZulipTestCase):delay=datetime.timedelta(hours=1),)self.assertEqual(ScheduledEmail.objects.count(), 1)- do_deactivate_user(user)+ do_deactivate_user(user, acting_user=None)self.assertEqual(ScheduledEmail.objects.count(), 0)def test_send_future_email_with_multiple_recipients(self) -> None:diff --git a/zproject/backends.py b/zproject/backends.pyindex 42efbe9cc8a..0d88f459a2c 100644--- a/zproject/backends.py+++ b/zproject/backends.py@@ -903,7 +903,7 @@ class ZulipLDAPUserPopulator(ZulipLDAPAuthBackendBase):"Deactivating user %s because they are disabled in LDAP.",user.delivery_email,)- do_deactivate_user(user)+ do_deactivate_user(user, acting_user=None)# Do an early return to avoid trying to sync additional data.return (user, built)elif not user.is_active:@@ -949,7 +949,7 @@ def sync_user_from_ldap(user_profile: UserProfile, logger: logging.Logger) -> boif settings.LDAP_DEACTIVATE_NON_MATCHING_USERS is Noneelse settings.LDAP_DEACTIVATE_NON_MATCHING_USERS):- do_deactivate_user(user_profile)+ do_deactivate_user(user_profile, acting_user=None)logger.info("Deactivated non-matching user: %s", user_profile.delivery_email)return Trueelif user_profile.is_active: