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