summaryrefslogtreecommitdiffstats
path: root/api/post_test.go
diff options
context:
space:
mode:
authorThomas Balthazar <tbalthazar@users.noreply.github.com>2016-05-26 17:37:54 +0200
committerJoram Wilander <jwawilander@gmail.com>2016-05-26 11:37:54 -0400
commit934ff34ccb99641beb2bb1ea73bca836fadbdd1b (patch)
tree935002f8b011eb85f71cfa5952967b2c63b735e9 /api/post_test.go
parent6bd9996dafcd1bed80b8e952126c0cae39c36fb3 (diff)
downloadchat-934ff34ccb99641beb2bb1ea73bca836fadbdd1b.tar.gz
chat-934ff34ccb99641beb2bb1ea73bca836fadbdd1b.tar.bz2
chat-934ff34ccb99641beb2bb1ea73bca836fadbdd1b.zip
Send Post ID in webhooks (#3120)
Diffstat (limited to 'api/post_test.go')
-rw-r--r--api/post_test.go137
1 files changed, 134 insertions, 3 deletions
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