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>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
ea86dc9a62
Коммит
33bfebc797
@@ -276,15 +276,18 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app.
|
|||||||
|
|
||||||
api.BaseRoutes.Channels = api.BaseRoutes.ApiRoot.PathPrefix("/channels").Subrouter()
|
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.Plugins = api.BaseRoutes.ApiRoot.PathPrefix("/plugins").Subrouter()
|
||||||
api.BaseRoutes.Plugin = api.BaseRoutes.Plugins.PathPrefix("/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").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.InitTeamLocal()
|
||||||
api.InitChannelLocal()
|
api.InitChannelLocal()
|
||||||
api.InitLicenseLocal()
|
api.InitLicenseLocal()
|
||||||
api.InitConfigLocal()
|
api.InitConfigLocal()
|
||||||
|
api.InitCommandLocal()
|
||||||
api.InitPluginLocal()
|
api.InitPluginLocal()
|
||||||
|
|
||||||
root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))
|
root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))
|
||||||
|
|||||||
41
api4/command_local.go
Обычный файл
41
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()))
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ func TestCreateCommand(t *testing.T) {
|
|||||||
th := Setup(t).InitBasic()
|
th := Setup(t).InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
Client := th.Client
|
Client := th.Client
|
||||||
|
LocalClient := th.LocalClient
|
||||||
|
|
||||||
enableCommands := *th.App.Config().ServiceSettings.EnableCommands
|
enableCommands := *th.App.Config().ServiceSettings.EnableCommands
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -47,6 +48,13 @@ func TestCreateCommand(t *testing.T) {
|
|||||||
CheckBadRequestStatus(t, resp)
|
CheckBadRequestStatus(t, resp)
|
||||||
CheckErrorMessage(t, resp, "api.command.duplicate_trigger.app_error")
|
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.Method = "Wrong"
|
||||||
newCmd.Trigger = "testcommand"
|
newCmd.Trigger = "testcommand"
|
||||||
_, resp = th.SystemAdminClient.CreateCommand(newCmd)
|
_, resp = th.SystemAdminClient.CreateCommand(newCmd)
|
||||||
@@ -59,6 +67,11 @@ func TestCreateCommand(t *testing.T) {
|
|||||||
_, resp = th.SystemAdminClient.CreateCommand(newCmd)
|
_, resp = th.SystemAdminClient.CreateCommand(newCmd)
|
||||||
CheckNotImplementedStatus(t, resp)
|
CheckNotImplementedStatus(t, resp)
|
||||||
CheckErrorMessage(t, resp, "api.command.disabled.app_error")
|
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) {
|
func TestUpdateCommand(t *testing.T) {
|
||||||
@@ -271,8 +284,8 @@ func TestListCommands(t *testing.T) {
|
|||||||
_, resp := th.SystemAdminClient.CreateCommand(newCmd)
|
_, resp := th.SystemAdminClient.CreateCommand(newCmd)
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
t.Run("ListSystemAndCustomCommands", func(t *testing.T) {
|
th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) {
|
||||||
listCommands, resp := th.SystemAdminClient.ListCommands(th.BasicTeam.Id, false)
|
listCommands, resp := c.ListCommands(th.BasicTeam.Id, false)
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
foundEcho := false
|
foundEcho := false
|
||||||
@@ -287,15 +300,15 @@ func TestListCommands(t *testing.T) {
|
|||||||
}
|
}
|
||||||
require.True(t, foundEcho, "Couldn't find echo command")
|
require.True(t, foundEcho, "Couldn't find echo command")
|
||||||
require.True(t, foundCustom, "Should list the custom command")
|
require.True(t, foundCustom, "Should list the custom command")
|
||||||
})
|
}, "ListSystemAndCustomCommands")
|
||||||
|
|
||||||
t.Run("ListCustomOnlyCommands", func(t *testing.T) {
|
th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) {
|
||||||
listCommands, resp := th.SystemAdminClient.ListCommands(th.BasicTeam.Id, true)
|
listCommands, resp := c.ListCommands(th.BasicTeam.Id, true)
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
require.Len(t, listCommands, 1, "Should list just one custom command")
|
require.Len(t, listCommands, 1, "Should list just one custom command")
|
||||||
require.Equal(t, listCommands[0].Trigger, "custom_command", "Wrong custom command trigger")
|
require.Equal(t, listCommands[0].Trigger, "custom_command", "Wrong custom command trigger")
|
||||||
})
|
}, "ListCustomOnlyCommands")
|
||||||
|
|
||||||
t.Run("UserWithNoPermissionForCustomCommands", func(t *testing.T) {
|
t.Run("UserWithNoPermissionForCustomCommands", func(t *testing.T) {
|
||||||
_, resp := Client.ListCommands(th.BasicTeam.Id, true)
|
_, resp := Client.ListCommands(th.BasicTeam.Id, true)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user