From adec45777d7604533bdcf623069329e2d9cd590c Mon Sep 17 00:00:00 2001 From: sahil839 Date: Thu, 8 Apr 2021 00:39:43 +0530 Subject: [PATCH] invite: Raise same error for all values of invite_to_realm_policy. We keep the error message same for all cases when a user is not allowed to invite others for all values of invite_to_realm_policy. We raise error with different message for guest cases because it is handled by decorators. We aim to change this behavior in future. Explaining the details in error message isn't much important as we do not show errors probably in API only, as we do not the show the options itself in the frontend. --- zerver/tests/test_signup.py | 6 +++--- zerver/views/invite.py | 15 ++++----------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 0203c852d6..3cc0eece2f 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -1255,7 +1255,7 @@ class InviteUserTest(InviteUserBase): invitee = f"Alice Test <{email}>, {email2}" self.assert_json_error( self.invite(invitee, ["Denmark"]), - "Only administrators can invite others to this organization.", + "Insufficient permission", ) # Now verify an administrator can do it @@ -1279,7 +1279,7 @@ class InviteUserTest(InviteUserBase): invitee = f"Carol Test <{email}>, {email2}" self.assert_json_error( self.invite(invitee, ["Denmark"]), - "Only administrators and moderators can invite others to this organization.", + "Insufficient permission", ) self.login("shiva") @@ -1323,7 +1323,7 @@ class InviteUserTest(InviteUserBase): invitee = f"Issac Test <{email}>, {email2}" self.assert_json_error( self.invite(invitee, ["Denmark"]), - "Your account is too new to invite others to this organization.", + "Insufficient permission", ) do_set_realm_property(realm, "waiting_period_threshold", 0, acting_user=None) diff --git a/zerver/views/invite.py b/zerver/views/invite.py index 01bc7a5c03..9c759a0605 100644 --- a/zerver/views/invite.py +++ b/zerver/views/invite.py @@ -18,7 +18,7 @@ from zerver.lib.request import REQ, JsonableError, has_request_variables from zerver.lib.response import json_error, json_success from zerver.lib.streams import access_stream_by_id from zerver.lib.validator import check_int, check_list -from zerver.models import MultiuseInvite, PreregistrationUser, Realm, Stream, UserProfile +from zerver.models import MultiuseInvite, PreregistrationUser, Stream, UserProfile def check_if_owner_required(invited_as: int, user_profile: UserProfile) -> None: @@ -40,16 +40,9 @@ def invite_users_backend( ) -> HttpResponse: if not user_profile.can_invite_others_to_realm(): - if user_profile.realm.invite_to_realm_policy == Realm.POLICY_ADMINS_ONLY: - return json_error(_("Only administrators can invite others to this organization.")) - if user_profile.realm.invite_to_realm_policy == Realm.POLICY_MODERATORS_ONLY: - return json_error( - _("Only administrators and moderators can invite others to this organization.") - ) - if user_profile.realm.invite_to_realm_policy == Realm.POLICY_FULL_MEMBERS_ONLY: - return json_error(_("Your account is too new to invite others to this organization.")) - # Guest case will be handled by require_member_or_admin decorator. - raise AssertionError("Unexpected policy validation failure") + # Guest users case will not be handled here as it will + # be handled by the decorator above. + raise JsonableError(_("Insufficient permission")) if invite_as not in PreregistrationUser.INVITE_AS.values(): return json_error(_("Must be invited as an valid type of user")) check_if_owner_required(invite_as, user_profile)