-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoonsla_test.go
270 lines (253 loc) · 5.55 KB
/
moonsla_test.go
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"testing"
"github.com/nlopes/slack"
)
func TestGetTimeStamp(t *testing.T) {
var tests = []struct {
description string
timeStamp string
want int64
}{
{
"Convert timestamp to something human readable",
"1530593277.000080",
1530593277,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
ts, _ := getTimeStamp(test.timeStamp)
got := ts.Unix()
want := test.want
if got != want {
t.Errorf("got '%v' want '%v'", got, want)
}
})
}
}
func TestFilterChannel(t *testing.T) {
var tests = []struct {
description string
id string
channels map[string]string
blacklist []string
whitelist []string
name string
whitelisted bool
}{
{
description: "Channel that is whitelisted",
id: "12345",
channels: map[string]string{
"12345": "channel-name",
},
blacklist: []string{""},
whitelist: []string{
"channel-name",
},
name: "channel-name",
whitelisted: true,
},
{
description: "Channel that is blacklisted",
id: "12345",
channels: map[string]string{
"12345": "channel-name",
},
blacklist: []string{"channel-name"},
whitelist: []string{""},
name: "channel-name",
whitelisted: false,
},
{
description: "Channel that is not whitelisted",
id: "12344",
channels: map[string]string{
"12345": "channel-name",
"12344": "spam-channel",
},
blacklist: []string{""},
whitelist: []string{
"channel-name",
},
name: "spam-channel",
whitelisted: false,
},
{
description: "Channel that is not in the channels list",
id: "123",
channels: map[string]string{
"12345": "channel-name",
"12344": "spam-channel",
},
blacklist: []string{""},
whitelist: []string{
"channel-name",
},
name: "123",
whitelisted: true,
},
{
description: "Empty whitelist matches all channels",
id: "12345",
channels: map[string]string{
"12345": "channel-name",
"12344": "spam-channel",
},
blacklist: []string{""},
whitelist: []string{
"",
},
name: "channel-name",
whitelisted: true,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
whitelisted, name := filterChannel(test.id, test.channels, test.whitelist, test.blacklist)
if name != test.name {
t.Errorf("got '%s' want '%s'", name, test.name)
}
if whitelisted != test.whitelisted {
t.Errorf("got '%v' want '%v'", whitelisted, test.whitelisted)
}
})
}
}
func TestFormatMentions(t *testing.T) {
var tests = []struct {
description string
message string
users map[string]string
want string
}{
{
"Replace mentions on message",
"hello <@U1234> how are you?",
map[string]string{
"U1234": "crazybus",
},
"hello @crazybus how are you?",
},
{
"Replace multiple mentions in a message",
"hello <@U1234> have you met <@U321> I think you would like them <@U1234>?",
map[string]string{
"U1234": "crazybus",
"U321": "notcrazybus",
},
"hello @crazybus have you met @notcrazybus I think you would like them @crazybus?",
},
{
"Don't replace anything if there are no mentions",
"hi",
map[string]string{
"U1234": "crazybus",
"U321": "notcrazybus",
},
"hi",
},
{
"Leave the id if the user can't be found",
"hi <@U999>!",
map[string]string{
"U1234": "crazybus",
"U321": "notcrazybus",
},
"hi <@U999>!",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
got := formatMentions(test.message, test.users)
want := test.want
if got != want {
t.Errorf("got '%s' want '%s'", got, want)
}
})
}
}
func TestFormatAttachments(t *testing.T) {
var tests = []struct {
description string
attachments []slack.Attachment
want string
}{
{
"Print attachment as single line",
[]slack.Attachment{slack.Attachment{
Title: "",
Text: "test message",
}},
"test message",
},
{
"Print attachment with title",
[]slack.Attachment{slack.Attachment{
Title: "title",
Text: "test message",
}},
"title: test message",
},
{
"Print multie attachments",
[]slack.Attachment{
slack.Attachment{
Title: "",
Text: "first message",
},
slack.Attachment{
Title: "",
Text: "second message",
}},
"first message\nsecond message",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
got := formatAttachments(test.attachments)
want := test.want
if got != want {
t.Errorf("got '%v' want '%v'", got, want)
}
})
}
}
func TestFormatUrls(t *testing.T) {
var tests = []struct {
description string
message string
want string
}{
{
"Message with no urls",
"test message",
"test message",
},
{
"Message with a url",
"hello <http://google.com|test> world",
"hello \x1b]8;;http://google.com\atest\x1b]8;;\a world",
},
{
"Message with a non-formatted url",
"hello <http://google.com> world",
"hello <http://google.com> world",
},
{
"Message with multiple urls",
"hello <http://google.com|test> world how <https://google.com|are you>",
"hello \x1b]8;;http://google.com\atest\x1b]8;;\a world how \x1b]8;;https://google.com\aare you\x1b]8;;\a",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
got := formatUrls(test.message)
want := test.want
if got != want {
t.Errorf("got '%v' want '%v'", got, want)
}
})
}
}