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