-
+
diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py
index 48801e3490..88ca1803c6 100644
--- a/zerver/tests/test_realm.py
+++ b/zerver/tests/test_realm.py
@@ -868,6 +868,18 @@ class RealmAPITest(ZulipTestCase):
self.assertEqual(realm.allow_message_deleting, True)
self.assertEqual(realm.message_content_delete_limit_seconds, 600)
+ def test_change_invite_to_realm_policy_by_owners_only(self) -> None:
+ self.login("iago")
+ req = {"invite_to_realm_policy": Realm.POLICY_ADMINS_ONLY}
+ result = self.client_patch("/json/realm", req)
+ self.assert_json_error(result, "Must be an organization owner")
+
+ self.login("desdemona")
+ result = self.client_patch("/json/realm", req)
+ self.assert_json_success(result)
+ realm = get_realm("zulip")
+ self.assertEqual(realm.invite_to_realm_policy, Realm.POLICY_ADMINS_ONLY)
+
class ScrubRealmTest(ZulipTestCase):
def test_scrub_realm(self) -> None:
diff --git a/zerver/views/realm.py b/zerver/views/realm.py
index ca35a77133..6b8ea67b49 100644
--- a/zerver/views/realm.py
+++ b/zerver/views/realm.py
@@ -150,6 +150,9 @@ def update_realm(
message_retention_days_raw, Realm.MESSAGE_RETENTION_SPECIAL_VALUES_MAP
)
+ if invite_to_realm_policy is not None and not user_profile.is_realm_owner:
+ raise OrganizationOwnerRequired()
+
# The user of `locals()` here is a bit of a code smell, but it's
# restricted to the elements present in realm.property_types.
#