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