diff --git a/zerver/lib/users.py b/zerver/lib/users.py index 7fa334c473..3b4a5070df 100644 --- a/zerver/lib/users.py +++ b/zerver/lib/users.py @@ -12,6 +12,8 @@ def check_full_name(full_name_raw): full_name = full_name_raw.strip() if len(full_name) > UserProfile.MAX_NAME_LENGTH: raise JsonableError(_("Name too long!")) + if len(full_name) < UserProfile.MIN_NAME_LENGTH: + raise JsonableError(_("Name too short!")) if list(set(full_name).intersection(UserProfile.NAME_INVALID_CHARS)): raise JsonableError(_("Invalid characters in name!")) return full_name diff --git a/zerver/models.py b/zerver/models.py index 7585dcf351..83df122b97 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -534,6 +534,7 @@ class UserProfile(ModelReprMixin, AbstractBaseUser, PermissionsMixin): USERNAME_FIELD = 'email' MAX_NAME_LENGTH = 100 + MIN_NAME_LENGTH = 3 API_KEY_LENGTH = 32 NAME_INVALID_CHARS = ['*', '`', '>', '"', '@'] diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.py index c958b8fe0e..3f1cd665b9 100644 --- a/zerver/tests/test_bots.py +++ b/zerver/tests/test_bots.py @@ -62,13 +62,25 @@ class BotTest(ZulipTestCase, UploadSerializeMixin): self.login("hamlet@zulip.com") self.assert_num_bots_equal(0) bot_info = dict( - full_name='', - short_name='', + full_name='My bot name', + short_name='@', ) result = self.client_post("/json/bots", bot_info) self.assert_json_error(result, 'Bad name or username') self.assert_num_bots_equal(0) + def test_add_bot_with_no_name(self): + # type: () -> None + self.login("hamlet@zulip.com") + self.assert_num_bots_equal(0) + bot_info = dict( + full_name='a', + short_name='bot', + ) + result = self.client_post("/json/bots", bot_info) + self.assert_json_error(result, 'Name too short!') + self.assert_num_bots_equal(0) + def test_add_bot(self): # type: () -> None self.login("hamlet@zulip.com") diff --git a/zerver/tests/test_settings.py b/zerver/tests/test_settings.py index c9eeb8f629..2da7ec4912 100644 --- a/zerver/tests/test_settings.py +++ b/zerver/tests/test_settings.py @@ -107,6 +107,11 @@ class ChangeSettingsTest(ZulipTestCase): dict(full_name='x' * 1000)) self.assert_json_error(json_result, 'Name too long!') + # Now try a too-short name + json_result = self.client_post("/json/settings/change", + dict(full_name='x')) + self.assert_json_error(json_result, 'Name too short!') + def test_illegal_characters_in_name_changes(self): # type: () -> None email = 'hamlet@zulip.com' diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index 449026d6db..1b29b33270 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -156,6 +156,14 @@ class PermissionTest(ZulipTestCase): result = self.client_patch('/json/users/hamlet@zulip.com', req) self.assert_json_error(result, 'Name too long!') + def test_admin_cannot_set_short_full_name(self): + # type: () -> None + new_name = 'a' + self.login('iago@zulip.com') + req = dict(full_name=ujson.dumps(new_name)) + result = self.client_patch('/json/users/hamlet@zulip.com', req) + self.assert_json_error(result, 'Name too short!') + def test_admin_cannot_set_full_name_with_invalid_characters(self): # type: () -> None new_name = 'Opheli*'