MM-29067 Add deterministic IDs for default sidebar categories (#16030)

Automatic Merge
Этот коммит содержится в:
Harrison Healey
2020-10-29 10:24:01 -04:00
коммит произвёл GitHub
родитель 28983fa88d
Коммит 8bb772638c
8 изменённых файлов: 113 добавлений и 10 удалений

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

@@ -6,6 +6,7 @@ package model
import (
"encoding/json"
"io"
"regexp"
)
type SidebarCategoryType string
@@ -109,3 +110,15 @@ func (o OrderedSidebarCategories) ToJson() []byte {
return b
}
}
var categoryIdPattern = regexp.MustCompile("(favorites|channels|direct_messages)_[a-z0-9]{26}_[a-z0-9]{26}")
func IsValidCategoryId(s string) bool {
// Category IDs can either be regular IDs
if IsValidId(s) {
return true
}
// Or default categories can follow the pattern {type}_{userID}_{teamID}
return categoryIdPattern.MatchString(s)
}

49
model/channel_sidebar_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,49 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsValidCategoryId(t *testing.T) {
for _, test := range []struct {
Name string
Input string
Expected bool
}{
{
Name: "should accept a regular ID",
Input: NewId(),
Expected: true,
},
{
Name: "should accept a favorites ID",
Input: fmt.Sprintf("favorites_%s_%s", NewId(), NewId()),
Expected: true,
},
{
Name: "should accept a channels ID",
Input: fmt.Sprintf("channels_%s_%s", NewId(), NewId()),
Expected: true,
},
{
Name: "should accept a direct messages ID",
Input: fmt.Sprintf("direct_messages_%s_%s", NewId(), NewId()),
Expected: true,
},
{
Name: "should reject a garbage ID",
Input: "a garbage ID",
Expected: false,
},
} {
t.Run(test.Name, func(t *testing.T) {
assert.Equal(t, test.Expected, IsValidCategoryId(test.Input))
})
}
}