Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made acting_user a mandatory keyword argument where present. #15958

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analytics/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def test_activate_or_deactivate_realm(self) -> None:

with mock.patch("analytics.views.do_deactivate_realm") as m:
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 & Co. deactivated"], result)

with mock.patch("analytics.views.do_send_realm_reactivation_email") as m:
Expand Down
2 changes: 1 addition & 1 deletion analytics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ def support(request: HttpRequest) -> HttpResponse:
do_send_realm_reactivation_email(realm)
context["message"] = f"Realm reactivation email sent to admins of {realm.name}."
elif status == "deactivated":
do_deactivate_realm(realm, request.user)
do_deactivate_realm(realm, acting_user=request.user)
context["message"] = f"{realm.name} deactivated."
elif request.POST.get("sponsorship_pending", None) is not None:
sponsorship_pending = request.POST.get("sponsorship_pending")
Expand Down
64 changes: 32 additions & 32 deletions zerver/lib/actions.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion zerver/management/commands/bulk_change_user_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ def handle(self, *args: Any, **options: str) -> None:
user_profile = self.get_user(email, realm)
old_name = user_profile.full_name
print(f"{email}: {old_name} -> {new_name}")
do_change_full_name(user_profile, new_name, None)
do_change_full_name(user_profile, new_name, acting_user=None)
except CommandError:
print(f"e-mail {email} doesn't exist in the realm {realm}, skipping")
4 changes: 2 additions & 2 deletions zerver/tests/test_audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ def test_change_bot_owner(self) -> None:
admin = self.example_user('iago')
bot = self.notification_bot()
bot_owner = self.example_user('hamlet')
do_change_bot_owner(bot, bot_owner, admin)
do_change_bot_owner(bot, bot_owner, acting_user=admin)
self.assertEqual(RealmAuditLog.objects.filter(event_type=RealmAuditLog.USER_BOT_OWNER_CHANGED,
event_time__gte=now).count(), 1)
self.assertEqual(bot_owner, bot.bot_owner)

def test_regenerate_api_key(self) -> None:
now = timezone_now()
user = self.example_user('hamlet')
do_regenerate_api_key(user, user)
do_regenerate_api_key(user, acting_user=user)
self.assertEqual(RealmAuditLog.objects.filter(event_type=RealmAuditLog.USER_API_KEY_CHANGED,
event_time__gte=now).count(), 1)
self.assertTrue(user.api_key)
Expand Down
4 changes: 2 additions & 2 deletions zerver/tests/test_auth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4593,11 +4593,11 @@ def test_user_in_multiple_realms(self) -> None:
)

self.change_ldap_user_attr('hamlet', 'cn', 'Second Hamlet')
expected_call_args = [hamlet2, 'Second Hamlet', None]
expected_call_args = [hamlet2, 'Second Hamlet']
with self.settings(AUTH_LDAP_USER_ATTR_MAP={'full_name': 'cn'}):
with mock.patch('zerver.lib.actions.do_change_full_name') as f:
self.perform_ldap_sync(hamlet2)
f.assert_called_once_with(*expected_call_args)
f.assert_called_once_with(*expected_call_args, acting_user=None)

# Get the updated model and make sure the full name is changed correctly:
hamlet2 = get_user_by_delivery_email(email, test_realm)
Expand Down
12 changes: 6 additions & 6 deletions zerver/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ def test_change_full_name(self) -> None:
lambda: do_change_full_name(
self.user_profile,
'Sir Hamlet',
self.user_profile))
acting_user=self.user_profile))
check_realm_user_update('events[0]', events[0], {'full_name'})

def test_change_user_delivery_email_email_address_visibilty_admins(self) -> None:
Expand Down Expand Up @@ -1466,13 +1466,13 @@ def test_create_bot(self) -> None:

def test_change_bot_full_name(self) -> None:
bot = self.create_bot('test')
action = lambda: do_change_full_name(bot, 'New Bot Name', self.user_profile)
action = lambda: do_change_full_name(bot, 'New Bot Name', acting_user=self.user_profile)
events = self.verify_action(action, num_events=2)
check_realm_bot_update('events[1]', events[1], 'full_name')

def test_regenerate_bot_api_key(self) -> None:
bot = self.create_bot('test')
action = lambda: do_regenerate_api_key(bot, self.user_profile)
action = lambda: do_regenerate_api_key(bot, acting_user=self.user_profile)
events = self.verify_action(action)
check_realm_bot_update('events[0]', events[0], 'api_key')

Expand Down Expand Up @@ -1559,23 +1559,23 @@ def test_change_bot_owner(self) -> None:
self.user_profile = self.example_user('iago')
owner = self.example_user('hamlet')
bot = self.create_bot('test')
action = lambda: do_change_bot_owner(bot, owner, self.user_profile)
action = lambda: do_change_bot_owner(bot, owner, acting_user=self.user_profile)
events = self.verify_action(action, num_events=2)
check_realm_bot_update('events[0]', events[0], 'owner_id')
check_realm_user_update('events[1]', events[1], {"bot_owner_id"})

self.user_profile = self.example_user('aaron')
owner = self.example_user('hamlet')
bot = self.create_bot('test1', full_name='Test1 Testerson')
action = lambda: do_change_bot_owner(bot, owner, self.user_profile)
action = lambda: do_change_bot_owner(bot, owner, acting_user=self.user_profile)
events = self.verify_action(action, num_events=2)
check_realm_bot_delete('events[0]', events[0])
check_realm_user_update('events[1]', events[1], {"bot_owner_id"})

