forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_test.go
68 lines (55 loc) · 2.17 KB
/
chat_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
package slack
import (
"encoding/json"
"net/http"
"testing"
)
func postMessageInvalidChannelHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(chatResponseFull{
SlackResponse: SlackResponse{Ok: false, Error: "channel_not_found"},
})
rw.Write(response)
}
func TestPostMessageInvalidChannel(t *testing.T) {
http.HandleFunc("/chat.postMessage", postMessageInvalidChannelHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
_, _, err := api.PostMessage("CXXXXXXXX", MsgOptionText("hello", false))
if err == nil {
t.Errorf("Expected error: channel_not_found; instead succeeded")
return
}
if err.Error() != "channel_not_found" {
t.Errorf("Expected error: channel_not_found; received: %s", err)
return
}
}
func TestGetPermalink(t *testing.T) {
channel := "C1H9RESGA"
timeStamp := "p135854651500008"
http.HandleFunc("/chat.getPermalink", func(rw http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; got != want {
t.Errorf("request uses unexpected content type: got %s, want %s", got, want)
}
if got, want := r.URL.Query().Get("channel"), channel; got != want {
t.Errorf("request contains unexpected channel: got %s, want %s", got, want)
}
if got, want := r.URL.Query().Get("message_ts"), timeStamp; got != want {
t.Errorf("request contains unexpected message timestamp: got %s, want %s", got, want)
}
rw.Header().Set("Content-Type", "application/json")
response := []byte("{\"ok\": true, \"channel\": \"" + channel + "\", \"permalink\": \"https://ghostbusters.slack.com/archives/" + channel + "/" + timeStamp + "\"}")
rw.Write(response)
})
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
pp := PermalinkParameters{Channel: channel, Ts: timeStamp}
pl, err := api.GetPermalink(&pp)
if got, want := pl, "https://ghostbusters.slack.com/archives/C1H9RESGA/p135854651500008"; got != want {
t.Errorf("unexpected permalink: got %s, want %s", got, want)
}
if err != nil {
t.Errorf("unexpected error returned: %v", err)
}
}