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