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