actions: Change do_change_is_admin Exception to an Assertion Error.

- do_change_is_admin now raises AssertionError when a non-admin
  permission is given.
- adds test to test_users to ensure admin asserts on invalid
  permission values.
This commit is contained in:
Baron Chandler
2018-05-15 17:45:31 +00:00
committed by Tim Abbott
parent 4bdc8332fa
commit f59adfa67c
2 changed files with 19 additions and 1 deletions

View File

@@ -2947,7 +2947,7 @@ def do_change_is_admin(user_profile: UserProfile, value: bool,
user_profile.is_api_super_user = value
user_profile.save(update_fields=["is_api_super_user"])
else:
raise Exception("Unknown permission")
raise AssertionError("Invalid admin permission")
if permission == 'administer':
event = dict(type="realm_user", op="update",

View File

@@ -56,6 +56,24 @@ def find_dict(lst: Iterable[Dict[K, V]], k: K, v: V) -> Dict[K, V]:
raise AssertionError('Cannot find element in list where key %s == %s' % (k, v))
class PermissionTest(ZulipTestCase):
def test_do_change_is_admin(self) -> None:
"""
Ensures change_is_admin raises an AssertionError when invalid permissions
are provided to it.
"""
# this should work fine
user_profile = self.example_user('hamlet')
do_change_is_admin(user_profile, True)
# this should work a-ok as well
do_change_is_admin(user_profile, True, permission='administer')
# this should "fail" with an AssertionError
with self.assertRaises(AssertionError):
do_change_is_admin(user_profile, True, permission='totally-not-valid-perm')
def test_get_admin_users(self) -> None:
user_profile = self.example_user('hamlet')
do_change_is_admin(user_profile, False)