github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/cmd/mattermost/commands/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/model"
    15  	"github.com/mattermost/mattermost-server/utils"
    16  )
    17  
    18  // There are no tests that actually run the Message Export job, because it can take a long time to complete depending
    19  // on the size of the database that the config is pointing to. As such, these tests just ensure that the CLI command
    20  // fails fast if invalid flags are supplied
    21  
    22  func TestMessageExportNotEnabled(t *testing.T) {
    23  	configPath := writeTempConfig(t, false)
    24  	defer os.RemoveAll(filepath.Dir(configPath))
    25  
    26  	// should fail fast because the feature isn't enabled
    27  	require.Error(t, RunCommand(t, "--config", configPath, "export", "schedule"))
    28  }
    29  
    30  func TestMessageExportInvalidFormat(t *testing.T) {
    31  	configPath := writeTempConfig(t, true)
    32  	defer os.RemoveAll(filepath.Dir(configPath))
    33  
    34  	// should fail fast because format isn't supported
    35  	require.Error(t, RunCommand(t, "--config", configPath, "--format", "not_actiance", "export", "schedule"))
    36  }
    37  
    38  func TestMessageExportNegativeExportFrom(t *testing.T) {
    39  	configPath := writeTempConfig(t, true)
    40  	defer os.RemoveAll(filepath.Dir(configPath))
    41  
    42  	// should fail fast because export from must be a valid timestamp
    43  	require.Error(t, RunCommand(t, "--config", configPath, "--format", "actiance", "--exportFrom", "-1", "export", "schedule"))
    44  }
    45  
    46  func TestMessageExportNegativeTimeoutSeconds(t *testing.T) {
    47  	configPath := writeTempConfig(t, true)
    48  	defer os.RemoveAll(filepath.Dir(configPath))
    49  
    50  	// should fail fast because timeout seconds must be a positive int
    51  	require.Error(t, RunCommand(t, "--config", configPath, "--format", "actiance", "--exportFrom", "0", "--timeoutSeconds", "-1", "export", "schedule"))
    52  }
    53  
    54  func writeTempConfig(t *testing.T, isMessageExportEnabled bool) string {
    55  	dir, err := ioutil.TempDir("", "")
    56  	require.NoError(t, err)
    57  
    58  	utils.TranslationsPreInit()
    59  	config, _, _, appErr := utils.LoadConfig("config.json")
    60  	require.Nil(t, appErr)
    61  	config.MessageExportSettings.EnableExport = model.NewBool(isMessageExportEnabled)
    62  	configPath := filepath.Join(dir, "foo.json")
    63  	require.NoError(t, ioutil.WriteFile(configPath, []byte(config.ToJson()), 0600))
    64  
    65  	return configPath
    66  }