refactor: Make acting_user a mandatory kwarg for do_create_user.

This commit is contained in:
shanukun 2021-02-06 18:57:06 +05:30 committed by Tim Abbott
parent 673af19a4d
commit 4b67946605
18 changed files with 57 additions and 35 deletions

View File

@ -1309,7 +1309,9 @@ class TestLoggingCountStats(AnalyticsTestCase):
def test_active_users_log_by_is_bot(self) -> None: def test_active_users_log_by_is_bot(self) -> None:
property = "active_users_log:is_bot:day" 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( self.assertEqual(
1, 1,
RealmCount.objects.filter(property=property, subgroup=False).aggregate(Sum("value"))[ 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"]]) self.assertTableState(UserCount, ["user", "subgroup"], [[user1, "false"], [user2, "false"]])
def test_end_to_end_with_actions_dot_py(self) -> None: def test_end_to_end_with_actions_dot_py(self) -> None:
user1 = do_create_user("email1", "password", self.default_realm, "full_name") user1 = do_create_user(
user2 = do_create_user("email2", "password", self.default_realm, "full_name") "email1", "password", self.default_realm, "full_name", acting_user=None
user3 = do_create_user("email3", "password", self.default_realm, "full_name") )
user4 = do_create_user("email4", "password", self.default_realm, "full_name") 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_deactivate_user(user2)
do_activate_user(user3) do_activate_user(user3)
do_reactivate_user(user4) do_reactivate_user(user4)
@ -1761,9 +1771,13 @@ class TestRealmActiveHumans(AnalyticsTestCase):
) )
def test_end_to_end(self) -> None: def test_end_to_end(self) -> None:
user1 = do_create_user("email1", "password", self.default_realm, "full_name") user1 = do_create_user(
user2 = do_create_user("email2", "password", self.default_realm, "full_name") "email1", "password", self.default_realm, "full_name", acting_user=None
do_create_user("email3", "password", self.default_realm, "full_name") )
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 time_zero = floor_to_day(timezone_now()) + self.DAY
update_user_activity_interval(user1, time_zero) update_user_activity_interval(user1, time_zero)
update_user_activity_interval(user2, time_zero) update_user_activity_interval(user2, time_zero)

View File

@ -2710,7 +2710,7 @@ class LicenseLedgerTest(StripeTestCase):
def test_user_changes(self) -> None: def test_user_changes(self) -> None:
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, "token") 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_deactivate_user(user)
do_reactivate_user(user) do_reactivate_user(user)
# Not a proper use of do_activate_user, but fine for this test # Not a proper use of do_activate_user, but fine for this test

View File

@ -73,10 +73,7 @@ with test_server_running(force=options.force, external_host="zulipdev.com:9981")
# Prepare the non-admin client # Prepare the non-admin client
email = "guest@zulip.com" # guest is not an admin email = "guest@zulip.com" # guest is not an admin
guest_user = do_create_user( guest_user = do_create_user(
"guest@zulip.com", "guest@zulip.com", "secret", get_realm("zulip"), "Mr. Guest", acting_user=None
"secret",
get_realm("zulip"),
"Mr. Guest",
) )
api_key = get_api_key(guest_user) api_key = get_api_key(guest_user)
nonadmin_client = Client( nonadmin_client = Client(

View File

@ -614,7 +614,8 @@ def do_create_user(
default_stream_groups: Sequence[DefaultStreamGroup] = [], default_stream_groups: Sequence[DefaultStreamGroup] = [],
source_profile: Optional[UserProfile] = None, source_profile: Optional[UserProfile] = None,
realm_creation: bool = False, realm_creation: bool = False,
acting_user: Optional[UserProfile] = None, *,
acting_user: Optional[UserProfile],
) -> UserProfile: ) -> UserProfile:
user_profile = create_user( user_profile = create_user(

View File

@ -271,6 +271,10 @@ def upload_custom_emoji() -> Dict[str, object]:
@openapi_param_value_generator(["/users/{user_id}:delete"]) @openapi_param_value_generator(["/users/{user_id}:delete"])
def deactivate_user() -> Dict[str, object]: def deactivate_user() -> Dict[str, object]:
user_profile = do_create_user( 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} return {"user_id": user_profile.id}

View File

@ -3245,7 +3245,9 @@ class GitHubAuthBackendTest(SocialAuthBase):
# Now we create the user account with the noreply email and verify that it's # Now we create the user account with the noreply email and verify that it's
# possible to sign in to it. # possible to sign in to it.
realm = get_realm("zulip") 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( result = self.social_auth_test(
account_data_dict, account_data_dict,
subdomain="zulip", subdomain="zulip",
@ -5349,12 +5351,7 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase):
test_realm = do_create_realm("test", "test", False) test_realm = do_create_realm("test", "test", False)
hamlet = self.example_user("hamlet") hamlet = self.example_user("hamlet")
email = hamlet.delivery_email email = hamlet.delivery_email
hamlet2 = do_create_user( hamlet2 = do_create_user(email, None, test_realm, hamlet.full_name, acting_user=None)
email,
None,
test_realm,
hamlet.full_name,
)
self.change_ldap_user_attr("hamlet", "cn", "Second Hamlet") self.change_ldap_user_attr("hamlet", "cn", "Second Hamlet")
expected_call_args = [hamlet2, "Second Hamlet", None] expected_call_args = [hamlet2, "Second Hamlet", None]

View File

@ -384,6 +384,7 @@ class TestDigestEmailMessages(ZulipTestCase):
realm, realm,
"some_bot", "some_bot",
bot_type=UserProfile.DEFAULT_BOT, bot_type=UserProfile.DEFAULT_BOT,
acting_user=None,
) )
# Check that bots are not sent emails # Check that bots are not sent emails

View File

@ -667,6 +667,7 @@ class NormalActionsTest(BaseAction):
self.user_profile.realm, self.user_profile.realm,
"full name", "full name",
prereg_user=prereg_user, prereg_user=prereg_user,
acting_user=None,
), ),
state_change_expected=True, state_change_expected=True,
num_events=4, num_events=4,

