github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/api4/plugin_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "bytes" 8 "encoding/json" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "testing" 13 14 "github.com/mattermost/mattermost-server/model" 15 "github.com/mattermost/mattermost-server/utils" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestPlugin(t *testing.T) { 21 pluginDir, err := ioutil.TempDir("", "mm-plugin-test") 22 require.NoError(t, err) 23 defer os.RemoveAll(pluginDir) 24 25 webappDir, err := ioutil.TempDir("", "mm-webapp-test") 26 require.NoError(t, err) 27 defer os.RemoveAll(webappDir) 28 29 th := Setup().InitBasic().InitSystemAdmin() 30 defer th.TearDown() 31 32 enablePlugins := *th.App.Config().PluginSettings.Enable 33 enableUploadPlugins := *th.App.Config().PluginSettings.EnableUploads 34 statesJson, _ := json.Marshal(th.App.Config().PluginSettings.PluginStates) 35 states := map[string]*model.PluginState{} 36 json.Unmarshal(statesJson, &states) 37 defer func() { 38 th.App.UpdateConfig(func(cfg *model.Config) { 39 *cfg.PluginSettings.Enable = enablePlugins 40 *cfg.PluginSettings.EnableUploads = enableUploadPlugins 41 cfg.PluginSettings.PluginStates = states 42 }) 43 th.App.SaveConfig(th.App.Config(), false) 44 }() 45 th.App.UpdateConfig(func(cfg *model.Config) { 46 *cfg.PluginSettings.Enable = true 47 *cfg.PluginSettings.EnableUploads = true 48 }) 49 50 th.App.InitPlugins(pluginDir, webappDir, nil) 51 defer func() { 52 th.App.ShutDownPlugins() 53 th.App.PluginEnv = nil 54 }() 55 56 path, _ := utils.FindDir("tests") 57 file, err := os.Open(filepath.Join(path, "testplugin.tar.gz")) 58 if err != nil { 59 t.Fatal(err) 60 } 61 defer file.Close() 62 63 // Successful upload 64 manifest, resp := th.SystemAdminClient.UploadPlugin(file) 65 defer os.RemoveAll("plugins/testplugin") 66 CheckNoError(t, resp) 67 68 assert.Equal(t, "testplugin", manifest.Id) 69 70 // Upload error cases 71 _, resp = th.SystemAdminClient.UploadPlugin(bytes.NewReader([]byte("badfile"))) 72 CheckBadRequestStatus(t, resp) 73 74 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) 75 _, resp = th.SystemAdminClient.UploadPlugin(file) 76 CheckNotImplementedStatus(t, resp) 77 78 th.App.UpdateConfig(func(cfg *model.Config) { 79 *cfg.PluginSettings.Enable = true 80 *cfg.PluginSettings.EnableUploads = false 81 }) 82 _, resp = th.SystemAdminClient.UploadPlugin(file) 83 CheckNotImplementedStatus(t, resp) 84 85 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.EnableUploads = true }) 86 _, resp = th.Client.UploadPlugin(file) 87 CheckForbiddenStatus(t, resp) 88 89 // Successful gets 90 pluginsResp, resp := th.SystemAdminClient.GetPlugins() 91 CheckNoError(t, resp) 92 93 found := false 94 for _, m := range pluginsResp.Inactive { 95 if m.Id == manifest.Id { 96 found = true 97 } 98 } 99 100 assert.True(t, found) 101 102 found = false 103 for _, m := range pluginsResp.Active { 104 if m.Id == manifest.Id { 105 found = true 106 } 107 } 108 109 assert.False(t, found) 110 111 // Successful activate 112 ok, resp := th.SystemAdminClient.ActivatePlugin(manifest.Id) 113 CheckNoError(t, resp) 114 assert.True(t, ok) 115 116 pluginsResp, resp = th.SystemAdminClient.GetPlugins() 117 CheckNoError(t, resp) 118 119 found = false 120 for _, m := range pluginsResp.Active { 121 if m.Id == manifest.Id { 122 found = true 123 } 124 } 125 126 assert.True(t, found) 127 128 // Activate error case 129 ok, resp = th.SystemAdminClient.ActivatePlugin("junk") 130 CheckBadRequestStatus(t, resp) 131 assert.False(t, ok) 132 133 // Successful deactivate 134 ok, resp = th.SystemAdminClient.DeactivatePlugin(manifest.Id) 135 CheckNoError(t, resp) 136 assert.True(t, ok) 137 138 pluginsResp, resp = th.SystemAdminClient.GetPlugins() 139 CheckNoError(t, resp) 140 141 found = false 142 for _, m := range pluginsResp.Inactive { 143 if m.Id == manifest.Id { 144 found = true 145 } 146 } 147 148 assert.True(t, found) 149 150 // Deactivate error case 151 ok, resp = th.SystemAdminClient.DeactivatePlugin("junk") 152 CheckBadRequestStatus(t, resp) 153 assert.False(t, ok) 154 155 // Get error cases 156 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) 157 _, resp = th.SystemAdminClient.GetPlugins() 158 CheckNotImplementedStatus(t, resp) 159 160 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = true }) 161 _, resp = th.Client.GetPlugins() 162 CheckForbiddenStatus(t, resp) 163 164 // Successful webapp get 165 _, resp = th.SystemAdminClient.ActivatePlugin(manifest.Id) 166 CheckNoError(t, resp) 167 168 manifests, resp := th.Client.GetWebappPlugins() 169 CheckNoError(t, resp) 170 171 found = false 172 for _, m := range manifests { 173 if m.Id == manifest.Id { 174 found = true 175 } 176 } 177 178 assert.True(t, found) 179 180 // Successful remove 181 ok, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) 182 CheckNoError(t, resp) 183 assert.True(t, ok) 184 185 // Remove error cases 186 ok, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) 187 CheckBadRequestStatus(t, resp) 188 assert.False(t, ok) 189 190 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) 191 _, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) 192 CheckNotImplementedStatus(t, resp) 193 194 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = true }) 195 _, resp = th.Client.RemovePlugin(manifest.Id) 196 CheckForbiddenStatus(t, resp) 197 198 _, resp = th.SystemAdminClient.RemovePlugin("bad.id") 199 CheckBadRequestStatus(t, resp) 200 }