[MM-45272] Fix MM-45272 (#24701)
* Fix MM-45272 * Properly handle permalinks * Fix * Fix tests * Handle only not found case for team member * Fix lint * Use proper config value * Separate permission in several statements * Add tests * Fix lint * Revert changes on utils * Address feedback and more fixes * Address feedback * Fix test * Fix test and related bug * Fix and reorder test * Address feedback * Address feedback --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c395ec6245
Коммит
2ff0fe343e
@@ -1001,51 +1001,49 @@ func TestCreatePost(t *testing.T) {
|
||||
directChannel, err := th.App.createDirectChannel(th.Context, user1.Id, user2.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
referencedPost := &model.Post{
|
||||
ChannelId: th.BasicChannel.Id,
|
||||
Message: "hello world",
|
||||
UserId: th.BasicUser.Id,
|
||||
}
|
||||
|
||||
th.Context.Session().UserId = th.BasicUser.Id
|
||||
|
||||
referencedPost, err = th.App.CreatePost(th.Context, referencedPost, th.BasicChannel, false, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
permalink := fmt.Sprintf("%s/%s/pl/%s", *th.App.Config().ServiceSettings.SiteURL, th.BasicTeam.Name, referencedPost.Id)
|
||||
|
||||
testCases := []struct {
|
||||
Description string
|
||||
Channel *model.Channel
|
||||
Author string
|
||||
Assert func(t assert.TestingT, object any, msgAndArgs ...any) bool
|
||||
Length int
|
||||
}{
|
||||
{
|
||||
Description: "removes metadata from post for members who cannot read channel",
|
||||
Channel: directChannel,
|
||||
Author: user1.Id,
|
||||
Assert: assert.Nil,
|
||||
Length: 0,
|
||||
},
|
||||
{
|
||||
Description: "does not remove metadata from post for members who can read channel",
|
||||
Channel: th.BasicChannel,
|
||||
Author: th.BasicUser.Id,
|
||||
Assert: assert.NotNil,
|
||||
Length: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.Description, func(t *testing.T) {
|
||||
previewPost := &model.Post{
|
||||
referencedPost := &model.Post{
|
||||
ChannelId: testCase.Channel.Id,
|
||||
Message: permalink,
|
||||
Message: "hello world",
|
||||
UserId: testCase.Author,
|
||||
}
|
||||
|
||||
previewPost, err = th.App.CreatePost(th.Context, previewPost, testCase.Channel, false, false)
|
||||
referencedPost, err = th.App.CreatePost(th.Context, referencedPost, testCase.Channel, false, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
testCase.Assert(t, previewPost.Metadata.Embeds[0].Data)
|
||||
permalink := fmt.Sprintf("%s/%s/pl/%s", *th.App.Config().ServiceSettings.SiteURL, th.BasicTeam.Name, referencedPost.Id)
|
||||
previewPost := &model.Post{
|
||||
ChannelId: th.BasicChannel.Id,
|
||||
Message: permalink,
|
||||
UserId: th.BasicUser.Id,
|
||||
}
|
||||
|
||||
previewPost, err = th.App.CreatePost(th.Context, previewPost, th.BasicChannel, false, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Len(t, previewPost.Metadata.Embeds, testCase.Length)
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -1451,73 +1449,6 @@ func TestUpdatePost(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, testPost.GetProps(), model.StringInterface{"previewed_post": referencedPost.Id})
|
||||
})
|
||||
|
||||
t.Run("sanitizes post metadata appropriately", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.SiteURL = "http://mymattermost.com"
|
||||
})
|
||||
|
||||
th.AddUserToChannel(th.BasicUser, th.BasicChannel)
|
||||
|
||||
user1 := th.CreateUser()
|
||||
user2 := th.CreateUser()
|
||||
directChannel, err := th.App.createDirectChannel(th.Context, user1.Id, user2.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
referencedPost := &model.Post{
|
||||
ChannelId: th.BasicChannel.Id,
|
||||
Message: "hello world",
|
||||
UserId: th.BasicUser.Id,
|
||||
}
|
||||
|
||||
th.Context.Session().UserId = th.BasicUser.Id
|
||||
|
||||
referencedPost, err = th.App.CreatePost(th.Context, referencedPost, th.BasicChannel, false, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
permalink := fmt.Sprintf("%s/%s/pl/%s", *th.App.Config().ServiceSettings.SiteURL, th.BasicTeam.Name, referencedPost.Id)
|
||||
|
||||
testCases := []struct {
|
||||
Description string
|
||||
Channel *model.Channel
|
||||
Author string
|
||||
Assert func(t assert.TestingT, object any, msgAndArgs ...any) bool
|
||||
}{
|
||||
{
|
||||
Description: "removes metadata from post for members who cannot read channel",
|
||||
Channel: directChannel,
|
||||
Author: user1.Id,
|
||||
Assert: assert.Nil,
|
||||
},
|
||||
{
|
||||
Description: "does not remove metadata from post for members who can read channel",
|
||||
Channel: th.BasicChannel,
|
||||
Author: th.BasicUser.Id,
|
||||
Assert: assert.NotNil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.Description, func(t *testing.T) {
|
||||
previewPost := &model.Post{
|
||||
ChannelId: testCase.Channel.Id,
|
||||
UserId: testCase.Author,
|
||||
}
|
||||
|
||||
previewPost, err = th.App.CreatePost(th.Context, previewPost, testCase.Channel, false, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
previewPost.Message = permalink
|
||||
previewPost, err = th.App.UpdatePost(th.Context, previewPost, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
testCase.Assert(t, previewPost.Metadata.Embeds[0].Data)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSearchPostsForUser(t *testing.T) {
|
||||
@@ -3043,26 +2974,64 @@ func TestGetPostIfAuthorized(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
privateChannel := th.CreatePrivateChannel(th.Context, th.BasicTeam)
|
||||
post, err := th.App.CreatePost(th.Context, &model.Post{UserId: th.BasicUser.Id, ChannelId: privateChannel.Id, Message: "Hello"}, privateChannel, false, false)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, post)
|
||||
t.Run("Private channel", func(t *testing.T) {
|
||||
privateChannel := th.CreatePrivateChannel(th.Context, th.BasicTeam)
|
||||
post, err := th.App.CreatePost(th.Context, &model.Post{UserId: th.BasicUser.Id, ChannelId: privateChannel.Id, Message: "Hello"}, privateChannel, false, false)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, post)
|
||||
|
||||
session1, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser.Id, Props: model.StringMap{}})
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, session1)
|
||||
session1, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser.Id, Props: model.StringMap{}})
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, session1)
|
||||
|
||||
session2, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser2.Id, Props: model.StringMap{}})
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, session2)
|
||||
session2, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser2.Id, Props: model.StringMap{}})
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, session2)
|
||||
|
||||
// User is not authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session2, false)
|
||||
require.NotNil(t, err)
|
||||
// User is not authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session2, false)
|
||||
require.NotNil(t, err)
|
||||
|
||||
// User is authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session1, false)
|
||||
require.Nil(t, err)
|
||||
// User is authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session1, false)
|
||||
require.Nil(t, err)
|
||||
})
|
||||
|
||||
t.Run("Public channel", func(t *testing.T) {
|
||||
publicChannel := th.CreateChannel(th.Context, th.BasicTeam)
|
||||
post, err := th.App.CreatePost(th.Context, &model.Post{UserId: th.BasicUser.Id, ChannelId: publicChannel.Id, Message: "Hello"}, publicChannel, false, false)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, post)
|
||||
|
||||
session1, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser.Id, Props: model.StringMap{}})
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, session1)
|
||||
|
||||
session2, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser2.Id, Props: model.StringMap{}})
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, session2)
|
||||
|
||||
// User is authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session2, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
// User is authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session1, false)
|
||||
require.Nil(t, err)
|
||||
|
||||
th.App.UpdateConfig(func(c *model.Config) {
|
||||
b := true
|
||||
c.ComplianceSettings.Enable = &b
|
||||
})
|
||||
|
||||
// User is not authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session2, false)
|
||||
require.NotNil(t, err)
|
||||
|
||||
// User is authorized to get post
|
||||
_, err = th.App.GetPostIfAuthorized(th.Context, post.Id, session1, false)
|
||||
require.Nil(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShouldNotRefollowOnOthersReply(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user