Debian Codegraph @

slacktest/server_test.go

raw symbols SCIP
1 package slacktest
2
3 import (
4 "encoding/json"
5 "fmt"
6 "testing"
7 "time"
8
9 "github.com/stretchr/testify/assert"
10
11 "github.com/slack-go/slack"
12 )
13
14 func TestDefaultNewServer(t *testing.T) {
15 s := NewTestServer()
16 assert.Equal(t, defaultBotID, s.BotID)
17 assert.Equal(t, defaultBotName, s.BotName)
18 assert.NotEmpty(t, s.ServerAddr)
19 s.Stop()
20 }
21
22 func TestCustomNewServer(t *testing.T) {
23 s := NewTestServer()
24 s.SetBotName("BobsBot")
25 assert.Equal(t, "BobsBot", s.BotName)
26 }
27
28 func TestServerSendMessageToChannel(t *testing.T) {
29 s := NewTestServer()
30 go s.Start()
31 s.SendMessageToChannel("C123456789", "some text")
32 time.Sleep(2 * time.Second)
33 assert.True(t, s.SawOutgoingMessage("some text"))
34 s.Stop()
35 }
36
37 func TestServerSendMessageToBot(t *testing.T) {
38 s := NewTestServer()
39 go s.Start()
40 s.SendMessageToBot("C123456789", "some text")
41 expectedMsg := fmt.Sprintf("<@%s> %s", s.BotID, "some text")
42 time.Sleep(2 * time.Second)
43 assert.True(t, s.SawOutgoingMessage(expectedMsg))
44 s.Stop()
45 }
46
47 func TestBotDirectMessageBotHandler(t *testing.T) {
48 s := NewTestServer()
49 go s.Start()
50 s.SendDirectMessageToBot("some text")
51 expectedMsg := fmt.Sprintf("some text")
52 time.Sleep(2 * time.Second)
53 assert.True(t, s.SawOutgoingMessage(expectedMsg))
54 s.Stop()
55 }
56
57 func TestGetSeenOutboundMessages(t *testing.T) {
58 maxWait := 5 * time.Second
59 s := NewTestServer()
60 go s.Start()
61
62 s.SendMessageToChannel("foo", "should see this message")
63 time.Sleep(maxWait)
64 seenOutbound := s.GetSeenOutboundMessages()
65 assert.True(t, len(seenOutbound) > 0)
66 hadMessage := false
67 for _, msg := range seenOutbound {
68 var m = slack.Message{}
69 jerr := json.Unmarshal([]byte(msg), &m)
70 assert.NoError(t, jerr, "messages should decode as slack.Message")
71 if m.Text == "should see this message" {
72 hadMessage = true
73 break
74 }
75 }
76 assert.True(t, hadMessage, "did not see my sent message")
77 }
78
79 func TestGetSeenInboundMessages(t *testing.T) {
80 maxWait := 5 * time.Second
81 s := NewTestServer()
82 go s.Start()
83
84 api := slack.New("ABCDEFG", slack.OptionAPIURL(s.GetAPIURL()))
85 rtm := api.NewRTM()
86 go rtm.ManageConnection()
87 rtm.SendMessage(&slack.OutgoingMessage{
88 Channel: "foo",
89 Text: "should see this inbound message",
90 })
91 time.Sleep(maxWait)
92 seenInbound := s.GetSeenInboundMessages()
93 assert.True(t, len(seenInbound) > 0)
94 hadMessage := false
95 for _, msg := range seenInbound {
96 var m = slack.Message{}
97 jerr := json.Unmarshal([]byte(msg), &m)
98 assert.NoError(t, jerr, "messages should decode as slack.Message")
99 if m.Text == "should see this inbound message" {
100 hadMessage = true
101 break
102 }
103 }
104 assert.True(t, hadMessage, "did not see my sent message")
105 assert.True(t, s.SawMessage("should see this inbound message"))
106 }
107
108 func TestSendChannelInvite(t *testing.T) {
109 maxWait := 5 * time.Second
110 s := NewTestServer()
111 go s.Start()
112 rtm := s.GetTestRTMInstance()
113 go rtm.ManageConnection()
114 evChan := make(chan (slack.Channel), 1)
115 go func() {
116 for msg := range rtm.IncomingEvents {
117 switch ev := msg.Data.(type) {
118 case *slack.ChannelJoinedEvent:
119 evChan <- ev.Channel
120 }
121 }
122 }()
123 s.SendBotChannelInvite()
124 time.Sleep(maxWait)
125 select {
126 case m := <-evChan:
127 assert.Equal(t, "C024BE92L", m.ID, "channel id should match")
128 assert.Equal(t, "Fun times", m.Topic.Value, "topic should match")
129 s.Stop()
130 break
131 case <-time.After(maxWait):
132 assert.FailNow(t, "did not get channel joined event in time")
133 }
134
135 }
136
137 func TestSendGroupInvite(t *testing.T) {
138 maxWait := 5 * time.Second
139 s := NewTestServer()
140 go s.Start()
141 rtm := s.GetTestRTMInstance()
142 go rtm.ManageConnection()
143 evChan := make(chan (slack.Channel), 1)
144 go func() {
145 for msg := range rtm.IncomingEvents {
146 switch ev := msg.Data.(type) {
147 case *slack.GroupJoinedEvent:
148 evChan <- ev.Channel
149 }
150 }
151 }()
152 s.SendBotGroupInvite()
153 time.Sleep(maxWait)
154 select {
155 case m := <-evChan:
156 assert.Equal(t, "G024BE91L", m.ID, "channel id should match")
157 assert.Equal(t, "Secret plans on hold", m.Topic.Value, "topic should match")
158 s.Stop()
159 break
160 case <-time.After(maxWait):
161 assert.FailNow(t, "did not get group joined event in time")
162 }
163
164 }
165
166 func TestServerSawMessage(t *testing.T) {
167 s := NewTestServer()
168 go s.Start()
169 assert.False(t, s.SawMessage("foo"), "should not have seen any message")
170 }
171
172 func TestServerSawOutgoingMessage(t *testing.T) {
173 s := NewTestServer()
174 go s.Start()
175 assert.False(t, s.SawOutgoingMessage("foo"), "should not have seen any message")
176 }