refactor: Make acting_user a mandatory kwarg for do_set_realm_property.

This commit is contained in:
shanukun 2021-03-01 16:03:24 +05:30 committed by Tim Abbott
parent a2da736181
commit 459710a897
22 changed files with 215 additions and 90 deletions

View File

@ -723,7 +723,10 @@ class TestSupportEndpoint(ZulipTestCase):
self.login("iago")
do_set_realm_property(
get_realm("zulip"), "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY
get_realm("zulip"),
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY,
acting_user=None,
)
customer = Customer.objects.create(realm=get_realm("lear"), stripe_customer_id="cus_123")

View File

@ -293,8 +293,9 @@ active users in a realm.
# zerver/lib/actions.py
def do_set_realm_property(realm: Realm, name: str, value: bool,
acting_user: Optional[UserProfile]=None) -> None:
def do_set_realm_property(
realm: Realm, name: str, value: Any, *, acting_user: Optional[UserProfile]
) -> None:
"""Takes in a realm object, the name of an attribute to update, the
value to update and and the user who initiated the update.
"""

View File

@ -769,7 +769,7 @@ def active_humans_in_realm(realm: Realm) -> Sequence[UserProfile]:
def do_set_realm_property(
realm: Realm, name: str, value: Any, acting_user: Optional[UserProfile] = None
realm: Realm, name: str, value: Any, *, acting_user: Optional[UserProfile]
) -> None:
"""Takes in a realm object, the name of an attribute to update, the
value to update and and the user who initiated the update.

View File

@ -232,7 +232,10 @@ def stdout_suppressed() -> Iterator[IO[str]]:
def reset_emails_in_zulip_realm() -> None:
realm = get_realm("zulip")
do_set_realm_property(
realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE
realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
acting_user=None,
)

View File

@ -1515,7 +1515,7 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase):
def test_social_auth_registration_without_is_signup_closed_realm(self) -> None:
"""If the user doesn't exist yet in closed realm, give an error"""
realm = get_realm("zulip")
do_set_realm_property(realm, "emails_restricted_to_domains", True)
do_set_realm_property(realm, "emails_restricted_to_domains", True, acting_user=None)
email = "nonexisting@phantom.com"
name = "Full Name"
account_data_dict = self.get_account_data_dict(email=email, name=name)
@ -5292,7 +5292,10 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase):
hamlet = self.example_user("hamlet")
realm = get_realm("zulip")
do_set_realm_property(
realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
hamlet.refresh_from_db()
@ -5855,7 +5858,7 @@ class EmailValidatorTestCase(ZulipTestCase):
cordelia = self.example_user("cordelia")
realm = inviter.realm
do_set_realm_property(realm, "emails_restricted_to_domains", True)
do_set_realm_property(realm, "emails_restricted_to_domains", True, acting_user=None)
inviter.realm.refresh_from_db()
error = validate_email_is_valid(
"fred+5555@zulip.com",

View File

@ -327,7 +327,10 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
# email_address_visiblity limited to admins
user = self.example_user("hamlet")
do_set_realm_property(
user.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
user.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
user.refresh_from_db()

View File

@ -1158,7 +1158,10 @@ class FetchAPIKeyTest(ZulipTestCase):
def test_fetch_api_key_email_address_visibility(self) -> None:
user = self.example_user("cordelia")
do_set_realm_property(
user.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
user.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
self.login_user(user)

View File

@ -66,6 +66,7 @@ class EmailChangeTestCase(ZulipTestCase):
user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
acting_user=None,
)
old_email = user_profile.delivery_email
@ -138,7 +139,12 @@ class EmailChangeTestCase(ZulipTestCase):
data = {"email": "hamlet-new@zulip.com"}
user_profile = self.example_user("hamlet")
self.login_user(user_profile)
do_set_realm_property(user_profile.realm, "email_changes_disabled", True)
do_set_realm_property(
user_profile.realm,
"email_changes_disabled",
True,
acting_user=None,
)
url = "/json/settings"
result = self.client_patch(url, data)
self.assertEqual(len(mail.outbox), 0)
@ -179,7 +185,12 @@ class EmailChangeTestCase(ZulipTestCase):
body = email_message.body
self.assertIn("We received a request to change the email", body)
do_set_realm_property(user_profile.realm, "email_changes_disabled", True)
do_set_realm_property(
user_profile.realm,
"email_changes_disabled",
True,
acting_user=None,
)
activation_url = [s for s in body.split("\n") if s][2]
response = self.client_get(activation_url)
@ -207,7 +218,10 @@ class EmailChangeTestCase(ZulipTestCase):
def test_change_delivery_email_end_to_end_with_admins_visibility(self) -> None:
user_profile = self.example_user("hamlet")
do_set_realm_property(
user_profile.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
self.login_user(user_profile)

View File

@ -409,7 +409,10 @@ class FetchInitialStateDataTest(ZulipTestCase):
self.assertFalse(user_profile.is_realm_admin)
do_set_realm_property(
user_profile.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE
user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
acting_user=None,
)
result = fetch_initial_state_data(user_profile)
@ -417,7 +420,10 @@ class FetchInitialStateDataTest(ZulipTestCase):
self.assertNotIn("delivery_email", value)
do_set_realm_property(
user_profile.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
result = fetch_initial_state_data(user_profile)
@ -429,14 +435,20 @@ class FetchInitialStateDataTest(ZulipTestCase):
self.assertTrue(user_profile.is_realm_admin)
do_set_realm_property(
user_profile.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE
user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
acting_user=None,
)
result = fetch_initial_state_data(user_profile)
for key, value in result["raw_users"].items():
self.assertNotIn("delivery_email", value)
do_set_realm_property(
user_profile.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
result = fetch_initial_state_data(user_profile)
for key, value in result["raw_users"].items():

View File

@ -819,6 +819,7 @@ class NormalActionsTest(BaseAction):
self.user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
events = self.verify_action(lambda: self.register("test1@zulip.com", "test1"))
@ -1029,6 +1030,7 @@ class NormalActionsTest(BaseAction):
self.user_profile.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
# Important: We need to refresh from the database here so that
# we don't have a stale UserProfile object with an old value

View File

@ -1706,25 +1706,25 @@ class MarkdownTest(ZulipTestCase):
msg_with_none = markdown_convert_wrapper(text.format("none"))
# Render with default=javascript
do_set_realm_property(realm, "default_code_block_language", "javascript")
do_set_realm_property(realm, "default_code_block_language", "javascript", acting_user=None)
msg_without_language_default_js = markdown_convert_wrapper(text.format(""))
msg_with_python_default_js = markdown_convert_wrapper(text.format("python"))
# Render with default=python
do_set_realm_property(realm, "default_code_block_language", "python")
do_set_realm_property(realm, "default_code_block_language", "python", acting_user=None)
msg_without_language_default_py = markdown_convert_wrapper(text.format(""))
msg_with_none_default_py = markdown_convert_wrapper(text.format("none"))
# Render with default=quote
do_set_realm_property(realm, "default_code_block_language", "quote")
do_set_realm_property(realm, "default_code_block_language", "quote", acting_user=None)
msg_without_language_default_quote = markdown_convert_wrapper(text.format(""))
# Render with default=math
do_set_realm_property(realm, "default_code_block_language", "math")
do_set_realm_property(realm, "default_code_block_language", "math", acting_user=None)
msg_without_language_default_math = markdown_convert_wrapper(text.format(""))
# Render without default language
do_set_realm_property(realm, "default_code_block_language", None)
do_set_realm_property(realm, "default_code_block_language", None, acting_user=None)
msg_without_language_final = markdown_convert_wrapper(text.format(""))
self.assertTrue(msg_with_js == msg_without_language_default_js)
@ -1738,12 +1738,12 @@ class MarkdownTest(ZulipTestCase):
# Test checking inside nested quotes
nested_text = "````quote\n\n{}\n\n{}````".format(text.format("js"), text.format(""))
do_set_realm_property(realm, "default_code_block_language", "javascript")
do_set_realm_property(realm, "default_code_block_language", "javascript", acting_user=None)
rendered = markdown_convert_wrapper(nested_text)
with_language, without_language = re.findall(r"<pre>(.*?)$", rendered, re.MULTILINE)
self.assertTrue(with_language == without_language)
do_set_realm_property(realm, "default_code_block_language", None)
do_set_realm_property(realm, "default_code_block_language", None, acting_user=None)
rendered = markdown_convert_wrapper(nested_text)
with_language, without_language = re.findall(r"<pre>(.*?)$", rendered, re.MULTILINE)
self.assertFalse(with_language == without_language)

View File

@ -246,7 +246,7 @@ class EditMessageTest(ZulipTestCase):
def test_edit_message_history_disabled(self) -> None:
user_profile = self.example_user("hamlet")
do_set_realm_property(user_profile.realm, "allow_edit_history", False)
do_set_realm_property(user_profile.realm, "allow_edit_history", False, acting_user=None)
self.login("hamlet")
# Single-line edit

View File

@ -1639,7 +1639,10 @@ class GetOldMessagesTest(ZulipTestCase):
self.login_user(hamlet)
do_set_realm_property(
hamlet.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE
hamlet.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
acting_user=None,
)
self.send_personal_message(hamlet, self.example_user("iago"))
@ -1654,7 +1657,10 @@ class GetOldMessagesTest(ZulipTestCase):
# Now verify client_gravatar doesn't run with EMAIL_ADDRESS_VISIBILITY_ADMINS
do_set_realm_property(
hamlet.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
hamlet.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
result = self.get_and_check_messages(dict(client_gravatar=orjson.dumps(True).decode()))
message = result["messages"][0]

View File

@ -239,7 +239,7 @@ class MessagePOSTTest(ZulipTestCase):
admin_profile = self.example_user("iago")
self.login_user(admin_profile)
do_set_realm_property(admin_profile.realm, "waiting_period_threshold", 10)
do_set_realm_property(admin_profile.realm, "waiting_period_threshold", 10, acting_user=None)
admin_profile.date_joined = timezone_now() - datetime.timedelta(days=9)
admin_profile.save()
self.assertTrue(admin_profile.is_provisional_member)
@ -1629,12 +1629,18 @@ class StreamMessagesTest(ZulipTestCase):
self.subscribe(shiva, stream_name)
do_set_realm_property(
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_EVERYONE
realm,
"wildcard_mention_policy",
Realm.WILDCARD_MENTION_POLICY_EVERYONE,
acting_user=None,
)
self.send_and_verify_wildcard_mention_message("polonius")
do_set_realm_property(
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_MEMBERS
realm,
"wildcard_mention_policy",
Realm.WILDCARD_MENTION_POLICY_MEMBERS,
acting_user=None,
)
self.send_and_verify_wildcard_mention_message("polonius", test_fails=True)
# There is no restriction on small streams.
@ -1642,9 +1648,12 @@ class StreamMessagesTest(ZulipTestCase):
self.send_and_verify_wildcard_mention_message("cordelia")
do_set_realm_property(
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_FULL_MEMBERS
realm,
"wildcard_mention_policy",
Realm.WILDCARD_MENTION_POLICY_FULL_MEMBERS,
acting_user=None,
)
do_set_realm_property(realm, "waiting_period_threshold", 10)
do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)
iago.date_joined = timezone_now()
iago.save()
shiva.date_joined = timezone_now()
@ -1662,7 +1671,10 @@ class StreamMessagesTest(ZulipTestCase):
self.send_and_verify_wildcard_mention_message("cordelia")
do_set_realm_property(
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_STREAM_ADMINS
realm,
"wildcard_mention_policy",
Realm.WILDCARD_MENTION_POLICY_STREAM_ADMINS,
acting_user=None,
)
# TODO: Change this when we implement stream administrators
self.send_and_verify_wildcard_mention_message("cordelia", test_fails=True)
@ -1673,7 +1685,7 @@ class StreamMessagesTest(ZulipTestCase):
cordelia.date_joined = timezone_now()
cordelia.save()
do_set_realm_property(
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_ADMINS
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_ADMINS, acting_user=None
)
self.send_and_verify_wildcard_mention_message("cordelia", test_fails=True)
# There is no restriction on small streams.
@ -1681,7 +1693,7 @@ class StreamMessagesTest(ZulipTestCase):
self.send_and_verify_wildcard_mention_message("iago")
do_set_realm_property(
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_NOBODY
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_NOBODY, acting_user=None
)
self.send_and_verify_wildcard_mention_message("iago", test_fails=True)
self.send_and_verify_wildcard_mention_message("iago", sub_count=10)
@ -1691,7 +1703,7 @@ class StreamMessagesTest(ZulipTestCase):
self.login_user(cordelia)
self.subscribe(cordelia, "test_stream")
do_set_realm_property(cordelia.realm, "wildcard_mention_policy", 10)
do_set_realm_property(cordelia.realm, "wildcard_mention_policy", 10, acting_user=None)
content = "@**all** test wildcard mention"
with mock.patch("zerver.lib.message.num_subscribers_for_stream_id", return_value=16):
with self.assertRaisesRegex(AssertionError, "Invalid wildcard mention policy"):
@ -1865,7 +1877,10 @@ class PersonalMessageSendTest(ZulipTestCase):
user_profile = self.example_user("hamlet")
self.login_user(user_profile)
do_set_realm_property(
user_profile.realm, "private_message_policy", Realm.PRIVATE_MESSAGE_POLICY_DISABLED
user_profile.realm,
"private_message_policy",
Realm.PRIVATE_MESSAGE_POLICY_DISABLED,
acting_user=None,
)
with self.assertRaises(JsonableError):
self.send_personal_message(user_profile, self.example_user("cordelia"))
@ -2116,7 +2131,7 @@ class InternalPrepTest(ZulipTestCase):
class TestCrossRealmPMs(ZulipTestCase):
def make_realm(self, domain: str) -> Realm:
realm = do_create_realm(string_id=domain, name=domain)
do_set_realm_property(realm, "invite_required", False)
do_set_realm_property(realm, "invite_required", False, acting_user=None)
RealmDomain.objects.create(realm=realm, domain=domain)
return realm

View File

@ -59,10 +59,9 @@ class RealmTest(ZulipTestCase):
"""The main complicated thing about setting realm names is fighting the
cache, and we start by populating the cache for Hamlet, and we end
by checking the cache to ensure that the new value is there."""
self.example_user("hamlet")
realm = get_realm("zulip")
new_name = "Zed You Elle Eye Pea"
do_set_realm_property(realm, "name", new_name)
do_set_realm_property(realm, "name", new_name, acting_user=None)
self.assertEqual(get_realm(realm.string_id).name, new_name)
self.assert_user_profile_cache_gets_new_name(self.example_user("hamlet"), new_name)
@ -71,7 +70,7 @@ class RealmTest(ZulipTestCase):
new_name = "Puliz"
events: List[Mapping[str, Any]] = []
with tornado_redirected_to_list(events):
do_set_realm_property(realm, "name", new_name)
do_set_realm_property(realm, "name", new_name, acting_user=None)
event = events[0]["event"]
self.assertEqual(
event,
@ -88,7 +87,7 @@ class RealmTest(ZulipTestCase):
new_description = "zulip dev group"
events: List[Mapping[str, Any]] = []
with tornado_redirected_to_list(events):
do_set_realm_property(realm, "description", new_description)
do_set_realm_property(realm, "description", new_description, acting_user=None)
event = events[0]["event"]
self.assertEqual(
event,
@ -159,7 +158,7 @@ class RealmTest(ZulipTestCase):
data = {"full_name": "Sir Hamlet"}
user_profile = self.example_user("hamlet")
self.login_user(user_profile)
do_set_realm_property(user_profile.realm, "name_changes_disabled", True)
do_set_realm_property(user_profile.realm, "name_changes_disabled", True, acting_user=None)
url = "/json/settings"
result = self.client_patch(url, data)
self.assertEqual(result.status_code, 200)

View File

@ -18,7 +18,7 @@ from zerver.models import DomainNotAllowedForRealmError, RealmDomain, UserProfil
class RealmDomainTest(ZulipTestCase):
def setUp(self) -> None:
realm = get_realm("zulip")
do_set_realm_property(realm, "emails_restricted_to_domains", True)
do_set_realm_property(realm, "emails_restricted_to_domains", True, acting_user=None)
def test_list_realm_domains(self) -> None:
self.login("iago")

View File

@ -953,16 +953,20 @@ class TestGetRealmAndStreamsForArchiving(ZulipTestCase):
archiving_enabled_zephyr_stream.save()
no_archiving_realm = do_create_realm(string_id="no_archiving", name="no_archiving")
do_set_realm_property(no_archiving_realm, "invite_required", False)
do_set_realm_property(no_archiving_realm, "message_retention_days", -1)
do_set_realm_property(no_archiving_realm, "invite_required", False, acting_user=None)
do_set_realm_property(no_archiving_realm, "message_retention_days", -1, acting_user=None)
# Realm for testing the edge case where it has a default retention policy,
# but all streams disable it.
realm_all_streams_archiving_disabled = do_create_realm(
string_id="with_archiving", name="with_archiving"
)
do_set_realm_property(realm_all_streams_archiving_disabled, "invite_required", False)
do_set_realm_property(realm_all_streams_archiving_disabled, "message_retention_days", 1)
do_set_realm_property(
realm_all_streams_archiving_disabled, "invite_required", False, acting_user=None
)
do_set_realm_property(
realm_all_streams_archiving_disabled, "message_retention_days", 1, acting_user=None
)
Stream.objects.filter(realm=realm_all_streams_archiving_disabled).update(
message_retention_days=-1
)

View File

@ -1362,7 +1362,7 @@ earl-test@zulip.com""",
Tests inviting with various missing or invalid parameters.
"""
realm = get_realm("zulip")
do_set_realm_property(realm, "emails_restricted_to_domains", True)
do_set_realm_property(realm, "emails_restricted_to_domains", True, acting_user=None)
self.login("hamlet")
invitee_emails = "foo@zulip.com"
@ -3052,7 +3052,7 @@ class UserSignUpTest(InviteUserBase):
password = "newpassword"
timezone = "US/Mountain"
realm = get_realm("zulip")
do_set_realm_property(realm, "default_language", "de")
do_set_realm_property(realm, "default_language", "de", acting_user=None)
result = self.client_post("/accounts/home/", {"email": email})
self.assertEqual(result.status_code, 302)
@ -3084,7 +3084,7 @@ class UserSignUpTest(InviteUserBase):
email = self.nonreg_email("newguy")
password = "newpassword"
realm = get_realm("zulip")
do_set_realm_property(realm, "default_twenty_four_hour_time", True)
do_set_realm_property(realm, "default_twenty_four_hour_time", True, acting_user=None)
result = self.client_post("/accounts/home/", {"email": email})
self.assertEqual(result.status_code, 302)
@ -3595,8 +3595,8 @@ class UserSignUpTest(InviteUserBase):
def test_failed_signup_due_to_restricted_domain(self) -> None:
realm = get_realm("zulip")
do_set_realm_property(realm, "invite_required", False)
do_set_realm_property(realm, "emails_restricted_to_domains", True)
do_set_realm_property(realm, "invite_required", False, acting_user=None)
do_set_realm_property(realm, "emails_restricted_to_domains", True, acting_user=None)
email = "user@acme.com"
form = HomepageForm({"email": email}, realm=realm)

View File

@ -1073,7 +1073,7 @@ class StreamAdminTest(ZulipTestCase):
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER)
do_change_subscription_property(user_profile, sub, stream, "role", Subscription.ROLE_MEMBER)
do_set_realm_property(user_profile.realm, "waiting_period_threshold", 10)
do_set_realm_property(user_profile.realm, "waiting_period_threshold", 10, acting_user=None)
def test_non_admin(how_old: int, is_new: bool, policy: int) -> None:
user_profile.date_joined = timezone_now() - timedelta(days=how_old)
@ -1696,9 +1696,14 @@ class StreamAdminTest(ZulipTestCase):
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER)
# Allow all members to create streams.
do_set_realm_property(user_profile.realm, "create_stream_policy", Realm.POLICY_MEMBERS_ONLY)
do_set_realm_property(
user_profile.realm,
"create_stream_policy",
Realm.POLICY_MEMBERS_ONLY,
acting_user=None,
)
# Set waiting period to 10 days.
do_set_realm_property(user_profile.realm, "waiting_period_threshold", 10)
do_set_realm_property(user_profile.realm, "waiting_period_threshold", 10, acting_user=None)
# Can successfully create stream despite being less than waiting period and not an admin,
# due to create stream policy.
@ -1707,7 +1712,12 @@ class StreamAdminTest(ZulipTestCase):
self.assert_json_success(result)
# Allow only administrators to create streams.
do_set_realm_property(user_profile.realm, "create_stream_policy", Realm.POLICY_ADMINS_ONLY)
do_set_realm_property(
user_profile.realm,
"create_stream_policy",
Realm.POLICY_ADMINS_ONLY,
acting_user=None,
)
# Cannot create stream because not an admin.
stream_name = ["admins_only"]
@ -1723,7 +1733,10 @@ class StreamAdminTest(ZulipTestCase):
# Allow users older than the waiting period to create streams.
do_set_realm_property(
user_profile.realm, "create_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY
user_profile.realm,
"create_stream_policy",
Realm.POLICY_FULL_MEMBERS_ONLY,
acting_user=None,
)
# Can successfully create stream despite being under waiting period because user is admin.
@ -1761,7 +1774,10 @@ class StreamAdminTest(ZulipTestCase):
cordelia_user.save()
do_set_realm_property(
hamlet_user.realm, "invite_to_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY
hamlet_user.realm,
"invite_to_stream_policy",
Realm.POLICY_FULL_MEMBERS_ONLY,
acting_user=None,
)
cordelia_user_id = cordelia_user.id
@ -1774,7 +1790,7 @@ class StreamAdminTest(ZulipTestCase):
# Can only invite users to stream if their account is ten days old..
do_change_user_role(hamlet_user, UserProfile.ROLE_MEMBER)
do_set_realm_property(hamlet_user.realm, "waiting_period_threshold", 10)
do_set_realm_property(hamlet_user.realm, "waiting_period_threshold", 10, acting_user=None)
# Attempt and fail to invite Cordelia to the stream..
result = self.common_subscribe_to_streams(
@ -1788,7 +1804,7 @@ class StreamAdminTest(ZulipTestCase):
)
# Anyone can invite users..
do_set_realm_property(hamlet_user.realm, "waiting_period_threshold", 0)
do_set_realm_property(hamlet_user.realm, "waiting_period_threshold", 0, acting_user=None)
# Attempt and succeed to invite Cordelia to the stream..
self.common_subscribe_to_streams(
@ -1796,7 +1812,7 @@ class StreamAdminTest(ZulipTestCase):
)
# Set threshold to 20 days..
do_set_realm_property(hamlet_user.realm, "waiting_period_threshold", 20)
do_set_realm_property(hamlet_user.realm, "waiting_period_threshold", 20, acting_user=None)
# Make Hamlet's account 21 days old..
hamlet_user.date_joined = timezone_now() - timedelta(days=21)
hamlet_user.save()
@ -3180,7 +3196,7 @@ class SubscriptionAPITest(ZulipTestCase):
def test_user_settings_for_adding_streams(self) -> None:
do_set_realm_property(
self.test_user.realm, "create_stream_policy", Realm.POLICY_ADMINS_ONLY
self.test_user.realm, "create_stream_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None
)
with mock.patch("zerver.models.UserProfile.can_create_streams", return_value=False):
result = self.common_subscribe_to_streams(self.test_user, ["stream1"], allow_fail=True)
@ -3197,7 +3213,9 @@ class SubscriptionAPITest(ZulipTestCase):
user_profile = self.example_user("cordelia")
realm = user_profile.realm
do_set_realm_property(realm, "create_stream_policy", Realm.POLICY_ADMINS_ONLY)
do_set_realm_property(
realm, "create_stream_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None
)
result = self.common_subscribe_to_streams(
user_profile,
["new_stream1"],
@ -3208,7 +3226,9 @@ class SubscriptionAPITest(ZulipTestCase):
do_change_user_role(user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR)
self.common_subscribe_to_streams(user_profile, ["new_stream1"])
do_set_realm_property(realm, "create_stream_policy", Realm.POLICY_MEMBERS_ONLY)
do_set_realm_property(
realm, "create_stream_policy", Realm.POLICY_MEMBERS_ONLY, acting_user=None
)
do_change_user_role(user_profile, UserProfile.ROLE_GUEST)
result = self.common_subscribe_to_streams(
user_profile,
@ -3223,8 +3243,10 @@ class SubscriptionAPITest(ZulipTestCase):
["new_stream2"],
)
do_set_realm_property(realm, "create_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY)
do_set_realm_property(realm, "waiting_period_threshold", 100000)
do_set_realm_property(
realm, "create_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY, acting_user=None
)
do_set_realm_property(realm, "waiting_period_threshold", 100000, acting_user=None)
result = self.common_subscribe_to_streams(
user_profile,
["new_stream3"],
@ -3232,7 +3254,7 @@ class SubscriptionAPITest(ZulipTestCase):
)
self.assert_json_error(result, "Your account is too new to create streams.")
do_set_realm_property(realm, "waiting_period_threshold", 0)
do_set_realm_property(realm, "waiting_period_threshold", 0, acting_user=None)
self.common_subscribe_to_streams(user_profile, ["new_stream3"])
def test_can_create_streams(self) -> None:
@ -3241,21 +3263,27 @@ class SubscriptionAPITest(ZulipTestCase):
self.assertTrue(othello.can_create_streams())
do_change_user_role(othello, UserProfile.ROLE_MEMBER)
do_set_realm_property(othello.realm, "create_stream_policy", Realm.POLICY_ADMINS_ONLY)
do_set_realm_property(
othello.realm, "create_stream_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None
)
# Make sure that we are checking the permission with a full member,
# as full member is the user just below admin in the role hierarchy.
self.assertFalse(othello.is_provisional_member)
self.assertFalse(othello.can_create_streams())
do_set_realm_property(othello.realm, "create_stream_policy", Realm.POLICY_MEMBERS_ONLY)
do_set_realm_property(
othello.realm, "create_stream_policy", Realm.POLICY_MEMBERS_ONLY, acting_user=None
)
do_change_user_role(othello, UserProfile.ROLE_GUEST)
self.assertFalse(othello.can_create_streams())
do_change_user_role(othello, UserProfile.ROLE_MEMBER)
self.assertTrue(othello.can_create_streams())
do_set_realm_property(othello.realm, "waiting_period_threshold", 1000)
do_set_realm_property(othello.realm, "create_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY)
do_set_realm_property(othello.realm, "waiting_period_threshold", 1000, acting_user=None)
do_set_realm_property(
othello.realm, "create_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY, acting_user=None
)
othello.date_joined = timezone_now() - timedelta(
days=(othello.realm.waiting_period_threshold - 1)
)
@ -3279,8 +3307,12 @@ class SubscriptionAPITest(ZulipTestCase):
invitee_user_id = user_profile.id
realm = user_profile.realm
do_set_realm_property(realm, "create_stream_policy", Realm.POLICY_MEMBERS_ONLY)
do_set_realm_property(realm, "invite_to_stream_policy", Realm.POLICY_ADMINS_ONLY)
do_set_realm_property(
realm, "create_stream_policy", Realm.POLICY_MEMBERS_ONLY, acting_user=None
)
do_set_realm_property(
realm, "invite_to_stream_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None
)
result = self.common_subscribe_to_streams(
self.test_user,
["stream1"],
@ -3294,7 +3326,9 @@ class SubscriptionAPITest(ZulipTestCase):
self.test_user, ["stream1"], {"principals": orjson.dumps([invitee_user_id]).decode()}
)
do_set_realm_property(realm, "invite_to_stream_policy", Realm.POLICY_MEMBERS_ONLY)
do_set_realm_property(
realm, "invite_to_stream_policy", Realm.POLICY_MEMBERS_ONLY, acting_user=None
)
do_change_user_role(self.test_user, UserProfile.ROLE_GUEST)
result = self.common_subscribe_to_streams(
self.test_user,
@ -3312,8 +3346,13 @@ class SubscriptionAPITest(ZulipTestCase):
)
self.unsubscribe(user_profile, "stream2")
do_set_realm_property(realm, "invite_to_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY)
do_set_realm_property(realm, "waiting_period_threshold", 100000)
do_set_realm_property(
realm,
"invite_to_stream_policy",
Realm.POLICY_FULL_MEMBERS_ONLY,
acting_user=None,
)
do_set_realm_property(realm, "waiting_period_threshold", 100000, acting_user=None)
result = self.common_subscribe_to_streams(
self.test_user,
["stream2"],
@ -3324,7 +3363,7 @@ class SubscriptionAPITest(ZulipTestCase):
result, "Your account is too new to modify other users' subscriptions."
)
do_set_realm_property(realm, "waiting_period_threshold", 0)
do_set_realm_property(realm, "waiting_period_threshold", 0, acting_user=None)
self.common_subscribe_to_streams(
self.test_user, ["stream2"], {"principals": orjson.dumps([invitee_user_id]).decode()}
)
@ -3338,23 +3377,30 @@ class SubscriptionAPITest(ZulipTestCase):
do_change_user_role(othello, UserProfile.ROLE_REALM_ADMINISTRATOR)
self.assertTrue(othello.can_subscribe_other_users())
do_set_realm_property(othello.realm, "invite_to_stream_policy", Realm.POLICY_ADMINS_ONLY)
do_set_realm_property(
othello.realm, "invite_to_stream_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None
)
do_change_user_role(othello, UserProfile.ROLE_MEMBER)
# Make sure that we are checking the permission with a full member,
# as full member is the user just below admin in the role hierarchy.
self.assertFalse(othello.is_provisional_member)
self.assertFalse(othello.can_subscribe_other_users())
do_set_realm_property(othello.realm, "invite_to_stream_policy", Realm.POLICY_MEMBERS_ONLY)
do_set_realm_property(
othello.realm, "invite_to_stream_policy", Realm.POLICY_MEMBERS_ONLY, acting_user=None
)
do_change_user_role(othello, UserProfile.ROLE_GUEST)
self.assertFalse(othello.can_subscribe_other_users())
do_change_user_role(othello, UserProfile.ROLE_MEMBER)
self.assertTrue(othello.can_subscribe_other_users())
do_set_realm_property(othello.realm, "waiting_period_threshold", 1000)
do_set_realm_property(othello.realm, "waiting_period_threshold", 1000, acting_user=None)
do_set_realm_property(
othello.realm, "invite_to_stream_policy", Realm.POLICY_FULL_MEMBERS_ONLY
othello.realm,
"invite_to_stream_policy",
Realm.POLICY_FULL_MEMBERS_ONLY,
acting_user=None,
)
othello.date_joined = timezone_now() - timedelta(
days=(othello.realm.waiting_period_threshold - 1)
@ -3602,7 +3648,7 @@ class SubscriptionAPITest(ZulipTestCase):
self.register(new_member_email, "test")
new_member = self.nonreg_user("test")
do_set_realm_property(new_member.realm, "waiting_period_threshold", 10)
do_set_realm_property(new_member.realm, "waiting_period_threshold", 10, acting_user=None)
self.assertTrue(new_member.is_provisional_member)
stream = self.make_stream("stream1")

View File

@ -568,7 +568,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
user3_email = "other-user@uploadtest.example.com"
r1 = do_create_realm(string_id=test_subdomain, name=test_subdomain)
do_set_realm_property(r1, "invite_required", False)
do_set_realm_property(r1, "invite_required", False, acting_user=None)
RealmDomain.objects.create(realm=r1, domain=test_subdomain)
user_1 = create_user(user1_email, test_subdomain)
@ -969,7 +969,12 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
Attempting to upload avatar on a realm with avatar changes disabled should fail.
"""
self.login("cordelia")
do_set_realm_property(self.example_user("cordelia").realm, "avatar_changes_disabled", True)
do_set_realm_property(
self.example_user("cordelia").realm,
"avatar_changes_disabled",
True,
acting_user=None,
)
with get_test_image_file("img.png") as fp1:
result = self.client_post("/json/users/me/avatar", {"f1": fp1})
@ -1214,11 +1219,11 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
cordelia.avatar_source = UserProfile.AVATAR_FROM_USER
cordelia.save()
do_set_realm_property(cordelia.realm, "avatar_changes_disabled", True)
do_set_realm_property(cordelia.realm, "avatar_changes_disabled", True, acting_user=None)
result = self.client_delete("/json/users/me/avatar")
self.assert_json_error(result, "Avatar changes are disabled in this organization.", 400)
do_set_realm_property(cordelia.realm, "avatar_changes_disabled", False)
do_set_realm_property(cordelia.realm, "avatar_changes_disabled", False, acting_user=None)
result = self.client_delete("/json/users/me/avatar")
user_profile = self.example_user("cordelia")

View File

@ -443,7 +443,10 @@ class UserGroupAPITestCase(ZulipTestCase):
cordelia = self.example_user("cordelia")
self.login_user(iago)
do_set_realm_property(
iago.realm, "user_group_edit_policy", Realm.USER_GROUP_EDIT_POLICY_ADMINS
iago.realm,
"user_group_edit_policy",
Realm.USER_GROUP_EDIT_POLICY_ADMINS,
acting_user=None,
)
params = {

View File

@ -282,7 +282,10 @@ class PermissionTest(ZulipTestCase):
# Now, switch email address visibility, check client_gravatar
# is automatically disabled for the user.
do_set_realm_property(
user.realm, "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
user.realm,
"email_address_visibility",
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
acting_user=None,
)
result = self.client_get("/json/users", {"client_gravatar": "true"})
self.assert_json_success(result)
@ -946,7 +949,7 @@ class AdminCreateUserTest(ZulipTestCase):
)
self.assert_json_error(result, "Bad name or username")
do_set_realm_property(realm, "emails_restricted_to_domains", True)
do_set_realm_property(realm, "emails_restricted_to_domains", True, acting_user=None)
result = self.client_post(
"/json/users",
dict(