summaryrefslogtreecommitdiffstats
path: root/cmd/mattermost/commands/message_export_test.go
blob: 89ef45a6a913207ef6490d41009571a185f4a900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package commands

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/utils"
)

// There are no tests that actually run the Message Export job, because it can take a long time to complete depending
// on the size of the database that the config is pointing to. As such, these tests just ensure that the CLI command
// fails fast if invalid flags are supplied

func TestMessageExportNotEnabled(t *testing.T) {
	configPath := writeTempConfig(t, false)
	defer os.RemoveAll(filepath.Dir(configPath))

	// should fail fast because the feature isn't enabled
	require.Error(t, RunCommand(t, "--config", configPath, "export", "schedule"))
}

func TestMessageExportInvalidFormat(t *testing.T) {
	configPath := writeTempConfig(t, true)
	defer os.RemoveAll(filepath.Dir(configPath))

	// should fail fast because format isn't supported
	require.Error(t, RunCommand(t, "--config", configPath, "--format", "not_actiance", "export", "schedule"))
}

func TestMessageExportNegativeExportFrom(t *testing.T) {
	configPath := writeTempConfig(t, true)
	defer os.RemoveAll(filepath.Dir(configPath))

	// should fail fast because export from must be a valid timestamp
	require.Error(t, RunCommand(t, "--config", configPath, "--format", "actiance", "--exportFrom", "-1", "export", "schedule"))
}

func TestMessageExportNegativeTimeoutSeconds(t *testing.T) {
	configPath := writeTempConfig(t, true)
	defer os.RemoveAll(filepath.Dir(configPath))

	// should fail fast because timeout seconds must be a positive int
	require.Error(t, RunCommand(t, "--config", configPath, "--format", "actiance", "--exportFrom", "0", "--timeoutSeconds", "-1", "export", "schedule"))
}

func writeTempConfig(t *testing.T, isMessageExportEnabled bool) string {
	dir, err := ioutil.TempDir("", "")
	require.NoError(t, err)

	utils.TranslationsPreInit()
	config, _, _, appErr := utils.LoadConfig("config.json")
	require.Nil(t, appErr)
	config.MessageExportSettings.EnableExport = model.NewBool(isMessageExportEnabled)
	configPath := filepath.Join(dir, "foo.json")
	require.NoError(t, ioutil.WriteFile(configPath, []byte(config.ToJson()), 0600))

	return configPath
}