forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_test.go
96 lines (73 loc) · 2.13 KB
/
discord_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
package discord
import (
"context"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
func TestDiscord_New(t *testing.T) {
t.Parallel()
assert := require.New(t)
assert.NotNil(New())
}
func TestDiscord_AddReceivers(t *testing.T) {
t.Parallel()
assert := require.New(t)
service := New()
assert.NotNil(service)
channels := []string{"1", "2", "3", "4", "5"}
service.AddReceivers(channels...)
assert.Equal(service.channelIDs, channels)
}
func TestDiscord_Authenticate(t *testing.T) {
t.Parallel()
assert := require.New(t)
service := New()
assert.NotNil(service)
// Note: The following might look confusing, because the validation mechanism is not mocked and never returns an
// error. The function name may be misleading because it is not actually testing the authentication mechanism. The
// actual authentication only happens when the service is sends a message.
// OAuth2
err := service.AuthenticateWithOAuth2Token("12345")
assert.Nil(err)
err = service.AuthenticateWithOAuth2Token("")
assert.Nil(err)
// Bot token
err = service.AuthenticateWithBotToken("12345")
assert.Nil(err)
err = service.AuthenticateWithBotToken("")
assert.Nil(err)
}
func TestDiscord_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 := newMockDiscordSession(t)
mockClient.
On("ChannelMessageSend", "1234", "subject\nmessage").
Return(nil, 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 = newMockDiscordSession(t)
mockClient.
On("ChannelMessageSend", "1234", "subject\nmessage").
Return(nil, nil)
mockClient.
On("ChannelMessageSend", "5678", "subject\nmessage").
Return(nil, nil)
service.client = mockClient
service.AddReceivers("5678")
err = service.Send(ctx, "subject", "message")
assert.Nil(err)
mockClient.AssertExpectations(t)
}