-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathslack_test.go
More file actions
145 lines (126 loc) · 4.19 KB
/
slack_test.go
File metadata and controls
145 lines (126 loc) · 4.19 KB
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
package notify
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSlack_Send(t *testing.T) {
ts := newMockSlackServer()
defer ts.Close()
tb := ts.newClient()
assert.NotNil(t, tb)
assert.Equal(t, "slack notifications destination", tb.String())
assert.Equal(t, "slack", tb.Schema())
err := tb.Send(context.Background(), "slack:general?title=title&attachmentText=test%20text&titleLink=https://example.org", "test text")
require.NoError(t, err)
ts.isServerDown = true
err = tb.Send(context.Background(), "slack:general?title=title&attachmentText=test%20text&titleLink=https://example.org", "test text")
assert.Contains(t, err.Error(), "slack server error", "send on broken client")
}
func TestSlackSendClientError(t *testing.T) {
ts := newMockSlackServer()
defer ts.Close()
slck := ts.newClient()
assert.NotNil(t, slck)
assert.Equal(t, "slack notifications destination", slck.String())
// no destination set
require.EqualError(t, slck.Send(context.Background(), "", ""),
"problem parsing destination: unsupported scheme , should be slack")
// wrong scheme
require.EqualError(t, slck.Send(context.Background(), "https://example.org", ""),
"problem parsing destination: unsupported scheme https, should be slack")
// bad destination set
require.EqualError(t, slck.Send(context.Background(), "%", ""),
`problem parsing destination: parse "%": invalid URL escape "%"`)
// can't retrieve channel ID
ts.listingIsBroken = true
require.EqualError(t, slck.Send(context.Background(), "slack:general", ""),
"problem parsing destination: problem retrieving channel ID for #general:"+
" slack server error: 500 Internal Server Error")
ts.listingIsBroken = false
// non-existing channel
require.EqualError(t, slck.Send(context.Background(), "slack:non-existent", ""),
"problem parsing destination: problem retrieving channel ID for #non-existent: no such channel")
// canceled context
ctx, cancel := context.WithCancel(context.Background())
cancel()
require.EqualError(t, slck.Send(ctx, "slack:general?title=test", ""), "context canceled")
}
type mockSlackServer struct {
*httptest.Server
isServerDown bool
listingIsBroken bool
}
func (ts *mockSlackServer) newClient() *Slack {
return NewSlack("any-token", slack.OptionAPIURL(ts.URL+"/"))
}
func newMockSlackServer() *mockSlackServer {
mockServer := mockSlackServer{}
mux := http.NewServeMux()
mux.HandleFunc("POST /conversations.list", func(w http.ResponseWriter, _ *http.Request) {
if mockServer.listingIsBroken {
w.WriteHeader(500)
} else {
s := `{
"ok": true,
"channels": [
{
"id": "C12345678",
"name": "general",
"is_channel": true,
"is_group": false,
"is_im": false,
"created": 1503888888,
"is_archived": false,
"is_general": false,
"unlinked": 0,
"name_normalized": "random",
"is_shared": false,
"parent_conversation": null,
"creator": "U12345678",
"is_ext_shared": false,
"is_org_shared": false,
"pending_shared": [],
"pending_connected_team_ids": [],
"is_pending_ext_shared": false,
"is_member": false,
"is_private": false,
"is_mpim": false,
"previous_names": [],
"num_members": 1
}
],
"response_metadata": {
"next_cursor": ""
}
}`
_, _ = w.Write([]byte(s))
}
})
mux.HandleFunc("POST /chat.postMessage", func(w http.ResponseWriter, _ *http.Request) {
if mockServer.isServerDown {
w.WriteHeader(500)
} else {
s := `{
"ok": true,
"channel": "C12345678",
"ts": "1617008342.000100",
"message": {
"type": "message",
"subtype": "bot_message",
"text": "wowo",
"ts": "1617008342.000100",
"username": "slackbot",
"bot_id": "B12345678"
}
}`
_, _ = w.Write([]byte(s))
}
})
mockServer.Server = httptest.NewServer(mux)
return &mockServer
}