invites: Use check_int_in to validate invite_as.

(cherry picked from commit ea9b05d88a)
This commit is contained in:
Josh Klar
2022-12-30 14:04:18 -08:00
committed by Josh Klar
parent 82aac9e113
commit 46966957ac
3 changed files with 12 additions and 4 deletions

View File

@@ -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:

View File

@@ -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")

View File

@@ -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