forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_test.go
84 lines (63 loc) · 1.74 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package slack
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestSlack_New(t *testing.T) {
t.Parallel()
assert := require.New(t)
service := New("")
assert.NotNil(service)
}
func TestSlack_AddReceivers(t *testing.T) {
t.Parallel()
assert := require.New(t)
service := New("")
assert.NotNil(service)
service.AddReceivers("")
assert.Len(service.channelIDs, 1)
service.AddReceivers("", "")
assert.Len(service.channelIDs, 3)
service.channelIDs = []string{}
receivers := []string{"", ""}
service.AddReceivers(receivers...)
diff := cmp.Diff(service.channelIDs, receivers)
assert.Equal("", diff)
}
func TestSlack_Send(t *testing.T) {
t.Parallel()
assert := require.New(t)
service := New("")
assert.NotNil(service)
// No receivers added
ctx := context.Background()
err := service.Send(ctx, "subject", "message")
assert.Nil(err)
// Test error response
mockClient := newMockSlackClient(t)
mockClient.
On("PostMessageContext", ctx, "1234", mock.AnythingOfType("MsgOption")).
Return("", "", errors.New("some error"))
service.client = mockClient
service.AddReceivers("1234")
err = service.Send(ctx, "subject", "message")
assert.NotNil(err)
mockClient.AssertExpectations(t)
// Test success response
mockClient = newMockSlackClient(t)
mockClient.
On("PostMessageContext", ctx, "1234", mock.AnythingOfType("MsgOption")).
Return("", "", nil)
mockClient.
On("PostMessageContext", ctx, "5678", mock.AnythingOfType("MsgOption")).
Return("", "", nil)
service.client = mockClient
service.AddReceivers("5678")
err = service.Send(ctx, "subject", "message")
assert.Nil(err)
mockClient.AssertExpectations(t)
}