forked from bwmarrin/discordgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_test.go
268 lines (211 loc) · 6.78 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package discordgo
import (
"os"
"runtime"
"sync/atomic"
"testing"
"time"
)
//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////// VARS NEEDED FOR TESTING
var (
dg *Session // Stores global discordgo session
envToken = os.Getenv("DG_TOKEN") // Token to use when authenticating
envEmail = os.Getenv("DG_EMAIL") // Email to use when authenticating
envPassword = os.Getenv("DG_PASSWORD") // Password to use when authenticating
envGuild = os.Getenv("DG_GUILD") // Guild ID to use for tests
envChannel = os.Getenv("DG_CHANNEL") // Channel ID to use for tests
// envUser = os.Getenv("DG_USER") // User ID to use for tests
envAdmin = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests
)
func init() {
if envEmail == "" || envPassword == "" || envToken == "" {
return
}
if d, err := New(envEmail, envPassword, envToken); err == nil {
dg = d
}
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////// START OF TESTS
// TestNew tests the New() function without any arguments. This should return
// a valid Session{} struct and no errors.
func TestNew(t *testing.T) {
_, err := New()
if err != nil {
t.Errorf("New() returned error: %+v", err)
}
}
// TestInvalidToken tests the New() function with an invalid token
func TestInvalidToken(t *testing.T) {
d, err := New("asjkldhflkjasdh")
if err != nil {
t.Fatalf("New(InvalidToken) returned error: %+v", err)
}
// New with just a token does not do any communication, so attempt an api call.
_, err = d.UserSettings()
if err == nil {
t.Errorf("New(InvalidToken), d.UserSettings returned nil error.")
}
}
// TestInvalidUserPass tests the New() function with an invalid Email and Pass
func TestInvalidEmailPass(t *testing.T) {
_, err := New("invalidemail", "invalidpassword")
if err == nil {
t.Errorf("New(InvalidEmail, InvalidPass) returned nil error.")
}
}
// TestInvalidPass tests the New() function with an invalid Password
func TestInvalidPass(t *testing.T) {
if envEmail == "" {
t.Skip("Skipping New(username,InvalidPass), DG_EMAIL not set")
return
}
_, err := New(envEmail, "invalidpassword")
if err == nil {
t.Errorf("New(Email, InvalidPass) returned nil error.")
}
}
// TestNewUserPass tests the New() function with a username and password.
// This should return a valid Session{}, a valid Session.Token.
func TestNewUserPass(t *testing.T) {
if envEmail == "" || envPassword == "" {
t.Skip("Skipping New(username,password), DG_EMAIL or DG_PASSWORD not set")
return
}
d, err := New(envEmail, envPassword)
if err != nil {
t.Fatalf("New(user,pass) returned error: %+v", err)
}
if d == nil {
t.Fatal("New(user,pass), d is nil, should be Session{}")
}
if d.Token == "" {
t.Fatal("New(user,pass), d.Token is empty, should be a valid Token.")
}
}
// TestNewToken tests the New() function with a Token. This should return
// the same as the TestNewUserPass function.
func TestNewToken(t *testing.T) {
if envToken == "" {
t.Skip("Skipping New(token), DG_TOKEN not set")
}
d, err := New(envToken)
if err != nil {
t.Fatalf("New(envToken) returned error: %+v", err)
}
if d == nil {
t.Fatal("New(envToken), d is nil, should be Session{}")
}
if d.Token == "" {
t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
}
}
// TestNewUserPassToken tests the New() function with a username, password and token.
// This should return the same as the TestNewUserPass function.
func TestNewUserPassToken(t *testing.T) {
if envEmail == "" || envPassword == "" || envToken == "" {
t.Skip("Skipping New(username,password,token), DG_EMAIL, DG_PASSWORD or DG_TOKEN not set")
return
}
d, err := New(envEmail, envPassword, envToken)
if err != nil {
t.Fatalf("New(user,pass,token) returned error: %+v", err)
}
if d == nil {
t.Fatal("New(user,pass,token), d is nil, should be Session{}")
}
if d.Token == "" {
t.Fatal("New(user,pass,token), d.Token is empty, should be a valid Token.")
}
}
func TestOpenClose(t *testing.T) {
if envToken == "" {
t.Skip("Skipping TestClose, DG_TOKEN not set")
}
d, err := New(envToken)
if err != nil {
t.Fatalf("TestClose, New(envToken) returned error: %+v", err)
}
if err = d.Open(); err != nil {
t.Fatalf("TestClose, d.Open failed: %+v", err)
}
// We need a better way to know the session is ready for use,
// this is totally gross.
start := time.Now()
for {
d.RLock()
if d.DataReady {
d.RUnlock()
break
}
d.RUnlock()
if time.Since(start) > 10*time.Second {
t.Fatal("DataReady never became true.yy")
}
runtime.Gosched()
}
// TODO find a better way
// Add a small sleep here to make sure heartbeat and other events
// have enough time to get fired. Need a way to actually check
// those events.
time.Sleep(2 * time.Second)
// UpdateStatus - maybe we move this into wsapi_test.go but the websocket
// created here is needed. This helps tests that the websocket was setup
// and it is working.
if err = d.UpdateStatus(0, time.Now().String()); err != nil {
t.Errorf("UpdateStatus error: %+v", err)
}
if err = d.Close(); err != nil {
t.Fatalf("TestClose, d.Close failed: %+v", err)
}
}
func TestAddHandler(t *testing.T) {
testHandlerCalled := int32(0)
testHandler := func(s *Session, m *MessageCreate) {
atomic.AddInt32(&testHandlerCalled, 1)
}
interfaceHandlerCalled := int32(0)
interfaceHandler := func(s *Session, i interface{}) {
atomic.AddInt32(&interfaceHandlerCalled, 1)
}
bogusHandlerCalled := int32(0)
bogusHandler := func(s *Session, se *Session) {
atomic.AddInt32(&bogusHandlerCalled, 1)
}
d := Session{}
d.AddHandler(testHandler)
d.AddHandler(testHandler)
d.AddHandler(interfaceHandler)
d.AddHandler(bogusHandler)
d.handle(&MessageCreate{})
d.handle(&MessageDelete{})
<-time.After(500 * time.Millisecond)
// testHandler will be called twice because it was added twice.
if atomic.LoadInt32(&testHandlerCalled) != 2 {
t.Fatalf("testHandler was not called twice.")
}
// interfaceHandler will be called twice, once for each event.
if atomic.LoadInt32(&interfaceHandlerCalled) != 2 {
t.Fatalf("interfaceHandler was not called twice.")
}
if atomic.LoadInt32(&bogusHandlerCalled) != 0 {
t.Fatalf("bogusHandler was called.")
}
}
func TestRemoveHandler(t *testing.T) {
testHandlerCalled := int32(0)
testHandler := func(s *Session, m *MessageCreate) {
atomic.AddInt32(&testHandlerCalled, 1)
}
d := Session{}
r := d.AddHandler(testHandler)
d.handle(&MessageCreate{})
r()
d.handle(&MessageCreate{})
<-time.After(500 * time.Millisecond)
// testHandler will be called once, as it was removed in between calls.
if atomic.LoadInt32(&testHandlerCalled) != 1 {
t.Fatalf("testHandler was not called once.")
}
}