events: Add test to remove existing value for custom profile field.

Adds a test for when a value for a user's custom profile field is
removed and not set to a new value. The omission of this event in
the tests was noted as a possibility in #22103, which updated the
API documentation for these events having `null` for the field
value.

When adding the test discovered that the events logic was not
deleting the field from the user object and instead setting it to
`None`, so fixes that logic as well. There was a similar bug fixed
in commit 96c61a1a41 for when custom profile fields are removed
from a realm.
This commit is contained in:
Lauryn Menard 2023-07-31 19:39:57 +02:00 committed by Tim Abbott
parent c1cd5c527c
commit 195e5b8dc1
3 changed files with 15 additions and 5 deletions

View File

@ -1068,7 +1068,7 @@ check_realm_user_remove = make_checker(realm_user_remove_event)
custom_profile_field_type = DictType( custom_profile_field_type = DictType(
required_keys=[ required_keys=[
("id", int), ("id", int),
("value", str), ("value", OptionalType(str)),
], ],
optional_keys=[ optional_keys=[
("rendered_value", str), ("rendered_value", str),

View File

@ -956,15 +956,17 @@ def apply_event(
p["is_billing_admin"] = person["is_billing_admin"] p["is_billing_admin"] = person["is_billing_admin"]
if "custom_profile_field" in person: if "custom_profile_field" in person:
custom_field_id = person["custom_profile_field"]["id"] custom_field_id = str(person["custom_profile_field"]["id"])
custom_field_new_value = person["custom_profile_field"]["value"] custom_field_new_value = person["custom_profile_field"]["value"]
if "rendered_value" in person["custom_profile_field"]: if custom_field_new_value is None and "profile_data" in p:
p["profile_data"][str(custom_field_id)] = { p["profile_data"].pop(custom_field_id, None)
elif "rendered_value" in person["custom_profile_field"]:
p["profile_data"][custom_field_id] = {
"value": custom_field_new_value, "value": custom_field_new_value,
"rendered_value": person["custom_profile_field"]["rendered_value"], "rendered_value": person["custom_profile_field"]["rendered_value"],
} }
else: else:
p["profile_data"][str(custom_field_id)] = { p["profile_data"][custom_field_id] = {
"value": custom_field_new_value, "value": custom_field_new_value,
} }

View File

@ -24,6 +24,7 @@ from zerver.actions.bots import (
) )
from zerver.actions.create_user import do_create_user, do_reactivate_user from zerver.actions.create_user import do_create_user, do_reactivate_user
from zerver.actions.custom_profile_fields import ( from zerver.actions.custom_profile_fields import (
check_remove_custom_profile_field_value,
do_remove_realm_custom_profile_field, do_remove_realm_custom_profile_field,
do_update_user_custom_profile_data_if_changed, do_update_user_custom_profile_data_if_changed,
try_add_realm_custom_profile_field, try_add_realm_custom_profile_field,
@ -1126,6 +1127,13 @@ class NormalActionsTest(BaseAction):
check_realm_user_update("events[0]", events[0], "custom_profile_field") check_realm_user_update("events[0]", events[0], "custom_profile_field")
self.assertEqual(events[0]["person"]["custom_profile_field"].keys(), {"id", "value"}) self.assertEqual(events[0]["person"]["custom_profile_field"].keys(), {"id", "value"})
# Test event for removing custom profile data
events = self.verify_action(
lambda: check_remove_custom_profile_field_value(self.user_profile, field_id)
)
check_realm_user_update("events[0]", events[0], "custom_profile_field")
self.assertEqual(events[0]["person"]["custom_profile_field"].keys(), {"id", "value"})
def test_presence_events(self) -> None: def test_presence_events(self) -> None:
events = self.verify_action( events = self.verify_action(
lambda: do_update_user_presence( lambda: do_update_user_presence(