diff options
author | Jesse Hallam <jesse.hallam@gmail.com> | 2018-10-10 13:56:54 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2018-10-10 10:56:54 -0700 |
commit | e87965f39d2ce6dbd0e7883c387956413c663f6a (patch) | |
tree | 62f2fe9f1ae2bc4c4c2f9e31f9a929c14e53ede0 /app/plugin_commands_test.go | |
parent | c36e85c9126b921cf00e578ac70c1f1ee0153abd (diff) | |
download | chat-e87965f39d2ce6dbd0e7883c387956413c663f6a.tar.gz chat-e87965f39d2ce6dbd0e7883c387956413c663f6a.tar.bz2 chat-e87965f39d2ce6dbd0e7883c387956413c663f6a.zip |
MM-11905: delete plugin commands on removal (#9601)
* defer plugin tear down for testing
* test expected plugin command unregistration
* MM-11905: uninstall plugin commands on remove
Diffstat (limited to 'app/plugin_commands_test.go')
-rw-r--r-- | app/plugin_commands_test.go | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/app/plugin_commands_test.go b/app/plugin_commands_test.go new file mode 100644 index 000000000..cc9184277 --- /dev/null +++ b/app/plugin_commands_test.go @@ -0,0 +1,104 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "testing" + + "github.com/mattermost/mattermost-server/model" + "github.com/stretchr/testify/require" +) + +func TestPluginCommand(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + args := &model.CommandArgs{} + args.TeamId = th.BasicTeam.Id + args.ChannelId = th.BasicChannel.Id + args.UserId = th.BasicUser.Id + args.Command = "/plugin" + + t.Run("error before plugin command registered", func(t *testing.T) { + _, err := th.App.ExecuteCommand(args) + require.NotNil(t, err) + }) + + t.Run("command handled by plugin", func(t *testing.T) { + th.App.UpdateConfig(func(cfg *model.Config) { + cfg.PluginSettings.Plugins["testloadpluginconfig"] = map[string]interface{}{ + "TeamId": args.TeamId, + } + }) + + tearDown, pluginIds, activationErrors := SetAppEnvironmentWithPlugins(t, []string{` + package main + + import ( + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/model" + ) + + type configuration struct { + TeamId string + } + + type MyPlugin struct { + plugin.MattermostPlugin + + configuration configuration + } + + func (p *MyPlugin) OnConfigurationChange() error { + p.API.LogError("hello") + if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil { + return err + } + + return nil + } + + func (p *MyPlugin) OnActivate() error { + p.API.LogError("team", "team", p.configuration.TeamId) + err := p.API.RegisterCommand(&model.Command{ + TeamId: p.configuration.TeamId, + Trigger: "plugin", + DisplayName: "Plugin Command", + }) + if err != nil { + p.API.LogError("error", "err", err) + } + p.API.LogDebug("team", "team", p.configuration.TeamId) + + return err + } + + func (p *MyPlugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { + return &model.CommandResponse{ + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + Text: "text", + }, nil + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } + `}, th.App, th.App.NewPluginAPI) + defer tearDown() + require.Len(t, activationErrors, 1) + require.Nil(t, nil, activationErrors[0]) + + resp, err := th.App.ExecuteCommand(args) + require.Nil(t, err) + require.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, resp.ResponseType) + require.Equal(t, "text", resp.Text) + + th.App.RemovePlugin(pluginIds[0]) + }) + + t.Run("error after plugin command unregistered", func(t *testing.T) { + _, err := th.App.ExecuteCommand(args) + require.NotNil(t, err) + }) +} |