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