forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglechat_test.go
100 lines (77 loc) · 2.38 KB
/
googlechat_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package googlechat
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"google.golang.org/api/chat/v1"
"google.golang.org/api/option"
)
func TestGoogleChat_New(t *testing.T) {
t.Parallel()
withCred := option.WithCredentialsFile("example_credentials.json")
assert := require.New(t)
service, err := New(withCred)
assert.Nil(err)
assert.NotNil(service)
}
func TestGoogleChat_NewWithContext(t *testing.T) {
t.Parallel()
withCred := option.WithCredentialsFile("example_credentials.json")
assert := require.New(t)
ctx := context.Background()
service, err := NewWithContext(ctx, withCred)
assert.Nil(err)
assert.NotNil(service)
}
func TestGoogleChat_AddReceivers(t *testing.T) {
t.Parallel()
assert := require.New(t)
service := &Service{}
service.AddReceivers("space_a")
assert.Len(service.spaces, 1)
service.AddReceivers("space_b", "space_c")
assert.Len(service.spaces, 3)
service.spaces = []string{}
receivers := []string{"space_a", "space_b"}
service.AddReceivers(receivers...)
diff := cmp.Diff(service.spaces, receivers)
assert.Equal("", diff) // assert that there is no difference
}
func TestGoogleChat_Send(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert := require.New(t)
service := &Service{}
// No receivers added
err := service.Send(ctx, "subject", "message")
assert.Nil(err)
mockMsgCreator := newMockSpacesMessageCreator(t)
service.messageCreator = mockMsgCreator
service.AddReceivers("space_a")
// Test error response
failedCall := newMockCreateCall(t)
failedCall.On("Do").Return(nil, errors.New("something happened"))
mockMsgCreator.
On("Create", "spaces/space_a", &chat.Message{Text: "subject\nfailure"}).
Return(failedCall)
err = service.Send(ctx, "subject", "failure")
assert.NotNil(err)
mockMsgCreator.AssertExpectations(t)
// Test success response
successCall := newMockCreateCall(t)
successCall.On("Do").Return(&chat.Message{Text: "subject\nsuccess"}, nil)
mockMsgCreator.
On("Create", "spaces/space_a", &chat.Message{Text: "subject\nsuccess"}).
Return(successCall)
err = service.Send(ctx, "subject", "success")
assert.Nil(err)
mockMsgCreator.AssertExpectations(t)
// Test context cancellation
ctx, cancel := context.WithCancel(ctx)
cancel()
err = service.Send(ctx, "subject", "success")
assert.NotNil(err)
mockMsgCreator.AssertExpectations(t)
}