previous_owner = self.example_user('aaron')
self.user_profile = self.example_user('hamlet')
bot = self.create_test_bot('test2', previous_owner, full_name='Test2 Testerson')
action = lambda: do_change_bot_owner(bot, self.user_profile, previous_owner)
action = lambda: do_change_bot_owner(bot, self.user_profile, acting_user=previous_owner)
events = self.verify_action(action, num_events=2)
check_realm_bot_add('events[0]', events[0])
check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
Expand Down
4 changes: 2 additions & 2 deletions zerver/tests/test_push_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def test_push_bouncer_api(self, mock_request: Any) -> None:
with mock.patch('zerver.worker.queue_processors.clear_push_device_tokens',
side_effect=PushNotificationBouncerRetryLaterError("test")), \
mock.patch('zerver.worker.queue_processors.retry_event') as mock_retry:
do_regenerate_api_key(user, user)
do_regenerate_api_key(user, acting_user=user)
mock_retry.assert_called()

# We didn't manage to communicate with the bouncer, to the tokens are still there:
Expand All @@ -395,7 +395,7 @@ def test_push_bouncer_api(self, mock_request: Any) -> None:
self.assertEqual(len(tokens), 2)

# Now we successfully remove them:
do_regenerate_api_key(user, user)
do_regenerate_api_key(user, acting_user=user)
tokens = list(RemotePushDeviceToken.objects.filter(user_id=user.id,
server=server))
self.assertEqual(len(tokens), 0)
Expand Down
2 changes: 1 addition & 1 deletion zerver/tests/test_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ def test_invite_links_in_name(self) -> None:
hamlet = self.example_user("hamlet")
self.login_user(hamlet)
# Test we properly handle links in user full names
do_change_full_name(hamlet, "</a> https://www.google.com", hamlet)
do_change_full_name(hamlet, "</a> https://www.google.com", acting_user=hamlet)

result = self.invite('newuser@zulip.com', ["Denmark"])
self.assert_json_success(result)
Expand Down
2 changes: 1 addition & 1 deletion zerver/views/realm.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,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()

@require_safe
Expand Down
2 changes: 1 addition & 1 deletion zerver/views/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def accounts_register(request: HttpRequest) -> HttpResponse:
user_profile = existing_user_profile
do_activate_user(user_profile, acting_user=user_profile)
do_change_password(user_profile, password)
do_change_full_name(user_profile, full_name, user_profile)
do_change_full_name(user_profile, full_name, acting_user=user_profile)
do_set_user_display_setting(user_profile, 'timezone', timezone)
# TODO: When we clean up the `do_activate_user` code path,
# make it respect invited_as_admin / is_realm_admin.
Expand Down
4 changes: 2 additions & 2 deletions zerver/views/user_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def json_change_settings(request: HttpRequest, user_profile: UserProfile,
pass
else:
# Note that check_change_full_name strips the passed name automatically
result['full_name'] = check_change_full_name(user_profile, full_name, user_profile)
result['full_name'] = check_change_full_name(user_profile, full_name, acting_user=user_profile)

return json_success(result)

Expand Down Expand Up @@ -271,7 +271,7 @@ def delete_avatar_backend(request: HttpRequest, user_profile: UserProfile) -> Ht
# a bot regenerating its own API key.
@has_request_variables
def regenerate_api_key(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
new_api_key = do_regenerate_api_key(user_profile, user_profile)
new_api_key = do_regenerate_api_key(user_profile, acting_user=user_profile)
json_result = dict(
api_key = new_api_key,
)
Expand Down
8 changes: 4 additions & 4 deletions zerver/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def update_user_backend(
full_name.strip() != ""):
# We don't respect `name_changes_disabled` here because the request
# is on behalf of the administrator.
check_change_full_name(target, full_name, user_profile)
check_change_full_name(target, full_name, acting_user=user_profile)

if profile_data is not None:
clean_profile_data = []
Expand Down Expand Up @@ -229,7 +229,7 @@ def patch_bot_backend(
bot = access_bot_by_id(user_profile, bot_id)

if full_name is not None:
check_change_bot_full_name(bot, full_name, user_profile)
check_change_bot_full_name(bot, full_name, acting_user=user_profile)
if bot_owner_id is not None:
try:
owner = get_user_profile_by_id_in_realm(bot_owner_id, user_profile.realm)
Expand All @@ -242,7 +242,7 @@ def patch_bot_backend(

previous_owner = bot.bot_owner
if previous_owner != owner:
do_change_bot_owner(bot, owner, user_profile)
do_change_bot_owner(bot, owner, acting_user=user_profile)

if default_sending_stream is not None:
if default_sending_stream == "":
Expand Down Expand Up @@ -302,7 +302,7 @@ def patch_bot_backend(
def regenerate_bot_api_key(request: HttpRequest, user_profile: UserProfile, bot_id: int) -> HttpResponse:
bot = access_bot_by_id(user_profile, bot_id)

new_api_key = do_regenerate_api_key(bot, user_profile)
new_api_key = do_regenerate_api_key(bot, acting_user=user_profile)
json_result = dict(
api_key=new_api_key,
)
Expand Down
2 changes: 1 addition & 1 deletion zproject/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def sync_full_name_from_ldap(self, user_profile: UserProfile,
full_name = check_full_name(full_name)
except JsonableError as e:
raise ZulipLDAPException(e.msg)
do_change_full_name(user_profile, full_name, None)
do_change_full_name(user_profile, full_name, acting_user=None)

def sync_custom_profile_fields_from_ldap(self, user_profile: UserProfile,
ldap_user: _LDAPUser) -> None:
Expand Down