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.
304 lines
6.2 KiB
Go
304 lines
6.2 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPostJson(t *testing.T) {
|
|
o := Post{Id: NewId(), Message: NewId()}
|
|
json := o.ToJson()
|
|
ro := PostFromJson(strings.NewReader(json))
|
|
|
|
if o.Id != ro.Id {
|
|
t.Fatal("Ids do not match")
|
|
}
|
|
}
|
|
|
|
func TestPostIsValid(t *testing.T) {
|
|
o := Post{}
|
|
maxPostSize := 10000
|
|
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.Id = NewId()
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.CreateAt = GetMillis()
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.UpdateAt = GetMillis()
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.UserId = NewId()
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.ChannelId = NewId()
|
|
o.RootId = "123"
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.RootId = ""
|
|
o.ParentId = "123"
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.ParentId = NewId()
|
|
o.RootId = ""
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.ParentId = ""
|
|
o.Message = strings.Repeat("0", maxPostSize+1)
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.Message = strings.Repeat("0", maxPostSize)
|
|
if err := o.IsValid(maxPostSize); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
o.Message = "test"
|
|
if err := o.IsValid(maxPostSize); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
o.Type = "junk"
|
|
if err := o.IsValid(maxPostSize); err == nil {
|
|
t.Fatal("should be invalid")
|
|
}
|
|
|
|
o.Type = POST_CUSTOM_TYPE_PREFIX + "type"
|
|
if err := o.IsValid(maxPostSize); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestPostPreSave(t *testing.T) {
|
|
o := Post{Message: "test"}
|
|
o.PreSave()
|
|
|
|
if o.CreateAt == 0 {
|
|
t.Fatal("should be set")
|
|
}
|
|
|
|
past := GetMillis() - 1
|
|
o = Post{Message: "test", CreateAt: past}
|
|
o.PreSave()
|
|
|
|
if o.CreateAt > past {
|
|
t.Fatal("should not be updated")
|
|
}
|
|
|
|
o.Etag()
|
|
}
|
|
|
|
func TestPostIsSystemMessage(t *testing.T) {
|
|
post1 := Post{Message: "test_1"}
|
|
post1.PreSave()
|
|
|
|
if post1.IsSystemMessage() {
|
|
t.Fatalf("TestPostIsSystemMessage failed, expected post1.IsSystemMessage() to be false")
|
|
}
|
|
|
|
post2 := Post{Message: "test_2", Type: POST_JOIN_LEAVE}
|
|
post2.PreSave()
|
|
if !post2.IsSystemMessage() {
|
|
t.Fatalf("TestPostIsSystemMessage failed, expected post2.IsSystemMessage() to be true")
|
|
}
|
|
}
|
|
|
|
func TestPostChannelMentions(t *testing.T) {
|
|
post := Post{Message: "~a ~b ~b ~c/~d."}
|
|
assert.Equal(t, []string{"a", "b", "c", "d"}, post.ChannelMentions())
|
|
}
|
|
|
|
func TestPostSanitizeProps(t *testing.T) {
|
|
post1 := &Post{
|
|
Message: "test",
|
|
}
|
|
|
|
post1.SanitizeProps()
|
|
|
|
if post1.Props[PROPS_ADD_CHANNEL_MEMBER] != nil {
|
|
t.Fatal("should be nil")
|
|
}
|
|
|
|
post2 := &Post{
|
|
Message: "test",
|
|
Props: StringInterface{
|
|
PROPS_ADD_CHANNEL_MEMBER: "test",
|
|
},
|
|
}
|
|
|
|
post2.SanitizeProps()
|
|
|
|
if post2.Props[PROPS_ADD_CHANNEL_MEMBER] != nil {
|
|
t.Fatal("should be nil")
|
|
}
|
|
|
|
post3 := &Post{
|
|
Message: "test",
|
|
Props: StringInterface{
|
|
PROPS_ADD_CHANNEL_MEMBER: "no good",
|
|
"attachments": "good",
|
|
},
|
|
}
|
|
|
|
post3.SanitizeProps()
|
|
|
|
if post3.Props[PROPS_ADD_CHANNEL_MEMBER] != nil {
|
|
t.Fatal("should be nil")
|
|
}
|
|
|
|
if post3.Props["attachments"] == nil {
|
|
t.Fatal("should not be nil")
|
|
}
|
|
}
|
|
|
|
var markdownSample, markdownSampleWithRewrittenImageURLs string
|
|
|
|
func init() {
|
|
bytes, err := ioutil.ReadFile("testdata/markdown-sample.md")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
markdownSample = string(bytes)
|
|
|
|
bytes, err = ioutil.ReadFile("testdata/markdown-sample-with-rewritten-image-urls.md")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
markdownSampleWithRewrittenImageURLs = string(bytes)
|
|
}
|
|
|
|
func TestRewriteImageURLs(t *testing.T) {
|
|
for name, tc := range map[string]struct {
|
|
Markdown string
|
|
Expected string
|
|
}{
|
|
"Empty": {
|
|
Markdown: ``,
|
|
Expected: ``,
|
|
},
|
|
"NoImages": {
|
|
Markdown: `foo`,
|
|
Expected: `foo`,
|
|
},
|
|
"Link": {
|
|
Markdown: `[foo](/url)`,
|
|
Expected: `[foo](/url)`,
|
|
},
|
|
"Image": {
|
|
Markdown: ``,
|
|
Expected: ``,
|
|
},
|
|
"SpacedURL": {
|
|
Markdown: ``,
|
|
Expected: ``,
|
|
},
|
|
"Title": {
|
|
Markdown: ``,
|
|
Expected: ``,
|
|
},
|
|
"Parentheses": {
|
|
Markdown: ` "title")`,
|
|
Expected: ` "title")`,
|
|
},
|
|
"AngleBrackets": {
|
|
Markdown: ``,
|
|
Expected: ``,
|
|
},
|
|
"MultipleLines": {
|
|
Markdown: ``,
|
|
Expected: ``,
|
|
},
|
|
"ReferenceLink": {
|
|
Markdown: `[foo]: </url\<1\>\\> "title"
|
|
[foo]`,
|
|
Expected: `[foo]: </url\<1\>\\> "title"
|
|
[foo]`,
|
|
},
|
|
"ReferenceImage": {
|
|
Markdown: `[foo]: </url\<1\>\\> "title"
|
|
![foo]`,
|
|
Expected: `[foo]: <rewritten:/url\<1\>\\> "title"
|
|
![foo]`,
|
|
},
|
|
"MultipleReferenceImages": {
|
|
Markdown: `[foo]: </url1> "title"
|
|
[bar]: </url2>
|
|
[baz]: /url3 "title"
|
|
[qux]: /url4
|
|
![foo]![qux]`,
|
|
Expected: `[foo]: <rewritten:/url1> "title"
|
|
[bar]: </url2>
|
|
[baz]: /url3 "title"
|
|
[qux]: rewritten:/url4
|
|
![foo]![qux]`,
|
|
},
|
|
"DuplicateReferences": {
|
|
Markdown: `[foo]: </url1> "title"
|
|
[foo]: </url2>
|
|
[foo]: /url3 "title"
|
|
[foo]: /url4
|
|
![foo]![foo]![foo]`,
|
|
Expected: `[foo]: <rewritten:/url1> "title"
|
|
[foo]: </url2>
|
|
[foo]: /url3 "title"
|
|
[foo]: /url4
|
|
![foo]![foo]![foo]`,
|
|
},
|
|
"TrailingURL": {
|
|
Markdown: "![foo]\n\n[foo]: /url",
|
|
Expected: "![foo]\n\n[foo]: rewritten:/url",
|
|
},
|
|
"Sample": {
|
|
Markdown: markdownSample,
|
|
Expected: markdownSampleWithRewrittenImageURLs,
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert.Equal(t, tc.Expected, RewriteImageURLs(tc.Markdown, func(url string) string {
|
|
return "rewritten:" + url
|
|
}))
|
|
})
|
|
}
|
|
}
|
|
|
|
var rewriteImageURLsSink string
|
|
|
|
func BenchmarkRewriteImageURLs(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
rewriteImageURLsSink = RewriteImageURLs(markdownSample, func(url string) string {
|
|
return "rewritten:" + url
|
|
})
|
|
}
|
|
}
|