Added duplicated trigger validation (#3124)

Этот коммит содержится в:
David Lu
2016-05-27 08:35:55 -07:00
коммит произвёл Corey Hulen
родитель 1e7805b790
Коммит 0d0734ac98
7 изменённых файлов: 135 добавлений и 13 удалений

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

@@ -288,6 +288,26 @@ func createCommand(c *Context, w http.ResponseWriter, r *http.Request) {
cmd.CreatorId = c.Session.UserId
cmd.TeamId = c.TeamId
if result := <-Srv.Store.Command().GetByTeam(c.TeamId); result.Err != nil {
c.Err = result.Err
return
} else {
teamCmds := result.Data.([]*model.Command)
for _, existingCommand := range teamCmds {
if cmd.Trigger == existingCommand.Trigger {
c.Err = model.NewLocAppError("createCommand", "api.command.duplicate_trigger.app_error", nil, "")
return
}
}
for _, builtInProvider := range commandProviders {
builtInCommand := *builtInProvider.GetCommand(c)
if cmd.Trigger == builtInCommand.Trigger {
c.Err = model.NewLocAppError("createCommand", "api.command.duplicate_trigger.app_error", nil, "")
return
}
}
}
if result := <-Srv.Store.Command().Save(cmd); result.Err != nil {
c.Err = result.Err
return

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

@@ -45,16 +45,28 @@ func TestCreateCommand(t *testing.T) {
}()
*utils.Cfg.ServiceSettings.EnableCommands = true
cmd := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"}
cmd1 := &model.Command{
CreatorId: user.Id,
TeamId: team.Id,
URL: "http://nowhere.com",
Method: model.COMMAND_METHOD_POST,
Trigger: "trigger"}
if _, err := Client.CreateCommand(cmd); err == nil {
if _, err := Client.CreateCommand(cmd1); err == nil {
t.Fatal("should have failed because not admin")
}
Client = th.SystemAdminClient
cmd2 := &model.Command{
CreatorId: user.Id,
TeamId: team.Id,
URL: "http://nowhere.com",
Method: model.COMMAND_METHOD_POST,
Trigger: "trigger"}
var rcmd *model.Command
if result, err := Client.CreateCommand(cmd); err != nil {
if result, err := Client.CreateCommand(cmd2); err != nil {
t.Fatal(err)
} else {
rcmd = result.Data.(*model.Command)
@@ -68,16 +80,19 @@ func TestCreateCommand(t *testing.T) {
t.Fatal("team ids didn't match")
}
cmd = &model.Command{CreatorId: "123", TeamId: "456", URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"}
if result, err := Client.CreateCommand(cmd); err != nil {
t.Fatal(err)
} else {
if result.Data.(*model.Command).CreatorId != user.Id {
t.Fatal("bad user id wasn't overwritten")
}
if result.Data.(*model.Command).TeamId != team.Id {
t.Fatal("bad team id wasn't overwritten")
}
cmd3 := &model.Command{
CreatorId: "123",
TeamId: "456",
URL: "http://nowhere.com",
Method: model.COMMAND_METHOD_POST,
Trigger: "trigger"}
if _, err := Client.CreateCommand(cmd3); err == nil {
t.Fatal("trigger cannot be duplicated")
}
cmd4 := cmd3
if _, err := Client.CreateCommand(cmd4); err == nil {
t.Fatal("command cannot be duplicated")
}
}

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

@@ -214,6 +214,23 @@ func createOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if result := <-Srv.Store.Webhook().GetOutgoingByTeam(c.TeamId); result.Err != nil {
c.Err = result.Err
return
} else {
allHooks := result.Data.([]*model.OutgoingWebhook)
for _, existingOutHook := range allHooks {
urlIntersect := utils.StringArrayIntersection(existingOutHook.CallbackURLs, hook.CallbackURLs)
triggerIntersect := utils.StringArrayIntersection(existingOutHook.TriggerWords, hook.TriggerWords)
if existingOutHook.ChannelId == hook.ChannelId && len(urlIntersect) != 0 && len(triggerIntersect) != 0 {
c.Err = model.NewLocAppError("createOutgoingHook", "api.webhook.create_outgoing.intersect.app_error", nil, "")
return
}
}
}
if result := <-Srv.Store.Webhook().SaveOutgoing(hook); result.Err != nil {
c.Err = result.Err
return

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

@@ -262,6 +262,17 @@ func TestCreateOutgoingHook(t *testing.T) {
t.Fatal("team ids didn't match")
}
hook = &model.OutgoingWebhook{ChannelId: channel1.Id, TriggerWords: []string{"cats", "dogs"}, CallbackURLs: []string{"http://nowhere.com", "http://cats.com"}}
hook1 := &model.OutgoingWebhook{ChannelId: channel1.Id, TriggerWords: []string{"cats"}, CallbackURLs: []string{"http://nowhere.com"}}
if _, err := Client.CreateOutgoingWebhook(hook); err != nil {
t.Fatal("multiple trigger words and urls failed")
}
if _, err := Client.CreateOutgoingWebhook(hook1); err == nil {
t.Fatal("should have failed - duplicate trigger words and urls")
}
hook = &model.OutgoingWebhook{ChannelId: "junk", CallbackURLs: []string{"http://nowhere.com"}}
if _, err := Client.CreateOutgoingWebhook(hook); err == nil {
t.Fatal("should have failed - bad channel id")