github.com/vnforks/kid@v5.11.1+incompatible/app/plugin_commands_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 "testing" 8 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/utils" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestPluginCommand(t *testing.T) { 15 th := Setup(t).InitBasic() 16 defer th.TearDown() 17 18 args := &model.CommandArgs{} 19 args.TeamId = th.BasicTeam.Id 20 args.ChannelId = th.BasicChannel.Id 21 args.UserId = th.BasicUser.Id 22 args.Command = "/plugin" 23 24 t.Run("error before plugin command registered", func(t *testing.T) { 25 _, err := th.App.ExecuteCommand(args) 26 require.NotNil(t, err) 27 }) 28 29 t.Run("command handled by plugin", func(t *testing.T) { 30 th.App.UpdateConfig(func(cfg *model.Config) { 31 cfg.PluginSettings.Plugins["testloadpluginconfig"] = map[string]interface{}{ 32 "TeamId": args.TeamId, 33 } 34 }) 35 36 tearDown, pluginIds, activationErrors := SetAppEnvironmentWithPlugins(t, []string{` 37 package main 38 39 import ( 40 "github.com/mattermost/mattermost-server/plugin" 41 "github.com/mattermost/mattermost-server/model" 42 ) 43 44 type configuration struct { 45 TeamId string 46 } 47 48 type MyPlugin struct { 49 plugin.MattermostPlugin 50 51 configuration configuration 52 } 53 54 func (p *MyPlugin) OnConfigurationChange() error { 55 p.API.LogError("hello") 56 if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil { 57 return err 58 } 59 60 return nil 61 } 62 63 func (p *MyPlugin) OnActivate() error { 64 p.API.LogError("team", "team", p.configuration.TeamId) 65 err := p.API.RegisterCommand(&model.Command{ 66 TeamId: p.configuration.TeamId, 67 Trigger: "plugin", 68 DisplayName: "Plugin Command", 69 AutoComplete: true, 70 AutoCompleteDesc: "autocomplete", 71 }) 72 if err != nil { 73 p.API.LogError("error", "err", err) 74 } 75 p.API.LogDebug("team", "team", p.configuration.TeamId) 76 77 return err 78 } 79 80 func (p *MyPlugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { 81 return &model.CommandResponse{ 82 ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, 83 Text: "text", 84 }, nil 85 } 86 87 func main() { 88 plugin.ClientMain(&MyPlugin{}) 89 } 90 `}, th.App, th.App.NewPluginAPI) 91 defer tearDown() 92 require.Len(t, activationErrors, 1) 93 require.Nil(t, nil, activationErrors[0]) 94 95 resp, err := th.App.ExecuteCommand(args) 96 require.Nil(t, err) 97 require.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, resp.ResponseType) 98 require.Equal(t, "text", resp.Text) 99 100 err2 := th.App.DisablePlugin(pluginIds[0]) 101 require.Nil(t, err2) 102 103 commands, err3 := th.App.ListAutocompleteCommands(args.TeamId, utils.T) 104 require.Nil(t, err3) 105 106 for _, commands := range commands { 107 require.NotEqual(t, "plugin", commands.Trigger) 108 } 109 110 th.App.RemovePlugin(pluginIds[0]) 111 }) 112 113 t.Run("error after plugin command unregistered", func(t *testing.T) { 114 _, err := th.App.ExecuteCommand(args) 115 require.NotNil(t, err) 116 }) 117 }