github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/incoming_webhook_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  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestIncomingWebhookJson(t *testing.T) {
    14  	o := IncomingWebhook{Id: NewId()}
    15  	json := o.ToJson()
    16  	ro := IncomingWebhookFromJson(strings.NewReader(json))
    17  
    18  	require.Equal(t, o.Id, ro.Id)
    19  }
    20  
    21  func TestIncomingWebhookIsValid(t *testing.T) {
    22  	o := IncomingWebhook{}
    23  
    24  	require.Error(t, o.IsValid())
    25  
    26  	o.Id = NewId()
    27  	require.Error(t, o.IsValid())
    28  
    29  	o.CreateAt = GetMillis()
    30  	require.Error(t, o.IsValid())
    31  
    32  	o.UpdateAt = GetMillis()
    33  	require.Error(t, o.IsValid())
    34  
    35  	o.UserId = "123"
    36  	require.Error(t, o.IsValid())
    37  
    38  	o.UserId = NewId()
    39  	require.Error(t, o.IsValid())
    40  
    41  	o.ChannelId = "123"
    42  	require.Error(t, o.IsValid())
    43  
    44  	o.ChannelId = NewId()
    45  	require.Error(t, o.IsValid())
    46  
    47  	o.TeamId = "123"
    48  	require.Error(t, o.IsValid())
    49  
    50  	o.TeamId = NewId()
    51  	require.Nil(t, o.IsValid())
    52  
    53  	o.DisplayName = strings.Repeat("1", 65)
    54  	require.Error(t, o.IsValid())
    55  
    56  	o.DisplayName = strings.Repeat("1", 64)
    57  	require.Nil(t, o.IsValid())
    58  
    59  	o.Description = strings.Repeat("1", 501)
    60  	require.Error(t, o.IsValid())
    61  
    62  	o.Description = strings.Repeat("1", 500)
    63  	require.Nil(t, o.IsValid())
    64  
    65  	o.Username = strings.Repeat("1", 65)
    66  	require.Error(t, o.IsValid())
    67  
    68  	o.Username = strings.Repeat("1", 64)
    69  	require.Nil(t, o.IsValid())
    70  
    71  	o.IconURL = strings.Repeat("1", 1025)
    72  	require.Error(t, o.IsValid())
    73  
    74  	o.IconURL = strings.Repeat("1", 1024)
    75  	require.Nil(t, o.IsValid())
    76  }
    77  
    78  func TestIncomingWebhookPreSave(t *testing.T) {
    79  	o := IncomingWebhook{}
    80  	o.PreSave()
    81  }
    82  
    83  func TestIncomingWebhookPreUpdate(t *testing.T) {
    84  	o := IncomingWebhook{}
    85  	o.PreUpdate()
    86  }
    87  
    88  func TestIncomingWebhookRequestFromJson(t *testing.T) {
    89  	texts := []string{
    90  		`this is a test`,
    91  		`this is a test
    92  			that contains a newline and tabs`,
    93  		`this is a test \"foo
    94  			that contains a newline and tabs`,
    95  		`this is a test \"foo\"
    96  			that contains a newline and tabs`,
    97  		`this is a test \"foo\"
    98  		\"			that contains a newline and tabs`,
    99  		`this is a test \"foo\"
   100  
   101  		\"			that contains a newline and tabs
   102  		`,
   103  	}
   104  
   105  	for _, text := range texts {
   106  		// build a sample payload with the text
   107  		payload := `{
   108          "text": "` + text + `",
   109          "attachments": [
   110              {
   111                  "fallback": "` + text + `",
   112  
   113                  "color": "#36a64f",
   114  
   115                  "pretext": "` + text + `",
   116  
   117                  "author_name": "` + text + `",
   118                  "author_link": "http://flickr.com/bobby/",
   119                  "author_icon": "http://flickr.com/icons/bobby.jpg",
   120  
   121                  "title": "` + text + `",
   122                  "title_link": "https://api.slack.com/",
   123  
   124                  "text": "` + text + `",
   125  
   126                  "fields": [
   127                      {
   128                          "title": "` + text + `",
   129                          "value": "` + text + `",
   130                          "short": false
   131                      }
   132                  ],
   133  
   134                  "image_url": "http://my-website.com/path/to/image.jpg",
   135                  "thumb_url": "http://example.com/path/to/thumb.png"
   136              }
   137          ]
   138      }`
   139  
   140  		// try to create an IncomingWebhookRequest from the payload
   141  		data := strings.NewReader(payload)
   142  		iwr, _ := IncomingWebhookRequestFromJson(data)
   143  
   144  		// After it has been decoded, the JSON string won't contain the escape char anymore
   145  		expected := strings.Replace(text, `\"`, `"`, -1)
   146  		require.NotNil(t, iwr)
   147  		require.Equal(t, expected, iwr.Text)
   148  
   149  		attachment := iwr.Attachments[0]
   150  		require.Equal(t, expected, attachment.Text)
   151  	}
   152  }
   153  
   154  func TestIncomingWebhookNullArrayItems(t *testing.T) {
   155  	payload := `{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`
   156  	iwr, _ := IncomingWebhookRequestFromJson(strings.NewReader(payload))
   157  	require.NotNil(t, iwr)
   158  	require.Len(t, iwr.Attachments, 1)
   159  	require.Len(t, iwr.Attachments[0].Fields, 1)
   160  }