From b3c2ecd9b9209413e7272b2fcd7bd3d04f2f85f4 Mon Sep 17 00:00:00 2001 From: Pradeep Murugesan Date: Wed, 25 Jul 2018 14:31:41 +0200 Subject: added the custom icon and username for the outgoing webhook and its response (#9141) * 8272 added the username and icon as part of the model and persisted the same * 8272 added the custome icon and name when set to the web hook response * 8272 changed the infinte loop to timeout after 5 seconds * 8272 fixed review comments --- app/webhook_test.go | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) (limited to 'app/webhook_test.go') diff --git a/app/webhook_test.go b/app/webhook_test.go index 8931100ac..85c52b144 100644 --- a/app/webhook_test.go +++ b/app/webhook_test.go @@ -11,6 +11,9 @@ import ( "github.com/stretchr/testify/require" "github.com/mattermost/mattermost-server/model" + "net/http" + "net/http/httptest" + "time" ) func TestCreateIncomingWebhookForChannel(t *testing.T) { @@ -470,3 +473,181 @@ func TestSplitWebhookPost(t *testing.T) { }) } } + +func TestCreateOutGoingWebhookWithUsernameAndIconURL(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + outgoingWebhook := model.OutgoingWebhook{ + ChannelId: th.BasicChannel.Id, + TeamId: th.BasicChannel.TeamId, + CallbackURLs: []string{"http://nowhere.com"}, + Username: "some-user-name", + IconURL: "http://some-icon/", + DisplayName: "some-display-name", + Description: "some-description", + CreatorId: th.BasicUser.Id, + } + + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOutgoingWebhooks = true }) + + createdHook, err := th.App.CreateOutgoingWebhook(&outgoingWebhook) + + if err != nil { + t.Fatalf("should not have failed: %v", err.Error()) + } + + assert.NotNil(t, createdHook, "should not be null") + + assert.Equal(t, createdHook.ChannelId, outgoingWebhook.ChannelId) + assert.Equal(t, createdHook.TeamId, outgoingWebhook.TeamId) + assert.Equal(t, createdHook.CallbackURLs, outgoingWebhook.CallbackURLs) + assert.Equal(t, createdHook.Username, outgoingWebhook.Username) + assert.Equal(t, createdHook.IconURL, outgoingWebhook.IconURL) + assert.Equal(t, createdHook.DisplayName, outgoingWebhook.DisplayName) + assert.Equal(t, createdHook.Description, outgoingWebhook.Description) + +} + +func TestTriggerOutGoingWebhookWithUsernameAndIconURL(t *testing.T) { + + getPayload := func(hook *model.OutgoingWebhook, th *TestHelper, channel *model.Channel) *model.OutgoingWebhookPayload { + return &model.OutgoingWebhookPayload{ + Token: hook.Token, + TeamId: hook.TeamId, + TeamDomain: th.BasicTeam.Name, + ChannelId: channel.Id, + ChannelName: channel.Name, + Timestamp: th.BasicPost.CreateAt, + UserId: th.BasicPost.UserId, + UserName: th.BasicUser.Username, + PostId: th.BasicPost.Id, + Text: th.BasicPost.Message, + TriggerWord: "Abracadabra", + FileIds: strings.Join(th.BasicPost.FileIds, ","), + } + } + + waitUntilWebhookResposeIsCreatedAsPost := func(channel *model.Channel, th *TestHelper, t *testing.T, createdPost chan *model.Post) { + go func() { + for i := 0; i < 5; i++ { + time.Sleep(time.Second) + posts, _ := th.App.GetPosts(channel.Id, 0, 5) + if len(posts.Posts) > 0 { + for _, post := range posts.Posts { + createdPost <- post + return + } + } + } + }() + } + + type TestCaseOutgoing struct { + EnablePostUsernameOverride bool + EnablePostIconOverride bool + ExpectedUsername string + ExpectedIconUrl string + WebhookResponse *model.OutgoingWebhookResponse + } + + createOutgoingWebhook := func(channel *model.Channel, testCallBackUrl string, th *TestHelper) (*model.OutgoingWebhook, *model.AppError) { + outgoingWebhook := model.OutgoingWebhook{ + ChannelId: channel.Id, + TeamId: channel.TeamId, + CallbackURLs: []string{testCallBackUrl}, + Username: "some-user-name", + IconURL: "http://some-icon/", + DisplayName: "some-display-name", + Description: "some-description", + CreatorId: th.BasicUser.Id, + TriggerWords: []string{"Abracadabra"}, + ContentType: "application/json", + } + + return th.App.CreateOutgoingWebhook(&outgoingWebhook) + } + + getTestCases := func() map[string]TestCaseOutgoing { + + webHookResponse := "sample response text from test server" + testCasesOutgoing := map[string]TestCaseOutgoing{ + + "Should override username and Icon": { + EnablePostUsernameOverride: true, + EnablePostIconOverride: true, + ExpectedUsername: "some-user-name", + ExpectedIconUrl: "http://some-icon/", + }, + "Should not override username and Icon": { + EnablePostUsernameOverride: false, + EnablePostIconOverride: false, + }, + "Should not override username and Icon if the webhook response already has it": { + EnablePostUsernameOverride: true, + EnablePostIconOverride: true, + ExpectedUsername: "webhookuser", + ExpectedIconUrl: "http://webhok/icon", + WebhookResponse: &model.OutgoingWebhookResponse{Text: &webHookResponse, Username: "webhookuser", IconURL: "http://webhok/icon"}, + }, + } + return testCasesOutgoing + } + + th := Setup().InitBasic() + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost 127.0.0.1" + }) + createdPost := make(chan *model.Post) + + for name, testCase := range getTestCases() { + t.Run(name, func(t *testing.T) { + + th.App.UpdateConfig(func(cfg *model.Config) { + cfg.ServiceSettings.EnableOutgoingWebhooks = true + cfg.ServiceSettings.EnablePostUsernameOverride = testCase.EnablePostUsernameOverride + cfg.ServiceSettings.EnablePostIconOverride = testCase.EnablePostIconOverride + }) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if testCase.WebhookResponse != nil { + w.Write([]byte(testCase.WebhookResponse.ToJson())) + } else { + w.Write([]byte(`{"text": "sample response text from test server"}`)) + } + })) + defer ts.Close() + + channel := th.CreateChannel(th.BasicTeam) + hook, _ := createOutgoingWebhook(channel, ts.URL, th) + payload := getPayload(hook, th, channel) + + th.App.TriggerWebhook(payload, hook, th.BasicPost, channel) + + waitUntilWebhookResposeIsCreatedAsPost(channel, th, t, createdPost) + + select { + case webhookPost := <-createdPost: + assert.Equal(t, webhookPost.Message, "sample response text from test server") + assert.Equal(t, webhookPost.Props["from_webhook"], "true") + if testCase.ExpectedIconUrl != "" { + assert.Equal(t, webhookPost.Props["override_icon_url"], testCase.ExpectedIconUrl) + } else { + assert.Nil(t, webhookPost.Props["override_icon_url"]) + } + + if testCase.ExpectedUsername != "" { + assert.Equal(t, webhookPost.Props["override_username"], testCase.ExpectedUsername) + } else { + assert.Nil(t, webhookPost.Props["override_username"]) + } + case <-time.After(5 * time.Second): + t.Fatal("Timeout, webhook response not created as post") + } + + }) + } + +} -- cgit v1.2.3-1-g7c22