From 9c0575ce6ef662c18ad7eb91bf6084c6fac1b7ae Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 24 Oct 2017 18:36:31 -0500 Subject: PLT-7599: webhook post splitting (#7707) * webhook post splitting * style fix * update old webhook test --- app/webhook_test.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'app/webhook_test.go') diff --git a/app/webhook_test.go b/app/webhook_test.go index d6293e2b7..8dc90b49b 100644 --- a/app/webhook_test.go +++ b/app/webhook_test.go @@ -4,8 +4,12 @@ package app import ( + "strings" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/utils" ) @@ -50,3 +54,95 @@ func TestCreateWebhookPost(t *testing.T) { t.Fatal("should have failed - bad post type") } } + +func TestSplitWebhookPost(t *testing.T) { + type TestCase struct { + Post *model.Post + Expected []*model.Post + } + + for name, tc := range map[string]TestCase{ + "LongPost": { + Post: &model.Post{ + Message: strings.Repeat("本", model.POST_MESSAGE_MAX_RUNES*3/2), + }, + Expected: []*model.Post{ + { + Message: strings.Repeat("本", model.POST_MESSAGE_MAX_RUNES), + }, + { + Message: strings.Repeat("本", model.POST_MESSAGE_MAX_RUNES/2), + }, + }, + }, + "LongPostAndMultipleAttachments": { + Post: &model.Post{ + Message: strings.Repeat("本", model.POST_MESSAGE_MAX_RUNES*3/2), + Props: map[string]interface{}{ + "attachments": []*model.SlackAttachment{ + &model.SlackAttachment{ + Text: strings.Repeat("本", 1000), + }, + &model.SlackAttachment{ + Text: strings.Repeat("本", 2000), + }, + &model.SlackAttachment{ + Text: strings.Repeat("本", model.POST_PROPS_MAX_USER_RUNES-1000), + }, + }, + }, + }, + Expected: []*model.Post{ + { + Message: strings.Repeat("本", model.POST_MESSAGE_MAX_RUNES), + }, + { + Message: strings.Repeat("本", model.POST_MESSAGE_MAX_RUNES/2), + Props: map[string]interface{}{ + "attachments": []*model.SlackAttachment{ + &model.SlackAttachment{ + Text: strings.Repeat("本", 1000), + }, + &model.SlackAttachment{ + Text: strings.Repeat("本", 2000), + }, + }, + }, + }, + { + Props: map[string]interface{}{ + "attachments": []*model.SlackAttachment{ + &model.SlackAttachment{ + Text: strings.Repeat("本", model.POST_PROPS_MAX_USER_RUNES-1000), + }, + }, + }, + }, + }, + }, + "UnsplittableProps": { + Post: &model.Post{ + Message: "foo", + Props: map[string]interface{}{ + "foo": strings.Repeat("x", model.POST_PROPS_MAX_USER_RUNES*2), + }, + }, + }, + } { + t.Run(name, func(t *testing.T) { + splits, err := SplitWebhookPost(tc.Post) + if tc.Expected == nil { + require.NotNil(t, err) + } else { + require.Nil(t, err) + } + assert.Equal(t, len(tc.Expected), len(splits)) + for i, split := range splits { + if i < len(tc.Expected) { + assert.Equal(t, tc.Expected[i].Message, split.Message) + assert.Equal(t, tc.Expected[i].Props["attachments"], split.Props["attachments"]) + } + } + }) + } +} -- cgit v1.2.3-1-g7c22