Cherry picker search-api-filter-guest-permission to release-10.11 (#35018)
Automatic Merge
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3b1b8d9114
Коммит
4b8b1e5ca0
@@ -4216,3 +4216,136 @@ func TestPopulateEditHistoryFileMetadata(t *testing.T) {
|
||||
require.Greater(t, post2.Metadata.Files[0].DeleteAt, int64(0))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterPostsByChannelPermissions(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t).InitBasic()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.GuestAccountsSettings.Enable = true
|
||||
})
|
||||
|
||||
guestUser := th.CreateGuest()
|
||||
_, _, appErr := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, guestUser.Id, "")
|
||||
require.Nil(t, appErr)
|
||||
|
||||
privateChannel := th.CreatePrivateChannel(th.Context, th.BasicTeam)
|
||||
|
||||
_, appErr = th.App.AddUserToChannel(th.Context, guestUser, privateChannel, false)
|
||||
require.Nil(t, appErr)
|
||||
_, appErr = th.App.AddUserToChannel(th.Context, guestUser, th.BasicChannel, false)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
post1 := th.CreatePost(th.BasicChannel)
|
||||
post2 := th.CreatePost(privateChannel)
|
||||
post3 := th.CreatePost(th.BasicChannel)
|
||||
|
||||
t.Run("should filter posts when user has read_channel_content permission", func(t *testing.T) {
|
||||
postList := model.NewPostList()
|
||||
postList.Posts[post1.Id] = post1
|
||||
postList.Posts[post2.Id] = post2
|
||||
postList.Posts[post3.Id] = post3
|
||||
postList.Order = []string{post1.Id, post2.Id, post3.Id}
|
||||
|
||||
appErr := th.App.FilterPostsByChannelPermissions(th.Context, postList, th.BasicUser.Id)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, postList.Posts, 3)
|
||||
require.Len(t, postList.Order, 3)
|
||||
})
|
||||
|
||||
t.Run("should filter posts when guest has read_channel_content permission", func(t *testing.T) {
|
||||
postList := model.NewPostList()
|
||||
postList.Posts[post1.Id] = post1
|
||||
postList.Posts[post2.Id] = post2
|
||||
postList.Posts[post3.Id] = post3
|
||||
postList.Order = []string{post1.Id, post2.Id, post3.Id}
|
||||
|
||||
appErr := th.App.FilterPostsByChannelPermissions(th.Context, postList, guestUser.Id)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, postList.Posts, 3)
|
||||
require.Len(t, postList.Order, 3)
|
||||
})
|
||||
|
||||
t.Run("should filter posts when guest does not have read_channel_content permission", func(t *testing.T) {
|
||||
channelGuestRole, appErr := th.App.GetRoleByName(context.Background(), model.ChannelGuestRoleId)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
originalPermissions := make([]string, len(channelGuestRole.Permissions))
|
||||
copy(originalPermissions, channelGuestRole.Permissions)
|
||||
|
||||
newPermissions := []string{}
|
||||
for _, perm := range channelGuestRole.Permissions {
|
||||
if perm != model.PermissionReadChannelContent.Id && perm != model.PermissionReadChannel.Id {
|
||||
newPermissions = append(newPermissions, perm)
|
||||
}
|
||||
}
|
||||
|
||||
_, appErr = th.App.PatchRole(channelGuestRole, &model.RolePatch{
|
||||
Permissions: &newPermissions,
|
||||
})
|
||||
require.Nil(t, appErr)
|
||||
|
||||
defer func() {
|
||||
_, err := th.App.PatchRole(channelGuestRole, &model.RolePatch{
|
||||
Permissions: &originalPermissions,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
}()
|
||||
|
||||
postList := model.NewPostList()
|
||||
postList.Posts[post1.Id] = post1
|
||||
postList.Posts[post2.Id] = post2
|
||||
postList.Posts[post3.Id] = post3
|
||||
postList.Order = []string{post1.Id, post2.Id, post3.Id}
|
||||
|
||||
appErr = th.App.FilterPostsByChannelPermissions(th.Context, postList, guestUser.Id)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, postList.Posts, 0)
|
||||
require.Len(t, postList.Order, 0)
|
||||
})
|
||||
|
||||
t.Run("should handle empty post list", func(t *testing.T) {
|
||||
postList := model.NewPostList()
|
||||
appErr := th.App.FilterPostsByChannelPermissions(th.Context, postList, th.BasicUser.Id)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, postList.Posts, 0)
|
||||
require.Len(t, postList.Order, 0)
|
||||
})
|
||||
|
||||
t.Run("should handle nil post list", func(t *testing.T) {
|
||||
appErr := th.App.FilterPostsByChannelPermissions(th.Context, nil, th.BasicUser.Id)
|
||||
require.Nil(t, appErr)
|
||||
})
|
||||
|
||||
t.Run("should handle posts with empty channel IDs", func(t *testing.T) {
|
||||
postList := model.NewPostList()
|
||||
postWithoutChannel := &model.Post{
|
||||
Id: model.NewId(),
|
||||
ChannelId: "",
|
||||
Message: "test",
|
||||
}
|
||||
postList.Posts[postWithoutChannel.Id] = postWithoutChannel
|
||||
postList.Order = []string{postWithoutChannel.Id}
|
||||
|
||||
appErr := th.App.FilterPostsByChannelPermissions(th.Context, postList, th.BasicUser.Id)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, postList.Posts, 0)
|
||||
require.Len(t, postList.Order, 0)
|
||||
})
|
||||
|
||||
t.Run("should handle posts from non-existent channels", func(t *testing.T) {
|
||||
postList := model.NewPostList()
|
||||
postWithInvalidChannel := &model.Post{
|
||||
Id: model.NewId(),
|
||||
ChannelId: model.NewId(),
|
||||
Message: "test",
|
||||
}
|
||||
postList.Posts[postWithInvalidChannel.Id] = postWithInvalidChannel
|
||||
postList.Order = []string{postWithInvalidChannel.Id}
|
||||
|
||||
appErr := th.App.FilterPostsByChannelPermissions(th.Context, postList, th.BasicUser.Id)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, postList.Posts, 0)
|
||||
require.Len(t, postList.Order, 0)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user