zerver/lib/events.py: Fix custom field values not updated in apply_events.

Custom profile field value are stored in different structure compare to
other profile fields in events, so generic way to update fields wasn't
updating custom profile fields in `apply_event` function.

Fix this by adding check for custom fields in `apply_event`.

This also adds the appropriate test_events test to verify this code path.

Fixes part of #9875.
This commit is contained in:
Yashashvi Dave
2018-07-09 15:19:08 +05:30
committed by Tim Abbott
parent 0c5dadf320
commit 649fccde6b
2 changed files with 29 additions and 1 deletions

View File

@@ -56,7 +56,7 @@ def get_raw_user_data(realm_id: int, client_gravatar: bool) -> Dict[int, Dict[st
# TODO: Consider optimizing this query away with caching.
custom_profile_field_values = CustomProfileFieldValue.objects.filter(user_profile__realm_id=realm_id)
profiles_by_user_id = defaultdict(dict) # type: Dict[int, Dict[str, Any]]
for profile_field in custom_profile_field_values: # nocoverage # TODO: Fix this.
for profile_field in custom_profile_field_values:
user_id = profile_field.user_profile_id
profiles_by_user_id[user_id][profile_field.field_id] = profile_field.value
@@ -385,6 +385,10 @@ def apply_event(state: Dict[str, Any],
for field in p:
if field in person:
p[field] = person[field]
if 'custom_profile_field' in person:
custom_field_id = person['custom_profile_field']['id']
custom_field_new_value = person['custom_profile_field']['value']
p['profile_data'][custom_field_id] = custom_field_new_value
elif event['type'] == 'realm_bot':
if event['op'] == 'add':

View File

@@ -91,6 +91,7 @@ from zerver.lib.actions import (
bulk_add_members_to_user_group,
remove_members_from_user_group,
check_delete_user_group,
do_update_user_custom_profile_data,
)
from zerver.lib.events import (
apply_events,
@@ -997,6 +998,29 @@ class EventsRegisterTest(ZulipTestCase):
error = schema_checker('events[0]', events[0])
self.assert_on_error(error)
def test_custom_profile_field_data_events(self) -> None:
schema_checker = self.check_events_dict([
('type', equals('realm_user')),
('op', equals('update')),
('person', check_dict_only([
('user_id', check_int),
('custom_profile_field', check_dict_only([
('id', check_int),
('value', check_none_or(check_string)),
])),
])),
])
realm = get_realm("zulip")
field_id = realm.customprofilefield_set.get(realm=realm, name='Biography').id
field = {
"id": field_id,
"value": "New value",
}
events = self.do_test(lambda: do_update_user_custom_profile_data(self.user_profile, [field]))
error = schema_checker('events[0]', events[0])
self.assert_on_error(error)
def test_presence_events(self) -> None:
schema_checker = self.check_events_dict([
('type', equals('presence')),