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