Adding test command
Этот коммит содержится в:
159
api/command.go
159
api/command.go
@@ -4,7 +4,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
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("/list_team_commands", ApiUserRequired(listTeamCommands)).Methods("GET")
|
||||||
sr.Handle("/regen_token", ApiUserRequired(regenCommandToken)).Methods("POST")
|
sr.Handle("/regen_token", ApiUserRequired(regenCommandToken)).Methods("POST")
|
||||||
sr.Handle("/delete", ApiUserRequired(deleteCommand)).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) {
|
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, " ")
|
parts := strings.Split(command, " ")
|
||||||
trigger := parts[0][1:]
|
trigger := parts[0][1:]
|
||||||
|
message := strings.Join(parts[1:], " ")
|
||||||
provider := GetCommandProvidersProvider(trigger)
|
provider := GetCommandProvidersProvider(trigger)
|
||||||
|
|
||||||
if provider != nil {
|
if provider != nil {
|
||||||
message := strings.Join(parts[1:], " ")
|
|
||||||
response := provider.DoCommand(c, channelId, message)
|
response := provider.DoCommand(c, channelId, message)
|
||||||
|
handleResponse(c, w, response, channelId)
|
||||||
if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL {
|
return
|
||||||
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()))
|
|
||||||
} else {
|
} else {
|
||||||
c.Err = model.NewAppError("command", "Command with a trigger of '"+trigger+"' not found", "")
|
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)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
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")
|
c.LogAudit("success")
|
||||||
w.Write([]byte(model.MapToJson(props)))
|
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()))
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/store"
|
"github.com/mattermost/platform/store"
|
||||||
@@ -206,3 +207,68 @@ func TestDeleteCommand(t *testing.T) {
|
|||||||
t.Fatal("delete didn't work properly")
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user