summaryrefslogtreecommitdiffstats
path: root/model/incoming_webhook_test.go
blob: 8246b6c0a7af15ea85ddffb918dd8f2b161d40ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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_Announcements(t *testing.T) {
	text := "This message will send a notification to all team members in the channel where you post the message, because it contains: <!channel>"
	expected := "This message will send a notification to all team members in the channel where you post the message, because it contains: @channel"

	// simple payload
	payload := `{"text": "` + text + `"}`
	data := strings.NewReader(payload)
	iwr := IncomingWebhookRequestFromJson(data)

	if iwr == nil {
		t.Fatal("IncomingWebhookRequest should not be nil")
	}
	if iwr.Text != expected {
		t.Fatalf("Sample text should be: %s, got: %s", expected, iwr.Text)
	}

	// payload with attachment (pretext, title, text, value)
	payload = `{
			"attachments": [
				{
					"pretext": "` + text + `",
					"title": "` + text + `",
					"text": "` + text + `",
					"fields": [
						{
							"title": "A title",
							"value": "` + text + `",
							"short": false
						}
					]
				}
			]
		}`

	data = strings.NewReader(payload)
	iwr = IncomingWebhookRequestFromJson(data)

	if iwr == nil {
		t.Fatal("IncomingWebhookRequest should not be nil")
	}

	attachments := iwr.Attachments.([]interface{})
	attachment := attachments[0].(map[string]interface{})
	if attachment["pretext"] != expected {
		t.Fatalf("Sample attachment pretext should be: %s, got: %s", expected, attachment["pretext"])
	}
	if attachment["text"] != expected {
		t.Fatalf("Sample attachment text should be: %s, got: %s", expected, attachment["text"])
	}
	if attachment["title"] != expected {
		t.Fatalf("Sample attachment title should be: %s, got: %s", expected, attachment["title"])
	}
	fields := attachment["fields"].([]interface{})
	field := fields[0].(map[string]interface{})
	if field["value"] != expected {
		t.Fatalf("Sample attachment field value should be: %s, got: %s", expected, field["value"])
	}
}

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"])
		}
	}
}