[MM-53057] Disallow malformed channel names for DMs (#24404)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
24f5672da8
Коммит
5d3ba7483b
@@ -196,7 +196,7 @@ func TestSearchFileInfoStore(t *testing.T, s store.Store, testEngine *SearchTest
|
||||
}
|
||||
|
||||
func testFileInfoSearchFileInfosIncludingDMs(t *testing.T, th *SearchTestHelper) {
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct-"+th.Team.Id, "direct-"+th.Team.Id, []*model.User{th.User, th.User2})
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct-"+th.Team.Id, []*model.User{th.User, th.User2})
|
||||
require.NoError(t, err)
|
||||
defer th.deleteChannel(direct)
|
||||
|
||||
@@ -237,7 +237,7 @@ func testFileInfoSearchFileInfosIncludingDMs(t *testing.T, th *SearchTestHelper)
|
||||
}
|
||||
|
||||
func testFileInfoSearchFileInfosWithPagination(t *testing.T, th *SearchTestHelper) {
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", "direct", []*model.User{th.User, th.User2})
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", []*model.User{th.User, th.User2})
|
||||
require.NoError(t, err)
|
||||
defer th.deleteChannel(direct)
|
||||
|
||||
@@ -651,7 +651,7 @@ func testFileInfoSearchOrExcludeFileInfosInChannel(t *testing.T, th *SearchTestH
|
||||
}
|
||||
|
||||
func testFileInfoSearchOrExcludeFileInfosInDMGM(t *testing.T, th *SearchTestHelper) {
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", "direct", []*model.User{th.User, th.User2})
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", []*model.User{th.User, th.User2})
|
||||
require.NoError(t, err)
|
||||
defer th.deleteChannel(direct)
|
||||
|
||||
|
||||
@@ -275,10 +275,10 @@ func (th *SearchTestHelper) createChannel(teamID, name, displayName, purpose str
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (th *SearchTestHelper) createDirectChannel(teamID, name, displayName string, users []*model.User) (*model.Channel, error) {
|
||||
func (th *SearchTestHelper) createDirectChannel(teamID, displayName string, users []*model.User) (*model.Channel, error) {
|
||||
channel := &model.Channel{
|
||||
TeamId: teamID,
|
||||
Name: name,
|
||||
Name: model.GetDMNameFromIds(users[0].Id, users[1].Id),
|
||||
DisplayName: displayName,
|
||||
Type: model.ChannelTypeDirect,
|
||||
}
|
||||
@@ -290,7 +290,7 @@ func (th *SearchTestHelper) createDirectChannel(teamID, name, displayName string
|
||||
|
||||
m2 := &model.ChannelMember{}
|
||||
m2.ChannelId = channel.Id
|
||||
m2.UserId = users[0].Id
|
||||
m2.UserId = users[1].Id
|
||||
m2.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
|
||||
channel, err := th.Store.Channel().SaveDirectChannel(channel, m1, m2)
|
||||
|
||||
@@ -288,7 +288,7 @@ func TestSearchPostStore(t *testing.T, s store.Store, testEngine *SearchTestEngi
|
||||
}
|
||||
|
||||
func testSearchPostsIncludingDMs(t *testing.T, th *SearchTestHelper) {
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", "direct", []*model.User{th.User, th.User2})
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", []*model.User{th.User, th.User2})
|
||||
require.NoError(t, err)
|
||||
defer th.deleteChannel(direct)
|
||||
|
||||
@@ -310,7 +310,7 @@ func testSearchPostsIncludingDMs(t *testing.T, th *SearchTestHelper) {
|
||||
}
|
||||
|
||||
func testSearchPostsWithPagination(t *testing.T, th *SearchTestHelper) {
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", "direct", []*model.User{th.User, th.User2})
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", []*model.User{th.User, th.User2})
|
||||
require.NoError(t, err)
|
||||
defer th.deleteChannel(direct)
|
||||
|
||||
@@ -675,7 +675,7 @@ func testSearchOrExcludePostsInChannel(t *testing.T, th *SearchTestHelper) {
|
||||
}
|
||||
|
||||
func testSearchOrExcludePostsInDMGM(t *testing.T, th *SearchTestHelper) {
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", "direct", []*model.User{th.User, th.User2})
|
||||
direct, err := th.createDirectChannel(th.Team.Id, "direct", []*model.User{th.User, th.User2})
|
||||
require.NoError(t, err)
|
||||
defer th.deleteChannel(direct)
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ func testChannelStoreSave(t *testing.T, ss store.Store) {
|
||||
require.Error(t, nErr, "should be unique name")
|
||||
|
||||
o1.Id = ""
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
_, nErr = ss.Channel().Save(&o1, -1)
|
||||
require.Error(t, nErr, "should not be able to save direct channel")
|
||||
@@ -209,7 +209,7 @@ func testChannelStoreSaveDirectChannel(t *testing.T, ss store.Store, s SqlStore)
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = teamId
|
||||
o1.DisplayName = "Name"
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
|
||||
u1 := &model.User{}
|
||||
@@ -272,7 +272,7 @@ func testChannelStoreSaveDirectChannel(t *testing.T, ss store.Store, s SqlStore)
|
||||
// Save yourself Direct Message
|
||||
o1.Id = ""
|
||||
o1.DisplayName = "Myself"
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
_, nErr = ss.Channel().SaveDirectChannel(&o1, &m1, &m1)
|
||||
require.NoError(t, nErr, "couldn't save direct channel", nErr)
|
||||
@@ -446,7 +446,7 @@ func testChannelStoreGet(t *testing.T, ss store.Store, s SqlStore) {
|
||||
o2 := model.Channel{}
|
||||
o2.TeamId = model.NewId()
|
||||
o2.DisplayName = "Direct Name"
|
||||
o2.Name = NewTestId()
|
||||
o2.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o2.Type = model.ChannelTypeDirect
|
||||
|
||||
m1 := model.ChannelMember{}
|
||||
@@ -548,7 +548,7 @@ func testChannelStoreGetChannelsByIds(t *testing.T, ss store.Store) {
|
||||
o2 := model.Channel{}
|
||||
o2.TeamId = model.NewId()
|
||||
o2.DisplayName = "Direct Name"
|
||||
o2.Name = "bb" + model.NewId()
|
||||
o2.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o2.Type = model.ChannelTypeDirect
|
||||
|
||||
o3 := model.Channel{}
|
||||
@@ -580,8 +580,7 @@ func testChannelStoreGetChannelsByIds(t *testing.T, ss store.Store) {
|
||||
r1, err := ss.Channel().GetChannelsByIds([]string{o1.Id, o2.Id}, false)
|
||||
require.NoError(t, err, err)
|
||||
require.Len(t, r1, 2, "invalid returned channels, expected 2 and got "+strconv.Itoa(len(r1)))
|
||||
require.Equal(t, channelToJSON(t, &o1), channelToJSON(t, r1[0]))
|
||||
require.Equal(t, channelToJSON(t, &o2), channelToJSON(t, r1[1]))
|
||||
require.ElementsMatch(t, []*model.Channel{&o1, &o2}, r1)
|
||||
})
|
||||
|
||||
t.Run("Get 1 existing and 1 not existing channel", func(t *testing.T) {
|
||||
@@ -589,16 +588,14 @@ func testChannelStoreGetChannelsByIds(t *testing.T, ss store.Store) {
|
||||
r2, err := ss.Channel().GetChannelsByIds([]string{o1.Id, nonexistentId}, false)
|
||||
require.NoError(t, err, err)
|
||||
require.Len(t, r2, 1, "invalid returned channels, expected 1 and got "+strconv.Itoa(len(r2)))
|
||||
require.Equal(t, channelToJSON(t, &o1), channelToJSON(t, r2[0]), "invalid returned channel")
|
||||
require.ElementsMatch(t, []*model.Channel{&o1}, r2, "invalid returned channel")
|
||||
})
|
||||
|
||||
t.Run("Get 2 existing and 1 deleted channel", func(t *testing.T) {
|
||||
r1, err := ss.Channel().GetChannelsByIds([]string{o1.Id, o2.Id, o3.Id}, true)
|
||||
require.NoError(t, err, err)
|
||||
require.Len(t, r1, 3, "invalid returned channels, expected 3 and got "+strconv.Itoa(len(r1)))
|
||||
require.Equal(t, channelToJSON(t, &o1), channelToJSON(t, r1[0]))
|
||||
require.Equal(t, channelToJSON(t, &o2), channelToJSON(t, r1[1]))
|
||||
require.Equal(t, channelToJSON(t, &o3), channelToJSON(t, r1[2]))
|
||||
require.ElementsMatch(t, []*model.Channel{&o1, &o2, &o3}, r1)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -640,7 +637,7 @@ func testGetChannelsWithTeamDataByIds(t *testing.T, ss store.Store) {
|
||||
c2 := model.Channel{}
|
||||
c2.TeamId = t1.Id
|
||||
c2.DisplayName = "Direct Name"
|
||||
c2.Name = "bb" + model.NewId()
|
||||
c2.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
c2.Type = model.ChannelTypeDirect
|
||||
|
||||
c3 := model.Channel{}
|
||||
@@ -671,10 +668,20 @@ func testGetChannelsWithTeamDataByIds(t *testing.T, ss store.Store) {
|
||||
res, err := ss.Channel().GetChannelsWithTeamDataByIds([]string{c1.Id, c2.Id}, false)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, res, 2)
|
||||
assert.Equal(t, res[0].Id, c1.Id)
|
||||
assert.Equal(t, res[0].TeamName, t1.Name)
|
||||
assert.Equal(t, res[1].Id, c2.Id)
|
||||
assert.Equal(t, res[1].TeamName, "")
|
||||
|
||||
if res[0].Id == c1.Id {
|
||||
assert.Equal(t, res[0].TeamName, t1.Name)
|
||||
|
||||
assert.Equal(t, res[1].Id, c2.Id)
|
||||
assert.Equal(t, res[1].TeamName, "")
|
||||
} else if res[0].Id == c2.Id {
|
||||
assert.Equal(t, res[0].TeamName, "")
|
||||
|
||||
assert.Equal(t, res[1].Id, c1.Id)
|
||||
assert.Equal(t, res[1].TeamName, t1.Name)
|
||||
} else {
|
||||
assert.Fail(t, "unknown channel id")
|
||||
}
|
||||
}
|
||||
|
||||
func testChannelStoreGetForPost(t *testing.T, ss store.Store) {
|
||||
@@ -7469,7 +7476,7 @@ func testChannelStoreExportAllDirectChannels(t *testing.T, ss store.Store, s Sql
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = teamId
|
||||
o1.DisplayName = "Name" + model.NewId()
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
|
||||
userIds := []string{model.NewId(), model.NewId(), model.NewId()}
|
||||
@@ -7526,7 +7533,7 @@ func testChannelStoreExportAllDirectChannelsExcludePrivateAndPublic(t *testing.T
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = teamId
|
||||
o1.DisplayName = "The Direct Channel" + model.NewId()
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
|
||||
o2 := model.Channel{}
|
||||
@@ -7588,7 +7595,7 @@ func testChannelStoreExportAllDirectChannelsDeletedChannel(t *testing.T, ss stor
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = teamId
|
||||
o1.DisplayName = "Different Name" + model.NewId()
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
|
||||
u1 := &model.User{}
|
||||
|
||||
@@ -3055,7 +3055,7 @@ func testPostStoreGetFlaggedPostsForTeam(t *testing.T, ss store.Store, s SqlStor
|
||||
|
||||
c2 := &model.Channel{}
|
||||
c2.DisplayName = "DMChannel1"
|
||||
c2.Name = NewTestId()
|
||||
c2.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
c2.Type = model.ChannelTypeDirect
|
||||
|
||||
m1 := &model.ChannelMember{}
|
||||
@@ -4410,7 +4410,7 @@ func testPostStoreGetDirectPostParentsForExportAfter(t *testing.T, ss store.Stor
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = teamId
|
||||
o1.DisplayName = "Name"
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
|
||||
u1 := &model.User{}
|
||||
@@ -4464,7 +4464,7 @@ func testPostStoreGetDirectPostParentsForExportAfterDeleted(t *testing.T, ss sto
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = teamId
|
||||
o1.DisplayName = "Name"
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
|
||||
u1 := &model.User{}
|
||||
@@ -4530,7 +4530,7 @@ func testPostStoreGetDirectPostParentsForExportAfterBatched(t *testing.T, ss sto
|
||||
o1 := model.Channel{}
|
||||
o1.TeamId = teamId
|
||||
o1.DisplayName = "Name"
|
||||
o1.Name = NewTestId()
|
||||
o1.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
o1.Type = model.ChannelTypeDirect
|
||||
|
||||
var postIds []string
|
||||
|
||||
@@ -2398,7 +2398,7 @@ func testUserUnreadCount(t *testing.T, ss store.Store) {
|
||||
c2 := model.Channel{}
|
||||
c2.TeamId = teamId
|
||||
c2.DisplayName = "Unread Direct"
|
||||
c2.Name = "unread-direct-" + model.NewId()
|
||||
c2.Name = model.GetDMNameFromIds(NewTestId(), NewTestId())
|
||||
c2.Type = model.ChannelTypeDirect
|
||||
|
||||
u1 := &model.User{}
|
||||
|
||||
Ссылка в новой задаче
Block a user