View File

@ -487,15 +487,13 @@ class HomeTest(ZulipTestCase):
full_name=bot_name, full_name=bot_name,
bot_type=UserProfile.DEFAULT_BOT, bot_type=UserProfile.DEFAULT_BOT,
bot_owner=owner, bot_owner=owner,
acting_user=None,
) )
return user return user
def create_non_active_user(self, realm: Realm, email: str, name: str) -> UserProfile: def create_non_active_user(self, realm: Realm, email: str, name: str) -> UserProfile:
user = do_create_user( user = do_create_user(
email=email, email=email, password="123", realm=realm, full_name=name, acting_user=None
password="123",
realm=realm,
full_name=name,
) )
# Doing a full-stack deactivation would be expensive here, # Doing a full-stack deactivation would be expensive here,

View File

@ -12,10 +12,7 @@ class TestGetNextHotspots(ZulipTestCase):
def setUp(self) -> None: def setUp(self) -> None:
super().setUp() super().setUp()
self.user = do_create_user( self.user = do_create_user(
"user@zulip.com", "user@zulip.com", "password", get_realm("zulip"), "user", acting_user=None
"password",
get_realm("zulip"),
"user",
) )
def test_first_hotspot(self) -> None: def test_first_hotspot(self) -> None:

View File

@ -818,6 +818,7 @@ class ImportExportTest(ZulipTestCase):
full_name="bot", full_name="bot",
bot_type=UserProfile.EMBEDDED_BOT, bot_type=UserProfile.EMBEDDED_BOT,
bot_owner=sample_user, bot_owner=sample_user,
acting_user=None,
) )
storage = StateHandler(bot_profile) storage = StateHandler(bot_profile)
storage.put("some key", "some value") storage.put("some key", "some value")

View File

@ -78,7 +78,7 @@ class TestZulipBaseCommand(ZulipTestCase):
with self.assertRaisesRegex(CommandError, "server does not contain a user with email"): with self.assertRaisesRegex(CommandError, "server does not contain a user with email"):
self.command.get_user("invalid_email@example.com", None) 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"): with self.assertRaisesRegex(CommandError, "server contains multiple users with that email"):
self.command.get_user(email, None) self.command.get_user(email, None)

View File

@ -210,6 +210,7 @@ class MessagePOSTTest(ZulipTestCase):
realm=non_admin_profile.realm, realm=non_admin_profile.realm,
full_name="freebot", full_name="freebot",
bot_type=UserProfile.DEFAULT_BOT, bot_type=UserProfile.DEFAULT_BOT,
acting_user=None,
) )
self._send_and_verify_message( self._send_and_verify_message(
bot_without_owner, bot_without_owner,
@ -297,6 +298,7 @@ class MessagePOSTTest(ZulipTestCase):
realm=non_admin_profile.realm, realm=non_admin_profile.realm,
full_name="freebot", full_name="freebot",
bot_type=UserProfile.DEFAULT_BOT, bot_type=UserProfile.DEFAULT_BOT,
acting_user=None,
) )
self._send_and_verify_message( self._send_and_verify_message(
bot_without_owner, stream_name, "New members cannot send to this stream." bot_without_owner, stream_name, "New members cannot send to this stream."
@ -1560,6 +1562,7 @@ class StreamMessagesTest(ZulipTestCase):
full_name="Normal Bot", full_name="Normal Bot",
bot_type=UserProfile.DEFAULT_BOT, bot_type=UserProfile.DEFAULT_BOT,
bot_owner=cordelia, bot_owner=cordelia,
acting_user=None,
) )
content = "test @**Normal Bot** rules" content = "test @**Normal Bot** rules"
@ -2305,6 +2308,7 @@ class CheckMessageTest(ZulipTestCase):
full_name="", full_name="",
bot_type=UserProfile.DEFAULT_BOT, bot_type=UserProfile.DEFAULT_BOT,
bot_owner=parent, bot_owner=parent,
acting_user=None,
) )
bot.last_reminder = None bot.last_reminder = None

