-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack_test.go
185 lines (166 loc) · 4.86 KB
/
slack_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
package bot
import (
"context"
"reflect"
"testing"
"time"
)
type mockPlugin struct {
NameFn func() string
InitFn func(ctx context.Context, out chan Message) error
HandleFn func(context.Context, interface{}) (handled bool, msg interface{})
}
func (m mockPlugin) Name() string {
if m.NameFn == nil {
panic("not implemented")
}
return m.NameFn()
}
func (m mockPlugin) Init(ctx context.Context, out chan Message, cli Client) error {
if m.InitFn == nil {
panic("not implemented")
}
return m.InitFn(ctx, out)
}
func (m mockPlugin) Handle(ctx context.Context, inMsg interface{}) (handled bool, msg interface{}) {
if m.HandleFn == nil {
panic("not implemented")
}
return m.HandleFn(ctx, inMsg)
}
func TestSlack_AddPlugin(t *testing.T) {
t.Run("full chain", func(t *testing.T) {
s, err := NewSlack("")
if err != nil {
t.Fatal(err)
}
p1 := mockPlugin{
NameFn: func() string { return "p1" },
InitFn: func(ctx context.Context, out chan Message) error { return nil },
HandleFn: func(ctx context.Context, m interface{}) (handled bool, msg interface{}) {
inMsg := m.(*Message)
inMsg.Text += "1"
return false, inMsg.WithContext(context.WithValue(ctx, "val", "1"))
},
}
p2 := mockPlugin{
NameFn: func() string { return "p2" },
InitFn: func(ctx context.Context, out chan Message) error { return nil },
HandleFn: func(ctx context.Context, m interface{}) (handled bool, msg interface{}) {
inMsg := m.(*Message)
inMsg.Text += "2"
val := inMsg.Context().Value("val")
return false, inMsg.WithContext(context.WithValue(ctx, "val", val.(string)+"2"))
},
}
err = s.AddPlugins(p1, p2)
if err != nil {
t.Fatal(err)
}
s.initPlugins(context.Background())
var m Message
handled, msgRaw := s.handler(&m)
msg := msgRaw.(*Message)
if got, want := handled, true; got != want {
t.Errorf("got handled %t want %t", got, want)
}
if msg == nil {
t.Fatal("nil message")
}
if got, want := msg.Text, "12"; got != want {
t.Errorf("got handled %s want %s", got, want)
}
got, want := msg.ctx.Value("val"), "12"
if got != want {
t.Errorf("got context value %s want %s", got, want)
}
})
t.Run("short circuit", func(t *testing.T) {
s, err := NewSlack("")
if err != nil {
t.Fatal(err)
}
p1 := mockPlugin{
NameFn: func() string { return "p1" },
InitFn: func(ctx context.Context, out chan Message) error { return nil },
HandleFn: func(ctx context.Context, in interface{}) (handled bool, msg interface{}) {
m := in.(*Message)
m.Text += "1"
return true, m
},
}
p2 := mockPlugin{
NameFn: func() string { return "p1" },
InitFn: func(ctx context.Context, out chan Message) error { return nil },
HandleFn: func(ctx context.Context, in interface{}) (handled bool, msg interface{}) {
m := in.(*Message)
m.Text += "2"
return true, m
},
}
ctx := context.Background()
err = s.AddPlugins(p1, p2)
if err != nil {
t.Fatal(err)
}
s.initPlugins(ctx)
var m Message
handled, rawMsg := s.handler(&m)
if got, want := handled, true; got != want {
t.Errorf("got handled %t want %t", got, want)
}
msg := rawMsg.(*Message)
if msg == nil {
t.Fatal("nil message")
}
if got, want := msg.Text, "1"; got != want {
t.Errorf("got handled %q want %q", got, want)
}
})
}
func TestParseIncomingMessage(t *testing.T) {
golden := []struct {
name string
raw string
want *Message
wantErr bool
}{
{
name: "with attachement",
raw: `{"text":"","username":"gerrit","bot_id":"B06P5RMNH","attachments":[{"fallback":"Patch Set 1:\n\n(1 comment)","text":"Patch Set 1:\n\n(1 comment)","pretext":"[icas] New comment on change <https://gerrit.ecg.so/58629|58629>","title":"Comment by Daniela Remenska (<mailto:[email protected]|[email protected]>)","id":1,"mrkdwn_in":["text","pretext"]}],"type":"message","subtype":"bot_message","team":"T03L14Q4E","channel":"G04EC7B6V","event_ts":"1497280551.238075","ts":"1497280551.238075"}`,
wantErr: false,
want: &Message{
ID: "1497280551.238075",
From: User{
Username: "gerrit",
},
Date: time.Unix(1497280551, 0),
Chat: Chat{
ID: "G04EC7B6V",
Type: Group,
},
Attachments: []Attachment{
{
Fallback: "Patch Set 1:\n\n(1 comment)",
Text: "Patch Set 1:\n\n(1 comment)",
Pretext: "[icas] New comment on change <https://gerrit.ecg.so/58629|58629>",
Title: "Comment by Daniela Remenska (<mailto:[email protected]|[email protected]>)",
ID: 1,
},
},
},
},
}
for _, tt := range golden {
t.Run(tt.name, func(t *testing.T) {
s := Slack{}
got, err := s.ParseRawMessage([]byte(tt.raw))
if (err != nil && !tt.wantErr) || (err == nil && tt.wantErr) {
t.Errorf("got error %s, want error %t ", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("got %+v want %+v", got, tt.want)
}
})
}
}