diff --git a/app/slashcommands/command_channel_rename_test.go b/app/slashcommands/command_channel_rename_test.go index eb6097450b..26fd9686df 100644 --- a/app/slashcommands/command_channel_rename_test.go +++ b/app/slashcommands/command_channel_rename_test.go @@ -28,7 +28,7 @@ func TestRenameProviderDoCommand(t *testing.T) { // Table Test for basic cases. Blank text in response indicates success for msg, expected := range map[string]string{ "": "api.command_channel_rename.message.app_error", - "o": "api.command_channel_rename.too_short.app_error", + "o": "", "joram": "", "More than 22 chars but less than 64": "", strings.Repeat("12345", 13): "api.command_channel_rename.too_long.app_error", diff --git a/i18n/en.json b/i18n/en.json index 9a49abeaf2..e05d4b48eb 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -7688,8 +7688,8 @@ "translation": "Invalid username." }, { - "id": "model.channel.is_valid.2_or_more.app_error", - "translation": "Name must be 2 or more lowercase alphanumeric characters." + "id": "model.channel.is_valid.1_or_more.app_error", + "translation": "Name must be 1 or more lowercase alphanumeric character." }, { "id": "model.channel.is_valid.create_at.app_error", diff --git a/model/channel.go b/model/channel.go index 350a923017..32742582c1 100644 --- a/model/channel.go +++ b/model/channel.go @@ -27,7 +27,7 @@ const ( ChannelGroupMinUsers = 3 DefaultChannelName = "town-square" ChannelDisplayNameMaxRunes = 64 - ChannelNameMinLength = 2 + ChannelNameMinLength = 1 ChannelNameMaxLength = 64 ChannelHeaderMaxRunes = 1024 ChannelPurposeMaxRunes = 250 @@ -216,7 +216,7 @@ func (o *Channel) IsValid() *AppError { } if !IsValidChannelIdentifier(o.Name) { - return NewAppError("Channel.IsValid", "model.channel.is_valid.2_or_more.app_error", nil, "id="+o.Id, http.StatusBadRequest) + return NewAppError("Channel.IsValid", "model.channel.is_valid.1_or_more.app_error", nil, "id="+o.Id, http.StatusBadRequest) } if !(o.Type == ChannelTypeOpen || o.Type == ChannelTypePrivate || o.Type == ChannelTypeDirect || o.Type == ChannelTypeGroup) { diff --git a/model/shared_channel.go b/model/shared_channel.go index ed069b28f5..453d18e48e 100644 --- a/model/shared_channel.go +++ b/model/shared_channel.go @@ -51,7 +51,7 @@ func (sc *SharedChannel) IsValid() *AppError { } if !IsValidChannelIdentifier(sc.ShareName) { - return NewAppError("SharedChannel.IsValid", "model.channel.is_valid.2_or_more.app_error", nil, "id="+sc.ChannelId, http.StatusBadRequest) + return NewAppError("SharedChannel.IsValid", "model.channel.is_valid.1_or_more.app_error", nil, "id="+sc.ChannelId, http.StatusBadRequest) } if utf8.RuneCountInString(sc.ShareHeader) > ChannelHeaderMaxRunes { diff --git a/model/utils.go b/model/utils.go index bc1bd7ae78..636e707ef2 100644 --- a/model/utils.go +++ b/model/utils.go @@ -510,21 +510,13 @@ var reservedName = []string{ } func IsValidChannelIdentifier(s string) bool { - - if !IsValidAlphaNumHyphenUnderscore(s, true) { - return false - } - - if len(s) < ChannelNameMinLength { - return false - } - - return true + return validSimpleAlphaNum.MatchString(s) && len(s) >= ChannelNameMinLength } var ( validAlphaNum = regexp.MustCompile(`^[a-z0-9]+([a-z\-0-9]+|(__)?)[a-z0-9]+$`) validAlphaNumHyphenUnderscore = regexp.MustCompile(`^[a-z0-9]+([a-z\-\_0-9]+|(__)?)[a-z0-9]+$`) + validSimpleAlphaNum = regexp.MustCompile(`^[a-z0-9]+([a-z\-\_0-9]+|(__)?)[a-z0-9]*$`) validSimpleAlphaNumHyphenUnderscore = regexp.MustCompile(`^[a-zA-Z0-9\-_]+$`) validSimpleAlphaNumHyphenUnderscorePlus = regexp.MustCompile(`^[a-zA-Z0-9+_-]+$`) ) diff --git a/model/utils_test.go b/model/utils_test.go index a584a0f6f7..35a1ccf93c 100644 --- a/model/utils_test.go +++ b/model/utils_test.go @@ -862,6 +862,55 @@ func TestSanitizeUnicode(t *testing.T) { } } +func TestIsValidChannelIdentifier(t *testing.T) { + cases := []struct { + Description string + Input string + Expected bool + }{ + { + Description: "less than min length", + Input: "", + Expected: false, + }, + { + Description: "single alphabetical char", + Input: "a", + Expected: true, + }, + { + Description: "single underscore", + Input: "_", + Expected: false, + }, + { + Description: "single hyphen", + Input: "-", + Expected: false, + }, + { + Description: "empty string", + Input: " ", + Expected: false, + }, + { + Description: "multiple with hyphen", + Input: "a-a", + Expected: true, + }, + { + Description: "multiple with hyphen", + Input: "a_a", + Expected: true, + }, + } + + for _, tc := range cases { + actual := IsValidChannelIdentifier(tc.Input) + require.Equalf(t, actual, tc.Expected, "case: '%v'\tshould returned: %#v", tc.Input, tc.Expected) + } +} + func TestIsValidHTTPURL(t *testing.T) { t.Parallel()