-
Notifications
You must be signed in to change notification settings - Fork 6
/
slack_test.go
69 lines (58 loc) · 1.53 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
// Copyright 2015 Bowery, Inc.
package slack
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
var (
testClient *Client
testChannel = "#testing"
testBadChannel = "#foobar"
testText = "trying this out"
testUsername = "drizzy drake"
)
func init() {
testClient = NewClient("some-token")
}
func TestSendMessageSuccessful(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(sendMessageHandlerOK))
defer server.Close()
slackAddr = server.URL
err := testClient.SendMessage(testChannel, testText, testUsername)
if err != nil {
t.Error(err)
}
}
func sendMessageHandlerOK(rw http.ResponseWriter, req *http.Request) {
res := &slackPostMessageRes{Ok: true}
body, _ := json.Marshal(res)
rw.Write(body)
}
func TestSendMessageMissingArgument(t *testing.T) {
t.Parallel()
err := testClient.SendMessage("", testText, testUsername)
if err == nil {
t.Error("should have failed, channel missing")
}
}
func TestSendMessageBadResponse(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(sendMessageHandlerBad))
defer server.Close()
slackAddr = server.URL
err := testClient.SendMessage(testBadChannel, testText, testUsername)
if err == nil {
t.Error("should have failed, invalid channel")
}
if err.Error() != "channel_not_found" {
t.Error("received unexpected error")
}
}
func sendMessageHandlerBad(rw http.ResponseWriter, req *http.Request) {
res := &slackPostMessageRes{Ok: false, Error: "channel_not_found"}
body, _ := json.Marshal(res)
rw.Write(body)
}