mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* MM-9661: rename POST_MESSAGE_MAX_RUNES to \0_v1 * MM-9661: s/4000/POST_MESSAGE_MAX_RUNES_V1/ in tests * MM-9661: introduce POST_MESSAGE_MAX_RUNES_V2 * MM-9661: migrate Postgres Posts.Message column to TEXT from VARCHAR(4000) This is safe to do in a production instance since the underyling type is not changing. We explicitly don't do this automatically for MySQL, but also don't need to since the ORM would have already created a TEXT column for MySQL in that case. * MM-9661: emit MaxPostSize in client config This value remains unconfigurable at this time, but exposes the current limit to the client. The limit remains at 4k in this commit. * MM-9661: introduce and use SqlPostStore.GetMaxPostSize Enforce a byte limitation in the database, and use 1/4 of that value as the rune count limitation (assuming a worst case UTF-8 representation). * move maxPostSizeCached, lastPostsCache and lastPostTimeCache out of the global context and onto the SqlPostStore * address feedback from code review: * ensure sqlstore unit tests are actually being run * move global caches into SqlPostStore * leverage sync.Once to address a race condition * modify upgrade semantics to match new db semantics gorp's behaviour on creating columns with a maximum length on Postgres differs from MySQL: * Postgres * gorp uses TEXT for string columns without a maximum length * gorp uses VARCHAR(N) for string columns with a maximum length of N * MySQL * gorp uses TEXT for string columns with a maximum length >= 256 * gorp uses VARCHAR(N) for string columns with a maximum length of N * gorp defaults to a maximum length of 255, implying VARCHAR(255) So the Message column has been TEXT on MySQL but VARCHAR(4000) on Postgres. With the new, longer limits of 65535, and without changes to gorp, the expected behaviour is TEXT on MySQL and VARCHAR(65535) on Postgres. This commit makes the upgrade semantics match the new database semantics. Ideally, we'd revisit the gorp behaviour at a later time. * allow TestMaxPostSize test cases to actually run in parallel * default maxPostSizeCached to POST_MESSAGE_MAX_RUNES_V1 in case the once initializer panics * fix casting error * MM-9661: skip the schema migration for Postgres It turns out resizing VARCHAR requires a rewrite in some versions of Postgres, but migrating VARCHAR to TEXT does not. Given the increasing complexity, let's defer the migration to the enduser instead.
473 lines
14 KiB
Go
473 lines
14 KiB
Go
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
)
|
|
|
|
func TestCreateIncomingWebhookForChannel(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
type TestCase struct {
|
|
EnableIncomingHooks bool
|
|
EnablePostUsernameOverride bool
|
|
EnablePostIconOverride bool
|
|
IncomingWebhook model.IncomingWebhook
|
|
|
|
ExpectedError bool
|
|
ExpectedIncomingWebhook *model.IncomingWebhook
|
|
}
|
|
|
|
for name, tc := range map[string]TestCase{
|
|
"webhooks not enabled": {
|
|
EnableIncomingHooks: false,
|
|
EnablePostUsernameOverride: false,
|
|
EnablePostIconOverride: false,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
|
|
ExpectedError: true,
|
|
ExpectedIncomingWebhook: nil,
|
|
},
|
|
"valid: username and post icon url ignored, since override not enabled": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: false,
|
|
EnablePostIconOverride: false,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: ":invalid and ignored:",
|
|
IconURL: "ignored",
|
|
},
|
|
|
|
ExpectedError: false,
|
|
ExpectedIncomingWebhook: &model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
},
|
|
"invalid username, override enabled": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: true,
|
|
EnablePostIconOverride: false,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: ":invalid:",
|
|
},
|
|
|
|
ExpectedError: true,
|
|
ExpectedIncomingWebhook: nil,
|
|
},
|
|
"valid, no username or post icon url provided": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: true,
|
|
EnablePostIconOverride: true,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
|
|
ExpectedError: false,
|
|
ExpectedIncomingWebhook: &model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
},
|
|
"valid, with username and post icon": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: true,
|
|
EnablePostIconOverride: true,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: "valid",
|
|
IconURL: "http://example.com/icon",
|
|
},
|
|
|
|
ExpectedError: false,
|
|
ExpectedIncomingWebhook: &model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: "valid",
|
|
IconURL: "http://example.com/icon",
|
|
},
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableIncomingWebhooks = tc.EnableIncomingHooks })
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
cfg.ServiceSettings.EnablePostUsernameOverride = tc.EnablePostUsernameOverride
|
|
})
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnablePostIconOverride = tc.EnablePostIconOverride })
|
|
|
|
createdHook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &tc.IncomingWebhook)
|
|
if tc.ExpectedError && err == nil {
|
|
t.Fatal("should have failed")
|
|
} else if !tc.ExpectedError && err != nil {
|
|
t.Fatalf("should not have failed: %v", err.Error())
|
|
}
|
|
if createdHook != nil {
|
|
defer th.App.DeleteIncomingWebhook(createdHook.Id)
|
|
}
|
|
if tc.ExpectedIncomingWebhook == nil {
|
|
assert.Nil(createdHook, "expected nil webhook")
|
|
} else if assert.NotNil(createdHook, "expected non-nil webhook") {
|
|
assert.Equal(tc.ExpectedIncomingWebhook.DisplayName, createdHook.DisplayName)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.Description, createdHook.Description)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.ChannelId, createdHook.ChannelId)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.Username, createdHook.Username)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.IconURL, createdHook.IconURL)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpdateIncomingWebhook(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
type TestCase struct {
|
|
EnableIncomingHooks bool
|
|
EnablePostUsernameOverride bool
|
|
EnablePostIconOverride bool
|
|
IncomingWebhook model.IncomingWebhook
|
|
|
|
ExpectedError bool
|
|
ExpectedIncomingWebhook *model.IncomingWebhook
|
|
}
|
|
|
|
for name, tc := range map[string]TestCase{
|
|
"webhooks not enabled": {
|
|
EnableIncomingHooks: false,
|
|
EnablePostUsernameOverride: false,
|
|
EnablePostIconOverride: false,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
|
|
ExpectedError: true,
|
|
ExpectedIncomingWebhook: nil,
|
|
},
|
|
"valid: username and post icon url ignored, since override not enabled": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: false,
|
|
EnablePostIconOverride: false,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: ":invalid and ignored:",
|
|
IconURL: "ignored",
|
|
},
|
|
|
|
ExpectedError: false,
|
|
ExpectedIncomingWebhook: &model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
},
|
|
"invalid username, override enabled": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: true,
|
|
EnablePostIconOverride: false,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: ":invalid:",
|
|
},
|
|
|
|
ExpectedError: true,
|
|
ExpectedIncomingWebhook: nil,
|
|
},
|
|
"valid, no username or post icon url provided": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: true,
|
|
EnablePostIconOverride: true,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
|
|
ExpectedError: false,
|
|
ExpectedIncomingWebhook: &model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
},
|
|
},
|
|
"valid, with username and post icon": {
|
|
EnableIncomingHooks: true,
|
|
EnablePostUsernameOverride: true,
|
|
EnablePostIconOverride: true,
|
|
IncomingWebhook: model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: "valid",
|
|
IconURL: "http://example.com/icon",
|
|
},
|
|
|
|
ExpectedError: false,
|
|
ExpectedIncomingWebhook: &model.IncomingWebhook{
|
|
DisplayName: "title",
|
|
Description: "description",
|
|
ChannelId: th.BasicChannel.Id,
|
|
Username: "valid",
|
|
IconURL: "http://example.com/icon",
|
|
},
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableIncomingWebhooks = true })
|
|
|
|
hook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &model.IncomingWebhook{
|
|
ChannelId: th.BasicChannel.Id,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err.Error())
|
|
}
|
|
defer th.App.DeleteIncomingWebhook(hook.Id)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableIncomingWebhooks = tc.EnableIncomingHooks })
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
cfg.ServiceSettings.EnablePostUsernameOverride = tc.EnablePostUsernameOverride
|
|
})
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnablePostIconOverride = tc.EnablePostIconOverride })
|
|
|
|
updatedHook, err := th.App.UpdateIncomingWebhook(hook, &tc.IncomingWebhook)
|
|
if tc.ExpectedError && err == nil {
|
|
t.Fatal("should have failed")
|
|
} else if !tc.ExpectedError && err != nil {
|
|
t.Fatalf("should not have failed: %v", err.Error())
|
|
}
|
|
if tc.ExpectedIncomingWebhook == nil {
|
|
assert.Nil(updatedHook, "expected nil webhook")
|
|
} else if assert.NotNil(updatedHook, "expected non-nil webhook") {
|
|
assert.Equal(tc.ExpectedIncomingWebhook.DisplayName, updatedHook.DisplayName)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.Description, updatedHook.Description)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.ChannelId, updatedHook.ChannelId)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.Username, updatedHook.Username)
|
|
assert.Equal(tc.ExpectedIncomingWebhook.IconURL, updatedHook.IconURL)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCreateWebhookPost(t *testing.T) {
|
|
th := Setup().InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableIncomingWebhooks = true })
|
|
|
|
hook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &model.IncomingWebhook{ChannelId: th.BasicChannel.Id})
|
|
if err != nil {
|
|
t.Fatal(err.Error())
|
|
}
|
|
defer th.App.DeleteIncomingWebhook(hook.Id)
|
|
|
|
post, err := th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, "foo", "user", "http://iconurl", model.StringInterface{
|
|
"attachments": []*model.SlackAttachment{
|
|
{
|
|
Text: "text",
|
|
},
|
|
},
|
|
"webhook_display_name": hook.DisplayName,
|
|
}, model.POST_SLACK_ATTACHMENT, "")
|
|
if err != nil {
|
|
t.Fatal(err.Error())
|
|
}
|
|
|
|
for _, k := range []string{"from_webhook", "attachments", "webhook_display_name"} {
|
|
if _, ok := post.Props[k]; !ok {
|
|
t.Log("missing one props: " + k)
|
|
t.Fatal(k)
|
|
}
|
|
}
|
|
|
|
_, err = th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, "foo", "user", "http://iconurl", nil, model.POST_SYSTEM_GENERIC, "")
|
|
if err == nil {
|
|
t.Fatal("should have failed - bad post type")
|
|
}
|
|
|
|
expectedText := "`<>|<>|`"
|
|
post, err = th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, expectedText, "user", "http://iconurl", model.StringInterface{
|
|
"attachments": []*model.SlackAttachment{
|
|
{
|
|
Text: "text",
|
|
},
|
|
},
|
|
"webhook_display_name": hook.DisplayName,
|
|
}, model.POST_SLACK_ATTACHMENT, "")
|
|
if err != nil {
|
|
t.Fatal(err.Error())
|
|
}
|
|
assert.Equal(t, expectedText, post.Message)
|
|
|
|
expectedText = "< | \n|\n>"
|
|
post, err = th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, expectedText, "user", "http://iconurl", model.StringInterface{
|
|
"attachments": []*model.SlackAttachment{
|
|
{
|
|
Text: "text",
|
|
},
|
|
},
|
|
"webhook_display_name": hook.DisplayName,
|
|
}, model.POST_SLACK_ATTACHMENT, "")
|
|
if err != nil {
|
|
t.Fatal(err.Error())
|
|
}
|
|
assert.Equal(t, expectedText, post.Message)
|
|
|
|
expectedText = `commit bc95839e4a430ace453e8b209a3723c000c1729a
|
|
Author: foo <foo@example.org>
|
|
Date: Thu Mar 1 19:46:54 2018 +0300
|
|
|
|
commit message 2
|
|
|
|
test | 1 +
|
|
1 file changed, 1 insertion(+)
|
|
|
|
commit 5df78b7139b543997838071cd912e375d8bd69b2
|
|
Author: foo <foo@example.org>
|
|
Date: Thu Mar 1 19:46:48 2018 +0300
|
|
|
|
commit message 1
|
|
|
|
test | 3 +++
|
|
1 file changed, 3 insertions(+)`
|
|
post, err = th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, expectedText, "user", "http://iconurl", model.StringInterface{
|
|
"attachments": []*model.SlackAttachment{
|
|
{
|
|
Text: "text",
|
|
},
|
|
},
|
|
"webhook_display_name": hook.DisplayName,
|
|
}, model.POST_SLACK_ATTACHMENT, "")
|
|
if err != nil {
|
|
t.Fatal(err.Error())
|
|
}
|
|
assert.Equal(t, expectedText, post.Message)
|
|
}
|
|
|
|
func TestSplitWebhookPost(t *testing.T) {
|
|
type TestCase struct {
|
|
Post *model.Post
|
|
Expected []*model.Post
|
|
}
|
|
|
|
maxPostSize := 10000
|
|
|
|
for name, tc := range map[string]TestCase{
|
|
"LongPost": {
|
|
Post: &model.Post{
|
|
Message: strings.Repeat("本", maxPostSize*3/2),
|
|
},
|
|
Expected: []*model.Post{
|
|
{
|
|
Message: strings.Repeat("本", maxPostSize),
|
|
},
|
|
{
|
|
Message: strings.Repeat("本", maxPostSize/2),
|
|
},
|
|
},
|
|
},
|
|
"LongPostAndMultipleAttachments": {
|
|
Post: &model.Post{
|
|
Message: strings.Repeat("本", maxPostSize*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("本", maxPostSize),
|
|
},
|
|
{
|
|
Message: strings.Repeat("本", maxPostSize/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, maxPostSize)
|
|
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"])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|