github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/model/post_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "io/ioutil" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestPostToJson(t *testing.T) { 15 o := Post{Id: NewId(), Message: NewId()} 16 j := o.ToJson() 17 ro := PostFromJson(strings.NewReader(j)) 18 19 assert.NotNil(t, ro) 20 assert.Equal(t, o, *ro) 21 } 22 23 func TestPostFromJsonError(t *testing.T) { 24 ro := PostFromJson(strings.NewReader("")) 25 assert.Nil(t, ro) 26 } 27 28 func TestPostIsValid(t *testing.T) { 29 o := Post{} 30 maxPostSize := 10000 31 32 if err := o.IsValid(maxPostSize); err == nil { 33 t.Fatal("should be invalid") 34 } 35 36 o.Id = NewId() 37 if err := o.IsValid(maxPostSize); err == nil { 38 t.Fatal("should be invalid") 39 } 40 41 o.CreateAt = GetMillis() 42 if err := o.IsValid(maxPostSize); err == nil { 43 t.Fatal("should be invalid") 44 } 45 46 o.UpdateAt = GetMillis() 47 if err := o.IsValid(maxPostSize); err == nil { 48 t.Fatal("should be invalid") 49 } 50 51 o.UserId = NewId() 52 if err := o.IsValid(maxPostSize); err == nil { 53 t.Fatal("should be invalid") 54 } 55 56 o.ChannelId = NewId() 57 o.RootId = "123" 58 if err := o.IsValid(maxPostSize); err == nil { 59 t.Fatal("should be invalid") 60 } 61 62 o.RootId = "" 63 o.ParentId = "123" 64 if err := o.IsValid(maxPostSize); err == nil { 65 t.Fatal("should be invalid") 66 } 67 68 o.ParentId = NewId() 69 o.RootId = "" 70 if err := o.IsValid(maxPostSize); err == nil { 71 t.Fatal("should be invalid") 72 } 73 74 o.ParentId = "" 75 o.Message = strings.Repeat("0", maxPostSize+1) 76 if err := o.IsValid(maxPostSize); err == nil { 77 t.Fatal("should be invalid") 78 } 79 80 o.Message = strings.Repeat("0", maxPostSize) 81 if err := o.IsValid(maxPostSize); err != nil { 82 t.Fatal(err) 83 } 84 85 o.Message = "test" 86 if err := o.IsValid(maxPostSize); err != nil { 87 t.Fatal(err) 88 } 89 90 o.Type = "junk" 91 if err := o.IsValid(maxPostSize); err == nil { 92 t.Fatal("should be invalid") 93 } 94 95 o.Type = POST_CUSTOM_TYPE_PREFIX + "type" 96 if err := o.IsValid(maxPostSize); err != nil { 97 t.Fatal(err) 98 } 99 } 100 101 func TestPostPreSave(t *testing.T) { 102 o := Post{Message: "test"} 103 o.PreSave() 104 105 if o.CreateAt == 0 { 106 t.Fatal("should be set") 107 } 108 109 past := GetMillis() - 1 110 o = Post{Message: "test", CreateAt: past} 111 o.PreSave() 112 113 if o.CreateAt > past { 114 t.Fatal("should not be updated") 115 } 116 117 o.Etag() 118 } 119 120 func TestPostIsSystemMessage(t *testing.T) { 121 post1 := Post{Message: "test_1"} 122 post1.PreSave() 123 124 if post1.IsSystemMessage() { 125 t.Fatalf("TestPostIsSystemMessage failed, expected post1.IsSystemMessage() to be false") 126 } 127 128 post2 := Post{Message: "test_2", Type: POST_JOIN_LEAVE} 129 post2.PreSave() 130 if !post2.IsSystemMessage() { 131 t.Fatalf("TestPostIsSystemMessage failed, expected post2.IsSystemMessage() to be true") 132 } 133 } 134 135 func TestPostChannelMentions(t *testing.T) { 136 post := Post{Message: "~a ~b ~b ~c/~d."} 137 assert.Equal(t, []string{"a", "b", "c", "d"}, post.ChannelMentions()) 138 } 139 140 func TestPostSanitizeProps(t *testing.T) { 141 post1 := &Post{ 142 Message: "test", 143 } 144 145 post1.SanitizeProps() 146 147 if post1.Props[PROPS_ADD_CHANNEL_MEMBER] != nil { 148 t.Fatal("should be nil") 149 } 150 151 post2 := &Post{ 152 Message: "test", 153 Props: StringInterface{ 154 PROPS_ADD_CHANNEL_MEMBER: "test", 155 }, 156 } 157 158 post2.SanitizeProps() 159 160 if post2.Props[PROPS_ADD_CHANNEL_MEMBER] != nil { 161 t.Fatal("should be nil") 162 } 163 164 post3 := &Post{ 165 Message: "test", 166 Props: StringInterface{ 167 PROPS_ADD_CHANNEL_MEMBER: "no good", 168 "attachments": "good", 169 }, 170 } 171 172 post3.SanitizeProps() 173 174 if post3.Props[PROPS_ADD_CHANNEL_MEMBER] != nil { 175 t.Fatal("should be nil") 176 } 177 178 if post3.Props["attachments"] == nil { 179 t.Fatal("should not be nil") 180 } 181 } 182 183 var markdownSample, markdownSampleWithRewrittenImageURLs string 184 185 func init() { 186 bytes, err := ioutil.ReadFile("testdata/markdown-sample.md") 187 if err != nil { 188 panic(err) 189 } 190 markdownSample = string(bytes) 191 192 bytes, err = ioutil.ReadFile("testdata/markdown-sample-with-rewritten-image-urls.md") 193 if err != nil { 194 panic(err) 195 } 196 markdownSampleWithRewrittenImageURLs = string(bytes) 197 } 198 199 func TestRewriteImageURLs(t *testing.T) { 200 for name, tc := range map[string]struct { 201 Markdown string 202 Expected string 203 }{ 204 "Empty": { 205 Markdown: ``, 206 Expected: ``, 207 }, 208 "NoImages": { 209 Markdown: `foo`, 210 Expected: `foo`, 211 }, 212 "Link": { 213 Markdown: `[foo](/url)`, 214 Expected: `[foo](/url)`, 215 }, 216 "Image": { 217 Markdown: ``, 218 Expected: ``, 219 }, 220 "SpacedURL": { 221 Markdown: ``, 222 Expected: ``, 223 }, 224 "Title": { 225 Markdown: ``, 226 Expected: ``, 227 }, 228 "Parentheses": { 229 Markdown: ` "title")`, 230 Expected: ` "title")`, 231 }, 232 "AngleBrackets": { 233 Markdown: ``, 234 Expected: ``, 235 }, 236 "MultipleLines": { 237 Markdown: ``, 241 Expected: ``, 245 }, 246 "ReferenceLink": { 247 Markdown: `[foo]: </url\<1\>\\> "title" 248 [foo]`, 249 Expected: `[foo]: </url\<1\>\\> "title" 250 [foo]`, 251 }, 252 "ReferenceImage": { 253 Markdown: `[foo]: </url\<1\>\\> "title" 254 ![foo]`, 255 Expected: `[foo]: <rewritten:/url\<1\>\\> "title" 256 ![foo]`, 257 }, 258 "MultipleReferenceImages": { 259 Markdown: `[foo]: </url1> "title" 260 [bar]: </url2> 261 [baz]: /url3 "title" 262 [qux]: /url4 263 ![foo]![qux]`, 264 Expected: `[foo]: <rewritten:/url1> "title" 265 [bar]: </url2> 266 [baz]: /url3 "title" 267 [qux]: rewritten:/url4 268 ![foo]![qux]`, 269 }, 270 "DuplicateReferences": { 271 Markdown: `[foo]: </url1> "title" 272 [foo]: </url2> 273 [foo]: /url3 "title" 274 [foo]: /url4 275 ![foo]![foo]![foo]`, 276 Expected: `[foo]: <rewritten:/url1> "title" 277 [foo]: </url2> 278 [foo]: /url3 "title" 279 [foo]: /url4 280 ![foo]![foo]![foo]`, 281 }, 282 "TrailingURL": { 283 Markdown: "![foo]\n\n[foo]: /url", 284 Expected: "![foo]\n\n[foo]: rewritten:/url", 285 }, 286 "Sample": { 287 Markdown: markdownSample, 288 Expected: markdownSampleWithRewrittenImageURLs, 289 }, 290 } { 291 t.Run(name, func(t *testing.T) { 292 assert.Equal(t, tc.Expected, RewriteImageURLs(tc.Markdown, func(url string) string { 293 return "rewritten:" + url 294 })) 295 }) 296 } 297 } 298 299 var rewriteImageURLsSink string 300 301 func BenchmarkRewriteImageURLs(b *testing.B) { 302 for i := 0; i < b.N; i++ { 303 rewriteImageURLsSink = RewriteImageURLs(markdownSample, func(url string) string { 304 return "rewritten:" + url 305 }) 306 } 307 }