forked from botopolis/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_internal_test.go
135 lines (120 loc) · 3.05 KB
/
proxy_internal_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
package slack
import (
"testing"
"github.com/botopolis/bot"
"github.com/botopolis/bot/mock"
"github.com/nlopes/slack"
"github.com/stretchr/testify/assert"
)
func TestProxy(t *testing.T) {
assert := assert.New(t)
room := "C1234"
user := "U1234"
text := "foo bar"
topic := "generally awesome"
proxyTestCases := []struct {
In slack.MessageEvent
Assert func(bot.Message, bot.Message)
}{
{
In: slack.MessageEvent(slack.Message{Msg: slack.Msg{
SubType: "",
User: user,
Channel: room,
Text: text,
}}),
Assert: func(expected bot.Message, result bot.Message) {
assert.Equal(bot.DefaultMessage, result.Type)
assert.Equal(expected.Text, result.Text)
},
},
{
In: slack.MessageEvent(slack.Message{Msg: slack.Msg{
SubType: "channel_join",
User: user,
Channel: room,
Text: text,
}}),
Assert: func(expected bot.Message, result bot.Message) {
assert.Equal(bot.Enter, result.Type)
assert.Equal(expected.Text, result.Text)
},
},
{
In: slack.MessageEvent(slack.Message{Msg: slack.Msg{
SubType: "channel_leave",
User: user,
Channel: room,
Text: text,
}}),
Assert: func(expected bot.Message, result bot.Message) {
assert.Equal(bot.Leave, result.Type)
assert.Equal(expected.Text, result.Text)
},
},
{
In: slack.MessageEvent(slack.Message{Msg: slack.Msg{
SubType: "channel_topic",
User: user,
Channel: room,
Topic: topic,
Text: text,
}}),
Assert: func(expected bot.Message, result bot.Message) {
assert.Equal(bot.Topic, result.Type)
assert.Equal(expected.Topic, result.Topic)
},
},
}
store := newTestStore()
p := proxy{
Adapter: New(""),
formatter: formatter{store},
}
for _, c := range proxyTestCases {
in := make(chan slack.RTMEvent, 2)
out := make(chan bot.Message, 2)
go p.Forward(in, out)
in <- slack.RTMEvent{Type: "message", Data: &c.In}
c.Assert(bot.Message{
User: "bob",
Room: "general",
Text: text,
Envelope: &c.In,
Topic: topic,
}, <-out)
close(in)
}
}
func TestProxyForward_connect(t *testing.T) {
var run bool
info := &slack.Info{User: &slack.UserDetails{ID: "B1234", Name: "beardroid"}}
store := newTestStore()
store.LoadFunc = func(i *slack.Info) {
assert.Equal(t, info, i)
run = true
}
p := proxy{Adapter: New("")}
p.Adapter.Store = store
p.Load(bot.New(mock.NewChat()))
ch := make(chan slack.RTMEvent, 2)
ch <- slack.RTMEvent{Data: &slack.ConnectedEvent{Info: info}}
close(ch)
p.Forward(ch, make(chan bot.Message))
assert.Equal(t, info.User.ID, p.Adapter.BotID)
assert.Equal(t, info.User.Name, p.Adapter.Username())
assert.True(t, run)
}
func TestProxyForward_invalidAuth(t *testing.T) {
done := make(chan bool)
p := proxy{Adapter: New("")}
p.Load(&bot.Robot{Logger: mock.NewLogger()})
// Send the event, explicitly don't close the channel
ch := make(chan slack.RTMEvent, 2)
ch <- slack.RTMEvent{Data: &slack.InvalidAuthEvent{}}
go func() {
p.Forward(ch, make(chan bot.Message))
done <- true
}()
assert.True(t, <-done)
}