-
Notifications
You must be signed in to change notification settings - Fork 19
/
slackrus_test.go
207 lines (173 loc) · 5.03 KB
/
slackrus_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
package slackrus
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/johntdyer/slack-go"
"github.com/sirupsen/logrus"
)
type Fixture struct {
Messages []slack.Message
Cleanup func()
MsgRcvd chan struct{}
server *httptest.Server
}
func (f *Fixture) URL() string {
return f.server.URL
}
func NewFixture(t *testing.T) *Fixture {
f := &Fixture{MsgRcvd: make(chan struct{}, 1)}
f.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var msg slack.Message
dec := json.NewDecoder(r.Body)
err := dec.Decode(&msg)
if err != nil {
w.WriteHeader(500)
return
}
f.Messages = append(f.Messages, msg)
select {
case f.MsgRcvd <- struct{}{}:
default:
}
}))
f.Cleanup = func() { f.server.Close() }
return f
}
func TestFieldSorting(t *testing.T) {
f := NewFixture(t)
defer f.Cleanup()
logger := logrus.New()
logger.AddHook(&SlackrusHook{
HookURL: f.URL(),
Channel: "#slack-testing",
SortFields: true,
})
first, second, third := "a_should_be_first", "b_should_be_second", "c_should_be_third"
logger.WithFields(logrus.Fields{
second: "b content",
third: "c content",
first: "a content",
}).Info("well hello there, you better sort my fields!!!!")
<-f.MsgRcvd
if exp, got := 1, len(f.Messages); exp != got {
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
}
msg := f.Messages[0]
if exp, got := 1, len(msg.Attachments); exp != got {
t.Fatalf("received unexpected number of Attachments in message: exp: %d, got: %d", exp, got)
}
fields := msg.Attachments[0].Fields
if exp, got := 3, len(fields); exp != got {
t.Fatalf("received unexpected number of Fields in attachment: exp: %d, got: %d", exp, got)
}
if exp, got := first, fields[0].Title; exp != got {
t.Errorf("0-th field title not as expected: exp: %q, got: %q", exp, got)
}
if exp, got := second, fields[1].Title; exp != got {
t.Errorf("1st field title not as expected: exp: %q, got: %q", exp, got)
}
if exp, got := third, fields[2].Title; exp != got {
t.Errorf("2nd field title not as expected: exp: %q, got: %q", exp, got)
}
}
func TestFieldSortPriorities(t *testing.T) {
f := NewFixture(t)
defer f.Cleanup()
first, second, third, fourth := "d_should_be_first", "b_should_be_second", "a_should_be_third", "c_should_be_fourth"
logger := logrus.New()
logger.AddHook(&SlackrusHook{
HookURL: f.URL(),
Channel: "#slack-testing",
SortFields: true,
SortPriorities: map[string]int{
first: 10,
second: 5,
},
})
logger.WithFields(logrus.Fields{
first: "first content",
second: "second content",
third: "third content",
fourth: "fourth content",
}).Info("well hello there, you better sort my fields!!!!")
<-f.MsgRcvd
if exp, got := 1, len(f.Messages); exp != got {
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
}
msg := f.Messages[0]
if exp, got := 1, len(msg.Attachments); exp != got {
t.Fatalf("received unexpected number of Attachments in message: exp: %d, got: %d", exp, got)
}
fields := msg.Attachments[0].Fields
if exp, got := 4, len(fields); exp != got {
t.Fatalf("received unexpected number of Fields in attachment: exp: %d, got: %d", exp, got)
}
if exp, got := first, fields[0].Title; exp != got {
t.Errorf("0-th field title not as expected: exp: %q, got: %q", exp, got)
}
if exp, got := second, fields[1].Title; exp != got {
t.Errorf("1st field title not as expected: exp: %q, got: %q", exp, got)
}
if exp, got := third, fields[2].Title; exp != got {
t.Errorf("2nd field title not as expected: exp: %q, got: %q", exp, got)
}
if exp, got := fourth, fields[3].Title; exp != got {
t.Errorf("3rd field title not as expected: exp: %q, got: %q", exp, got)
}
}
type filterMock struct {
called int
rejectNext bool
}
func (f *filterMock) Filter(e *logrus.Entry) bool {
f.called++
if f.rejectNext {
f.rejectNext = false
return false
}
return true
}
func TestFilters(t *testing.T) {
f := NewFixture(t)
defer f.Cleanup()
mock := filterMock{}
logger := logrus.New()
logger.AddHook(&SlackrusHook{
HookURL: f.URL(),
Channel: "#slack-testing",
Filters: []Filter{
mock.Filter,
},
})
logger.Info("hi there")
<-f.MsgRcvd
if exp, got := 1, len(f.Messages); exp != got {
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
}
if mock.called != 1 {
t.Fatalf("filter should have been called")
}
logger.Info("hi there")
<-f.MsgRcvd
if exp, got := 2, len(f.Messages); exp != got {
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
}
if mock.called != 2 {
t.Fatalf("filter should have been called")
}
// set the mock to reject the next
mock.rejectNext = true
logger.Info("hi there")
select {
case <-f.MsgRcvd:
t.Fatalf("did not expect a message to be send")
case <-time.After(10 * time.Millisecond):
// no message after 10ms, should be fine
}
if exp, got := 2, len(f.Messages); exp != got {
t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got)
}
}