github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/commands/plugin_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  package commands
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/config"
    11  	"github.com/mattermost/mattermost-server/v5/utils/fileutils"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestPlugin(t *testing.T) {
    17  	th := Setup(t).InitBasic()
    18  	defer th.TearDown()
    19  
    20  	cfg := th.Config()
    21  	*cfg.PluginSettings.EnableUploads = true
    22  	*cfg.PluginSettings.Directory = "./test-plugins"
    23  	*cfg.PluginSettings.ClientDirectory = "./test-client-plugins"
    24  	th.SetConfig(cfg)
    25  
    26  	err := os.MkdirAll("./test-plugins", os.ModePerm)
    27  	require.Nil(t, err)
    28  	err = os.MkdirAll("./test-client-plugins", os.ModePerm)
    29  	require.Nil(t, err)
    30  
    31  	path, _ := fileutils.FindDir("tests")
    32  
    33  	output := th.CheckCommand(t, "plugin", "add", filepath.Join(path, "testplugin.tar.gz"))
    34  	assert.Contains(t, output, "Added plugin:")
    35  	output = th.CheckCommand(t, "plugin", "enable", "testplugin")
    36  	assert.Contains(t, output, "Enabled plugin: testplugin")
    37  
    38  	fs, err := config.NewFileStore(th.ConfigPath(), false)
    39  	require.Nil(t, err)
    40  	require.NotNil(t, fs.Get().PluginSettings.PluginStates["testplugin"])
    41  	assert.True(t, fs.Get().PluginSettings.PluginStates["testplugin"].Enable)
    42  	fs.Close()
    43  
    44  	output = th.CheckCommand(t, "plugin", "disable", "testplugin")
    45  	assert.Contains(t, output, "Disabled plugin: testplugin")
    46  	fs, err = config.NewFileStore(th.ConfigPath(), false)
    47  	require.Nil(t, err)
    48  	require.NotNil(t, fs.Get().PluginSettings.PluginStates["testplugin"])
    49  	assert.False(t, fs.Get().PluginSettings.PluginStates["testplugin"].Enable)
    50  	fs.Close()
    51  
    52  	th.CheckCommand(t, "plugin", "list")
    53  
    54  	th.CheckCommand(t, "plugin", "delete", "testplugin")
    55  }
    56  
    57  func TestPluginPublicKeys(t *testing.T) {
    58  	th := Setup(t).InitBasic()
    59  	defer th.TearDown()
    60  
    61  	cfg := th.Config()
    62  	cfg.PluginSettings.SignaturePublicKeyFiles = []string{"public-key"}
    63  	th.SetConfig(cfg)
    64  
    65  	output := th.CheckCommand(t, "plugin", "keys")
    66  	assert.Contains(t, output, "public-key")
    67  	assert.NotContains(t, output, "Plugin name:")
    68  }
    69  
    70  func TestPluginPublicKeyDetails(t *testing.T) {
    71  	th := Setup(t).InitBasic()
    72  	defer th.TearDown()
    73  
    74  	cfg := th.Config()
    75  	cfg.PluginSettings.SignaturePublicKeyFiles = []string{"public-key"}
    76  
    77  	th.SetConfig(cfg)
    78  
    79  	output := th.CheckCommand(t, "plugin", "keys", "--verbose", "true")
    80  	assert.Contains(t, output, "Plugin name: public-key")
    81  	output = th.CheckCommand(t, "plugin", "keys", "--verbose")
    82  	assert.Contains(t, output, "Plugin name: public-key")
    83  }
    84  
    85  func TestAddPluginPublicKeys(t *testing.T) {
    86  	th := Setup(t).InitBasic()
    87  	defer th.TearDown()
    88  
    89  	cfg := th.Config()
    90  	cfg.PluginSettings.SignaturePublicKeyFiles = []string{"public-key"}
    91  	th.SetConfig(cfg)
    92  
    93  	err := th.RunCommand(t, "plugin", "keys", "add", "pk1")
    94  	assert.NotNil(t, err)
    95  }
    96  
    97  func TestDeletePluginPublicKeys(t *testing.T) {
    98  	th := Setup(t).InitBasic()
    99  	defer th.TearDown()
   100  
   101  	cfg := th.Config()
   102  	cfg.PluginSettings.SignaturePublicKeyFiles = []string{"pk1"}
   103  	th.SetConfig(cfg)
   104  
   105  	output := th.CheckCommand(t, "plugin", "keys", "delete", "pk1")
   106  	assert.Contains(t, output, "Deleted public key: pk1")
   107  }
   108  
   109  func TestPluginPublicKeysFlow(t *testing.T) {
   110  	th := Setup(t).InitBasic()
   111  	defer th.TearDown()
   112  
   113  	path, _ := fileutils.FindDir("tests")
   114  	name := "test-public-key.plugin.gpg"
   115  	output := th.CheckCommand(t, "plugin", "keys", "add", filepath.Join(path, name))
   116  	assert.Contains(t, output, "Added public key: "+filepath.Join(path, name))
   117  
   118  	output = th.CheckCommand(t, "plugin", "keys")
   119  	assert.Contains(t, output, name)
   120  	assert.NotContains(t, output, "Plugin name:")
   121  
   122  	output = th.CheckCommand(t, "plugin", "keys", "--verbose")
   123  	assert.Contains(t, output, "Plugin name: "+name)
   124  
   125  	output = th.CheckCommand(t, "plugin", "keys", "delete", name)
   126  	assert.Contains(t, output, "Deleted public key: "+name)
   127  }