github.com/keys-pub/mattermost-server@v4.10.10+incompatible/cmd/commands/message_export_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/mattermost/mattermost-server/cmd"
    15  	"github.com/mattermost/mattermost-server/model"
    16  	"github.com/mattermost/mattermost-server/utils"
    17  )
    18  
    19  // There are no tests that actually run the Message Export job, because it can take a long time to complete depending
    20  // on the size of the database that the config is pointing to. As such, these tests just ensure that the CLI command
    21  // fails fast if invalid flags are supplied
    22  
    23  func TestMessageExportNotEnabled(t *testing.T) {
    24  	configPath := writeTempConfig(t, false)
    25  	defer os.RemoveAll(filepath.Dir(configPath))
    26  
    27  	// should fail fast because the feature isn't enabled
    28  	require.Error(t, cmd.RunCommand(t, "--config", configPath, "export"))
    29  }
    30  
    31  func TestMessageExportInvalidFormat(t *testing.T) {
    32  	configPath := writeTempConfig(t, true)
    33  	defer os.RemoveAll(filepath.Dir(configPath))
    34  
    35  	// should fail fast because format isn't supported
    36  	require.Error(t, cmd.RunCommand(t, "--config", configPath, "--format", "not_actiance", "export"))
    37  }
    38  
    39  func TestMessageExportNegativeExportFrom(t *testing.T) {
    40  	configPath := writeTempConfig(t, true)
    41  	defer os.RemoveAll(filepath.Dir(configPath))
    42  
    43  	// should fail fast because export from must be a valid timestamp
    44  	require.Error(t, cmd.RunCommand(t, "--config", configPath, "--format", "actiance", "--exportFrom", "-1", "export"))
    45  }
    46  
    47  func TestMessageExportNegativeTimeoutSeconds(t *testing.T) {
    48  	configPath := writeTempConfig(t, true)
    49  	defer os.RemoveAll(filepath.Dir(configPath))
    50  
    51  	// should fail fast because timeout seconds must be a positive int
    52  	require.Error(t, cmd.RunCommand(t, "--config", configPath, "--format", "actiance", "--exportFrom", "0", "--timeoutSeconds", "-1", "export"))
    53  }
    54  
    55  func writeTempConfig(t *testing.T, isMessageExportEnabled bool) string {
    56  	dir, err := ioutil.TempDir("", "")
    57  	require.NoError(t, err)
    58  
    59  	utils.TranslationsPreInit()
    60  	config, _, _, appErr := utils.LoadConfig("config.json")
    61  	require.Nil(t, appErr)
    62  	config.MessageExportSettings.EnableExport = model.NewBool(isMessageExportEnabled)
    63  	configPath := filepath.Join(dir, "foo.json")
    64  	require.NoError(t, ioutil.WriteFile(configPath, []byte(config.ToJson()), 0600))
    65  
    66  	return configPath
    67  }