From 06fa99e87ca298fd4d3405935194fdf53b3969cb Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Sun, 11 Aug 2024 22:38:04 +0200 Subject: [PATCH] do_change_user_delivery_email: Add acting_user kwarg. This is standard for our do_change_... functions. --- zerver/actions/user_settings.py | 6 ++++-- zerver/lib/scim.py | 2 +- zerver/management/commands/change_user_email.py | 2 +- zerver/tests/test_audit_log.py | 2 +- zerver/tests/test_events.py | 14 ++++++++++---- zerver/views/development/email_log.py | 2 +- zerver/views/user_settings.py | 2 +- 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/zerver/actions/user_settings.py b/zerver/actions/user_settings.py index 8f5454f0d5..6a78d0c6b8 100644 --- a/zerver/actions/user_settings.py +++ b/zerver/actions/user_settings.py @@ -109,7 +109,9 @@ def send_delivery_email_update_events( @transaction.atomic(savepoint=False) -def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) -> None: +def do_change_user_delivery_email( + user_profile: UserProfile, new_email: str, *, acting_user: UserProfile | None +) -> None: delete_user_profile_caches([user_profile], user_profile.realm_id) user_profile.delivery_email = new_email @@ -140,7 +142,7 @@ def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) -> event_time = timezone_now() RealmAuditLog.objects.create( realm=user_profile.realm, - acting_user=user_profile, + acting_user=acting_user, modified_user=user_profile, event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time=event_time, diff --git a/zerver/lib/scim.py b/zerver/lib/scim.py index df7975c11f..609002a301 100644 --- a/zerver/lib/scim.py +++ b/zerver/lib/scim.py @@ -317,7 +317,7 @@ class ZulipSCIMUser(SCIMUser): check_change_full_name(self.obj, full_name_new_value, acting_user=None) if email_new_value: - do_change_user_delivery_email(self.obj, email_new_value) + do_change_user_delivery_email(self.obj, email_new_value, acting_user=None) if role_new_value is not None: do_change_user_role(self.obj, role_new_value, acting_user=None) diff --git a/zerver/management/commands/change_user_email.py b/zerver/management/commands/change_user_email.py index c97df0e76f..7a36522dd3 100644 --- a/zerver/management/commands/change_user_email.py +++ b/zerver/management/commands/change_user_email.py @@ -24,4 +24,4 @@ class Command(ZulipBaseCommand): realm = self.get_realm(options) user_profile = self.get_user(old_email, realm) - do_change_user_delivery_email(user_profile, new_email) + do_change_user_delivery_email(user_profile, new_email, acting_user=None) diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index 49c99d0cfc..8a3521712d 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -278,7 +278,7 @@ class TestRealmAuditLog(ZulipTestCase): now = timezone_now() user = self.example_user("hamlet") new_email = "test@example.com" - do_change_user_delivery_email(user, new_email) + do_change_user_delivery_email(user, new_email, acting_user=user) self.assertEqual( RealmAuditLog.objects.filter( event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 0f2ce8cb47..771ca53f7b 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -2161,7 +2161,9 @@ class NormalActionsTest(BaseAction): # for email being passed into this next function. self.user_profile.refresh_from_db() with self.verify_action(num_events=2, client_gravatar=False) as events: - do_change_user_delivery_email(self.user_profile, "newhamlet@zulip.com") + do_change_user_delivery_email( + self.user_profile, "newhamlet@zulip.com", acting_user=self.user_profile + ) check_realm_user_update("events[0]", events[0], "delivery_email") check_realm_user_update("events[1]", events[1], "avatar_fields") @@ -2180,7 +2182,9 @@ class NormalActionsTest(BaseAction): # for email being passed into this next function. self.user_profile.refresh_from_db() with self.verify_action(num_events=3, client_gravatar=False) as events: - do_change_user_delivery_email(self.user_profile, "newhamlet@zulip.com") + do_change_user_delivery_email( + self.user_profile, "newhamlet@zulip.com", acting_user=self.user_profile + ) check_realm_user_update("events[0]", events[0], "delivery_email") check_realm_user_update("events[1]", events[1], "avatar_fields") @@ -2189,7 +2193,9 @@ class NormalActionsTest(BaseAction): assert isinstance(events[1]["person"]["avatar_url_medium"], str) # Reset hamlet's email to original email. - do_change_user_delivery_email(self.user_profile, "hamlet@zulip.com") + do_change_user_delivery_email( + self.user_profile, "hamlet@zulip.com", acting_user=self.user_profile + ) self.set_up_db_for_testing_user_access() cordelia = self.example_user("cordelia") @@ -2201,7 +2207,7 @@ class NormalActionsTest(BaseAction): ) self.user_profile = self.example_user("polonius") with self.verify_action(num_events=0, state_change_expected=False): - do_change_user_delivery_email(cordelia, "newcordelia@zulip.com") + do_change_user_delivery_email(cordelia, "newcordelia@zulip.com", acting_user=None) def test_change_realm_authentication_methods(self) -> None: def fake_backends() -> Any: diff --git a/zerver/views/development/email_log.py b/zerver/views/development/email_log.py index 69199e732f..e7f138e762 100644 --- a/zerver/views/development/email_log.py +++ b/zerver/views/development/email_log.py @@ -135,7 +135,7 @@ def generate_all_emails(request: HttpRequest) -> HttpResponse: assert result.status_code == 200 # Reset the email value so we can run this again - do_change_user_delivery_email(user_profile, registered_email) + do_change_user_delivery_email(user_profile, registered_email, acting_user=None) # Initial email with new account information for normal user send_account_registered_email(user_profile) diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py index 7d3b69d433..eef6dbdbfe 100644 --- a/zerver/views/user_settings.py +++ b/zerver/views/user_settings.py @@ -117,7 +117,7 @@ def confirm_email_change(request: HttpRequest, confirmation_key: str) -> HttpRes return redirect_to_deactivation_notice() validate_email_change_request(user_profile, new_email) - do_change_user_delivery_email(user_profile, new_email) + do_change_user_delivery_email(user_profile, new_email, acting_user=user_profile) user_profile = UserProfile.objects.get(id=email_change_object.user_profile_id) context = {"realm_name": user_profile.realm.name, "new_email": new_email}