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.

scim: Run user update operations in a single transaction.

bhuvan-somisettyfc6d57b80129
diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py
index 96b90b58541..35100ddfbd7 100644
--- a/tools/linter_lib/custom_check.py
+++ b/tools/linter_lib/custom_check.py
@@ -497,6 +497,10 @@ python_rules = RuleList(
"zproject/backends.py",
"@transaction.atomic(savepoint=True) # intentional use of savepoint=True",
),
+ (
+ "zerver/lib/scim.py",
+ "with transaction.atomic(savepoint=True): # intentional use of savepoint=True",
+ ),
},
},
*whitespace_rules,
diff --git a/zerver/lib/scim.py b/zerver/lib/scim.py
index a80320dc0da..bfb64da6674 100644
--- a/zerver/lib/scim.py
+++ b/zerver/lib/scim.py
@@ -399,30 +399,29 @@ class ZulipSCIMUser(SCIMUser):
)
return
- # TODO: The below operations should ideally be executed in a single
- # atomic block to avoid failing with partial changes getting saved.
- # This can be fixed once we figure out how do_deactivate_user can be run
- # inside an atomic block.
+ # We create a savepoint here, unlike the savepoint=False
+ # convention followed by the action functions below, so that a
+ # failure rolls back only our own changes when this runs nested
+ # inside an outer atomic block during tests in Django's TestCase.
+ with transaction.atomic(savepoint=True): # intentional use of savepoint=True
+ if full_name_new_value:
+ check_change_full_name(self.obj, full_name_new_value, acting_user=None)
- # We process full_name first here, since it's the only one that can fail.
- if full_name_new_value:
- check_change_full_name(self.obj, full_name_new_value, acting_user=None)
+ if email_new_value is not None:
+ do_change_user_delivery_email(self.obj, email_new_value, acting_user=None)
- if email_new_value is not None:
- do_change_user_delivery_email(self.obj, email_new_value, acting_user=None)
-
- if role_new_value is not None:
- do_change_user_role(self.obj, role_new_value, acting_user=None, notify=True)
+ if role_new_value is not None:
+ do_change_user_role(self.obj, role_new_value, acting_user=None, notify=True)
- if is_active_new_value is not None and is_active_new_value:
- do_reactivate_user(self.obj, acting_user=None)
- elif is_active_new_value is not None and not is_active_new_value:
- do_deactivate_user(self.obj, acting_user=None)
+ if is_active_new_value is not None and is_active_new_value:
+ do_reactivate_user(self.obj, acting_user=None)
+ elif is_active_new_value is not None and not is_active_new_value:
+ do_deactivate_user(self.obj, acting_user=None)
- if custom_profile_data:
- do_update_user_custom_profile_data_if_changed(
- self.obj, custom_profile_data, None, notify=True
- )
+ if custom_profile_data:
+ do_update_user_custom_profile_data_if_changed(
+ self.obj, custom_profile_data, None, notify=True
+ )
def delete(self) -> None:
"""
diff --git a/zerver/tests/test_scim.py b/zerver/tests/test_scim.py
index 78f89c337b8..503321aa3c6 100644
--- a/zerver/tests/test_scim.py
+++ b/zerver/tests/test_scim.py
@@ -687,6 +687,40 @@ class TestSCIMUser(SCIMTestCase):
expected_response_schema = self.generate_user_schema(hamlet)
self.assertEqual(output_data, expected_response_schema)
+ def test_put_change_fails_atomically(self) -> None:
+ """
+ If one of the changes requested in a SCIM update fails partway
+ through, none of the other changes in that same request should
+ be persisted either.
+ """
+ hamlet = self.example_user("hamlet")
+ original_email = hamlet.delivery_email
+ self.assertEqual(hamlet.role, UserProfile.ROLE_MEMBER)
+
+ # This payload changes both the email and the role. We simulate
+ # the role change failing after the email change has already run,
+ # to verify the email change gets rolled back too.
+ payload = {
+ "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
+ "id": hamlet.id,
+ "userName": "[email protected]",
+ "role": "administrator",
+ }
+ with (
+ mock.patch(
+ "zerver.lib.scim.do_change_user_role", side_effect=Exception("simulated failure")
+ ),
+ self.assertLogs("django_scim.views", "ERROR") as mock_scim_logger,
+ self.assertLogs("django.request", "ERROR"),
+ ):
+ result = self.json_put(f"/scim/v2/Users/{hamlet.id}", payload, **self.scim_headers())
+ self.assertEqual(result.status_code, 500)
+ self.assertIn("simulated failure", mock_scim_logger.output[0])
+
+ hamlet.refresh_from_db()
+ self.assertEqual(hamlet.delivery_email, original_email)
+ self.assertEqual(hamlet.role, UserProfile.ROLE_MEMBER)
+
def test_put_deactivate_reactivate_user(self) -> None:
hamlet = self.example_user("hamlet")
# This payload flips the active attribute to deactivate the user.