-
Notifications
You must be signed in to change notification settings - Fork 812
/
restapi_test.go
277 lines (220 loc) · 5.8 KB
/
restapi_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
269
270
271
272
273
274
275
276
277
package discordgo
import (
"context"
"errors"
"net/http"
"testing"
)
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////// START OF TESTS
// TestChannelMessageSend tests the ChannelMessageSend() function. This should not return an error.
func TestChannelMessageSend(t *testing.T) {
if envChannel == "" {
t.Skip("Skipping, DG_CHANNEL not set.")
}
if dg == nil {
t.Skip("Skipping, dg not set.")
}
_, err := dg.ChannelMessageSend(envChannel, "Running REST API Tests!")
if err != nil {
t.Errorf("ChannelMessageSend returned error: %+v", err)
}
}
/*
// removed for now, only works on BOT accounts now
func TestUserAvatar(t *testing.T) {
if dg == nil {
t.Skip("Cannot TestUserAvatar, dg not set.")
}
u, err := dg.User("@me")
if err != nil {
t.Error("error fetching @me user,", err)
}
a, err := dg.UserAvatar(u.ID)
if err != nil {
if err.Error() == `HTTP 404 NOT FOUND, {"code": 0, "message": "404: Not Found"}` {
t.Skip("Skipped, @me doesn't have an Avatar")
}
t.Errorf(err.Error())
}
if a == nil {
t.Errorf("a == nil, should be image.Image")
}
}
*/
/* Running this causes an error due to 2/hour rate limit on username changes
func TestUserUpdate(t *testing.T) {
if dg == nil {
t.Skip("Cannot test logout, dg not set.")
}
u, err := dg.User("@me")
if err != nil {
t.Errorf(err.Error())
}
s, err := dg.UserUpdate(envEmail, envPassword, "testname", u.Avatar, "")
if err != nil {
t.Error(err.Error())
}
if s.Username != "testname" {
t.Error("Username != testname")
}
s, err = dg.UserUpdate(envEmail, envPassword, u.Username, u.Avatar, "")
if err != nil {
t.Error(err.Error())
}
if s.Username != u.Username {
t.Error("Username != " + u.Username)
}
}
*/
//func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
func TestUserChannelCreate(t *testing.T) {
if dg == nil {
t.Skip("Cannot TestUserChannelCreate, dg not set.")
}
if envAdmin == "" {
t.Skip("Skipped, DG_ADMIN not set.")
}
_, err := dg.UserChannelCreate(envAdmin)
if err != nil {
t.Errorf(err.Error())
}
// TODO make sure the channel was added
}
func TestUserGuilds(t *testing.T) {
if dg == nil {
t.Skip("Cannot TestUserGuilds, dg not set.")
}
_, err := dg.UserGuilds(10, "", "", false)
if err != nil {
t.Errorf(err.Error())
}
}
func TestGateway(t *testing.T) {
if dg == nil {
t.Skip("Skipping, dg not set.")
}
_, err := dg.Gateway()
if err != nil {
t.Errorf("Gateway() returned error: %+v", err)
}
}
func TestGatewayBot(t *testing.T) {
if dgBot == nil {
t.Skip("Skipping, dgBot not set.")
}
_, err := dgBot.GatewayBot()
if err != nil {
t.Errorf("GatewayBot() returned error: %+v", err)
}
}
func TestVoiceRegions(t *testing.T) {
if dg == nil {
t.Skip("Skipping, dg not set.")
}
_, err := dg.VoiceRegions()
if err != nil {
t.Errorf("VoiceRegions() returned error: %+v", err)
}
}
func TestGuildRoles(t *testing.T) {
if envGuild == "" {
t.Skip("Skipping, DG_GUILD not set.")
}
if dg == nil {
t.Skip("Skipping, dg not set.")
}
_, err := dg.GuildRoles(envGuild)
if err != nil {
t.Errorf("GuildRoles(envGuild) returned error: %+v", err)
}
}
func TestGuildMemberNickname(t *testing.T) {
if envGuild == "" {
t.Skip("Skipping, DG_GUILD not set.")
}
if dg == nil {
t.Skip("Skipping, dg not set.")
}
err := dg.GuildMemberNickname(envGuild, "@me/nick", "B1nzyRocks")
if err != nil {
t.Errorf("GuildNickname returned error: %+v", err)
}
}
// TestChannelMessageSend2 tests the ChannelMessageSend() function. This should not return an error.
func TestChannelMessageSend2(t *testing.T) {
if envChannel == "" {
t.Skip("Skipping, DG_CHANNEL not set.")
}
if dg == nil {
t.Skip("Skipping, dg not set.")
}
_, err := dg.ChannelMessageSend(envChannel, "All done running REST API Tests!")
if err != nil {
t.Errorf("ChannelMessageSend returned error: %+v", err)
}
}
// TestGuildPruneCount tests GuildPruneCount() function. This should not return an error.
func TestGuildPruneCount(t *testing.T) {
if envGuild == "" {
t.Skip("Skipping, DG_GUILD not set.")
}
if dg == nil {
t.Skip("Skipping, dg not set.")
}
_, err := dg.GuildPruneCount(envGuild, 1)
if err != nil {
t.Errorf("GuildPruneCount returned error: %+v", err)
}
}
/*
// TestGuildPrune tests GuildPrune() function. This should not return an error.
func TestGuildPrune(t *testing.T) {
if envGuild == "" {
t.Skip("Skipping, DG_GUILD not set.")
}
if dg == nil {
t.Skip("Skipping, dg not set.")
}
_, err := dg.GuildPrune(envGuild, 1)
if err != nil {
t.Errorf("GuildPrune returned error: %+v", err)
}
}
*/
func Test_unmarshal(t *testing.T) {
err := unmarshal([]byte{}, &struct{}{})
if !errors.Is(err, ErrJSONUnmarshal) {
t.Errorf("Unexpected error type: %T", err)
}
}
func TestWithContext(t *testing.T) {
// Set up a test context.
type key struct{}
ctx := context.WithValue(context.Background(), key{}, "value")
// Set up a test client.
session, err := New("")
if err != nil {
t.Fatal(err)
}
testErr := errors.New("test")
// Intercept the request to assert the context.
session.Client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
val, _ := r.Context().Value(key{}).(string)
if val != "value" {
t.Errorf("missing value in context (got %q, wanted %q)", val, "value")
}
return nil, testErr
})
// Run any client method using WithContext.
_, err = session.User("", WithContext(ctx))
// Verify that the assertion code was actually run.
if !errors.Is(err, testErr) {
t.Errorf("unexpected error %v returned from client", err)
}
}
// roundTripperFunc implements http.RoundTripper.
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}