[MM-62079] Rewrite TestCreatePost (#29903)

Этот коммит содержится в:
Ben Schumacher
2025-01-28 10:02:03 +01:00
коммит произвёл GitHub
родитель 7cd025d1fb
Коммит d8d65b8ec7

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

@@ -38,70 +38,75 @@ func TestCreatePost(t *testing.T) {
defer th.TearDown() defer th.TearDown()
client := th.Client client := th.Client
post := &model.Post{ basicPost := func() *model.Post {
p := &model.Post{
ChannelId: th.BasicChannel.Id, ChannelId: th.BasicChannel.Id,
Message: "#hashtag a" + model.NewId() + "a", Message: "#hashtag a" + model.NewId() + "a",
Props: model.StringInterface{model.PropsAddChannelMember: "no good"},
DeleteAt: 101, DeleteAt: 101,
} }
p.AddProp(model.PropsAddChannelMember, "no good")
return p
}
rpost, resp, err2 := client.CreatePost(context.Background(), post) post := basicPost()
rootPost, resp2, err2 := client.CreatePost(context.Background(), post)
require.NoError(t, err2) require.NoError(t, err2)
CheckCreatedStatus(t, resp) CheckCreatedStatus(t, resp2)
require.NotNil(t, rootPost)
require.Equal(t, post.Message, rpost.Message, "message didn't match") require.Equal(t, post.Message, rootPost.Message, "message didn't match")
require.Equal(t, "#hashtag", rpost.Hashtags, "hashtag didn't match") require.Equal(t, "#hashtag", rootPost.Hashtags, "hashtag didn't match")
require.Empty(t, rpost.FileIds) require.Empty(t, rootPost.FileIds)
require.Equal(t, 0, int(rpost.EditAt), "newly created post shouldn't have EditAt set") require.Equal(t, 0, int(rootPost.EditAt), "newly created post shouldn't have EditAt set")
require.Nil(t, rpost.GetProp(model.PropsAddChannelMember), "newly created post shouldn't have Props['add_channel_member'] set") require.Nil(t, rootPost.GetProp(model.PropsAddChannelMember), "newly created post shouldn't have Props['add_channel_member'] set")
require.Equal(t, 0, int(rpost.DeleteAt), "newly created post shouldn't have DeleteAt set") require.Equal(t, 0, int(rootPost.DeleteAt), "newly created post shouldn't have DeleteAt set")
post.RootId = rpost.Id post = basicPost()
_, _, err2 = client.CreatePost(context.Background(), post) post.RootId = rootPost.Id
childPost, resp2, err2 := client.CreatePost(context.Background(), post)
require.NoError(t, err2) require.NoError(t, err2)
CheckCreatedStatus(t, resp2)
post.RootId = "junk" require.NotNil(t, childPost)
_, resp, err2 = client.CreatePost(context.Background(), post)
require.Error(t, err2)
CheckBadRequestStatus(t, resp)
post2 := &model.Post{ChannelId: th.BasicChannel2.Id, Message: "zz" + model.NewId() + "a", CreateAt: 123}
rpost2, _, _ := client.CreatePost(context.Background(), post2)
require.NotEqual(t, post2.CreateAt, rpost2.CreateAt, "create at should not match")
t.Run("with file uploaded by same user", func(t *testing.T) { t.Run("with file uploaded by same user", func(t *testing.T) {
fileResp, _, err := client.UploadFile(context.Background(), []byte("data"), th.BasicChannel.Id, "test") fileResp, resp, err := client.UploadFile(context.Background(), []byte("data"), th.BasicChannel.Id, "test")
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
fileId := fileResp.FileInfos[0].Id fileId := fileResp.FileInfos[0].Id
postWithFiles, _, err := client.CreatePost(context.Background(), &model.Post{ postWithFiles, resp, err := client.CreatePost(context.Background(), &model.Post{
ChannelId: th.BasicChannel.Id, ChannelId: th.BasicChannel.Id,
Message: "with files", Message: "with files",
FileIds: model.StringArray{fileId}, FileIds: model.StringArray{fileId},
}) })
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
assert.Equal(t, model.StringArray{fileId}, postWithFiles.FileIds) assert.Equal(t, model.StringArray{fileId}, postWithFiles.FileIds)
actualPostWithFiles, _, err := client.GetPost(context.Background(), postWithFiles.Id, "") actualPostWithFiles, resp, err := client.GetPost(context.Background(), postWithFiles.Id, "")
require.NoError(t, err) require.NoError(t, err)
CheckOKStatus(t, resp)
assert.Equal(t, model.StringArray{fileId}, actualPostWithFiles.FileIds) assert.Equal(t, model.StringArray{fileId}, actualPostWithFiles.FileIds)
}) })
t.Run("with file uploaded by different user", func(t *testing.T) { t.Run("with file uploaded by different user", func(t *testing.T) {
fileResp, _, err := th.SystemAdminClient.UploadFile(context.Background(), []byte("data"), th.BasicChannel.Id, "test") fileResp, resp, err := th.SystemAdminClient.UploadFile(context.Background(), []byte("data"), th.BasicChannel.Id, "test")
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
fileId := fileResp.FileInfos[0].Id fileId := fileResp.FileInfos[0].Id
postWithFiles, _, err := client.CreatePost(context.Background(), &model.Post{ postWithFiles, resp, err := client.CreatePost(context.Background(), &model.Post{
ChannelId: th.BasicChannel.Id, ChannelId: th.BasicChannel.Id,
Message: "with files", Message: "with files",
FileIds: model.StringArray{fileId}, FileIds: model.StringArray{fileId},
}) })
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
assert.Empty(t, postWithFiles.FileIds) assert.Empty(t, postWithFiles.FileIds)
actualPostWithFiles, _, err := client.GetPost(context.Background(), postWithFiles.Id, "") actualPostWithFiles, resp, err := client.GetPost(context.Background(), postWithFiles.Id, "")
require.NoError(t, err) require.NoError(t, err)
CheckOKStatus(t, resp)
assert.Empty(t, actualPostWithFiles.FileIds) assert.Empty(t, actualPostWithFiles.FileIds)
}) })
@@ -110,67 +115,73 @@ func TestCreatePost(t *testing.T) {
require.Nil(t, appErr) require.Nil(t, appErr)
fileId := fileInfo.Id fileId := fileInfo.Id
postWithFiles, _, err := client.CreatePost(context.Background(), &model.Post{ postWithFiles, resp, err := client.CreatePost(context.Background(), &model.Post{
ChannelId: th.BasicChannel.Id, ChannelId: th.BasicChannel.Id,
Message: "with files", Message: "with files",
FileIds: model.StringArray{fileId}, FileIds: model.StringArray{fileId},
}) })
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
assert.Equal(t, model.StringArray{fileId}, postWithFiles.FileIds) assert.Equal(t, model.StringArray{fileId}, postWithFiles.FileIds)
actualPostWithFiles, _, err := client.GetPost(context.Background(), postWithFiles.Id, "") actualPostWithFiles, resp, err := client.GetPost(context.Background(), postWithFiles.Id, "")
require.NoError(t, err) require.NoError(t, err)
CheckOKStatus(t, resp)
assert.Equal(t, model.StringArray{fileId}, actualPostWithFiles.FileIds) assert.Equal(t, model.StringArray{fileId}, actualPostWithFiles.FileIds)
}) })
t.Run("Create posts without the USE_CHANNEL_MENTIONS Permission - returns ephemeral message with mentions and no ephemeral message without mentions", func(t *testing.T) { t.Run("Create posts without the USE_CHANNEL_MENTIONS Permission - returns ephemeral message with mentions and no ephemeral message without mentions", func(t *testing.T) {
WebSocketClient, err := th.CreateWebSocketClient() wsClient := th.CreateConnectedWebSocketClient(t)
WebSocketClient.Listen()
require.NoError(t, err)
defaultPerms := th.SaveDefaultRolePermissions() defaultPerms := th.SaveDefaultRolePermissions()
defer th.RestoreDefaultRolePermissions(defaultPerms) defer th.RestoreDefaultRolePermissions(defaultPerms)
th.RemovePermissionFromRole(model.PermissionUseChannelMentions.Id, model.ChannelUserRoleId) th.RemovePermissionFromRole(model.PermissionUseChannelMentions.Id, model.ChannelUserRoleId)
post.RootId = rpost.Id post := basicPost()
post.RootId = rootPost.Id
post.Message = "a post with no channel mentions" post.Message = "a post with no channel mentions"
_, _, err = client.CreatePost(context.Background(), post) rPost, resp, err := client.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
require.NotNil(t, rPost)
// Message with no channel mentions should result in no ephemeral message // Message with no channel mentions should result in no ephemeral message
timeout := time.After(5 * time.Second) timeout := time.After(5 * time.Second)
waiting := true waiting := true
for waiting { for waiting {
select { select {
case event := <-WebSocketClient.EventChannel: case event := <-wsClient.EventChannel:
require.NotEqual(t, model.WebsocketEventEphemeralMessage, event.EventType(), "should not have ephemeral message event") require.NotEqual(t, model.WebsocketEventEphemeralMessage, event.EventType(), "should not have ephemeral message event")
case <-timeout: case <-timeout:
waiting = false waiting = false
} }
} }
post.RootId = rpost.Id
post.Message = "a post with @channel" post.Message = "a post with @channel"
_, _, err = client.CreatePost(context.Background(), post) rPost, resp, err = client.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
require.NotNil(t, rPost)
post.RootId = rpost.Id
post.Message = "a post with @all" post.Message = "a post with @all"
_, _, err = client.CreatePost(context.Background(), post) rPost, resp, err = client.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
require.NotNil(t, rPost)
post.RootId = rpost.Id
post.Message = "a post with @here" post.Message = "a post with @here"
_, _, err = client.CreatePost(context.Background(), post) rPost, resp, err = client.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
CheckCreatedStatus(t, resp)
require.NotNil(t, rPost)
timeout = time.After(5 * time.Second) timeout = time.After(5 * time.Second)
expectedEvents := 3 // 3 Posts created with @ mentions should result in 3 websocket events expectedEvents := 3 // 3 Posts created with @ mentions should result in 3 websocket events
gotEvents := 0 gotEvents := 0
for gotEvents < expectedEvents { for gotEvents < expectedEvents {
select { select {
case event := <-WebSocketClient.EventChannel: case event := <-wsClient.EventChannel:
if event.EventType() == model.WebsocketEventEphemeralMessage { if event.EventType() == model.WebsocketEventEphemeralMessage {
gotEvents++ gotEvents++
} }
@@ -191,7 +202,7 @@ func TestCreatePost(t *testing.T) {
*cfg.ServiceSettings.ExperimentalEnableHardenedMode = originalHardenedModeSetting *cfg.ServiceSettings.ExperimentalEnableHardenedMode = originalHardenedModeSetting
}) })
_, postResp, postErr := client.CreatePost(context.Background(), &model.Post{ rpost, postResp, postErr := client.CreatePost(context.Background(), &model.Post{
ChannelId: th.BasicChannel.Id, ChannelId: th.BasicChannel.Id,
Message: "with props", Message: "with props",
Props: model.StringInterface{model.PostPropsFromWebhook: "true"}, Props: model.StringInterface{model.PostPropsFromWebhook: "true"},
@@ -199,46 +210,80 @@ func TestCreatePost(t *testing.T) {
require.Error(t, postErr) require.Error(t, postErr)
CheckBadRequestStatus(t, postResp) CheckBadRequestStatus(t, postResp)
assert.Nil(t, rpost)
}) })
post.RootId = "" t.Run("invalid post type", func(t *testing.T) {
post := basicPost()
post.Type = model.PostTypeSystemGeneric post.Type = model.PostTypeSystemGeneric
_, resp, err := client.CreatePost(context.Background(), post) rpost, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
assert.Nil(t, rpost)
})
post.Type = "" t.Run("invalid rootId type", func(t *testing.T) {
post.RootId = rpost2.Id post := basicPost()
_, resp, err = client.CreatePost(context.Background(), post) post.RootId = "junk"
rpost, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
assert.Nil(t, rpost)
})
post.RootId = "" t.Run("RootId points to child post", func(t *testing.T) {
post := basicPost()
post.RootId = childPost.Id
rpost, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
assert.Nil(t, rpost)
})
t.Run("invalid ChannelId", func(t *testing.T) {
post := basicPost()
post.ChannelId = "junk" post.ChannelId = "junk"
_, resp, err = client.CreatePost(context.Background(), post) rpost, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
assert.Nil(t, rpost)
})
t.Run("invalid ChannelId", func(t *testing.T) {
post := basicPost()
post.ChannelId = model.NewId() post.ChannelId = model.NewId()
_, resp, err = client.CreatePost(context.Background(), post) rpost, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
assert.Nil(t, rpost)
})
t.Run("invalid payload", func(t *testing.T) {
r, err := client.DoAPIPost(context.Background(), "/posts", "garbage") r, err := client.DoAPIPost(context.Background(), "/posts", "garbage")
require.Error(t, err) require.Error(t, err)
require.Equal(t, http.StatusBadRequest, r.StatusCode) require.Equal(t, http.StatusBadRequest, r.StatusCode)
})
_, err = client.Logout(context.Background()) t.Run("not logged in", func(t *testing.T) {
resp, err := client.Logout(context.Background())
require.NoError(t, err) require.NoError(t, err)
_, resp, err = client.CreatePost(context.Background(), post) CheckOKStatus(t, resp)
post := basicPost()
rpost, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
assert.Nil(t, rpost)
})
post.ChannelId = th.BasicChannel.Id t.Run("CreateAt should match the one provided in the request", func(t *testing.T) {
post := basicPost()
post.CreateAt = 123 post.CreateAt = 123
rpost, _, err = th.SystemAdminClient.CreatePost(context.Background(), post) rpost, resp, err := th.SystemAdminClient.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, post.CreateAt, rpost.CreateAt, "create at should match") CheckCreatedStatus(t, resp)
assert.Equal(t, post.CreateAt, rpost.CreateAt, "create at should match")
})
t.Run("Should not be able to define the RemoteId of a post from the API", func(t *testing.T) { t.Run("Should not be able to define the RemoteId of a post from the API", func(t *testing.T) {
newPost := &model.Post{ newPost := &model.Post{