From 4b00e5da7262ea51bebe8fb37237b6a113b1c0b1 Mon Sep 17 00:00:00 2001 From: shanukun Date: Fri, 2 Apr 2021 20:41:25 +0530 Subject: [PATCH] refactor: Make acting_user a mandatory kwarg for do_deactivate_realm. --- analytics/tests/test_views.py | 2 +- analytics/views.py | 2 +- corporate/tests/test_stripe.py | 4 ++-- zerver/lib/actions.py | 4 ++-- zerver/management/commands/deactivate_realm.py | 2 +- zerver/management/commands/export.py | 2 +- zerver/tests/test_audit_log.py | 5 +++-- zerver/tests/test_auth_backends.py | 8 ++++---- zerver/tests/test_decorators.py | 4 ++-- zerver/tests/test_email_mirror.py | 2 +- zerver/tests/test_events.py | 2 +- zerver/tests/test_realm.py | 14 +++++++------- zerver/tests/test_signup.py | 4 ++-- zerver/views/realm.py | 2 +- 14 files changed, 29 insertions(+), 28 deletions(-) diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.py index 1f04e28b71..30f0f75755 100644 --- a/analytics/tests/test_views.py +++ b/analytics/tests/test_views.py @@ -985,7 +985,7 @@ class TestSupportEndpoint(ZulipTestCase): result = self.client_post( "/activity/support", {"realm_id": f"{lear_realm.id}", "status": "deactivated"} ) - m.assert_called_once_with(lear_realm, self.example_user("iago")) + m.assert_called_once_with(lear_realm, acting_user=self.example_user("iago")) self.assert_in_success_response(["lear deactivated"], result) with mock.patch("analytics.views.do_send_realm_reactivation_email") as m: diff --git a/analytics/views.py b/analytics/views.py index c7a0e4a51a..41759e4e87 100644 --- a/analytics/views.py +++ b/analytics/views.py @@ -1312,7 +1312,7 @@ def support(request: HttpRequest) -> HttpResponse: "success_message" ] = f"Realm reactivation email sent to admins of {realm.string_id}." elif status == "deactivated": - do_deactivate_realm(realm, request.user) + do_deactivate_realm(realm, acting_user=request.user) context["success_message"] = f"{realm.string_id} deactivated." elif request.POST.get("billing_method", None) is not None: billing_method = request.POST.get("billing_method") diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index 28797d6ec4..b999b6a9fd 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -2271,7 +2271,7 @@ class StripeTest(StripeTestCase): self.assertEqual(last_ledger_entry.licenses, 20) self.assertEqual(last_ledger_entry.licenses_at_next_renewal, 20) - do_deactivate_realm(get_realm("zulip")) + do_deactivate_realm(get_realm("zulip"), acting_user=None) plan.refresh_from_db() self.assertTrue(get_realm("zulip").deactivated) @@ -2302,7 +2302,7 @@ class StripeTest(StripeTestCase): with patch("corporate.lib.stripe.timezone_now", return_value=self.now): self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token") - do_deactivate_realm(get_realm("zulip")) + do_deactivate_realm(get_realm("zulip"), acting_user=None) self.assertTrue(get_realm("zulip").deactivated) do_reactivate_realm(get_realm("zulip")) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 67ce96ac85..e08d6c5606 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -968,7 +968,7 @@ def do_set_realm_signup_notifications_stream( send_event(realm, event, active_user_ids(realm.id)) -def do_deactivate_realm(realm: Realm, acting_user: Optional[UserProfile] = None) -> None: +def do_deactivate_realm(realm: Realm, *, acting_user: Optional[UserProfile]) -> None: """ Deactivate this realm. Do NOT deactivate the users -- we need to be able to tell the difference between users that were intentionally deactivated, @@ -1048,7 +1048,7 @@ def do_change_realm_subdomain(realm: Realm, new_subdomain: str) -> None: # it's deactivated redirect to new_subdomain so that we can tell the users that # the realm has been moved to a new subdomain. placeholder_realm = do_create_realm(old_subdomain, "placeholder-realm") - do_deactivate_realm(placeholder_realm) + do_deactivate_realm(placeholder_realm, acting_user=None) do_add_deactivated_redirect(placeholder_realm, realm.uri) diff --git a/zerver/management/commands/deactivate_realm.py b/zerver/management/commands/deactivate_realm.py index 1de4daa701..084b595a48 100644 --- a/zerver/management/commands/deactivate_realm.py +++ b/zerver/management/commands/deactivate_realm.py @@ -28,5 +28,5 @@ class Command(ZulipBaseCommand): exit(0) print("Deactivating", options["realm_id"]) - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) print("Done!") diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py index 93341b2316..51977bc57c 100644 --- a/zerver/management/commands/export.py +++ b/zerver/management/commands/export.py @@ -204,7 +204,7 @@ class Command(ZulipBaseCommand): if options["deactivate_realm"]: print(f"\033[94mDeactivating realm\033[0m: {realm.string_id}") - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) def percent_callback(bytes_transferred: Any) -> None: sys.stdout.write(".") diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index 45d4a2cd82..a32c31166e 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -305,9 +305,10 @@ class TestRealmAuditLog(ZulipTestCase): def test_realm_activation(self) -> None: realm = get_realm("zulip") - do_deactivate_realm(realm) + user = self.example_user("desdemona") + do_deactivate_realm(realm, acting_user=user) log_entry = RealmAuditLog.objects.get( - realm=realm, event_type=RealmAuditLog.REALM_DEACTIVATED + realm=realm, event_type=RealmAuditLog.REALM_DEACTIVATED, acting_user=user ) extra_data = orjson.loads(log_entry.extra_data) self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT]) diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index 7af6eb9283..f66ddfbe8e 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -182,7 +182,7 @@ class AuthBackendTest(ZulipTestCase): self.assertEqual(user_profile, result) # Verify auth fails with a deactivated realm - do_deactivate_realm(user_profile.realm) + do_deactivate_realm(user_profile.realm, acting_user=None) self.assertIsNone(backend.authenticate(**good_kwargs)) # Verify auth works again after reactivating the realm @@ -3821,7 +3821,7 @@ class FetchAPIKeyTest(ZulipTestCase): self.assert_json_error_contains(result, "Your account has been disabled", 403) def test_deactivated_realm(self) -> None: - do_deactivate_realm(self.user_profile.realm) + do_deactivate_realm(self.user_profile.realm, acting_user=None) result = self.client_post( "/api/v1/fetch_api_key", dict(username=self.email, password=initial_password(self.email)), @@ -3859,7 +3859,7 @@ class DevFetchAPIKeyTest(ZulipTestCase): self.assert_json_error_contains(result, "Your account has been disabled", 403) def test_deactivated_realm(self) -> None: - do_deactivate_realm(self.user_profile.realm) + do_deactivate_realm(self.user_profile.realm, acting_user=None) result = self.client_post("/api/v1/dev_fetch_api_key", dict(username=self.email)) self.assert_json_error_contains(result, "This organization has been deactivated", 403) @@ -5073,7 +5073,7 @@ class TestLDAP(ZulipLDAPTestCase): with self.settings(AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map): backend = self.backend email = "nonexisting@zulip.com" - do_deactivate_realm(backend._realm) + do_deactivate_realm(backend._realm, acting_user=None) with self.assertRaisesRegex(Exception, "Realm has been deactivated"): backend.get_or_build_user(email, _LDAPUser()) diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py index a12914bb78..8788a3e043 100644 --- a/zerver/tests/test_decorators.py +++ b/zerver/tests/test_decorators.py @@ -1037,7 +1037,7 @@ class DeactivatedRealmTest(ZulipTestCase): """ realm = get_realm("zulip") - do_deactivate_realm(get_realm("zulip")) + do_deactivate_realm(get_realm("zulip"), acting_user=None) result = self.client_post( "/json/messages", @@ -1107,7 +1107,7 @@ class DeactivatedRealmTest(ZulipTestCase): Using a webhook while in a deactivated realm fails """ - do_deactivate_realm(get_realm("zulip")) + do_deactivate_realm(get_realm("zulip"), acting_user=None) user_profile = self.example_user("hamlet") 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.py index a719899b83..edfa922c79 100644 --- a/zerver/tests/test_email_mirror.py +++ b/zerver/tests/test_email_mirror.py @@ -1078,7 +1078,7 @@ class TestMissedMessageEmailMessages(ZulipTestCase): mm_address = create_missed_message_address(user_profile, message) - do_deactivate_realm(user_profile.realm) + do_deactivate_realm(user_profile.realm, 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.py index 982ec02f01..daf1550c70 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -1499,7 +1499,7 @@ class NormalActionsTest(BaseAction): def test_do_deactivate_realm(self) -> None: realm = self.user_profile.realm - action = lambda: do_deactivate_realm(realm) + action = lambda: do_deactivate_realm(realm, acting_user=None) # We delete sessions of all active users when a realm is # deactivated, and redirect them to a deactivated page in diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index fb1274ba94..4b862bc727 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -180,7 +180,7 @@ class RealmTest(ZulipTestCase): hamlet_id = self.example_user("hamlet").id get_user_profile_by_id(hamlet_id) realm = get_realm("zulip") - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) user = get_user_profile_by_id(hamlet_id) self.assertTrue(user.realm.deactivated) @@ -210,7 +210,7 @@ class RealmTest(ZulipTestCase): delay=datetime.timedelta(hours=1), ) self.assertEqual(ScheduledEmail.objects.count(), 1) - do_deactivate_realm(user.realm) + do_deactivate_realm(user.realm, acting_user=None) self.assertEqual(ScheduledEmail.objects.count(), 0) def test_do_change_realm_description_clears_cached_descriptions(self) -> None: @@ -234,10 +234,10 @@ class RealmTest(ZulipTestCase): realm = get_realm("zulip") self.assertFalse(realm.deactivated) - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) self.assertTrue(realm.deactivated) - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) self.assertTrue(realm.deactivated) def test_do_set_deactivated_redirect_on_deactivated_realm(self) -> None: @@ -245,7 +245,7 @@ class RealmTest(ZulipTestCase): realm = get_realm("zulip") redirect_url = "new_server.zulip.com" - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) self.assertTrue(realm.deactivated) do_add_deactivated_redirect(realm, redirect_url) self.assertEqual(realm.deactivated_redirect, redirect_url) @@ -257,7 +257,7 @@ class RealmTest(ZulipTestCase): def test_realm_reactivation_link(self) -> None: realm = get_realm("zulip") - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) self.assertTrue(realm.deactivated) confirmation_url = create_confirmation_link(realm, Confirmation.REALM_REACTIVATION) response = self.client_get(confirmation_url) @@ -269,7 +269,7 @@ class RealmTest(ZulipTestCase): def test_realm_reactivation_confirmation_object(self) -> None: realm = get_realm("zulip") - do_deactivate_realm(realm) + do_deactivate_realm(realm, acting_user=None) self.assertTrue(realm.deactivated) create_confirmation_link(realm, Confirmation.REALM_REACTIVATION) confirmation = Confirmation.objects.last() diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index bfff8c8ecd..414e4a8a4b 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -467,7 +467,7 @@ class PasswordResetTest(ZulipTestCase): def test_password_reset_with_deactivated_realm(self) -> None: user_profile = self.example_user("hamlet") email = user_profile.delivery_email - do_deactivate_realm(user_profile.realm) + do_deactivate_realm(user_profile.realm, acting_user=None) # start the password reset process by supplying an email address with self.assertLogs(level="INFO") as m: @@ -4779,7 +4779,7 @@ class TestFindMyTeam(ZulipTestCase): self.assertEqual(len(outbox), 0) def test_find_team_deactivated_realm(self) -> None: - do_deactivate_realm(get_realm("zulip")) + do_deactivate_realm(get_realm("zulip"), 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/views/realm.py b/zerver/views/realm.py index 3aafe3d028..6cb49e5d86 100644 --- a/zerver/views/realm.py +++ b/zerver/views/realm.py @@ -236,7 +236,7 @@ def update_realm( @has_request_variables def deactivate_realm(request: HttpRequest, user: UserProfile) -> HttpResponse: realm = user.realm - do_deactivate_realm(realm, user) + do_deactivate_realm(realm, acting_user=user) return json_success()