github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/app/plugin_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"errors"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/gorilla/mux"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/mattermost/mattermost-server/model"
    18  	"github.com/mattermost/mattermost-server/plugin"
    19  	"github.com/mattermost/mattermost-server/plugin/plugintest"
    20  )
    21  
    22  func TestPluginKeyValueStore(t *testing.T) {
    23  	th := Setup().InitBasic()
    24  	defer th.TearDown()
    25  
    26  	pluginId := "testpluginid"
    27  
    28  	assert.Nil(t, th.App.SetPluginKey(pluginId, "key", []byte("test")))
    29  	ret, err := th.App.GetPluginKey(pluginId, "key")
    30  	assert.Nil(t, err)
    31  	assert.Equal(t, []byte("test"), ret)
    32  
    33  	// Test inserting over existing entries
    34  	assert.Nil(t, th.App.SetPluginKey(pluginId, "key", []byte("test2")))
    35  
    36  	// Test getting non-existent key
    37  	ret, err = th.App.GetPluginKey(pluginId, "notakey")
    38  	assert.Nil(t, err)
    39  	assert.Nil(t, ret)
    40  
    41  	assert.Nil(t, th.App.DeletePluginKey(pluginId, "stringkey"))
    42  	assert.Nil(t, th.App.DeletePluginKey(pluginId, "intkey"))
    43  	assert.Nil(t, th.App.DeletePluginKey(pluginId, "postkey"))
    44  	assert.Nil(t, th.App.DeletePluginKey(pluginId, "notrealkey"))
    45  }
    46  
    47  func TestServePluginRequest(t *testing.T) {
    48  	th := Setup().InitBasic()
    49  	defer th.TearDown()
    50  
    51  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false })
    52  
    53  	w := httptest.NewRecorder()
    54  	r := httptest.NewRequest("GET", "/plugins/foo/bar", nil)
    55  	th.App.ServePluginRequest(w, r)
    56  	assert.Equal(t, http.StatusNotImplemented, w.Result().StatusCode)
    57  }
    58  
    59  func TestHandlePluginRequest(t *testing.T) {
    60  	th := Setup().InitBasic()
    61  	defer th.TearDown()
    62  
    63  	th.App.UpdateConfig(func(cfg *model.Config) {
    64  		*cfg.PluginSettings.Enable = false
    65  		*cfg.ServiceSettings.EnableUserAccessTokens = true
    66  	})
    67  
    68  	token, err := th.App.CreateUserAccessToken(&model.UserAccessToken{
    69  		UserId: th.BasicUser.Id,
    70  	})
    71  	require.Nil(t, err)
    72  
    73  	var assertions func(*http.Request)
    74  	router := mux.NewRouter()
    75  	router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}/{anything:.*}", func(_ http.ResponseWriter, r *http.Request) {
    76  		th.App.servePluginRequest(nil, r, func(_ http.ResponseWriter, r *http.Request) {
    77  			assertions(r)
    78  		})
    79  	})
    80  
    81  	r := httptest.NewRequest("GET", "/plugins/foo/bar", nil)
    82  	r.Header.Add("Authorization", "Bearer "+token.Token)
    83  	assertions = func(r *http.Request) {
    84  		assert.Equal(t, "/bar", r.URL.Path)
    85  		assert.Equal(t, th.BasicUser.Id, r.Header.Get("Mattermost-User-Id"))
    86  	}
    87  	router.ServeHTTP(nil, r)
    88  
    89  	r = httptest.NewRequest("GET", "/plugins/foo/bar?a=b&access_token="+token.Token+"&c=d", nil)
    90  	assertions = func(r *http.Request) {
    91  		assert.Equal(t, "/bar", r.URL.Path)
    92  		assert.Equal(t, "a=b&c=d", r.URL.RawQuery)
    93  		assert.Equal(t, th.BasicUser.Id, r.Header.Get("Mattermost-User-Id"))
    94  	}
    95  	router.ServeHTTP(nil, r)
    96  
    97  	r = httptest.NewRequest("GET", "/plugins/foo/bar?a=b&access_token=asdf&c=d", nil)
    98  	assertions = func(r *http.Request) {
    99  		assert.Equal(t, "/bar", r.URL.Path)
   100  		assert.Equal(t, "a=b&c=d", r.URL.RawQuery)
   101  		assert.Empty(t, r.Header.Get("Mattermost-User-Id"))
   102  	}
   103  	router.ServeHTTP(nil, r)
   104  }
   105  
   106  type testPlugin struct {
   107  	plugintest.Hooks
   108  }
   109  
   110  func (p *testPlugin) OnConfigurationChange() error {
   111  	return nil
   112  }
   113  
   114  func (p *testPlugin) OnDeactivate() error {
   115  	return nil
   116  }
   117  
   118  type pluginCommandTestPlugin struct {
   119  	testPlugin
   120  
   121  	TeamId string
   122  }
   123  
   124  func (p *pluginCommandTestPlugin) OnActivate(api plugin.API) error {
   125  	if err := api.RegisterCommand(&model.Command{
   126  		Trigger: "foo",
   127  		TeamId:  p.TeamId,
   128  	}); err != nil {
   129  		return err
   130  	}
   131  	if err := api.RegisterCommand(&model.Command{
   132  		Trigger: "foo2",
   133  		TeamId:  p.TeamId,
   134  	}); err != nil {
   135  		return err
   136  	}
   137  	return api.UnregisterCommand(p.TeamId, "foo2")
   138  }
   139  
   140  func (p *pluginCommandTestPlugin) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
   141  	if args.Command == "/foo" {
   142  		return &model.CommandResponse{
   143  			Text: "bar",
   144  		}, nil
   145  	}
   146  	return nil, model.NewAppError("ExecuteCommand", "this is an error", nil, "", http.StatusBadRequest)
   147  }
   148  
   149  func TestPluginCommands(t *testing.T) {
   150  	th := Setup().InitBasic()
   151  	defer th.TearDown()
   152  
   153  	th.InstallPlugin(&model.Manifest{
   154  		Id: "foo",
   155  	}, &pluginCommandTestPlugin{
   156  		TeamId: th.BasicTeam.Id,
   157  	})
   158  
   159  	require.Nil(t, th.App.EnablePlugin("foo"))
   160  
   161  	resp, err := th.App.ExecuteCommand(&model.CommandArgs{
   162  		Command:   "/foo2",
   163  		TeamId:    th.BasicTeam.Id,
   164  		UserId:    th.BasicUser.Id,
   165  		ChannelId: th.BasicChannel.Id,
   166  	})
   167  	require.NotNil(t, err)
   168  	assert.Equal(t, http.StatusNotFound, err.StatusCode)
   169  
   170  	resp, err = th.App.ExecuteCommand(&model.CommandArgs{
   171  		Command:   "/foo",
   172  		TeamId:    th.BasicTeam.Id,
   173  		UserId:    th.BasicUser.Id,
   174  		ChannelId: th.BasicChannel.Id,
   175  	})
   176  	require.Nil(t, err)
   177  	assert.Equal(t, "bar", resp.Text)
   178  
   179  	resp, err = th.App.ExecuteCommand(&model.CommandArgs{
   180  		Command:   "/foo baz",
   181  		TeamId:    th.BasicTeam.Id,
   182  		UserId:    th.BasicUser.Id,
   183  		ChannelId: th.BasicChannel.Id,
   184  	})
   185  	require.NotNil(t, err)
   186  	require.Equal(t, "this is an error", err.Message)
   187  	assert.Nil(t, resp)
   188  
   189  	require.Nil(t, th.App.RemovePlugin("foo"))
   190  
   191  	resp, err = th.App.ExecuteCommand(&model.CommandArgs{
   192  		Command:   "/foo",
   193  		TeamId:    th.BasicTeam.Id,
   194  		UserId:    th.BasicUser.Id,
   195  		ChannelId: th.BasicChannel.Id,
   196  	})
   197  	require.NotNil(t, err)
   198  	assert.Equal(t, http.StatusNotFound, err.StatusCode)
   199  }
   200  
   201  type pluginBadActivation struct {
   202  	testPlugin
   203  }
   204  
   205  func (p *pluginBadActivation) OnActivate(api plugin.API) error {
   206  	return errors.New("won't activate for some reason")
   207  }
   208  
   209  func TestPluginBadActivation(t *testing.T) {
   210  	th := Setup().InitBasic()
   211  	defer th.TearDown()
   212  
   213  	th.InstallPlugin(&model.Manifest{
   214  		Id: "foo",
   215  	}, &pluginBadActivation{})
   216  
   217  	t.Run("EnablePlugin bad activation", func(t *testing.T) {
   218  		err := th.App.EnablePlugin("foo")
   219  		assert.NotNil(t, err)
   220  		assert.True(t, strings.Contains(err.DetailedError, "won't activate for some reason"))
   221  	})
   222  }