| 1 | package slacktest |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | |
| 9 | "github.com/slack-go/slack" |
| 10 | ) |
| 11 | |
| 12 | func TestRTMInfo(t *testing.T) { |
| 13 | maxWait := 10 * time.Millisecond |
| 14 | s := NewTestServer() |
| 15 | go s.Start() |
| 16 | |
| 17 | api := slack.New("ABCDEFG", slack.OptionAPIURL(s.GetAPIURL())) |
| 18 | rtm := api.NewRTM() |
| 19 | go rtm.ManageConnection() |
| 20 | messageChan := make(chan (*slack.ConnectedEvent), 1) |
| 21 | go func() { |
| 22 | for msg := range rtm.IncomingEvents { |
| 23 | switch ev := msg.Data.(type) { |
| 24 | case *slack.ConnectedEvent: |
| 25 | messageChan <- ev |
| 26 | } |
| 27 | } |
| 28 | }() |
| 29 | select { |
| 30 | case m := <-messageChan: |
| 31 | assert.Equal(t, s.BotID, m.Info.User.ID, "bot id did not match") |
| 32 | assert.Equal(t, s.BotName, m.Info.User.Name, "bot name did not match") |
| 33 | break |
| 34 | case <-time.After(maxWait): |
| 35 | assert.FailNow(t, "did not get connected event in time") |
| 36 | |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestRTMPing(t *testing.T) { |
| 41 | if testing.Short() { |
| 42 | t.Skip("skipping timered test") |
| 43 | } |
| 44 | maxWait := 45 * time.Second |
| 45 | s := NewTestServer() |
| 46 | go s.Start() |
| 47 | |
| 48 | api := slack.New("ABCDEFG", slack.OptionAPIURL(s.GetAPIURL())) |
| 49 | rtm := api.NewRTM() |
| 50 | go rtm.ManageConnection() |
| 51 | messageChan := make(chan (*slack.LatencyReport), 1) |
| 52 | go func() { |
| 53 | for msg := range rtm.IncomingEvents { |
| 54 | switch ev := msg.Data.(type) { |
| 55 | case *slack.LatencyReport: |
| 56 | messageChan <- ev |
| 57 | } |
| 58 | } |
| 59 | }() |
| 60 | select { |
| 61 | case m := <-messageChan: |
| 62 | assert.NotEmpty(t, m.Value, "latency report should value a value") |
| 63 | assert.True(t, m.Value > 0, "latency report should be greater than 0") |
| 64 | break |
| 65 | case <-time.After(maxWait): |
| 66 | assert.FailNow(t, "did not get latency report in time") |
| 67 | |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestRTMDirectMessage(t *testing.T) { |
| 72 | maxWait := 5 * time.Second |
| 73 | s := NewTestServer() |
| 74 | go s.Start() |
| 75 | |
| 76 | api := slack.New("ABCDEFG", slack.OptionAPIURL(s.GetAPIURL())) |
| 77 | rtm := api.NewRTM() |
| 78 | go rtm.ManageConnection() |
| 79 | messageChan := make(chan (*slack.MessageEvent), 1) |
| 80 | go func() { |
| 81 | for msg := range rtm.IncomingEvents { |
| 82 | switch ev := msg.Data.(type) { |
| 83 | case *slack.MessageEvent: |
| 84 | messageChan <- ev |
| 85 | } |
| 86 | } |
| 87 | }() |
| 88 | s.SendDirectMessageToBot("some text") |
| 89 | select { |
| 90 | case m := <-messageChan: |
| 91 | assert.Equal(t, defaultNonBotUserID, m.User) |
| 92 | assert.Equal(t, "D024BE91L", m.Channel) |
| 93 | assert.Equal(t, "some text", m.Text) |
| 94 | break |
| 95 | case <-time.After(maxWait): |
| 96 | assert.FailNow(t, "did not get direct message in time") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestRTMChannelMessage(t *testing.T) { |
| 101 | maxWait := 5 * time.Second |
| 102 | s := NewTestServer() |
| 103 | go s.Start() |
| 104 | |
| 105 | api := slack.New("ABCDEFG", slack.OptionAPIURL(s.GetAPIURL())) |
| 106 | rtm := api.NewRTM() |
| 107 | go rtm.ManageConnection() |
| 108 | messageChan := make(chan (*slack.MessageEvent), 1) |
| 109 | go func() { |
| 110 | for msg := range rtm.IncomingEvents { |
| 111 | switch ev := msg.Data.(type) { |
| 112 | case *slack.MessageEvent: |
| 113 | messageChan <- ev |
| 114 | } |
| 115 | } |
| 116 | }() |
| 117 | s.SendMessageToChannel("#foochan", "some text") |
| 118 | select { |
| 119 | case m := <-messageChan: |
| 120 | assert.Equal(t, "#foochan", m.Channel) |
| 121 | assert.Equal(t, "some text", m.Text) |
| 122 | break |
| 123 | case <-time.After(maxWait): |
| 124 | assert.FailNow(t, "did not get channel message in time") |
| 125 | } |
| 126 | |
| 127 | } |