mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[MM-34216] app/import: do not break import process if a dm channel is invalid (#17391)
* app/import: do not break import process if a dm channel is invalid * update test headers Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
f6505e1ee6
commit
5f9870ac06
@@ -610,6 +610,11 @@ func (a *App) exportAllDirectChannels(writer io.Writer) *model.AppError {
|
||||
for _, channel := range channels {
|
||||
afterId = channel.Id
|
||||
|
||||
// Skip if there are no active members in the channel
|
||||
if len(*channel.Members) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip deleted.
|
||||
if channel.DeleteAt != 0 {
|
||||
continue
|
||||
|
||||
@@ -218,38 +218,69 @@ func TestExportAllUsers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExportDMChannel(t *testing.T) {
|
||||
th1 := Setup(t).InitBasic()
|
||||
t.Run("Export a DM channel to another server", func(t *testing.T) {
|
||||
th1 := Setup(t).InitBasic()
|
||||
defer th1.TearDown()
|
||||
|
||||
// DM Channel
|
||||
th1.CreateDmChannel(th1.BasicUser2)
|
||||
// DM Channel
|
||||
th1.CreateDmChannel(th1.BasicUser2)
|
||||
|
||||
var b bytes.Buffer
|
||||
err := th1.App.BulkExport(&b, "somePath", BulkExportOpts{})
|
||||
require.Nil(t, err)
|
||||
var b bytes.Buffer
|
||||
err := th1.App.BulkExport(&b, "somePath", BulkExportOpts{})
|
||||
require.Nil(t, err)
|
||||
|
||||
channels, nErr := th1.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
assert.Equal(t, 1, len(channels))
|
||||
channels, nErr := th1.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
assert.Equal(t, 1, len(channels))
|
||||
|
||||
th1.TearDown()
|
||||
th2 := Setup(t).InitBasic()
|
||||
defer th2.TearDown()
|
||||
|
||||
th2 := Setup(t)
|
||||
defer th2.TearDown()
|
||||
channels, nErr = th2.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
assert.Equal(t, 0, len(channels))
|
||||
|
||||
channels, nErr = th2.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
assert.Equal(t, 0, len(channels))
|
||||
// import the exported channel
|
||||
err, i := th2.App.BulkImport(&b, false, 5)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 0, i)
|
||||
|
||||
// import the exported channel
|
||||
err, i := th2.App.BulkImport(&b, false, 5)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 0, i)
|
||||
// Ensure the Members of the imported DM channel is the same was from the exported
|
||||
channels, nErr = th2.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
require.Equal(t, 1, len(channels))
|
||||
assert.ElementsMatch(t, []string{th1.BasicUser.Username, th1.BasicUser2.Username}, *channels[0].Members)
|
||||
})
|
||||
|
||||
// Ensure the Members of the imported DM channel is the same was from the exported
|
||||
channels, nErr = th2.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
assert.Equal(t, 1, len(channels))
|
||||
assert.ElementsMatch(t, []string{th1.BasicUser.Username, th1.BasicUser2.Username}, *channels[0].Members)
|
||||
t.Run("Invalid DM channel export", func(t *testing.T) {
|
||||
th1 := Setup(t).InitBasic()
|
||||
defer th1.TearDown()
|
||||
|
||||
// DM Channel
|
||||
th1.CreateDmChannel(th1.BasicUser2)
|
||||
|
||||
channels, nErr := th1.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
assert.Equal(t, 1, len(channels))
|
||||
|
||||
th1.App.PermanentDeleteUser(th1.BasicUser2)
|
||||
th1.App.PermanentDeleteUser(th1.BasicUser)
|
||||
|
||||
var b bytes.Buffer
|
||||
err := th1.App.BulkExport(&b, "somePath", BulkExportOpts{})
|
||||
require.Nil(t, err)
|
||||
|
||||
th2 := Setup(t).InitBasic()
|
||||
defer th2.TearDown()
|
||||
|
||||
// import the exported channel
|
||||
err, _ = th2.App.BulkImport(&b, true, 5)
|
||||
require.Nil(t, err)
|
||||
|
||||
channels, nErr = th2.App.Srv().Store.Channel().GetAllDirectChannelsForExportAfter(1000, "00000000")
|
||||
require.NoError(t, nErr)
|
||||
assert.Empty(t, channels)
|
||||
})
|
||||
}
|
||||
|
||||
func TestExportDMChannelToSelf(t *testing.T) {
|
||||
|
||||
@@ -23,11 +23,16 @@ const (
|
||||
)
|
||||
|
||||
func stopOnError(err LineImportWorkerError) bool {
|
||||
if err.Error.Id == "api.file.upload_file.large_image.app_error" {
|
||||
switch err.Error.Id {
|
||||
case "api.file.upload_file.large_image.app_error":
|
||||
mlog.Warn("Large image import error", mlog.Err(err.Error))
|
||||
return false
|
||||
case "app.import.validate_direct_channel_import_data.members_too_few.error", "app.import.validate_direct_channel_import_data.members_too_many.error":
|
||||
mlog.Warn("Invalid direct channel import data", mlog.Err(err.Error))
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func rewriteAttachmentPaths(files *[]AttachmentImportData, basePath string) {
|
||||
|
||||
@@ -139,6 +139,16 @@ func TestStopOnError(t *testing.T) {
|
||||
model.NewAppError("test", "api.file.upload_file.large_image.app_error", nil, "", http.StatusBadRequest),
|
||||
1,
|
||||
}))
|
||||
|
||||
assert.False(t, stopOnError(LineImportWorkerError{
|
||||
model.NewAppError("test", "app.import.validate_direct_channel_import_data.members_too_few.error", nil, "", http.StatusBadRequest),
|
||||
1,
|
||||
}))
|
||||
|
||||
assert.False(t, stopOnError(LineImportWorkerError{
|
||||
model.NewAppError("test", "app.import.validate_direct_channel_import_data.members_too_many.error", nil, "", http.StatusBadRequest),
|
||||
1,
|
||||
}))
|
||||
}
|
||||
|
||||
func TestImportBulkImport(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user