summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2017-04-04 06:20:04 +0200
committerCorey Hulen <corey@hulen.com>2017-04-03 21:20:04 -0700
commit0a81dd9fff606d041ee08c62c655bf6966c7a66a (patch)
tree1521cebb48f15df64688d30a60023e10fa0077a3
parent348374fba5db8415d37d5cd8b897048b1300f415 (diff)
downloadchat-0a81dd9fff606d041ee08c62c655bf6966c7a66a.tar.gz
chat-0a81dd9fff606d041ee08c62c655bf6966c7a66a.tar.bz2
chat-0a81dd9fff606d041ee08c62c655bf6966c7a66a.zip
implement GET /teams/{team_id}/commands/autocomplete (#5951)
-rw-r--r--api4/command.go22
-rw-r--r--api4/command_test.go60
-rw-r--r--model/client4.go14
3 files changed, 96 insertions, 0 deletions
diff --git a/api4/command.go b/api4/command.go
index 41a85eac3..d6102bd70 100644
--- a/api4/command.go
+++ b/api4/command.go
@@ -18,6 +18,8 @@ func InitCommand() {
BaseRoutes.Commands.Handle("", ApiSessionRequired(createCommand)).Methods("POST")
BaseRoutes.Commands.Handle("", ApiSessionRequired(listCommands)).Methods("GET")
+
+ BaseRoutes.Team.Handle("/commands/autocomplete", ApiSessionRequired(listAutocompleteCommands)).Methods("GET")
}
func createCommand(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -91,3 +93,23 @@ func listCommands(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.CommandListToJson(commands)))
}
+
+func listAutocompleteCommands(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireTeamId()
+ if c.Err != nil {
+ return
+ }
+
+ if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) {
+ c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
+ return
+ }
+
+ commands, err := app.ListAutocompleteCommands(c.Params.TeamId, c.T)
+ if err != nil {
+ c.Err = err
+ return
+ }
+
+ w.Write([]byte(model.CommandListToJson(commands)))
+}
diff --git a/api4/command_test.go b/api4/command_test.go
index 3c8076470..75842886c 100644
--- a/api4/command_test.go
+++ b/api4/command_test.go
@@ -136,3 +136,63 @@ func TestListCommands(t *testing.T) {
}
})
}
+
+func TestListAutocompleteCommands(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+
+ newCmd := &model.Command{
+ CreatorId: th.BasicUser.Id,
+ TeamId: th.BasicTeam.Id,
+ URL: "http://nowhere.com",
+ Method: model.COMMAND_METHOD_POST,
+ Trigger: "custom_command"}
+
+ _, resp := th.SystemAdminClient.CreateCommand(newCmd)
+ CheckNoError(t, resp)
+
+ t.Run("ListAutocompleteCommandsOnly", func(t *testing.T) {
+ listCommands, resp := th.SystemAdminClient.ListAutocompleteCommands(th.BasicTeam.Id)
+ CheckNoError(t, resp)
+
+ foundEcho := false
+ foundCustom := false
+ for _, command := range listCommands {
+ if command.Trigger == "echo" {
+ foundEcho = true
+ }
+ if command.Trigger == "custom_command" {
+ foundCustom = true
+ }
+ }
+ if !foundEcho {
+ t.Fatal("Couldn't find echo command")
+ }
+ if foundCustom {
+ t.Fatal("Should not list the custom command")
+ }
+ })
+
+ t.Run("RegularUserCanListOnlySystemCommands", func(t *testing.T) {
+ listCommands, resp := Client.ListAutocompleteCommands(th.BasicTeam.Id)
+ CheckNoError(t, resp)
+
+ foundEcho := false
+ foundCustom := false
+ for _, command := range listCommands {
+ if command.Trigger == "echo" {
+ foundEcho = true
+ }
+ if command.Trigger == "custom_command" {
+ foundCustom = true
+ }
+ }
+ if !foundEcho {
+ t.Fatal("Couldn't find echo command")
+ }
+ if foundCustom {
+ t.Fatal("Should not list the custom command")
+ }
+ })
+}
diff --git a/model/client4.go b/model/client4.go
index 91ecaa134..cd9a6de75 100644
--- a/model/client4.go
+++ b/model/client4.go
@@ -78,6 +78,10 @@ func (c *Client4) GetTeamRoute(teamId string) string {
return fmt.Sprintf(c.GetTeamsRoute()+"/%v", teamId)
}
+func (c *Client4) GetTeamAutoCompleteCommandsRoute(teamId string) string {
+ return fmt.Sprintf(c.GetTeamsRoute()+"/%v/commands/autocomplete", teamId)
+}
+
func (c *Client4) GetTeamByNameRoute(teamName string) string {
return fmt.Sprintf(c.GetTeamsRoute()+"/name/%v", teamName)
}
@@ -2131,6 +2135,16 @@ func (c *Client4) ListCommands(teamId string, customOnly bool) ([]*Command, *Res
}
}
+// ListCommands will retrieve a list of commands available in the team.
+func (c *Client4) ListAutocompleteCommands(teamId string) ([]*Command, *Response) {
+ if r, err := c.DoApiGet(c.GetTeamAutoCompleteCommandsRoute(teamId), ""); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return CommandListFromJson(r.Body), BuildResponse(r)
+ }
+}
+
// Status Section
// GetUserStatus returns a user based on the provided user id string.