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 <serdaracikgoz86@gmail.com>
Co-authored-by: Miguel de la Cruz <miguel@mcrx.me>
Co-authored-by: mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
Ashish Bhate 2020-05-22 17:18:22 +05:30 committed by GitHub
parent ea86dc9a62
commit 33bfebc797
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 8 deletions

View file

@ -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))

41
api4/command_local.go Normal file
View file

@ -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()))
}

View file

@ -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)