From 934ff34ccb99641beb2bb1ea73bca836fadbdd1b Mon Sep 17 00:00:00 2001 From: Thomas Balthazar Date: Thu, 26 May 2016 17:37:54 +0200 Subject: Send Post ID in webhooks (#3120) --- api/post.go | 2 + api/post_test.go | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 136 insertions(+), 3 deletions(-) diff --git a/api/post.go b/api/post.go index 92cbaf390..b20e7b47c 100644 --- a/api/post.go +++ b/api/post.go @@ -412,6 +412,8 @@ func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel p.Set("user_id", post.UserId) p.Set("user_name", user.Username) + p.Set("post_id", post.Id) + p.Set("text", post.Message) p.Set("trigger_word", firstWord) diff --git a/api/post_test.go b/api/post_test.go index b4c23ff06..b23511384 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -4,13 +4,17 @@ package api import ( - "github.com/mattermost/platform/model" - "github.com/mattermost/platform/utils" "net/http" - //"strings" + "net/http/httptest" + "strconv" + "strings" + "fmt" "testing" "time" + + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) func TestCreatePost(t *testing.T) { @@ -106,6 +110,133 @@ func TestCreatePost(t *testing.T) { } } +func TestCreatePostWithOutgoingHook(t *testing.T) { + th := Setup().InitSystemAdmin() + Client := th.SystemAdminClient + team := th.SystemAdminTeam + user := th.SystemAdminUser + channel := th.CreateChannel(Client, team) + + enableOutgoingHooks := utils.Cfg.ServiceSettings.EnableOutgoingWebhooks + defer func() { + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = enableOutgoingHooks + }() + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = true + + var hook *model.OutgoingWebhook + var post *model.Post + + // Create a test server that is the target of the outgoing webhook. It will + // validate the webhook body fields and write to the success channel on + // success/failure. + success := make(chan bool) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + t.Logf("Error parsing form: %q", err) + success <- false + return + } + + if got, want := r.Form.Get("token"), hook.Token; got != want { + t.Logf("Token is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("team_id"), hook.TeamId; got != want { + t.Logf("TeamId is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("team_domain"), team.Name; got != want { + t.Logf("TeamDomain is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("channel_id"), post.ChannelId; got != want { + t.Logf("ChannelId is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("channel_name"), channel.Name; got != want { + t.Logf("ChannelName is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("timestamp"), strconv.FormatInt(post.CreateAt/1000, 10); got != want { + t.Logf("Timestamp is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("user_id"), post.UserId; got != want { + t.Logf("UserId is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("user_name"), user.Username; got != want { + t.Logf("Username is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("post_id"), post.Id; got != want { + t.Logf("PostId is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("text"), post.Message; got != want { + t.Logf("Message is %s, should be %s", got, want) + success <- false + return + } + if got, want := r.Form.Get("trigger_word"), strings.Fields(post.Message)[0]; got != want { + t.Logf("TriggerWord is %s, should be %s", got, want) + success <- false + return + } + + success <- true + })) + defer ts.Close() + + // create an outgoing webhook, passing it the test server URL + triggerWord := "bingo" + hook = &model.OutgoingWebhook{ + ChannelId: channel.Id, + TriggerWords: []string{triggerWord}, + CallbackURLs: []string{ts.URL}, + } + + if result, err := Client.CreateOutgoingWebhook(hook); err != nil { + t.Fatal(err) + } else { + hook = result.Data.(*model.OutgoingWebhook) + } + + // create a post to trigger the webhook + message := triggerWord + " lorem ipusm" + post = &model.Post{ + ChannelId: channel.Id, + Message: message, + } + + if result, err := Client.CreatePost(post); err != nil { + t.Fatal(err) + } else { + post = result.Data.(*model.Post) + } + + // We wait for the test server to write to the success channel and we make + // the test fail if that doesn't happen before the timeout. + select { + case ok := <-success: + if !ok { + t.Fatal("Test server was sent an invalid webhook.") + } + case <-time.After(time.Second): + t.Fatal("Timeout, test server wasn't sent the webhook.") + } +} + func TestUpdatePost(t *testing.T) { th := Setup().InitBasic() Client := th.BasicClient -- cgit v1.2.3-1-g7c22