2017-05-11 00:16:45 +01:00
|
|
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2017-08-29 16:14:59 -05:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2018-02-08 10:45:25 -05:00
|
|
|
"strings"
|
Relax 4k post message limit (#8478)
* 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.
2018-03-26 17:55:35 -04:00
|
|
|
"sync/atomic"
|
2017-05-11 00:16:45 +01:00
|
|
|
"testing"
|
2017-07-04 15:17:54 -04:00
|
|
|
"time"
|
2017-05-11 00:16:45 +01:00
|
|
|
|
2018-02-08 10:45:25 -05:00
|
|
|
"github.com/dyatlov/go-opengraph/opengraph"
|
2017-08-29 16:14:59 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2017-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
Relax 4k post message limit (#8478)
* 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.
2018-03-26 17:55:35 -04:00
|
|
|
"github.com/mattermost/mattermost-server/store"
|
|
|
|
|
"github.com/mattermost/mattermost-server/store/storetest"
|
2017-05-11 00:16:45 +01:00
|
|
|
)
|
|
|
|
|
|
2017-07-04 15:17:54 -04:00
|
|
|
func TestUpdatePostEditAt(t *testing.T) {
|
2017-09-12 09:19:52 -05:00
|
|
|
th := Setup().InitBasic()
|
2017-10-04 13:09:41 -07:00
|
|
|
defer th.TearDown()
|
2017-07-04 15:17:54 -04:00
|
|
|
|
|
|
|
|
post := &model.Post{}
|
|
|
|
|
*post = *th.BasicPost
|
|
|
|
|
|
|
|
|
|
post.IsPinned = true
|
2017-09-12 09:19:52 -05:00
|
|
|
if saved, err := th.App.UpdatePost(post, true); err != nil {
|
2017-07-04 15:17:54 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if saved.EditAt != post.EditAt {
|
|
|
|
|
t.Fatal("shouldn't have updated post.EditAt when pinning post")
|
|
|
|
|
|
|
|
|
|
*post = *saved
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
|
|
post.Message = model.NewId()
|
2017-09-12 09:19:52 -05:00
|
|
|
if saved, err := th.App.UpdatePost(post, true); err != nil {
|
2017-07-04 15:17:54 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if saved.EditAt == post.EditAt {
|
|
|
|
|
t.Fatal("should have updated post.EditAt when updating post message")
|
|
|
|
|
}
|
2018-06-25 12:33:13 -07:00
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
2017-07-04 15:17:54 -04:00
|
|
|
}
|
|
|
|
|
|
2018-02-09 16:31:01 +01:00
|
|
|
func TestUpdatePostTimeLimit(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
|
|
|
|
defer th.TearDown()
|
|
|
|
|
|
|
|
|
|
post := &model.Post{}
|
|
|
|
|
*post = *th.BasicPost
|
|
|
|
|
|
2018-02-13 13:35:52 +00:00
|
|
|
th.App.SetLicense(model.NewTestLicense())
|
2018-02-09 16:31:01 +01:00
|
|
|
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
*cfg.ServiceSettings.PostEditTimeLimit = -1
|
|
|
|
|
})
|
|
|
|
|
if _, err := th.App.UpdatePost(post, true); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
*cfg.ServiceSettings.PostEditTimeLimit = 1000000000
|
|
|
|
|
})
|
|
|
|
|
post.Message = model.NewId()
|
|
|
|
|
if _, err := th.App.UpdatePost(post, true); err != nil {
|
|
|
|
|
t.Fatal("should allow you to edit the post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
*cfg.ServiceSettings.PostEditTimeLimit = 1
|
|
|
|
|
})
|
|
|
|
|
post.Message = model.NewId()
|
|
|
|
|
if _, err := th.App.UpdatePost(post, true); err == nil {
|
|
|
|
|
t.Fatal("should fail on update old post")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
*cfg.ServiceSettings.PostEditTimeLimit = -1
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 00:16:45 +01:00
|
|
|
func TestPostReplyToPostWhereRootPosterLeftChannel(t *testing.T) {
|
|
|
|
|
// This test ensures that when replying to a root post made by a user who has since left the channel, the reply
|
|
|
|
|
// post completes successfully. This is a regression test for PLT-6523.
|
2017-09-12 09:19:52 -05:00
|
|
|
th := Setup().InitBasic()
|
2017-10-04 13:09:41 -07:00
|
|
|
defer th.TearDown()
|
2017-05-11 00:16:45 +01:00
|
|
|
|
|
|
|
|
channel := th.BasicChannel
|
|
|
|
|
userInChannel := th.BasicUser2
|
|
|
|
|
userNotInChannel := th.BasicUser
|
|
|
|
|
rootPost := th.BasicPost
|
|
|
|
|
|
2017-09-12 09:19:52 -05:00
|
|
|
if _, err := th.App.AddUserToChannel(userInChannel, channel); err != nil {
|
2017-05-11 00:16:45 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 09:19:52 -05:00
|
|
|
if err := th.App.RemoveUserFromChannel(userNotInChannel.Id, "", channel); err != nil {
|
2017-05-11 00:16:45 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
replyPost := model.Post{
|
2017-07-04 15:17:54 -04:00
|
|
|
Message: "asd",
|
|
|
|
|
ChannelId: channel.Id,
|
|
|
|
|
RootId: rootPost.Id,
|
|
|
|
|
ParentId: rootPost.Id,
|
2017-05-11 00:16:45 +01:00
|
|
|
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
|
2017-07-04 15:17:54 -04:00
|
|
|
UserId: userInChannel.Id,
|
|
|
|
|
CreateAt: 0,
|
2017-05-11 00:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
2017-09-12 09:19:52 -05:00
|
|
|
if _, err := th.App.CreatePostAsUser(&replyPost); err != nil {
|
2017-05-11 00:16:45 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-29 16:14:59 -05:00
|
|
|
|
|
|
|
|
func TestPostAction(t *testing.T) {
|
2017-09-12 09:19:52 -05:00
|
|
|
th := Setup().InitBasic()
|
2017-10-04 13:09:41 -07:00
|
|
|
defer th.TearDown()
|
2017-08-29 16:14:59 -05:00
|
|
|
|
2017-10-31 09:39:31 -05:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
*cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost 127.0.0.1"
|
|
|
|
|
})
|
2017-08-29 16:14:59 -05:00
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var request model.PostActionIntegrationRequest
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&request)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, request.UserId, th.BasicUser.Id)
|
|
|
|
|
assert.Equal(t, "foo", request.Context["s"])
|
|
|
|
|
assert.EqualValues(t, 3, request.Context["n"])
|
|
|
|
|
fmt.Fprintf(w, `{"update": {"message": "updated"}, "ephemeral_text": "foo"}`)
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
interactivePost := model.Post{
|
|
|
|
|
Message: "Interactive post",
|
|
|
|
|
ChannelId: th.BasicChannel.Id,
|
|
|
|
|
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
|
|
|
|
|
UserId: th.BasicUser.Id,
|
|
|
|
|
Props: model.StringInterface{
|
|
|
|
|
"attachments": []*model.SlackAttachment{
|
2017-10-18 15:36:43 -07:00
|
|
|
{
|
2017-08-29 16:14:59 -05:00
|
|
|
Text: "hello",
|
|
|
|
|
Actions: []*model.PostAction{
|
2017-10-18 15:36:43 -07:00
|
|
|
{
|
2017-08-29 16:14:59 -05:00
|
|
|
Integration: &model.PostActionIntegration{
|
|
|
|
|
Context: model.StringInterface{
|
|
|
|
|
"s": "foo",
|
|
|
|
|
"n": 3,
|
|
|
|
|
},
|
|
|
|
|
URL: ts.URL,
|
|
|
|
|
},
|
|
|
|
|
Name: "action",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 09:19:52 -05:00
|
|
|
post, err := th.App.CreatePostAsUser(&interactivePost)
|
2017-08-29 16:14:59 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
attachments, ok := post.Props["attachments"].([]*model.SlackAttachment)
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
require.NotEmpty(t, attachments[0].Actions)
|
|
|
|
|
require.NotEmpty(t, attachments[0].Actions[0].Id)
|
|
|
|
|
|
2017-09-12 09:19:52 -05:00
|
|
|
err = th.App.DoPostAction(post.Id, "notavalidid", th.BasicUser.Id)
|
2017-08-29 16:14:59 -05:00
|
|
|
require.NotNil(t, err)
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, err.StatusCode)
|
|
|
|
|
|
2017-09-12 09:19:52 -05:00
|
|
|
err = th.App.DoPostAction(post.Id, attachments[0].Actions[0].Id, th.BasicUser.Id)
|
2017-08-29 16:14:59 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
}
|
2017-11-28 15:02:56 -06:00
|
|
|
|
|
|
|
|
func TestPostChannelMentions(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
|
|
|
|
defer th.TearDown()
|
|
|
|
|
|
|
|
|
|
channel := th.BasicChannel
|
|
|
|
|
user := th.BasicUser
|
|
|
|
|
|
|
|
|
|
channelToMention, err := th.App.CreateChannel(&model.Channel{
|
|
|
|
|
DisplayName: "Mention Test",
|
|
|
|
|
Name: "mention-test",
|
|
|
|
|
Type: model.CHANNEL_OPEN,
|
|
|
|
|
TeamId: th.BasicTeam.Id,
|
|
|
|
|
}, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
defer th.App.PermanentDeleteChannel(channelToMention)
|
|
|
|
|
|
|
|
|
|
_, err = th.App.AddUserToChannel(user, channel)
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
post := &model.Post{
|
|
|
|
|
Message: fmt.Sprintf("hello, ~%v!", channelToMention.Name),
|
|
|
|
|
ChannelId: channel.Id,
|
|
|
|
|
PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()),
|
|
|
|
|
UserId: user.Id,
|
|
|
|
|
CreateAt: 0,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := th.App.CreatePostAsUser(post)
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
assert.Equal(t, map[string]interface{}{
|
|
|
|
|
"mention-test": map[string]interface{}{
|
|
|
|
|
"display_name": "Mention Test",
|
|
|
|
|
},
|
|
|
|
|
}, result.Props["channel_mentions"])
|
2017-12-05 13:18:45 -06:00
|
|
|
|
|
|
|
|
post.Message = fmt.Sprintf("goodbye, ~%v!", channelToMention.Name)
|
|
|
|
|
result, err = th.App.UpdatePost(post, false)
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
assert.Equal(t, map[string]interface{}{
|
|
|
|
|
"mention-test": map[string]interface{}{
|
|
|
|
|
"display_name": "Mention Test",
|
|
|
|
|
},
|
|
|
|
|
}, result.Props["channel_mentions"])
|
2017-11-28 15:02:56 -06:00
|
|
|
}
|
2018-01-22 15:32:50 -06:00
|
|
|
|
|
|
|
|
func TestImageProxy(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
|
|
|
|
defer th.TearDown()
|
|
|
|
|
|
2018-02-09 15:41:06 -06:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
*cfg.ServiceSettings.SiteURL = "http://mymattermost.com"
|
|
|
|
|
})
|
|
|
|
|
|
2018-01-22 15:32:50 -06:00
|
|
|
for name, tc := range map[string]struct {
|
|
|
|
|
ProxyType string
|
|
|
|
|
ProxyURL string
|
|
|
|
|
ProxyOptions string
|
|
|
|
|
ImageURL string
|
|
|
|
|
ProxiedImageURL string
|
|
|
|
|
}{
|
|
|
|
|
"atmos/camo": {
|
|
|
|
|
ProxyType: "atmos/camo",
|
|
|
|
|
ProxyURL: "https://127.0.0.1",
|
|
|
|
|
ProxyOptions: "foo",
|
|
|
|
|
ImageURL: "http://mydomain.com/myimage",
|
|
|
|
|
ProxiedImageURL: "https://127.0.0.1/f8dace906d23689e8d5b12c3cefbedbf7b9b72f5/687474703a2f2f6d79646f6d61696e2e636f6d2f6d79696d616765",
|
|
|
|
|
},
|
2018-02-12 13:05:01 -06:00
|
|
|
"atmos/camo_SameSite": {
|
|
|
|
|
ProxyType: "atmos/camo",
|
2018-02-09 15:41:06 -06:00
|
|
|
ProxyURL: "https://127.0.0.1",
|
2018-02-12 13:05:01 -06:00
|
|
|
ProxyOptions: "foo",
|
2018-02-09 15:41:06 -06:00
|
|
|
ImageURL: "http://mymattermost.com/myimage",
|
|
|
|
|
ProxiedImageURL: "http://mymattermost.com/myimage",
|
|
|
|
|
},
|
2018-02-12 13:05:01 -06:00
|
|
|
"atmos/camo_PathOnly": {
|
|
|
|
|
ProxyType: "atmos/camo",
|
2018-02-09 20:08:39 -06:00
|
|
|
ProxyURL: "https://127.0.0.1",
|
2018-02-12 13:05:01 -06:00
|
|
|
ProxyOptions: "foo",
|
2018-02-09 20:08:39 -06:00
|
|
|
ImageURL: "/myimage",
|
|
|
|
|
ProxiedImageURL: "/myimage",
|
|
|
|
|
},
|
2018-02-12 13:05:01 -06:00
|
|
|
"atmos/camo_EmptyImageURL": {
|
|
|
|
|
ProxyType: "atmos/camo",
|
2018-02-01 20:31:49 -06:00
|
|
|
ProxyURL: "https://127.0.0.1",
|
2018-02-12 13:05:01 -06:00
|
|
|
ProxyOptions: "foo",
|
2018-02-01 20:31:49 -06:00
|
|
|
ImageURL: "",
|
|
|
|
|
ProxiedImageURL: "",
|
|
|
|
|
},
|
2018-01-22 15:32:50 -06:00
|
|
|
} {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
cfg.ServiceSettings.ImageProxyType = model.NewString(tc.ProxyType)
|
|
|
|
|
cfg.ServiceSettings.ImageProxyOptions = model.NewString(tc.ProxyOptions)
|
|
|
|
|
cfg.ServiceSettings.ImageProxyURL = model.NewString(tc.ProxyURL)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
post := &model.Post{
|
|
|
|
|
Id: model.NewId(),
|
|
|
|
|
Message: "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list := model.NewPostList()
|
|
|
|
|
list.Posts[post.Id] = post
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "", th.App.PostListWithProxyAddedToImageURLs(list).Posts[post.Id].Message)
|
|
|
|
|
assert.Equal(t, "", th.App.PostWithProxyAddedToImageURLs(post).Message)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "", th.App.PostWithProxyRemovedFromImageURLs(post).Message)
|
|
|
|
|
post.Message = ""
|
|
|
|
|
assert.Equal(t, "", th.App.PostWithProxyRemovedFromImageURLs(post).Message)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-01 01:54:11 +09:00
|
|
|
func BenchmarkForceHTMLEncodingToUTF8(b *testing.B) {
|
|
|
|
|
HTML := `
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta property="og:url" content="https://example.com/apps/mattermost">
|
|
|
|
|
<meta property="og:image" content="https://images.example.com/image.png">
|
|
|
|
|
</head>
|
|
|
|
|
</html>
|
|
|
|
|
`
|
|
|
|
|
ContentType := "text/html; utf-8"
|
|
|
|
|
|
|
|
|
|
b.Run("with converting", func(b *testing.B) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
r := forceHTMLEncodingToUTF8(strings.NewReader(HTML), ContentType)
|
|
|
|
|
|
|
|
|
|
og := opengraph.NewOpenGraph()
|
|
|
|
|
og.ProcessHTML(r)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
b.Run("without converting", func(b *testing.B) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
og := opengraph.NewOpenGraph()
|
|
|
|
|
og.ProcessHTML(strings.NewReader(HTML))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 10:45:25 -05:00
|
|
|
func TestMakeOpenGraphURLsAbsolute(t *testing.T) {
|
|
|
|
|
for name, tc := range map[string]struct {
|
|
|
|
|
HTML string
|
|
|
|
|
RequestURL string
|
|
|
|
|
URL string
|
|
|
|
|
ImageURL string
|
|
|
|
|
}{
|
|
|
|
|
"absolute URLs": {
|
|
|
|
|
HTML: `
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta property="og:url" content="https://example.com/apps/mattermost">
|
|
|
|
|
<meta property="og:image" content="https://images.example.com/image.png">
|
|
|
|
|
</head>
|
|
|
|
|
</html>`,
|
|
|
|
|
RequestURL: "https://example.com",
|
|
|
|
|
URL: "https://example.com/apps/mattermost",
|
|
|
|
|
ImageURL: "https://images.example.com/image.png",
|
|
|
|
|
},
|
2018-02-09 10:05:23 -05:00
|
|
|
"URLs starting with /": {
|
2018-02-08 10:45:25 -05:00
|
|
|
HTML: `
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta property="og:url" content="/apps/mattermost">
|
|
|
|
|
<meta property="og:image" content="/image.png">
|
|
|
|
|
</head>
|
|
|
|
|
</html>`,
|
|
|
|
|
RequestURL: "http://example.com",
|
|
|
|
|
URL: "http://example.com/apps/mattermost",
|
|
|
|
|
ImageURL: "http://example.com/image.png",
|
|
|
|
|
},
|
2018-02-09 10:05:23 -05:00
|
|
|
"HTTPS URLs starting with /": {
|
2018-02-08 10:45:25 -05:00
|
|
|
HTML: `
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta property="og:url" content="/apps/mattermost">
|
|
|
|
|
<meta property="og:image" content="/image.png">
|
|
|
|
|
</head>
|
|
|
|
|
</html>`,
|
|
|
|
|
RequestURL: "https://example.com",
|
|
|
|
|
URL: "https://example.com/apps/mattermost",
|
|
|
|
|
ImageURL: "https://example.com/image.png",
|
|
|
|
|
},
|
|
|
|
|
"missing image URL": {
|
|
|
|
|
HTML: `
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta property="og:url" content="/apps/mattermost">
|
|
|
|
|
</head>
|
|
|
|
|
</html>`,
|
|
|
|
|
RequestURL: "http://example.com",
|
|
|
|
|
URL: "http://example.com/apps/mattermost",
|
|
|
|
|
ImageURL: "",
|
|
|
|
|
},
|
2018-02-09 10:05:23 -05:00
|
|
|
"relative URLs": {
|
|
|
|
|
HTML: `
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta property="og:url" content="index.html">
|
|
|
|
|
<meta property="og:image" content="../resources/image.png">
|
|
|
|
|
</head>
|
|
|
|
|
</html>`,
|
|
|
|
|
RequestURL: "http://example.com/content/index.html",
|
|
|
|
|
URL: "http://example.com/content/index.html",
|
|
|
|
|
ImageURL: "http://example.com/resources/image.png",
|
|
|
|
|
},
|
2018-02-08 10:45:25 -05:00
|
|
|
} {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
og := opengraph.NewOpenGraph()
|
|
|
|
|
if err := og.ProcessHTML(strings.NewReader(tc.HTML)); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2018-01-22 15:32:50 -06:00
|
|
|
|
2018-02-09 10:05:23 -05:00
|
|
|
makeOpenGraphURLsAbsolute(og, tc.RequestURL)
|
2018-01-22 15:32:50 -06:00
|
|
|
|
2018-02-08 10:45:25 -05:00
|
|
|
if og.URL != tc.URL {
|
|
|
|
|
t.Fatalf("incorrect url, expected %v, got %v", tc.URL, og.URL)
|
|
|
|
|
}
|
2018-01-22 15:32:50 -06:00
|
|
|
|
2018-02-08 10:45:25 -05:00
|
|
|
if len(og.Images) > 0 {
|
|
|
|
|
if og.Images[0].URL != tc.ImageURL {
|
|
|
|
|
t.Fatalf("incorrect image url, expected %v, got %v", tc.ImageURL, og.Images[0].URL)
|
|
|
|
|
}
|
|
|
|
|
} else if tc.ImageURL != "" {
|
2018-02-12 08:58:38 -05:00
|
|
|
t.Fatalf("missing image url, expected %v, got nothing", tc.ImageURL)
|
2018-02-08 10:45:25 -05:00
|
|
|
}
|
|
|
|
|
})
|
2018-01-22 15:32:50 -06:00
|
|
|
}
|
|
|
|
|
}
|
Relax 4k post message limit (#8478)
* 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.
2018-03-26 17:55:35 -04:00
|
|
|
|
|
|
|
|
func TestMaxPostSize(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
Description string
|
|
|
|
|
StoreMaxPostSize int
|
|
|
|
|
ExpectedMaxPostSize int
|
|
|
|
|
ExpectedError *model.AppError
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
"error fetching max post size",
|
|
|
|
|
0,
|
|
|
|
|
model.POST_MESSAGE_MAX_RUNES_V1,
|
|
|
|
|
model.NewAppError("TestMaxPostSize", "this is an error", nil, "", http.StatusBadRequest),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"4000 rune limit",
|
|
|
|
|
4000,
|
|
|
|
|
4000,
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"16383 rune limit",
|
|
|
|
|
16383,
|
|
|
|
|
16383,
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
|
testCase := testCase
|
|
|
|
|
t.Run(testCase.Description, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
mockStore := &storetest.Store{}
|
|
|
|
|
defer mockStore.AssertExpectations(t)
|
|
|
|
|
|
|
|
|
|
mockStore.PostStore.On("GetMaxPostSize").Return(
|
|
|
|
|
storetest.NewStoreChannel(store.StoreResult{
|
|
|
|
|
Data: testCase.StoreMaxPostSize,
|
|
|
|
|
Err: testCase.ExpectedError,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
app := App{
|
|
|
|
|
Srv: &Server{
|
|
|
|
|
Store: mockStore,
|
|
|
|
|
},
|
|
|
|
|
config: atomic.Value{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, testCase.ExpectedMaxPostSize, app.MaxPostSize())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|