From 4b6794660548908b17300928b6c75a38a10e9144 Mon Sep 17 00:00:00 2001 From: shanukun Date: Sat, 6 Feb 2021 18:57:06 +0530 Subject: [PATCH] refactor: Make acting_user a mandatory kwarg for do_create_user. --- analytics/tests/test_counts.py | 30 ++++++++++++++----- corporate/tests/test_stripe.py | 2 +- tools/test-api | 5 +--- zerver/lib/actions.py | 3 +- zerver/openapi/curl_param_value_generators.py | 6 +++- zerver/tests/test_auth_backends.py | 11 +++---- zerver/tests/test_digest.py | 1 + zerver/tests/test_events.py | 1 + zerver/tests/test_home.py | 6 ++-- zerver/tests/test_hotspots.py | 5 +--- zerver/tests/test_import_export.py | 1 + zerver/tests/test_management_commands.py | 2 +- zerver/tests/test_message_send.py | 4 +++ zerver/tests/test_outgoing_webhook_system.py | 1 + zerver/tests/test_realm_emoji.py | 5 +--- zerver/tests/test_service_bot_system.py | 5 ++++ zerver/tests/test_signup.py | 1 + zerver/tests/test_users.py | 3 ++ 18 files changed, 57 insertions(+), 35 deletions(-) diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.py index 7b892a7e6f..579391ed8b 100644 --- a/analytics/tests/test_counts.py +++ b/analytics/tests/test_counts.py @@ -1309,7 +1309,9 @@ class TestLoggingCountStats(AnalyticsTestCase): def test_active_users_log_by_is_bot(self) -> None: property = "active_users_log:is_bot:day" - user = do_create_user("email", "password", self.default_realm, "full_name") + user = do_create_user( + "email", "password", self.default_realm, "full_name", acting_user=None + ) self.assertEqual( 1, RealmCount.objects.filter(property=property, subgroup=False).aggregate(Sum("value"))[ @@ -1644,10 +1646,18 @@ class TestActiveUsersAudit(AnalyticsTestCase): self.assertTableState(UserCount, ["user", "subgroup"], [[user1, "false"], [user2, "false"]]) def test_end_to_end_with_actions_dot_py(self) -> None: - user1 = do_create_user("email1", "password", self.default_realm, "full_name") - user2 = do_create_user("email2", "password", self.default_realm, "full_name") - user3 = do_create_user("email3", "password", self.default_realm, "full_name") - user4 = do_create_user("email4", "password", self.default_realm, "full_name") + user1 = do_create_user( + "email1", "password", self.default_realm, "full_name", acting_user=None + ) + user2 = do_create_user( + "email2", "password", self.default_realm, "full_name", acting_user=None + ) + user3 = do_create_user( + "email3", "password", self.default_realm, "full_name", acting_user=None + ) + user4 = do_create_user( + "email4", "password", self.default_realm, "full_name", acting_user=None + ) do_deactivate_user(user2) do_activate_user(user3) do_reactivate_user(user4) @@ -1761,9 +1771,13 @@ class TestRealmActiveHumans(AnalyticsTestCase): ) def test_end_to_end(self) -> None: - user1 = do_create_user("email1", "password", self.default_realm, "full_name") - user2 = do_create_user("email2", "password", self.default_realm, "full_name") - do_create_user("email3", "password", self.default_realm, "full_name") + user1 = do_create_user( + "email1", "password", self.default_realm, "full_name", acting_user=None + ) + user2 = do_create_user( + "email2", "password", self.default_realm, "full_name", acting_user=None + ) + do_create_user("email3", "password", self.default_realm, "full_name", acting_user=None) time_zero = floor_to_day(timezone_now()) + self.DAY update_user_activity_interval(user1, time_zero) update_user_activity_interval(user2, time_zero) diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index 4ed30496e8..95e7ac9573 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -2710,7 +2710,7 @@ class LicenseLedgerTest(StripeTestCase): def test_user_changes(self) -> None: self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token") - user = do_create_user("email", "password", get_realm("zulip"), "name") + user = do_create_user("email", "password", get_realm("zulip"), "name", acting_user=None) do_deactivate_user(user) do_reactivate_user(user) # Not a proper use of do_activate_user, but fine for this test diff --git a/tools/test-api b/tools/test-api index 1f6ddb1cbc..0af88172cd 100755 --- a/tools/test-api +++ b/tools/test-api @@ -73,10 +73,7 @@ with test_server_running(force=options.force, external_host="zulipdev.com:9981") # Prepare the non-admin client email = "guest@zulip.com" # guest is not an admin guest_user = do_create_user( - "guest@zulip.com", - "secret", - get_realm("zulip"), - "Mr. Guest", + "guest@zulip.com", "secret", get_realm("zulip"), "Mr. Guest", acting_user=None ) api_key = get_api_key(guest_user) nonadmin_client = Client( diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index b4b10ef550..a39f83943f 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -614,7 +614,8 @@ def do_create_user( default_stream_groups: Sequence[DefaultStreamGroup] = [], source_profile: Optional[UserProfile] = None, realm_creation: bool = False, - acting_user: Optional[UserProfile] = None, + *, + acting_user: Optional[UserProfile], ) -> UserProfile: user_profile = create_user( diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py index b32da7b1c2..71fb467e63 100644 --- a/zerver/openapi/curl_param_value_generators.py +++ b/zerver/openapi/curl_param_value_generators.py @@ -271,6 +271,10 @@ def upload_custom_emoji() -> Dict[str, object]: @openapi_param_value_generator(["/users/{user_id}:delete"]) def deactivate_user() -> Dict[str, object]: user_profile = do_create_user( - email="testuser@zulip.com", password=None, full_name="test_user", realm=get_realm("zulip") + email="testuser@zulip.com", + password=None, + full_name="test_user", + realm=get_realm("zulip"), + acting_user=None, ) return {"user_id": user_profile.id} diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index f277328edd..b9370815fe 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -3245,7 +3245,9 @@ class GitHubAuthBackendTest(SocialAuthBase): # Now we create the user account with the noreply email and verify that it's # possible to sign in to it. realm = get_realm("zulip") - do_create_user(noreply_email, "password", realm, account_data_dict["name"]) + do_create_user( + noreply_email, "password", realm, account_data_dict["name"], acting_user=None + ) result = self.social_auth_test( account_data_dict, subdomain="zulip", @@ -5349,12 +5351,7 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase): test_realm = do_create_realm("test", "test", False) hamlet = self.example_user("hamlet") email = hamlet.delivery_email - hamlet2 = do_create_user( - email, - None, - test_realm, - hamlet.full_name, - ) + hamlet2 = do_create_user(email, None, test_realm, hamlet.full_name, acting_user=None) self.change_ldap_user_attr("hamlet", "cn", "Second Hamlet") expected_call_args = [hamlet2, "Second Hamlet", None] diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.py index 409ee28d78..99ace90524 100644 --- a/zerver/tests/test_digest.py +++ b/zerver/tests/test_digest.py @@ -384,6 +384,7 @@ class TestDigestEmailMessages(ZulipTestCase): realm, "some_bot", bot_type=UserProfile.DEFAULT_BOT, + acting_user=None, ) # Check that bots are not sent emails diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index bcf3dd0ace..dfecda37ec 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -667,6 +667,7 @@ class NormalActionsTest(BaseAction): self.user_profile.realm, "full name", prereg_user=prereg_user, + acting_user=None, ), state_change_expected=True, num_events=4, diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index 1c2e57e31e..c2cda7a4ba 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -487,15 +487,13 @@ class HomeTest(ZulipTestCase): full_name=bot_name, bot_type=UserProfile.DEFAULT_BOT, bot_owner=owner, + acting_user=None, ) return user def create_non_active_user(self, realm: Realm, email: str, name: str) -> UserProfile: user = do_create_user( - email=email, - password="123", - realm=realm, - full_name=name, + email=email, password="123", realm=realm, full_name=name, acting_user=None ) # Doing a full-stack deactivation would be expensive here, diff --git a/zerver/tests/test_hotspots.py b/zerver/tests/test_hotspots.py index 1ac264e777..f421364972 100644 --- a/zerver/tests/test_hotspots.py +++ b/zerver/tests/test_hotspots.py @@ -12,10 +12,7 @@ class TestGetNextHotspots(ZulipTestCase): def setUp(self) -> None: super().setUp() self.user = do_create_user( - "user@zulip.com", - "password", - get_realm("zulip"), - "user", + "user@zulip.com", "password", get_realm("zulip"), "user", acting_user=None ) def test_first_hotspot(self) -> None: diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py index f73de71cea..32f77ebf40 100644 --- a/zerver/tests/test_import_export.py +++ b/zerver/tests/test_import_export.py @@ -818,6 +818,7 @@ class ImportExportTest(ZulipTestCase): full_name="bot", bot_type=UserProfile.EMBEDDED_BOT, bot_owner=sample_user, + acting_user=None, ) storage = StateHandler(bot_profile) storage.put("some key", "some value") diff --git a/zerver/tests/test_management_commands.py b/zerver/tests/test_management_commands.py index d5c0a50577..f3277e9c7a 100644 --- a/zerver/tests/test_management_commands.py +++ b/zerver/tests/test_management_commands.py @@ -78,7 +78,7 @@ class TestZulipBaseCommand(ZulipTestCase): with self.assertRaisesRegex(CommandError, "server does not contain a user with email"): self.command.get_user("invalid_email@example.com", None) - do_create_user(email, "password", mit_realm, "full_name") + do_create_user(email, "password", mit_realm, "full_name", acting_user=None) with self.assertRaisesRegex(CommandError, "server contains multiple users with that email"): self.command.get_user(email, None) diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py index 93d4e8c116..5382a24062 100644 --- a/zerver/tests/test_message_send.py +++ b/zerver/tests/test_message_send.py @@ -210,6 +210,7 @@ class MessagePOSTTest(ZulipTestCase): realm=non_admin_profile.realm, full_name="freebot", bot_type=UserProfile.DEFAULT_BOT, + acting_user=None, ) self._send_and_verify_message( bot_without_owner, @@ -297,6 +298,7 @@ class MessagePOSTTest(ZulipTestCase): realm=non_admin_profile.realm, full_name="freebot", bot_type=UserProfile.DEFAULT_BOT, + acting_user=None, ) self._send_and_verify_message( bot_without_owner, stream_name, "New members cannot send to this stream." @@ -1560,6 +1562,7 @@ class StreamMessagesTest(ZulipTestCase): full_name="Normal Bot", bot_type=UserProfile.DEFAULT_BOT, bot_owner=cordelia, + acting_user=None, ) content = "test @**Normal Bot** rules" @@ -2305,6 +2308,7 @@ class CheckMessageTest(ZulipTestCase): full_name="", bot_type=UserProfile.DEFAULT_BOT, bot_owner=parent, + acting_user=None, ) bot.last_reminder = None diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.py index f1c5b7406d..01b178085c 100644 --- a/zerver/tests/test_outgoing_webhook_system.py +++ b/zerver/tests/test_outgoing_webhook_system.py @@ -245,6 +245,7 @@ class TestOutgoingWebhookMessaging(ZulipTestCase): email="whatever", realm=bot_owner.realm, password=None, + acting_user=None, ) add_service( diff --git a/zerver/tests/test_realm_emoji.py b/zerver/tests/test_realm_emoji.py index ec361813a5..94d910fa07 100644 --- a/zerver/tests/test_realm_emoji.py +++ b/zerver/tests/test_realm_emoji.py @@ -268,10 +268,7 @@ class RealmEmojiTest(ZulipTestCase): # having same name can be administered independently. realm_1 = do_create_realm("test_realm", "test_realm") emoji_author_1 = do_create_user( - "abc@example.com", - password="abc", - realm=realm_1, - full_name="abc", + "abc@example.com", password="abc", realm=realm_1, full_name="abc", acting_user=None ) self.create_test_emoji("test_emoji", emoji_author_1) diff --git a/zerver/tests/test_service_bot_system.py b/zerver/tests/test_service_bot_system.py index f5f03dab6d..775f80318a 100644 --- a/zerver/tests/test_service_bot_system.py +++ b/zerver/tests/test_service_bot_system.py @@ -30,6 +30,7 @@ class TestServiceBotBasics(ZulipTestCase): full_name="BarBot", bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_owner=self.example_user("cordelia"), + acting_user=None, ) return outgoing_bot @@ -178,6 +179,7 @@ class TestServiceBotStateHandler(ZulipTestCase): full_name="EmbeddedBo1", bot_type=UserProfile.EMBEDDED_BOT, bot_owner=self.user_profile, + acting_user=None, ) self.second_bot_profile = do_create_user( email="embedded-bot-2@zulip.com", @@ -186,6 +188,7 @@ class TestServiceBotStateHandler(ZulipTestCase): full_name="EmbeddedBot2", bot_type=UserProfile.EMBEDDED_BOT, bot_owner=self.user_profile, + acting_user=None, ) def test_basic_storage_and_retrieval(self) -> None: @@ -436,6 +439,7 @@ class TestServiceBotEventTriggers(ZulipTestCase): full_name="FooBot", bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_owner=self.user_profile, + acting_user=None, ) self.second_bot_profile = do_create_user( email="bar-bot@zulip.com", @@ -444,6 +448,7 @@ class TestServiceBotEventTriggers(ZulipTestCase): full_name="BarBot", bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_owner=self.user_profile, + acting_user=None, ) @for_all_bot_types diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 00ac297773..36db670ce3 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -1786,6 +1786,7 @@ so we didn't send them an invitation. We did send invitations to everyone else!" self.user_profile.realm, "full name", prereg_user=prereg_user, + acting_user=None, ) accepted_invite = PreregistrationUser.objects.filter( diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index 589817a6af..37282388d4 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -808,6 +808,7 @@ class QueryCountTest(ZulipTestCase): realm=realm, full_name="Fred Flintstone", prereg_user=prereg_user, + acting_user=None, ) self.assert_length(queries, 68) @@ -1658,6 +1659,7 @@ class RecipientInfoTest(ZulipTestCase): realm=realm, full_name="", bot_type=UserProfile.EMBEDDED_BOT, + acting_user=None, ) info = get_recipient_info( @@ -1680,6 +1682,7 @@ class RecipientInfoTest(ZulipTestCase): realm=realm, full_name="", bot_type=UserProfile.DEFAULT_BOT, + acting_user=None, ) info = get_recipient_info(