View File

@ -245,6 +245,7 @@ class TestOutgoingWebhookMessaging(ZulipTestCase):
email="whatever", email="whatever",
realm=bot_owner.realm, realm=bot_owner.realm,
password=None, password=None,
acting_user=None,
) )
add_service( add_service(

View File

@ -268,10 +268,7 @@ class RealmEmojiTest(ZulipTestCase):
# having same name can be administered independently. # having same name can be administered independently.
realm_1 = do_create_realm("test_realm", "test_realm") realm_1 = do_create_realm("test_realm", "test_realm")
emoji_author_1 = do_create_user( emoji_author_1 = do_create_user(
"abc@example.com", "abc@example.com", password="abc", realm=realm_1, full_name="abc", acting_user=None
password="abc",
realm=realm_1,
full_name="abc",
) )
self.create_test_emoji("test_emoji", emoji_author_1) self.create_test_emoji("test_emoji", emoji_author_1)

View File

@ -30,6 +30,7 @@ class TestServiceBotBasics(ZulipTestCase):
full_name="BarBot", full_name="BarBot",
bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_type=UserProfile.OUTGOING_WEBHOOK_BOT,
bot_owner=self.example_user("cordelia"), bot_owner=self.example_user("cordelia"),
acting_user=None,
) )
return outgoing_bot return outgoing_bot
@ -178,6 +179,7 @@ class TestServiceBotStateHandler(ZulipTestCase):
full_name="EmbeddedBo1", full_name="EmbeddedBo1",
bot_type=UserProfile.EMBEDDED_BOT, bot_type=UserProfile.EMBEDDED_BOT,
bot_owner=self.user_profile, bot_owner=self.user_profile,
acting_user=None,
) )
self.second_bot_profile = do_create_user( self.second_bot_profile = do_create_user(
email="embedded-bot-2@zulip.com", email="embedded-bot-2@zulip.com",
@ -186,6 +188,7 @@ class TestServiceBotStateHandler(ZulipTestCase):
full_name="EmbeddedBot2", full_name="EmbeddedBot2",
bot_type=UserProfile.EMBEDDED_BOT, bot_type=UserProfile.EMBEDDED_BOT,
bot_owner=self.user_profile, bot_owner=self.user_profile,
acting_user=None,
) )
def test_basic_storage_and_retrieval(self) -> None: def test_basic_storage_and_retrieval(self) -> None:
@ -436,6 +439,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):
full_name="FooBot", full_name="FooBot",
bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_type=UserProfile.OUTGOING_WEBHOOK_BOT,
bot_owner=self.user_profile, bot_owner=self.user_profile,
acting_user=None,
) )
self.second_bot_profile = do_create_user( self.second_bot_profile = do_create_user(
email="bar-bot@zulip.com", email="bar-bot@zulip.com",
@ -444,6 +448,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):
full_name="BarBot", full_name="BarBot",
bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_type=UserProfile.OUTGOING_WEBHOOK_BOT,
bot_owner=self.user_profile, bot_owner=self.user_profile,
acting_user=None,
) )
@for_all_bot_types @for_all_bot_types

View File

@ -1786,6 +1786,7 @@ so we didn't send them an invitation. We did send invitations to everyone else!"
self.user_profile.realm, self.user_profile.realm,
"full name", "full name",
prereg_user=prereg_user, prereg_user=prereg_user,
acting_user=None,
) )
accepted_invite = PreregistrationUser.objects.filter( accepted_invite = PreregistrationUser.objects.filter(

View File

@ -808,6 +808,7 @@ class QueryCountTest(ZulipTestCase):
realm=realm, realm=realm,
full_name="Fred Flintstone", full_name="Fred Flintstone",
prereg_user=prereg_user, prereg_user=prereg_user,
acting_user=None,
) )
self.assert_length(queries, 68) self.assert_length(queries, 68)
@ -1658,6 +1659,7 @@ class RecipientInfoTest(ZulipTestCase):
realm=realm, realm=realm,
full_name="", full_name="",
bot_type=UserProfile.EMBEDDED_BOT, bot_type=UserProfile.EMBEDDED_BOT,
acting_user=None,
) )
info = get_recipient_info( info = get_recipient_info(
@ -1680,6 +1682,7 @@ class RecipientInfoTest(ZulipTestCase):
realm=realm, realm=realm,
full_name="", full_name="",
bot_type=UserProfile.DEFAULT_BOT, bot_type=UserProfile.DEFAULT_BOT,
acting_user=None,
) )
info = get_recipient_info( info = get_recipient_info(