* MM-23881: global drafts endpoints and ws events Adds endpoints: - create/update drafts - delete draft - get drafts Adds WS events: - draft_updated - draft_created - draft_deleted * Ordering and WS event name fixes * Adds PostID to the drafts table In the future the drafts will include edited posts, this commit adds the post id in the combined pkey of the table. * Fixes route for deleting a thread draft * Fixes failed checks * Fixes migrations * Fixes migration * Extract translation strings * Removes PostID since we won't sync editing posts * Fixes tests * Fixes i18n * Update migrations for global drafts * update branch with latest master changes * Add feature flag for global drafts * Set global drafts feature flag default to true * Added support for files in drafts * Fix failing i18n check * Added support for deleting files in drafts * Revert "Added support for deleting files in drafts" This reverts commit 45dfd04a760359de2e8814d652c9ef46daf994f6. * Triggering new test server * Add config setting 'AllowSyncedDrafts' for syncing drafts with server * Triggering new test server * Triggering new test server * Add guard for config setting and add initial tests * Fix i18n and lint errors * Triggering new test server * Add tests for drafts * fix lint issues * Add tests for model/draft * Triggering new test server * Triggering new test server * Trigger new test server * Address PR comments * Change left join to regular join in GetDraftsForUser * Fix broken test Maybe consider adding an inclDeleted field if we want to get deleted drafts in the future * fix translations * Add store tests for drafts * fix test naming * remove comment * update migrations * set feature flag default to false * update migrations Co-authored-by: Mylon Suren <mylonsuren@gmail.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
439 строки
14 KiB
Go
439 строки
14 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
"github.com/mattermost/mattermost-server/v6/utils/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetDraft(t *testing.T) {
|
|
os.Setenv("MM_FEATUREFLAGS_GLOBALDRAFTS", "true")
|
|
defer os.Unsetenv("MM_FEATUREFLAGS_GLOBALDRAFTS")
|
|
os.Setenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS", "true")
|
|
defer os.Unsetenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS")
|
|
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.Server.platform.SetConfigReadOnlyFF(false)
|
|
defer th.Server.platform.SetConfigReadOnlyFF(true)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
user := th.BasicUser
|
|
channel := th.BasicChannel
|
|
|
|
draft := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00001,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft",
|
|
}
|
|
|
|
_, upsertDraftErr := th.App.UpsertDraft(th.Context, draft, "")
|
|
assert.Nil(t, upsertDraftErr)
|
|
|
|
t.Run("get draft", func(t *testing.T) {
|
|
draftResp, err := th.App.GetDraft(user.Id, channel.Id, "")
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draft.Message, draftResp.Message)
|
|
assert.Equal(t, draft.ChannelId, draftResp.ChannelId)
|
|
})
|
|
|
|
t.Run("get draft feature flag", func(t *testing.T) {
|
|
os.Setenv("MM_FEATUREFLAGS_GLOBALDRAFTS", "false")
|
|
defer os.Unsetenv("MM_FEATUREFLAGS_GLOBALDRAFTS")
|
|
os.Setenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS", "false")
|
|
defer os.Unsetenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS")
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = false })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = false })
|
|
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
_, err := th.App.GetDraft(user.Id, channel.Id, "")
|
|
assert.NotNil(t, err)
|
|
})
|
|
}
|
|
|
|
func TestUpsertDraft(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.Server.platform.SetConfigReadOnlyFF(false)
|
|
defer th.Server.platform.SetConfigReadOnlyFF(true)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
user := th.BasicUser
|
|
channel := th.BasicChannel
|
|
|
|
draft1 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00001,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft1",
|
|
}
|
|
|
|
draft2 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00002,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft2",
|
|
}
|
|
|
|
_, createDraftErr := th.App.CreateDraft(th.Context, draft1, "")
|
|
assert.Nil(t, createDraftErr)
|
|
|
|
t.Run("upsert draft", func(t *testing.T) {
|
|
draftResp, err := th.App.UpsertDraft(th.Context, draft2, "")
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draft2.Message, draftResp.Message)
|
|
assert.Equal(t, draft2.ChannelId, draftResp.ChannelId)
|
|
assert.Equal(t, draft2.CreateAt, draftResp.CreateAt)
|
|
|
|
assert.NotEqual(t, draft1.UpdateAt, draftResp.UpdateAt)
|
|
})
|
|
|
|
t.Run("upsert draft feature flag", func(t *testing.T) {
|
|
os.Setenv("MM_FEATUREFLAGS_GLOBALDRAFTS", "false")
|
|
defer os.Unsetenv("MM_FEATUREFLAGS_GLOBALDRAFTS")
|
|
os.Setenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS", "false")
|
|
defer os.Unsetenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS")
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = false })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = false })
|
|
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
_, err := th.App.UpsertDraft(th.Context, draft1, "")
|
|
assert.NotNil(t, err)
|
|
})
|
|
}
|
|
|
|
func TestCreateDraft(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.Server.platform.SetConfigReadOnlyFF(false)
|
|
defer th.Server.platform.SetConfigReadOnlyFF(true)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
user := th.BasicUser
|
|
channel := th.BasicChannel
|
|
channel2 := th.CreateChannel(th.Context, th.BasicTeam)
|
|
th.AddUserToChannel(user, channel2)
|
|
|
|
draft1 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00001,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft",
|
|
}
|
|
|
|
draft2 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00001,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel2.Id,
|
|
Message: "draft2",
|
|
}
|
|
|
|
t.Run("create draft", func(t *testing.T) {
|
|
draftResp, err := th.App.CreateDraft(th.Context, draft1, "")
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draft1.Message, draftResp.Message)
|
|
assert.Equal(t, draft1.ChannelId, draftResp.ChannelId)
|
|
})
|
|
|
|
t.Run("create draft with files", func(t *testing.T) {
|
|
// upload file
|
|
sent, readFileErr := testutils.ReadTestFile("test.png")
|
|
require.NoError(t, readFileErr)
|
|
|
|
fileResp, uploadFileErr := th.App.UploadFile(th.Context, sent, channel.Id, "test.png")
|
|
assert.Nil(t, uploadFileErr)
|
|
|
|
draftWithFiles := draft2
|
|
draftWithFiles.FileIds = []string{fileResp.Id}
|
|
|
|
draftResp, err := th.App.CreateDraft(th.Context, draftWithFiles, "")
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draftWithFiles.Message, draftResp.Message)
|
|
assert.Equal(t, draftWithFiles.ChannelId, draftResp.ChannelId)
|
|
assert.ElementsMatch(t, draftWithFiles.FileIds, draftResp.FileIds)
|
|
})
|
|
|
|
t.Run("create draft feature flag", func(t *testing.T) {
|
|
os.Setenv("MM_FEATUREFLAGS_GLOBALDRAFTS", "false")
|
|
defer os.Unsetenv("MM_FEATUREFLAGS_GLOBALDRAFTS")
|
|
os.Setenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS", "false")
|
|
defer os.Unsetenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS")
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = false })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = false })
|
|
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
_, err := th.App.CreateDraft(th.Context, draft1, "")
|
|
assert.NotNil(t, err)
|
|
})
|
|
}
|
|
|
|
func TestUpdateDraft(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.Server.platform.SetConfigReadOnlyFF(false)
|
|
defer th.Server.platform.SetConfigReadOnlyFF(true)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
user := th.BasicUser
|
|
channel := th.BasicChannel
|
|
|
|
draft1 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00001,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft1",
|
|
}
|
|
|
|
draft2 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00002,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft2",
|
|
}
|
|
|
|
_, createDraftErr := th.App.CreateDraft(th.Context, draft1, "")
|
|
assert.Nil(t, createDraftErr)
|
|
|
|
t.Run("update draft", func(t *testing.T) {
|
|
draftResp, err := th.App.UpdateDraft(th.Context, draft2, "")
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draft2.Message, draftResp.Message)
|
|
assert.Equal(t, draft2.ChannelId, draftResp.ChannelId)
|
|
|
|
assert.NotEqual(t, draft1.UpdateAt, draftResp.UpdateAt)
|
|
})
|
|
|
|
t.Run("update draft with files", func(t *testing.T) {
|
|
// upload file
|
|
sent, readFileErr := testutils.ReadTestFile("test.png")
|
|
require.NoError(t, readFileErr)
|
|
|
|
fileResp, uploadFileErr := th.App.UploadFile(th.Context, sent, channel.Id, "test.png")
|
|
assert.Nil(t, uploadFileErr)
|
|
|
|
draftWithFiles := draft1
|
|
draftWithFiles.FileIds = []string{fileResp.Id}
|
|
|
|
draftResp, err := th.App.UpdateDraft(th.Context, draft1, "")
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draftWithFiles.Message, draftResp.Message)
|
|
assert.Equal(t, draftWithFiles.ChannelId, draftResp.ChannelId)
|
|
assert.ElementsMatch(t, draftWithFiles.FileIds, draftResp.FileIds)
|
|
})
|
|
|
|
t.Run("create draft feature flag", func(t *testing.T) {
|
|
os.Setenv("MM_FEATUREFLAGS_GLOBALDRAFTS", "false")
|
|
defer os.Unsetenv("MM_FEATUREFLAGS_GLOBALDRAFTS")
|
|
os.Setenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS", "false")
|
|
defer os.Unsetenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS")
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = false })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = false })
|
|
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
_, err := th.App.UpdateDraft(th.Context, draft1, "")
|
|
assert.NotNil(t, err)
|
|
})
|
|
}
|
|
|
|
func TestGetDraftsForUser(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.Server.platform.SetConfigReadOnlyFF(false)
|
|
defer th.Server.platform.SetConfigReadOnlyFF(true)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
user := th.BasicUser
|
|
channel := th.BasicChannel
|
|
channel2 := th.CreateChannel(th.Context, th.BasicTeam)
|
|
th.AddUserToChannel(user, channel2)
|
|
|
|
draft1 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00001,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft1",
|
|
}
|
|
|
|
draft2 := &model.Draft{
|
|
CreateAt: 00005,
|
|
UpdateAt: 00005,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel2.Id,
|
|
Message: "draft2",
|
|
}
|
|
|
|
_, createDraftErr1 := th.App.CreateDraft(th.Context, draft1, "")
|
|
assert.Nil(t, createDraftErr1)
|
|
|
|
_, createDraftErr2 := th.App.CreateDraft(th.Context, draft2, "")
|
|
assert.Nil(t, createDraftErr2)
|
|
|
|
t.Run("get drafts", func(t *testing.T) {
|
|
draftResp, err := th.App.GetDraftsForUser(user.Id, th.BasicTeam.Id)
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draft2.Message, draftResp[0].Message)
|
|
assert.Equal(t, draft2.ChannelId, draftResp[0].ChannelId)
|
|
|
|
assert.Equal(t, draft1.Message, draftResp[1].Message)
|
|
assert.Equal(t, draft1.ChannelId, draftResp[1].ChannelId)
|
|
})
|
|
|
|
t.Run("get drafts with files", func(t *testing.T) {
|
|
// upload file
|
|
sent, readFileErr := testutils.ReadTestFile("test.png")
|
|
require.NoError(t, readFileErr)
|
|
|
|
fileResp, updateDraftErr := th.App.UploadFile(th.Context, sent, channel.Id, "test.png")
|
|
assert.Nil(t, updateDraftErr)
|
|
|
|
draftWithFiles := draft1
|
|
draftWithFiles.FileIds = []string{fileResp.Id}
|
|
|
|
draftResp, updateDraftErr := th.App.UpdateDraft(th.Context, draft1, "")
|
|
assert.Nil(t, updateDraftErr)
|
|
|
|
assert.Equal(t, draftWithFiles.Message, draftResp.Message)
|
|
assert.Equal(t, draftWithFiles.ChannelId, draftResp.ChannelId)
|
|
assert.ElementsMatch(t, draftWithFiles.FileIds, draftResp.FileIds)
|
|
|
|
draftsWithFilesResp, err := th.App.GetDraftsForUser(user.Id, th.BasicTeam.Id)
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draftWithFiles.Message, draftsWithFilesResp[0].Message)
|
|
assert.Equal(t, draftWithFiles.ChannelId, draftsWithFilesResp[0].ChannelId)
|
|
assert.ElementsMatch(t, draftWithFiles.FileIds, draftsWithFilesResp[0].FileIds)
|
|
|
|
assert.Equal(t, fileResp.Name, draftsWithFilesResp[0].Metadata.Files[0].Name)
|
|
|
|
assert.Len(t, draftsWithFilesResp, 2)
|
|
})
|
|
|
|
t.Run("get drafts feature flag", func(t *testing.T) {
|
|
os.Setenv("MM_FEATUREFLAGS_GLOBALDRAFTS", "false")
|
|
defer os.Unsetenv("MM_FEATUREFLAGS_GLOBALDRAFTS")
|
|
os.Setenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS", "false")
|
|
defer os.Unsetenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS")
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = false })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = false })
|
|
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
_, err := th.App.GetDraftsForUser(user.Id, th.BasicTeam.Id)
|
|
assert.NotNil(t, err)
|
|
})
|
|
}
|
|
|
|
func TestDeleteDraft(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.Server.platform.SetConfigReadOnlyFF(false)
|
|
defer th.Server.platform.SetConfigReadOnlyFF(true)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
user := th.BasicUser
|
|
channel := th.BasicChannel
|
|
|
|
draft1 := &model.Draft{
|
|
CreateAt: 00001,
|
|
UpdateAt: 00001,
|
|
DeleteAt: 0,
|
|
UserId: user.Id,
|
|
ChannelId: channel.Id,
|
|
Message: "draft1",
|
|
}
|
|
|
|
_, createDraftErr := th.App.CreateDraft(th.Context, draft1, "")
|
|
assert.Nil(t, createDraftErr)
|
|
|
|
t.Run("delete draft", func(t *testing.T) {
|
|
draftResp, err := th.App.DeleteDraft(user.Id, channel.Id, "", "")
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, draft1.Message, draftResp.Message)
|
|
assert.Equal(t, draft1.ChannelId, draftResp.ChannelId)
|
|
|
|
assert.Equal(t, draft1.Message, draftResp.Message)
|
|
assert.Equal(t, draft1.ChannelId, draftResp.ChannelId)
|
|
})
|
|
|
|
t.Run("get drafts feature flag", func(t *testing.T) {
|
|
os.Setenv("MM_FEATUREFLAGS_GLOBALDRAFTS", "false")
|
|
defer os.Unsetenv("MM_FEATUREFLAGS_GLOBALDRAFTS")
|
|
os.Setenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS", "false")
|
|
defer os.Unsetenv("MM_SERVICESETTINGS_ALLOWSYNCEDDRAFTS")
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = false })
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = false })
|
|
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.GlobalDrafts = true })
|
|
defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowSyncedDrafts = true })
|
|
|
|
_, err := th.App.DeleteDraft(user.Id, channel.Id, "", "")
|
|
assert.NotNil(t, err)
|
|
})
|
|
}
|