Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

invites: Use check_int_in to validate invite_as.

Josh Klarea9b05d88a78
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 66d137296f0..b83ff74cfa3 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -161,6 +161,11 @@ def check_int(var_name: str, val: object) -> int:
def check_int_in(possible_values: List[int]) -> Validator[int]:
+ """
+ Assert that the input is an integer and is contained in `possible_values`. If the input is not in
+ `possible_values`, a `ValidationError` is raised containing the failing field's name.
+ """
+
def validator(var_name: str, val: object) -> int:
n = check_int(var_name, val)
if n not in possible_values:
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index 1047ccbb3f7..8a39c7d275f 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -1501,7 +1501,7 @@ class InviteUserTest(InviteUserBase):
self.login("iago")
invitee = self.nonreg_email("alice")
response = self.invite(invitee, ["Denmark"], invite_as=10)
- self.assert_json_error(response, "Must be invited as an valid type of user")
+ self.assert_json_error(response, "Invalid invite_as")
def test_successful_invite_user_as_guest_from_normal_account(self) -> None:
self.login("hamlet")
diff --git a/zerver/views/invite.py b/zerver/views/invite.py
index c3a214d1b6b..9a175832866 100644
--- a/zerver/views/invite.py
+++ b/zerver/views/invite.py
@@ -46,7 +46,12 @@ def invite_users_backend(
invite_expires_in_minutes: Optional[int] = REQ(
json_validator=check_none_or(check_int), default=INVITATION_LINK_VALIDITY_MINUTES
),
- invite_as: int = REQ(json_validator=check_int, default=PreregistrationUser.INVITE_AS["MEMBER"]),
+ invite_as: int = REQ(
+ json_validator=check_int_in(
+ list(PreregistrationUser.INVITE_AS.values()),
+ ),
+ default=PreregistrationUser.INVITE_AS["MEMBER"],
+ ),
stream_ids: List[int] = REQ(json_validator=check_list(check_int)),
) -> HttpResponse:
@@ -54,8 +59,6 @@ def invite_users_backend(
# 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():
- raise JsonableError(_("Must be invited as an valid type of user"))
check_if_owner_required(invite_as, user_profile)
if (
invite_as