github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/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  
    11  func TestIncomingWebhookJson(t *testing.T) {
    12  	o := IncomingWebhook{Id: NewId()}
    13  	json := o.ToJson()
    14  	ro := IncomingWebhookFromJson(strings.NewReader(json))
    15  
    16  	if o.Id != ro.Id {
    17  		t.Fatal("Ids do not match")
    18  	}
    19  }
    20  
    21  func TestIncomingWebhookIsValid(t *testing.T) {
    22  	o := IncomingWebhook{}
    23  
    24  	if err := o.IsValid(); err == nil {
    25  		t.Fatal("should be invalid")
    26  	}
    27  
    28  	o.Id = NewId()
    29  	if err := o.IsValid(); err == nil {
    30  		t.Fatal("should be invalid")
    31  	}
    32  
    33  	o.CreateAt = GetMillis()
    34  	if err := o.IsValid(); err == nil {
    35  		t.Fatal("should be invalid")
    36  	}
    37  
    38  	o.UpdateAt = GetMillis()
    39  	if err := o.IsValid(); err == nil {
    40  		t.Fatal("should be invalid")
    41  	}
    42  
    43  	o.UserId = "123"
    44  	if err := o.IsValid(); err == nil {
    45  		t.Fatal("should be invalid")
    46  	}
    47  
    48  	o.UserId = NewId()
    49  	if err := o.IsValid(); err == nil {
    50  		t.Fatal("should be invalid")
    51  	}
    52  
    53  	o.ChannelId = "123"
    54  	if err := o.IsValid(); err == nil {
    55  		t.Fatal("should be invalid")
    56  	}
    57  
    58  	o.ChannelId = NewId()
    59  	if err := o.IsValid(); err == nil {
    60  		t.Fatal("should be invalid")
    61  	}
    62  
    63  	o.TeamId = "123"
    64  	if err := o.IsValid(); err == nil {
    65  		t.Fatal("should be invalid")
    66  	}
    67  
    68  	o.TeamId = NewId()
    69  	if err := o.IsValid(); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	o.DisplayName = strings.Repeat("1", 65)
    74  	if err := o.IsValid(); err == nil {
    75  		t.Fatal("should be invalid")
    76  	}
    77  
    78  	o.DisplayName = strings.Repeat("1", 64)
    79  	if err := o.IsValid(); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	o.Description = strings.Repeat("1", 129)
    84  	if err := o.IsValid(); err == nil {
    85  		t.Fatal("should be invalid")
    86  	}
    87  
    88  	o.Description = strings.Repeat("1", 128)
    89  	if err := o.IsValid(); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	o.Username = strings.Repeat("1", 65)
    94  	if err := o.IsValid(); err == nil {
    95  		t.Fatal("should be invalid")
    96  	}
    97  
    98  	o.Username = strings.Repeat("1", 64)
    99  	if err := o.IsValid(); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	o.IconURL = strings.Repeat("1", 1025)
   104  	if err := o.IsValid(); err == nil {
   105  		t.Fatal("should be invalid")
   106  	}
   107  
   108  	o.IconURL = strings.Repeat("1", 1024)
   109  	if err := o.IsValid(); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  }
   113  
   114  func TestIncomingWebhookPreSave(t *testing.T) {
   115  	o := IncomingWebhook{}
   116  	o.PreSave()
   117  }
   118  
   119  func TestIncomingWebhookPreUpdate(t *testing.T) {
   120  	o := IncomingWebhook{}
   121  	o.PreUpdate()
   122  }
   123  
   124  func TestIncomingWebhookRequestFromJson(t *testing.T) {
   125  	texts := []string{
   126  		`this is a test`,
   127  		`this is a test
   128  			that contains a newline and tabs`,
   129  		`this is a test \"foo
   130  			that contains a newline and tabs`,
   131  		`this is a test \"foo\"
   132  			that contains a newline and tabs`,
   133  		`this is a test \"foo\"
   134  		\"			that contains a newline and tabs`,
   135  		`this is a test \"foo\"
   136  
   137  		\"			that contains a newline and tabs
   138  		`,
   139  	}
   140  
   141  	for i, text := range texts {
   142  		// build a sample payload with the text
   143  		payload := `{
   144          "text": "` + text + `",
   145          "attachments": [
   146              {
   147                  "fallback": "` + text + `",
   148  
   149                  "color": "#36a64f",
   150  
   151                  "pretext": "` + text + `",
   152  
   153                  "author_name": "` + text + `",
   154                  "author_link": "http://flickr.com/bobby/",
   155                  "author_icon": "http://flickr.com/icons/bobby.jpg",
   156  
   157                  "title": "` + text + `",
   158                  "title_link": "https://api.slack.com/",
   159  
   160                  "text": "` + text + `",
   161  
   162                  "fields": [
   163                      {
   164                          "title": "` + text + `",
   165                          "value": "` + text + `",
   166                          "short": false
   167                      }
   168                  ],
   169  
   170                  "image_url": "http://my-website.com/path/to/image.jpg",
   171                  "thumb_url": "http://example.com/path/to/thumb.png"
   172              }
   173          ]
   174      }`
   175  
   176  		// try to create an IncomingWebhookRequest from the payload
   177  		data := strings.NewReader(payload)
   178  		iwr, _ := IncomingWebhookRequestFromJson(data)
   179  
   180  		// After it has been decoded, the JSON string won't contain the escape char anymore
   181  		expected := strings.Replace(text, `\"`, `"`, -1)
   182  		if iwr == nil {
   183  			t.Fatal("IncomingWebhookRequest should not be nil")
   184  		}
   185  		if iwr.Text != expected {
   186  			t.Fatalf("Sample %d text should be: %s, got: %s", i, expected, iwr.Text)
   187  		}
   188  
   189  		attachment := iwr.Attachments[0]
   190  		if attachment.Text != expected {
   191  			t.Fatalf("Sample %d attachment text should be: %s, got: %s", i, expected, attachment.Text)
   192  		}
   193  	}
   194  }
   195  
   196  func TestIncomingWebhookNullArrayItems(t *testing.T) {
   197  	payload := `{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`
   198  	iwr, _ := IncomingWebhookRequestFromJson(strings.NewReader(payload))
   199  	if iwr == nil {
   200  		t.Fatal("IncomingWebhookRequest should not be nil")
   201  	}
   202  	if len(iwr.Attachments) != 1 {
   203  		t.Fatalf("expected one attachment")
   204  	}
   205  	if len(iwr.Attachments[0].Fields) != 1 {
   206  		t.Fatalf("expected one field")
   207  	}
   208  }