refactor: Make acting_user a mandatory kwarg for do_deactivate_realm.

This commit is contained in:
shanukun 2021-04-02 20:41:25 +05:30 committed by Tim Abbott
parent 626cf52723
commit 4b00e5da72
14 changed files with 29 additions and 28 deletions

View File

@ -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:

View File

@ -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")

View File

@ -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"))

View File

@ -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)

View File

@ -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!")

View File

@ -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(".")

View File

@ -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])

View File

@ -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())

View File

@ -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"

View File

@ -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")

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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()