forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
175 lines (158 loc) · 4.3 KB
/
server_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
package slacktest
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/nlopes/slack"
"github.com/stretchr/testify/assert"
)
func TestDefaultNewServer(t *testing.T) {
s := NewTestServer()
assert.Equal(t, defaultBotID, s.BotID)
assert.Equal(t, defaultBotName, s.BotName)
assert.NotEmpty(t, s.ServerAddr)
s.Stop()
}
func TestCustomNewServer(t *testing.T) {
s := NewTestServer()
s.SetBotName("BobsBot")
assert.Equal(t, "BobsBot", s.BotName)
}
func TestServerSendMessageToChannel(t *testing.T) {
s := NewTestServer()
go s.Start()
s.SendMessageToChannel("C123456789", "some text")
time.Sleep(2 * time.Second)
assert.True(t, s.SawOutgoingMessage("some text"))
s.Stop()
}
func TestServerSendMessageToBot(t *testing.T) {
s := NewTestServer()
go s.Start()
s.SendMessageToBot("C123456789", "some text")
expectedMsg := fmt.Sprintf("<@%s> %s", s.BotID, "some text")
time.Sleep(2 * time.Second)
assert.True(t, s.SawOutgoingMessage(expectedMsg))
s.Stop()
}
func TestBotDirectMessageBotHandler(t *testing.T) {
s := NewTestServer()
go s.Start()
s.SendDirectMessageToBot("some text")
expectedMsg := fmt.Sprintf("some text")
time.Sleep(2)
assert.True(t, s.SawOutgoingMessage(expectedMsg))
s.Stop()
}
func TestGetSeenOutboundMessages(t *testing.T) {
maxWait := 5 * time.Second
s := NewTestServer()
go s.Start()
s.SendMessageToChannel("foo", "should see this message")
time.Sleep(maxWait)
seenOutbound := s.GetSeenOutboundMessages()
assert.True(t, len(seenOutbound) > 0)
hadMessage := false
for _, msg := range seenOutbound {
var m = slack.Message{}
jerr := json.Unmarshal([]byte(msg), &m)
assert.NoError(t, jerr, "messages should decode as slack.Message")
if m.Text == "should see this message" {
hadMessage = true
break
}
}
assert.True(t, hadMessage, "did not see my sent message")
}
func TestGetSeenInboundMessages(t *testing.T) {
maxWait := 5 * time.Second
s := NewTestServer()
go s.Start()
api := slack.New("ABCDEFG", slack.OptionAPIURL(s.GetAPIURL()))
rtm := api.NewRTM()
go rtm.ManageConnection()
rtm.SendMessage(&slack.OutgoingMessage{
Channel: "foo",
Text: "should see this inbound message",
})
time.Sleep(maxWait)
seenInbound := s.GetSeenInboundMessages()
assert.True(t, len(seenInbound) > 0)
hadMessage := false
for _, msg := range seenInbound {
var m = slack.Message{}
jerr := json.Unmarshal([]byte(msg), &m)
assert.NoError(t, jerr, "messages should decode as slack.Message")
if m.Text == "should see this inbound message" {
hadMessage = true
break
}
}
assert.True(t, hadMessage, "did not see my sent message")
assert.True(t, s.SawMessage("should see this inbound message"))
}
func TestSendChannelInvite(t *testing.T) {
maxWait := 5 * time.Second
s := NewTestServer()
go s.Start()
rtm := s.GetTestRTMInstance()
go rtm.ManageConnection()
evChan := make(chan (slack.Channel), 1)
go func() {
for msg := range rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.ChannelJoinedEvent:
evChan <- ev.Channel
}
}
}()
s.SendBotChannelInvite()
time.Sleep(maxWait)
select {
case m := <-evChan:
assert.Equal(t, "C024BE92L", m.ID, "channel id should match")
assert.Equal(t, "Fun times", m.Topic.Value, "topic should match")
s.Stop()
break
case <-time.After(maxWait):
assert.FailNow(t, "did not get channel joined event in time")
}
}
func TestSendGroupInvite(t *testing.T) {
maxWait := 5 * time.Second
s := NewTestServer()
go s.Start()
rtm := s.GetTestRTMInstance()
go rtm.ManageConnection()
evChan := make(chan (slack.Channel), 1)
go func() {
for msg := range rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.GroupJoinedEvent:
evChan <- ev.Channel
}
}
}()
s.SendBotGroupInvite()
time.Sleep(maxWait)
select {
case m := <-evChan:
assert.Equal(t, "G024BE91L", m.ID, "channel id should match")
assert.Equal(t, "Secret plans on hold", m.Topic.Value, "topic should match")
s.Stop()
break
case <-time.After(maxWait):
assert.FailNow(t, "did not get group joined event in time")
}
}
func TestServerSawMessage(t *testing.T) {
s := NewTestServer()
go s.Start()
assert.False(t, s.SawMessage("foo"), "should not have seen any message")
}
func TestServerSawOutgoingMessage(t *testing.T) {
s := NewTestServer()
go s.Start()
assert.False(t, s.SawOutgoingMessage("foo"), "should not have seen any message")
}