Files
mattermost/model/incoming_webhook_test.go
Thomas Balthazar 5580c28e54 PLT-2188 Integrations: Support raw new lines in the text payload (#2993)
* Integrations: Support raw new lines in the text payload

* Improve support for raw new lines in text payload

The regexp used to escape control characters now also searches for
additional fields:
text|fallback|pretext|author_name|title|value
2016-05-17 08:56:38 -04:00

175 lines
4.0 KiB
Go

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"strings"
"testing"
)
func TestIncomingWebhookJson(t *testing.T) {
o := IncomingWebhook{Id: NewId()}
json := o.ToJson()
ro := IncomingWebhookFromJson(strings.NewReader(json))
if o.Id != ro.Id {
t.Fatal("Ids do not match")
}
}
func TestIncomingWebhookIsValid(t *testing.T) {
o := IncomingWebhook{}
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.Id = NewId()
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.CreateAt = GetMillis()
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.UpdateAt = GetMillis()
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.UserId = "123"
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.UserId = NewId()
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.ChannelId = "123"
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.ChannelId = NewId()
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.TeamId = "123"
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.TeamId = NewId()
if err := o.IsValid(); err != nil {
t.Fatal(err)
}
o.DisplayName = strings.Repeat("1", 65)
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.DisplayName = strings.Repeat("1", 64)
if err := o.IsValid(); err != nil {
t.Fatal(err)
}
o.Description = strings.Repeat("1", 129)
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.Description = strings.Repeat("1", 128)
if err := o.IsValid(); err != nil {
t.Fatal(err)
}
}
func TestIncomingWebhookPreSave(t *testing.T) {
o := IncomingWebhook{}
o.PreSave()
}
func TestIncomingWebhookPreUpdate(t *testing.T) {
o := IncomingWebhook{}
o.PreUpdate()
}
func TestIncomingWebhookRequestFromJson(t *testing.T) {
texts := []string{
`this is a test`,
`this is a test
that contains a newline and tabs`,
`this is a test \"foo
that contains a newline and tabs`,
`this is a test \"foo\"
that contains a newline and tabs`,
`this is a test \"foo\"
\" that contains a newline and tabs`,
`this is a test \"foo\"
\" that contains a newline and tabs
`,
}
for i, text := range texts {
// build a sample payload with the text
payload := `{
"text": "` + text + `",
"attachments": [
{
"fallback": "` + text + `",
"color": "#36a64f",
"pretext": "` + text + `",
"author_name": "` + text + `",
"author_link": "http://flickr.com/bobby/",
"author_icon": "http://flickr.com/icons/bobby.jpg",
"title": "` + text + `",
"title_link": "https://api.slack.com/",
"text": "` + text + `",
"fields": [
{
"title": "` + text + `",
"value": "` + text + `",
"short": false
}
],
"image_url": "http://my-website.com/path/to/image.jpg",
"thumb_url": "http://example.com/path/to/thumb.png"
}
]
}`
// try to create an IncomingWebhookRequest from the payload
data := strings.NewReader(payload)
iwr := IncomingWebhookRequestFromJson(data)
// After it has been decoded, the JSON string won't contain the escape char anymore
expected := strings.Replace(text, `\"`, `"`, -1)
if iwr == nil {
t.Fatal("IncomingWebhookRequest should not be nil")
}
if iwr.Text != expected {
t.Fatalf("Sample %d text should be: %s, got: %s", i, expected, iwr.Text)
}
attachments := iwr.Attachments.([]interface{})
attachment := attachments[0].(map[string]interface{})
if attachment["text"] != expected {
t.Fatalf("Sample %d attachment text should be: %s, got: %s", i, expected, attachment["text"])
}
}
}