From 33bfebc79730b46d4e060d05a26c8c824e88f7d2 Mon Sep 17 00:00:00 2001 From: Ashish Bhate Date: Fri, 22 May 2020 17:18:22 +0530 Subject: [PATCH] MM-24845/MM-24846: local mode handler for createCommand and listCommands (#14486) * Add local mode handler for createCommand * Add local mode handler for listCommands * Fix bad merge Co-authored-by: Ibrahim Serdar Acikgoz Co-authored-by: Miguel de la Cruz Co-authored-by: mattermod --- api4/api.go | 7 +++++-- api4/command_local.go | 41 +++++++++++++++++++++++++++++++++++++++++ api4/command_test.go | 25 +++++++++++++++++++------ 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 api4/command_local.go diff --git a/api4/api.go b/api4/api.go index 7cea10c6e0..4ec42d7332 100644 --- a/api4/api.go +++ b/api4/api.go @@ -276,15 +276,18 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app. api.BaseRoutes.Channels = api.BaseRoutes.ApiRoot.PathPrefix("/channels").Subrouter() - api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter() - api.BaseRoutes.Plugins = api.BaseRoutes.ApiRoot.PathPrefix("/plugins").Subrouter() api.BaseRoutes.Plugin = api.BaseRoutes.Plugins.PathPrefix("/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() + api.BaseRoutes.Commands = api.BaseRoutes.ApiRoot.PathPrefix("/commands").Subrouter() + + api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter() + api.InitTeamLocal() api.InitChannelLocal() api.InitLicenseLocal() api.InitConfigLocal() + api.InitCommandLocal() api.InitPluginLocal() root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404)) diff --git a/api4/command_local.go b/api4/command_local.go new file mode 100644 index 0000000000..06590f7c6c --- /dev/null +++ b/api4/command_local.go @@ -0,0 +1,41 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package api4 + +import ( + "net/http" + + "github.com/mattermost/mattermost-server/v5/audit" + "github.com/mattermost/mattermost-server/v5/model" +) + +func (api *API) InitCommandLocal() { + api.BaseRoutes.Commands.Handle("", api.ApiLocal(localCreateCommand)).Methods("POST") + api.BaseRoutes.Commands.Handle("", api.ApiLocal(listCommands)).Methods("GET") +} + +func localCreateCommand(c *Context, w http.ResponseWriter, r *http.Request) { + cmd := model.CommandFromJson(r.Body) + if cmd == nil { + c.SetInvalidParam("command") + return + } + + auditRec := c.MakeAuditRecord("localCreateCommand", audit.Fail) + defer c.LogAuditRec(auditRec) + c.LogAudit("attempt") + + rcmd, err := c.App.CreateCommand(cmd) + if err != nil { + c.Err = err + return + } + + auditRec.Success() + c.LogAudit("success") + auditRec.AddMeta("command", rcmd) + + w.WriteHeader(http.StatusCreated) + w.Write([]byte(rcmd.ToJson())) +} diff --git a/api4/command_test.go b/api4/command_test.go index bac8a1078f..85970b747a 100644 --- a/api4/command_test.go +++ b/api4/command_test.go @@ -20,6 +20,7 @@ func TestCreateCommand(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() Client := th.Client + LocalClient := th.LocalClient enableCommands := *th.App.Config().ServiceSettings.EnableCommands defer func() { @@ -47,6 +48,13 @@ func TestCreateCommand(t *testing.T) { CheckBadRequestStatus(t, resp) CheckErrorMessage(t, resp, "api.command.duplicate_trigger.app_error") + newCmd.Trigger = "Local" + localCreatedCmd, resp := LocalClient.CreateCommand(newCmd) + CheckNoError(t, resp) + CheckCreatedStatus(t, resp) + require.Equal(t, th.BasicUser.Id, localCreatedCmd.CreatorId, "local client: user ids didn't match") + require.Equal(t, th.BasicTeam.Id, localCreatedCmd.TeamId, "local client: team ids didn't match") + newCmd.Method = "Wrong" newCmd.Trigger = "testcommand" _, resp = th.SystemAdminClient.CreateCommand(newCmd) @@ -59,6 +67,11 @@ func TestCreateCommand(t *testing.T) { _, resp = th.SystemAdminClient.CreateCommand(newCmd) CheckNotImplementedStatus(t, resp) CheckErrorMessage(t, resp, "api.command.disabled.app_error") + + // Confirm that local clients can't override disable command setting + newCmd.Trigger = "LocalOverride" + _, resp = LocalClient.CreateCommand(newCmd) + CheckErrorMessage(t, resp, "api.command.disabled.app_error") } func TestUpdateCommand(t *testing.T) { @@ -271,8 +284,8 @@ func TestListCommands(t *testing.T) { _, resp := th.SystemAdminClient.CreateCommand(newCmd) CheckNoError(t, resp) - t.Run("ListSystemAndCustomCommands", func(t *testing.T) { - listCommands, resp := th.SystemAdminClient.ListCommands(th.BasicTeam.Id, false) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + listCommands, resp := c.ListCommands(th.BasicTeam.Id, false) CheckNoError(t, resp) foundEcho := false @@ -287,15 +300,15 @@ func TestListCommands(t *testing.T) { } require.True(t, foundEcho, "Couldn't find echo command") require.True(t, foundCustom, "Should list the custom command") - }) + }, "ListSystemAndCustomCommands") - t.Run("ListCustomOnlyCommands", func(t *testing.T) { - listCommands, resp := th.SystemAdminClient.ListCommands(th.BasicTeam.Id, true) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + listCommands, resp := c.ListCommands(th.BasicTeam.Id, true) CheckNoError(t, resp) require.Len(t, listCommands, 1, "Should list just one custom command") require.Equal(t, listCommands[0].Trigger, "custom_command", "Wrong custom command trigger") - }) + }, "ListCustomOnlyCommands") t.Run("UserWithNoPermissionForCustomCommands", func(t *testing.T) { _, resp := Client.ListCommands(th.BasicTeam.Id, true)