do_change_full_name: Noop if value isn't really changing.

In do_change_full_name, we shouldn't create RealmAuditLog
entry and sends events if the value isn't changing.
This commit is contained in:
Prakhar Pratyush
2024-08-21 17:04:55 +05:30
committed by Tim Abbott
parent 6718ff5166
commit bfe7eb6b2c
2 changed files with 26 additions and 0 deletions

View File

@@ -217,6 +217,9 @@ def do_change_full_name(
user_profile: UserProfile, full_name: str, acting_user: UserProfile | None
) -> None:
old_name = user_profile.full_name
if old_name == full_name:
return
user_profile.full_name = full_name
user_profile.save(update_fields=["full_name"])
event_time = timezone_now()

View File

@@ -2079,9 +2079,32 @@ class NormalActionsTest(BaseAction):
)
def test_change_full_name(self) -> None:
now = timezone_now()
with self.verify_action() as events:
do_change_full_name(self.user_profile, "Sir Hamlet", self.user_profile)
check_realm_user_update("events[0]", events[0], "full_name")
self.assertEqual(
RealmAuditLog.objects.filter(
realm=self.user_profile.realm,
event_type=RealmAuditLog.USER_FULL_NAME_CHANGED,
event_time__gte=now,
acting_user=self.user_profile,
).count(),
1,
)
# Verify no operation if the value isn't changing.
with self.verify_action(num_events=0, state_change_expected=False):
do_change_full_name(self.user_profile, "Sir Hamlet", self.user_profile)
self.assertEqual(
RealmAuditLog.objects.filter(
realm=self.user_profile.realm,
event_type=RealmAuditLog.USER_FULL_NAME_CHANGED,
event_time__gte=now,
acting_user=self.user_profile,
).count(),
1,
)
self.set_up_db_for_testing_user_access()
cordelia = self.example_user("cordelia")