APIv4 DELETE /commands/{command_id} (#6012)

Этот коммит содержится в:
Saturnino Abril
2017-04-10 22:27:10 +09:00
коммит произвёл Harrison Healey
родитель 38eced0290
Коммит 3b1088f3bd
3 изменённых файлов: 111 добавлений и 0 удалений

Просмотреть файл

@@ -20,6 +20,7 @@ func InitCommand() {
BaseRoutes.Commands.Handle("", ApiSessionRequired(listCommands)).Methods("GET")
BaseRoutes.Command.Handle("", ApiSessionRequired(updateCommand)).Methods("PUT")
BaseRoutes.Command.Handle("", ApiSessionRequired(deleteCommand)).Methods("DELETE")
BaseRoutes.Team.Handle("/commands/autocomplete", ApiSessionRequired(listAutocompleteCommands)).Methods("GET")
}
@@ -99,6 +100,43 @@ func updateCommand(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(rcmd.ToJson()))
}
func deleteCommand(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireCommandId()
if c.Err != nil {
return
}
c.LogAudit("attempt")
cmd, err := app.GetCommand(c.Params.CommandId)
if err != nil {
c.Err = err
return
}
if !app.SessionHasPermissionToTeam(c.Session, cmd.TeamId, model.PERMISSION_MANAGE_SLASH_COMMANDS) {
c.LogAudit("fail - inappropriate permissions")
c.SetPermissionError(model.PERMISSION_MANAGE_SLASH_COMMANDS)
return
}
if c.Session.UserId != cmd.CreatorId && !app.SessionHasPermissionToTeam(c.Session, cmd.TeamId, model.PERMISSION_MANAGE_OTHERS_SLASH_COMMANDS) {
c.LogAudit("fail - inappropriate permissions")
c.SetPermissionError(model.PERMISSION_MANAGE_OTHERS_SLASH_COMMANDS)
return
}
err = app.DeleteCommand(cmd.Id)
if err != nil {
c.Err = err
return
}
c.LogAudit("success")
ReturnStatusOK(w)
}
func listCommands(c *Context, w http.ResponseWriter, r *http.Request) {
customOnly, failConv := strconv.ParseBool(r.URL.Query().Get("custom_only"))
if failConv != nil {

Просмотреть файл

@@ -146,6 +146,69 @@ func TestUpdateCommand(t *testing.T) {
CheckUnauthorizedStatus(t, resp)
}
func TestDeleteCommand(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.SystemAdminClient
user := th.SystemAdminUser
team := th.BasicTeam
enableCommands := *utils.Cfg.ServiceSettings.EnableCommands
defer func() {
utils.Cfg.ServiceSettings.EnableCommands = &enableCommands
}()
*utils.Cfg.ServiceSettings.EnableCommands = true
cmd1 := &model.Command{
CreatorId: user.Id,
TeamId: team.Id,
URL: "http://nowhere.com",
Method: model.COMMAND_METHOD_POST,
Trigger: "trigger1",
}
rcmd1, _ := app.CreateCommand(cmd1)
ok, resp := Client.DeleteCommand(rcmd1.Id)
CheckNoError(t, resp)
if !ok {
t.Fatal("should have returned true")
}
rcmd1, _ = app.GetCommand(rcmd1.Id)
if rcmd1 != nil {
t.Fatal("should be nil")
}
ok, resp = Client.DeleteCommand("junk")
CheckBadRequestStatus(t, resp)
if ok {
t.Fatal("should have returned false")
}
_, resp = Client.DeleteCommand(GenerateTestId())
CheckNotFoundStatus(t, resp)
cmd2 := &model.Command{
CreatorId: user.Id,
TeamId: team.Id,
URL: "http://nowhere.com",
Method: model.COMMAND_METHOD_POST,
Trigger: "trigger2",
}
rcmd2, _ := app.CreateCommand(cmd2)
_, resp = th.Client.DeleteCommand(rcmd2.Id)
CheckForbiddenStatus(t, resp)
Client.Logout()
_, resp = Client.DeleteCommand(rcmd2.Id)
CheckUnauthorizedStatus(t, resp)
}
func TestListCommands(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()

Просмотреть файл

@@ -2212,6 +2212,16 @@ func (c *Client4) UpdateCommand(cmd *Command) (*Command, *Response) {
}
}
// DeleteCommand deletes a command based on the provided command id string
func (c *Client4) DeleteCommand(commandId string) (bool, *Response) {
if r, err := c.DoApiDelete(c.GetCommandRoute(commandId)); err != nil {
return false, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return CheckStatusOK(r), BuildResponse(r)
}
}
// ListCommands will retrieve a list of commands available in the team.
func (c *Client4) ListCommands(teamId string, customOnly bool) ([]*Command, *Response) {
query := fmt.Sprintf("?team_id=%v&custom_only=%v", teamId, customOnly)