forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users_test.go
434 lines (374 loc) · 13.1 KB
/
users_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package slack
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/draw"
"image/png"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
"strconv"
"sync/atomic"
"testing"
)
func getTestUserProfile() UserProfile {
return UserProfile{
StatusText: "testStatus",
StatusEmoji: ":construction:",
RealName: "Test Real Name",
RealNameNormalized: "Test Real Name Normalized",
DisplayName: "Test Display Name",
DisplayNameNormalized: "Test Display Name Normalized",
Email: "[email protected]",
Image24: "https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2016-10-18/92962080834_ef14c1469fc0741caea1_24.jpg",
Image32: "https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2016-10-18/92962080834_ef14c1469fc0741caea1_32.jpg",
Image48: "https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2016-10-18/92962080834_ef14c1469fc0741caea1_48.jpg",
Image72: "https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2016-10-18/92962080834_ef14c1469fc0741caea1_72.jpg",
Image192: "https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2016-10-18/92962080834_ef14c1469fc0741caea1_192.jpg",
}
}
func getTestUser() User {
return User{
ID: "UXXXXXXXX",
Name: "Test User",
Deleted: false,
Color: "9f69e7",
RealName: "testuser",
TZ: "America/Los_Angeles",
TZLabel: "Pacific Daylight Time",
TZOffset: -25200,
Profile: getTestUserProfile(),
IsBot: false,
IsAdmin: false,
IsOwner: false,
IsPrimaryOwner: false,
IsRestricted: false,
IsUltraRestricted: false,
Has2FA: false,
}
}
func getUserIdentity(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response := []byte(`{
"ok": true,
"user": {
"id": "UXXXXXXXX",
"name": "Test User",
"email": "[email protected]",
"image_24": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_24.jpg",
"image_32": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_32.jpg",
"image_48": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_48.jpg",
"image_72": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_72.jpg",
"image_192": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_192.jpg",
"image_512": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_512.jpg"
},
"team": {
"id": "TXXXXXXXX",
"name": "team-name",
"domain": "team-domain",
"image_34": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_34.jpg",
"image_44": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_44.jpg",
"image_68": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_68.jpg",
"image_88": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_88.jpg",
"image_102": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_102.jpg",
"image_132": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_132.jpg",
"image_230": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_230.jpg",
"image_original": "https:\/\/s3-us-west-2.amazonaws.com\/slack-files2\/avatars\/2016-10-18\/92962080834_ef14c1469fc0741caea1_original.jpg"
}
}`)
rw.Write(response)
}
func getUserByEmail(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(struct {
Ok bool
User User
}{
Ok: true,
User: getTestUser(),
})
rw.Write(response)
}
func httpTestErrReply(w http.ResponseWriter, clientErr bool, msg string) {
if clientErr {
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
body, _ := json.Marshal(&SlackResponse{
Ok: false, Error: msg,
})
w.Write(body)
}
func newProfileHandler(up *UserProfile) (setter func(http.ResponseWriter, *http.Request)) {
return func(w http.ResponseWriter, r *http.Request) {
if up == nil {
httpTestErrReply(w, false, "err: UserProfile is nil")
return
}
if err := r.ParseForm(); err != nil {
httpTestErrReply(w, true, fmt.Sprintf("err parsing form: %s", err.Error()))
return
}
values := r.Form
if len(values["profile"]) == 0 {
httpTestErrReply(w, true, `POST data must include a "profile" field`)
return
}
profile := []byte(values["profile"][0])
userProfile := UserProfile{}
if err := json.Unmarshal(profile, &userProfile); err != nil {
httpTestErrReply(w, true, fmt.Sprintf("err parsing JSON: %s\n\njson: `%s`", err.Error(), profile))
return
}
*up = userProfile
// TODO(theckman): enhance this to return a full User object
fmt.Fprint(w, `{"ok":true}`)
}
}
func TestGetUserIdentity(t *testing.T) {
http.HandleFunc("/users.identity", getUserIdentity)
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
identity, err := api.GetUserIdentity()
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
// t.Fatal refers to -> t.Errorf & return
if identity.User.ID != "UXXXXXXXX" {
t.Fatal(ErrIncorrectResponse)
}
if identity.User.Name != "Test User" {
t.Fatal(ErrIncorrectResponse)
}
if identity.User.Email != "[email protected]" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.ID != "TXXXXXXXX" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.Name != "team-name" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.Domain != "team-domain" {
t.Fatal(ErrIncorrectResponse)
}
if identity.User.Image24 == "" {
t.Fatal(ErrIncorrectResponse)
}
if identity.Team.Image34 == "" {
t.Fatal(ErrIncorrectResponse)
}
}
func TestGetUserByEmail(t *testing.T) {
http.HandleFunc("/users.lookupByEmail", getUserByEmail)
expectedUser := getTestUser()
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
user, err := api.GetUserByEmail("[email protected]")
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if !reflect.DeepEqual(expectedUser, *user) {
t.Fatal(ErrIncorrectResponse)
}
}
func TestUserCustomStatus(t *testing.T) {
up := &UserProfile{}
setUserProfile := newProfileHandler(up)
http.HandleFunc("/users.profile.set", setUserProfile)
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
testSetUserCustomStatus(api, up, t)
testUnsetUserCustomStatus(api, up, t)
}
func testSetUserCustomStatus(api *Client, up *UserProfile, t *testing.T) {
const (
statusText = "testStatus"
statusEmoji = ":construction:"
)
if err := api.SetUserCustomStatus(statusText, statusEmoji); err != nil {
t.Fatalf(`SetUserCustomStatus(%q, %q) = %#v, want <nil>`, statusText, statusEmoji, err)
}
if up.StatusText != statusText {
t.Fatalf(`UserProfile.StatusText = %q, want %q`, up.StatusText, statusText)
}
if up.StatusEmoji != statusEmoji {
t.Fatalf(`UserProfile.StatusEmoji = %q, want %q`, up.StatusEmoji, statusEmoji)
}
}
func testUnsetUserCustomStatus(api *Client, up *UserProfile, t *testing.T) {
if err := api.UnsetUserCustomStatus(); err != nil {
t.Fatalf(`UnsetUserCustomStatus() = %#v, want <nil>`, err)
}
if up.StatusText != "" {
t.Fatalf(`UserProfile.StatusText = %q, want %q`, up.StatusText, "")
}
if up.StatusEmoji != "" {
t.Fatalf(`UserProfile.StatusEmoji = %q, want %q`, up.StatusEmoji, "")
}
}
func TestGetUsers(t *testing.T) {
http.HandleFunc("/users.list", getUserPage(4))
expectedUser := getTestUser()
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
users, err := api.GetUsers()
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if !reflect.DeepEqual([]User{expectedUser, expectedUser, expectedUser, expectedUser}, users) {
t.Fatal(ErrIncorrectResponse)
}
}
// returns n pages users.
func getUserPage(max int64) func(rw http.ResponseWriter, r *http.Request) {
var n int64
return func(rw http.ResponseWriter, r *http.Request) {
var cpage int64
sresp := SlackResponse{
Ok: true,
}
members := []User{
getTestUser(),
}
rw.Header().Set("Content-Type", "application/json")
if cpage = atomic.AddInt64(&n, 1); cpage == max {
response, _ := json.Marshal(userResponseFull{
SlackResponse: sresp,
Members: members,
})
rw.Write(response)
return
}
response, _ := json.Marshal(userResponseFull{
SlackResponse: sresp,
Members: members,
Metadata: ResponseMetadata{Cursor: strconv.Itoa(int(cpage))},
})
rw.Write(response)
return
}
}
func TestSetUserPhoto(t *testing.T) {
file, fileContent, teardown := createUserPhoto(t)
defer teardown()
params := UserSetPhotoParams{CropX: 0, CropY: 0, CropW: 32}
http.HandleFunc("/users.setPhoto", setUserPhotoHandler(fileContent, params))
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New(validToken)
err := api.SetUserPhoto(file.Name(), params)
if err != nil {
t.Fatalf("unexpected error: %+v\n", err)
}
}
func setUserPhotoHandler(wantBytes []byte, wantParams UserSetPhotoParams) http.HandlerFunc {
const maxMemory = 1 << 20 // 1 MB
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(maxMemory); err != nil {
httpTestErrReply(w, false, fmt.Sprintf("failed to parse multipart/form: %+v", err))
return
}
// Test for expected token
if v := r.Form.Get("token"); v != validToken {
httpTestErrReply(w, true, fmt.Sprintf("expected multipart form value token=%v", validToken))
return
}
// Test for expected crop params
if wantParams.CropX != DEFAULT_USER_PHOTO_CROP_X {
if cx, err := strconv.Atoi(r.Form.Get("crop_x")); err != nil || cx != wantParams.CropX {
httpTestErrReply(w, true, fmt.Sprintf("expected multipart form value crop_x=%d", wantParams.CropX))
return
}
}
if wantParams.CropY != DEFAULT_USER_PHOTO_CROP_Y {
if cy, err := strconv.Atoi(r.Form.Get("crop_y")); err != nil || cy != wantParams.CropY {
httpTestErrReply(w, true, fmt.Sprintf("expected multipart form value crop_y=%d", wantParams.CropY))
return
}
}
if wantParams.CropW != DEFAULT_USER_PHOTO_CROP_W {
if cw, err := strconv.Atoi(r.Form.Get("crop_w")); err != nil || cw != wantParams.CropW {
httpTestErrReply(w, true, fmt.Sprintf("expected multipart form value crop_w=%d", wantParams.CropW))
return
}
}
// Test for expected image
f, ok := r.MultipartForm.File["image"]
if !ok || len(f) == 0 {
httpTestErrReply(w, true, `expected multipart form file "image"`)
return
}
file, err := f[0].Open()
if err != nil {
httpTestErrReply(w, true, fmt.Sprintf("failed to open uploaded file: %+v", err))
return
}
gotBytes, err := ioutil.ReadAll(file)
if err != nil {
httpTestErrReply(w, true, fmt.Sprintf("failed to read uploaded file: %+v", err))
return
}
if !bytes.Equal(wantBytes, gotBytes) {
httpTestErrReply(w, true, "uploaded bytes did not match expected bytes")
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"ok":true}`)
}
}
// createUserPhoto generates a temp photo for testing. It returns the file handle, the file
// contents, and a function that can be called to remove the file.
func createUserPhoto(t *testing.T) (*os.File, []byte, func()) {
photo := image.NewRGBA(image.Rect(0, 0, 64, 64))
draw.Draw(photo, photo.Bounds(), image.Black, image.ZP, draw.Src)
f, err := ioutil.TempFile(os.TempDir(), "profile.png")
if err != nil {
t.Fatalf("failed to create test photo: %+v\n", err)
}
var buf bytes.Buffer
if err := png.Encode(io.MultiWriter(&buf, f), photo); err != nil {
t.Fatalf("failed to write test photo: %+v\n", err)
}
teardown := func() {
if err := os.Remove(f.Name()); err != nil {
t.Fatalf("failed to remove test photo: %+v\n", err)
}
}
return f, buf.Bytes(), teardown
}
func getUserProfileHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
profile := getTestUserProfile()
resp, _ := json.Marshal(&getUserProfileResponse{
SlackResponse: SlackResponse{Ok: true},
Profile: &profile})
rw.Write(resp)
}
func TestGetUserProfile(t *testing.T) {
http.HandleFunc("/users.profile.get", getUserProfileHandler)
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
profile, err := api.GetUserProfile("UXXXXXXXX", false)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
exp := getTestUserProfile()
if profile.DisplayName != exp.DisplayName {
t.Fatalf(`profile.DisplayName = "%s", wanted "%s"`, profile.DisplayName, exp.DisplayName)
}
}