events: Fix broken custom_profile_fields events logic.

I noticed this because the test_events.py tests had the extremely
weird pattern of calling the actual change function, and then testing
the `notify` function's state changes (which should always be noops),
rather than actually testing the state change function.

Fixing the test made it clear that the actual logic in events.py
simply did not handle deleting custom_profile_field_value elements
from user objects when a custom_profile_field object was deleted.

So we fix that bit of logic as well.

It appears this bug was unique -- at least we don't have any other
notify_* functions being used directly in test_events.py, and the
handful of state_change_expected=False entries are all events for data
not present in page_params.
This commit is contained in:
Tim Abbott
2021-03-26 10:03:27 -07:00
committed by Tim Abbott
parent bc2d58ad4a
commit 96c61a1a41
2 changed files with 15 additions and 15 deletions

View File

@@ -530,6 +530,16 @@ def apply_event(
state["hotspots"] = event["hotspots"]
elif event["type"] == "custom_profile_fields":
state["custom_profile_fields"] = event["fields"]
custom_profile_field_ids = {field["id"] for field in state["custom_profile_fields"]}
if "raw_users" in state:
for user_dict in state["raw_users"].values():
if "profile_data" not in user_dict:
continue
profile_data = user_dict["profile_data"]
for (field_id, field_data) in list(profile_data.items()):
if int(field_id) not in custom_profile_field_ids:
del profile_data[field_id]
elif event["type"] == "realm_user":
person = event["person"]
person_user_id = person["user_id"]