MM-41909: Allow 1 char channel name (#19845)

* [ MM-41909 ] Allow 1 character channel names

Change minimal length to 1

* [ MM-41909 ] Fix localization string

* [ MM-41909 ] Modify condition to allow 1 char channel name

* [ MM-41909 ] fix formatting

* [MM-41909] Linting fix

* [MM-41909] Fix regex

* [MM-41909] Localization update

* [MM-41909] Attempt to fix test

Co-authored-by: Kevin Jiang <>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Kevin Sicong Jiang
2022-04-13 03:17:15 +12:00
коммит произвёл GitHub
родитель 053e92a683
Коммит 5cb31a114a
6 изменённых файлов: 57 добавлений и 16 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@@ -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+_-]+$`)
)

Просмотреть файл

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