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