diff options
author | =Corey Hulen <corey@hulen.com> | 2016-01-15 10:58:51 -0600 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2016-01-15 10:58:51 -0600 |
commit | 6e2c1b7fd5248c6a4a91edcd59fa124c8d3c744a (patch) | |
tree | 759d6cff16d19944f97fac465b228941c6000432 /api | |
parent | cc71c3e70646d4e8da5e8477933fc0c1576a3087 (diff) | |
download | chat-6e2c1b7fd5248c6a4a91edcd59fa124c8d3c744a.tar.gz chat-6e2c1b7fd5248c6a4a91edcd59fa124c8d3c744a.tar.bz2 chat-6e2c1b7fd5248c6a4a91edcd59fa124c8d3c744a.zip |
Adding test command
Diffstat (limited to 'api')
-rw-r--r-- | api/command.go | 151 | ||||
-rw-r--r-- | api/command_test.go | 66 |
2 files changed, 201 insertions, 16 deletions
diff --git a/api/command.go b/api/command.go index 6e2133f34..faa7804f6 100644 --- a/api/command.go +++ b/api/command.go @@ -4,7 +4,9 @@ package api import ( + "io/ioutil" "net/http" + "net/url" "strings" l4g "github.com/alecthomas/log4go" @@ -45,6 +47,9 @@ func InitCommand(r *mux.Router) { sr.Handle("/list_team_commands", ApiUserRequired(listTeamCommands)).Methods("GET") sr.Handle("/regen_token", ApiUserRequired(regenCommandToken)).Methods("POST") sr.Handle("/delete", ApiUserRequired(deleteCommand)).Methods("POST") + + sr.Handle("/test", ApiAppHandler(testCommand)).Methods("POST") + sr.Handle("/test", ApiAppHandler(testCommand)).Methods("GET") } func listCommands(c *Context, w http.ResponseWriter, r *http.Request) { @@ -96,32 +101,126 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) { parts := strings.Split(command, " ") trigger := parts[0][1:] + message := strings.Join(parts[1:], " ") provider := GetCommandProvidersProvider(trigger) if provider != nil { - message := strings.Join(parts[1:], " ") + response := provider.DoCommand(c, channelId, message) + handleResponse(c, w, response, channelId) + return + } else { + chanChan := Srv.Store.Channel().Get(channelId) + teamChan := Srv.Store.Team().Get(c.Session.TeamId) + userChan := Srv.Store.User().Get(c.Session.UserId) + + if result := <-Srv.Store.Command().GetByTeam(c.Session.TeamId); result.Err != nil { + c.Err = result.Err + return + } else { + + var team *model.Team + if tr := <-teamChan; tr.Err != nil { + c.Err = tr.Err + return + } else { + team = tr.Data.(*model.Team) - if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL { - post := &model.Post{} - post.ChannelId = channelId - post.Message = response.Text - if _, err := CreatePost(c, post, true); err != nil { - c.Err = model.NewAppError("command", "An error while saving the command response to the channel", "") } - } else if response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL { - post := &model.Post{} - post.ChannelId = channelId - post.Message = "TODO_EPHEMERAL: " + response.Text - if _, err := CreatePost(c, post, true); err != nil { - c.Err = model.NewAppError("command", "An error while saving the command response to the channel", "") + + var user *model.User + if ur := <-userChan; ur.Err != nil { + c.Err = ur.Err + return + } else { + user = ur.Data.(*model.User) + } + + var channel *model.Channel + if cr := <-chanChan; cr.Err != nil { + c.Err = cr.Err + return + } else { + channel = cr.Data.(*model.Channel) } + + teamCmds := result.Data.([]*model.Command) + for _, cmd := range teamCmds { + if trigger == cmd.Trigger { + l4g.Debug("Executing cmd=" + trigger + " userId=" + c.Session.UserId) + + p := url.Values{} + p.Set("token", cmd.Token) + + p.Set("team_id", cmd.TeamId) + p.Set("team_domain", team.Name) + + p.Set("channel_id", channelId) + p.Set("channel_name", channel.Name) + + p.Set("user_id", c.Session.UserId) + p.Set("user_name", user.Username) + + p.Set("command", "/"+trigger) + p.Set("text", message) + p.Set("response_url", "not supported yet") + + method := "POST" + if cmd.Method == model.COMMAND_METHOD_GET { + method = "GET" + } + + client := &http.Client{} + req, _ := http.NewRequest(method, cmd.URL, strings.NewReader(p.Encode())) + req.Header.Set("Accept", "application/json") + if cmd.Method == model.COMMAND_METHOD_POST { + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + } + + if resp, err := client.Do(req); err != nil { + c.Err = model.NewAppError("command", "Command with a trigger of '"+trigger+"' failed", err.Error()) + } else { + if resp.StatusCode == http.StatusOK { + response := model.CommandResponseFromJson(resp.Body) + if response == nil { + c.Err = model.NewAppError("command", "Command with a trigger of '"+trigger+"' returned an empty response", "") + } else { + handleResponse(c, w, response, channelId) + } + } else { + body, _ := ioutil.ReadAll(resp.Body) + c.Err = model.NewAppError("command", "Command with a trigger of '"+trigger+"' returned response "+resp.Status, string(body)) + } + } + + return + } + } + } + } - w.Write([]byte(response.ToJson())) - } else { - c.Err = model.NewAppError("command", "Command with a trigger of '"+trigger+"' not found", "") + c.Err = model.NewAppError("command", "Command with a trigger of '"+trigger+"' not found", "") +} + +func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandResponse, channelId string) { + if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL { + post := &model.Post{} + post.ChannelId = channelId + post.Message = response.Text + if _, err := CreatePost(c, post, true); err != nil { + c.Err = model.NewAppError("command", "An error while saving the command response to the channel", "") + } + } else if response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL { + post := &model.Post{} + post.ChannelId = channelId + post.Message = "TODO_EPHEMERAL: " + response.Text + if _, err := CreatePost(c, post, true); err != nil { + c.Err = model.NewAppError("command", "An error while saving the command response to the channel", "") + } } + + w.Write([]byte(response.ToJson())) } func createCommand(c *Context, w http.ResponseWriter, r *http.Request) { @@ -278,3 +377,23 @@ func deleteCommand(c *Context, w http.ResponseWriter, r *http.Request) { c.LogAudit("success") w.Write([]byte(model.MapToJson(props))) } + +func testCommand(c *Context, w http.ResponseWriter, r *http.Request) { + r.ParseForm() + + msg := "" + if r.Method == "POST" { + msg = msg + "\ntoken=" + r.FormValue("token") + msg = msg + "\nteam_domain=" + r.FormValue("team_domain") + } else { + body, _ := ioutil.ReadAll(r.Body) + msg = string(body) + } + + rc := &model.CommandResponse{ + Text: "test command response " + msg, + ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, + } + + w.Write([]byte(rc.ToJson())) +} diff --git a/api/command_test.go b/api/command_test.go index e5e954170..541e62e51 100644 --- a/api/command_test.go +++ b/api/command_test.go @@ -5,6 +5,7 @@ package api import ( "testing" + "time" "github.com/mattermost/platform/model" "github.com/mattermost/platform/store" @@ -206,3 +207,68 @@ func TestDeleteCommand(t *testing.T) { t.Fatal("delete didn't work properly") } } + +func TestTestCommand(t *testing.T) { + Setup() + enableCommands := *utils.Cfg.ServiceSettings.EnableCommands + defer func() { + utils.Cfg.ServiceSettings.EnableCommands = &enableCommands + }() + *utils.Cfg.ServiceSettings.EnableCommands = true + + team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} + team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team) + + user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"} + user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user.Id)) + + c := &Context{} + c.RequestId = model.NewId() + c.IpAddress = "cmd_line" + UpdateRoles(c, user, model.ROLE_SYSTEM_ADMIN) + Client.LoginByEmail(team.Name, user.Email, "pwd") + + channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} + channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) + + cmd1 := &model.Command{ + URL: "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress + "/api/v1/commands/test", + Method: model.COMMAND_METHOD_POST, + Trigger: "test", + } + + cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command) + + r1 := Client.Must(Client.Command(channel1.Id, "/test", false)).Data.(*model.CommandResponse) + if r1 == nil { + t.Fatal("Test command failed to execute") + } + + time.Sleep(100 * time.Millisecond) + + p1 := Client.Must(Client.GetPosts(channel1.Id, 0, 2, "")).Data.(*model.PostList) + if len(p1.Order) != 1 { + t.Fatal("Test command failed to send") + } + + cmd2 := &model.Command{ + URL: "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress + "/api/v1/commands/test", + Method: model.COMMAND_METHOD_GET, + Trigger: "test2", + } + + cmd2 = Client.Must(Client.CreateCommand(cmd2)).Data.(*model.Command) + + r2 := Client.Must(Client.Command(channel1.Id, "/test2", false)).Data.(*model.CommandResponse) + if r2 == nil { + t.Fatal("Test2 command failed to execute") + } + + time.Sleep(100 * time.Millisecond) + + p2 := Client.Must(Client.GetPosts(channel1.Id, 0, 2, "")).Data.(*model.PostList) + if len(p2.Order) != 2 { + t.Fatal("Test command failed to send") + } +} |