Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

sync_user_profile_custom_fields: Eliminate redundant checks.

Mateusz Mandera742f219087ee
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 72853af29b5..48ba979879c 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -7817,37 +7817,6 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase):
).value
self.assertEqual(actual_value, expected_value)
- def test_update_custom_profile_field_no_update(self) -> None:
- hamlet = self.example_user("hamlet")
- phone_number_field = CustomProfileField.objects.get(realm=hamlet.realm, name="Phone number")
- birthday_field = CustomProfileField.objects.get(realm=hamlet.realm, name="Birthday")
- phone_number_field_value = CustomProfileFieldValue.objects.get(
- user_profile=hamlet, field=phone_number_field
- )
- phone_number_field_value.value = "123456789"
- phone_number_field_value.save(update_fields=["value"])
- expected_call_args = [
- hamlet,
- [
- {
- "id": birthday_field.id,
- "value": "1900-09-08",
- },
- ],
- ]
- with (
- self.settings(
- AUTH_LDAP_USER_ATTR_MAP={
- "full_name": "cn",
- "custom_profile_field__birthday": "birthDate",
- "custom_profile_field__phone_number": "homePhone",
- }
- ),
- mock.patch("zproject.backends.do_update_user_custom_profile_data_if_changed") as f,
- ):
- self.perform_ldap_sync(self.example_user("hamlet"))
- f.assert_called_once_with(*expected_call_args)
-
def test_update_custom_profile_field_not_present_in_ldap(self) -> None:
hamlet = self.example_user("hamlet")
no_op_field = CustomProfileField.objects.get(realm=hamlet.realm, name="Birthday")
diff --git a/zproject/backends.py b/zproject/backends.py
index 974aaef5d5d..6e517b9aaa7 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -1716,19 +1716,12 @@ def sync_user_profile_custom_fields(
var_name = "_".join(field.name.lower().split(" "))
fields_by_var_name[var_name] = field
- existing_values = {}
- for data in user_profile.profile_data():
- var_name = "_".join(data["name"].lower().split(" "))
- existing_values[var_name] = data["value"]
-
profile_data: list[ProfileDataElementUpdateDict] = []
for var_name, value in custom_field_name_to_value.items():
try:
field = fields_by_var_name[var_name]
except KeyError:
raise SyncUserError(f"Custom profile field with name {var_name} not found.")
- if existing_values.get(var_name) == value:
- continue
try:
validate_user_custom_profile_field(user_profile.realm.id, field, value)
except ValidationError as error: