Skip to content

Conversation

@0xthedance
Copy link
Collaborator

This adds a helper method that wraps do_change_user_role for use in tests. This is a preparatory commit for PR #36208.

In PR #36208, a variable notify is added to do_change_user_role. This PR will make it easy to update all the tests with notify=False.

Updates all existing test files to use the new helper method instead of calling do_change_user_role directly.

Self-review checklist
  • Self-reviewed the changes for clarity and maintainability
    (variable names, code reuse, readability, etc.).

Communicate decisions, questions, and potential concerns.

  • Explains differences from previous plans (e.g., issue description).
  • Highlights technical choices and bugs encountered.
  • Calls out remaining decisions and concerns.
  • Automated tests verify logic where appropriate.

Individual commits are ready for review (see commit discipline).

  • Each commit is a coherent idea.
  • Commit message(s) explain reasoning and motivation for changes.

Completed manual review and testing of the following:

  • Visual appearance of the changes.
  • Responsiveness and internationalization.
  • Strings and tooltips.
  • End-to-end functionality of buttons, interactions and flows.
  • Corner cases, error conditions, and easily imagined bugs.

do_change_user_role(hamlet, UserProfile.ROLE_REALM_OWNER, acting_user=None)
do_change_user_role(iago, UserProfile.ROLE_REALM_OWNER, acting_user=None)
self.set_user_role(hamlet, UserProfile.ROLE_REALM_OWNER, acting_user=None)
self.set_user_role(iago, UserProfile.ROLE_REALM_OWNER, acting_user=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent was for this new test helper to have acting_user=None be the default, so all these tests don't need to spell that out either.

@0xthedance 0xthedance force-pushed the test_helper_change_role branch from 7697d6d to abac33f Compare December 11, 2025 20:59
@timabbott
Copy link
Member

@0xthedance just a small note: You should always post a comment explaining how you addressed the feedback on GitHub PR threads; that can help a lot with explicit handoffs and results in faster review.

# It is a good idea for your tests to clearly demonstrate a
# **change** to a value. So here we want to make sure that
# do_change_user_role will change Hamlet such that
# self.set_user_role will change Hamlet such that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks to be incorrectly changed.

@timabbott
Copy link
Member

Looks great, merging with these changes:

diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py
index 64af88a707..cef674a94f 100644
--- a/zerver/lib/test_classes.py
+++ b/zerver/lib/test_classes.py
@@ -2369,6 +2369,16 @@ class ZulipTestCase(ZulipTestCaseMixin, TestCase):
         )
 
     def set_user_role(self, user: UserProfile, role: int) -> None:
+        """
+        Test helper for switching a user to a given role. Hardcodes
+        acting_user=None, which means the change is treated as
+        though it was done by a management command, not another
+        user.
+
+        Tests using this should consider using users who have the
+        appropriate initial role; this is usually more readable and
+        a bit faster.
+        """
         do_change_user_role(user, role, acting_user=None)
 
 
diff --git a/zerver/tests/test_example.py b/zerver/tests/test_example.py
index b19d03dd6e..f74b5d892f 100644
--- a/zerver/tests/test_example.py
+++ b/zerver/tests/test_example.py
@@ -60,7 +60,7 @@ class TestBasicUserStuff(ZulipTestCase):
 
         # It is a good idea for your tests to clearly demonstrate a
         # **change** to a value.  So here we want to make sure that
-        # self.set_user_role will change Hamlet such that
+        # do_change_user_role will change Hamlet such that
         # is_administrator_role becomes True, but we first assert it's
         # False.
         self.assertFalse(is_administrator_role(hamlet.role))

This commit adds a set_user_role helper method to ZulipTestCase with
acting_user defaulting to None, and updates all test files to use this
helper.

This is a prep commit for PR zulip#36208.
@timabbott timabbott force-pushed the test_helper_change_role branch from abac33f to 1b82537 Compare December 12, 2025 17:31
@timabbott timabbott merged commit 1b82537 into zulip:main Dec 12, 2025
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants