-
Notifications
You must be signed in to change notification settings - Fork 31
/
channel_test.go
688 lines (567 loc) · 18.7 KB
/
channel_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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package stream_chat
import (
"context"
"log"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func (ch *Channel) refresh(ctx context.Context) error {
_, err := ch.RefreshState(ctx)
return err
}
func TestClient_TestQuery(t *testing.T) {
c := initClient(t)
membersID := randomUsersID(t, c, 3)
ch := initChannel(t, c, membersID...)
ctx := context.Background()
msg, err := ch.SendMessage(ctx, &Message{Text: "test message", Pinned: true}, ch.CreatedBy.ID)
require.NoError(t, err)
q := &QueryRequest{
State: true,
Messages: &MessagePaginationParamsRequest{PaginationParamsRequest: PaginationParamsRequest{Limit: 1, IDLT: msg.Message.ID}},
Members: &PaginationParamsRequest{Limit: 3, Offset: 0},
Watchers: &PaginationParamsRequest{Limit: 3, Offset: 0},
}
resp, err := ch.Query(ctx, q)
require.NoError(t, err)
require.Len(t, resp.Members, 3)
for _, read := range resp.Read {
if ch.CreatedBy.ID == read.User.ID {
require.Equal(t, 0, read.UnreadMessages)
continue
}
require.Equal(t, 1, read.UnreadMessages)
}
}
func TestClient_CreateChannel(t *testing.T) {
c := initClient(t)
userID := randomUser(t, c).ID
ctx := context.Background()
t.Run("get existing channel", func(t *testing.T) {
membersID := randomUsersID(t, c, 3)
ch := initChannel(t, c, membersID...)
resp, err := c.CreateChannel(ctx, ch.Type, ch.ID, userID, nil)
require.NoError(t, err, "create channel", ch)
channel := resp.Channel
assert.Equal(t, c, channel.client, "client link")
assert.Equal(t, ch.Type, channel.Type, "channel type")
assert.Equal(t, ch.ID, channel.ID, "channel id")
assert.Equal(t, ch.MemberCount, channel.MemberCount, "member count")
assert.Len(t, channel.Members, ch.MemberCount, "members length")
})
tests := []struct {
name string
channelType string
id string
userID string
data *ChannelRequest
options []CreateChannelOptionFunc
wantErr bool
}{
{"create channel with ID", "messaging", randomString(12), userID, nil, nil, false},
{"create channel without ID and members", "messaging", "", userID, nil, nil, true},
{
"create channel without ID but with members", "messaging", "", userID,
&ChannelRequest{Members: randomUsersID(t, c, 2)},
nil, false,
},
{
"create channel with HideForCreator", "messaging", "", userID,
&ChannelRequest{Members: []string{userID, randomUsersID(t, c, 1)[0]}},
[]CreateChannelOptionFunc{HideForCreator(true)},
false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
resp, err := c.CreateChannel(ctx, tt.channelType, tt.id, tt.userID, tt.data, tt.options...)
if tt.wantErr {
require.Error(t, err, "create channel", tt)
return
}
require.NoError(t, err, "create channel", tt)
channel := resp.Channel
assert.Equal(t, tt.channelType, channel.Type, "channel type")
assert.NotEmpty(t, channel.ID)
if tt.id != "" {
assert.Equal(t, tt.id, channel.ID, "channel id")
}
assert.Equal(t, tt.userID, channel.CreatedBy.ID, "channel created by")
})
}
}
func TestChannel_GetManyMessages(t *testing.T) {
ctx := context.Background()
c := initClient(t)
userA := randomUser(t, c)
userB := randomUser(t, c)
ch := initChannel(t, c, userA.ID, userB.ID)
msg := &Message{Text: "test message"}
messageResp, err := ch.SendMessage(ctx, msg, userB.ID)
require.NoError(t, err)
getMsgResp, err := ch.GetMessages(ctx, []string{messageResp.Message.ID})
require.NoError(t, err)
require.Len(t, getMsgResp.Messages, 1)
require.Equal(t, messageResp.Message.ID, getMsgResp.Messages[0].ID)
}
func TestChannel_AddMembers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
chanID := randomString(12)
resp, err := c.CreateChannel(ctx, "messaging", chanID, randomUser(t, c).ID, nil)
require.NoError(t, err, "create channel")
ch := resp.Channel
assert.Empty(t, ch.Members, "members are empty")
user := randomUser(t, c)
msg := &Message{Text: "some members", User: &User{ID: user.ID}}
_, err = ch.AddMembers(ctx,
[]string{user.ID},
AddMembersWithMessage(msg),
AddMembersWithHideHistory(),
)
require.NoError(t, err, "add members")
// refresh channel state
require.NoError(t, ch.refresh(ctx), "refresh channel")
assert.Equal(t, user.ID, ch.Members[0].User.ID, "members contain user id")
}
func TestChannel_AssignRoles(t *testing.T) {
c := initClient(t)
ctx := context.Background()
owner := randomUser(t, c)
other := randomUser(t, c)
chanID := randomString(12)
resp, err := c.CreateChannel(ctx, "messaging", chanID, owner.ID, nil)
require.NoError(t, err, "create channel")
ch := resp.Channel
a := []*RoleAssignment{{ChannelRole: "channel_moderator", UserID: other.ID}}
_, err = ch.AssignRole(ctx, a, nil)
require.NoError(t, err)
}
func TestChannel_QueryMembers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
chanID := randomString(12)
resp1, err := c.CreateChannel(ctx, "messaging", chanID, randomUser(t, c).ID, nil)
require.NoError(t, err, "create channel")
ch := resp1.Channel
assert.Empty(t, ch.Members, "members are empty")
prefix := randomString(12)
names := []string{"paul", "george", "john", "jessica", "john2"}
for _, name := range names {
id := prefix + name
_, err := c.UpsertUser(ctx, &User{ID: id, Name: id})
require.NoError(t, err)
_, err = ch.AddMembers(ctx, []string{id})
require.NoError(t, err)
}
resp2, err := ch.QueryMembers(ctx, &QueryOption{
Filter: map[string]interface{}{
"name": map[string]interface{}{"$autocomplete": prefix + "j"},
},
Offset: 1,
Limit: 10,
}, &SortOption{Field: "created_at", Direction: 1})
members := resp2.Members
require.NoError(t, err)
require.Len(t, members, 2)
require.Equal(t, prefix+"jessica", members[0].User.ID)
require.Equal(t, prefix+"john2", members[1].User.ID)
}
// See https://getstream.io/chat/docs/channel_members/ for more details.
func ExampleChannel_AddModerators() {
channel := &Channel{}
newModerators := []string{"bob", "sue"}
ctx := context.Background()
_, _ = channel.AddModerators(ctx, "thierry", "josh")
_, _ = channel.AddModerators(ctx, newModerators...)
_, _ = channel.DemoteModerators(ctx, newModerators...)
}
func TestChannel_InviteMembers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
chanID := randomString(12)
resp, err := c.CreateChannel(ctx, "messaging", chanID, randomUser(t, c).ID, nil)
require.NoError(t, err, "create channel")
ch := resp.Channel
assert.Empty(t, ch.Members, "members are empty")
user := randomUser(t, c)
_, err = ch.InviteMembers(ctx, user.ID)
require.NoError(t, err, "invite members")
// refresh channel state
require.NoError(t, ch.refresh(ctx), "refresh channel")
assert.Equal(t, user.ID, ch.Members[0].User.ID, "members contain user id")
assert.True(t, ch.Members[0].Invited, "member is invited")
assert.Nil(t, ch.Members[0].InviteAcceptedAt, "invite is not accepted")
assert.Nil(t, ch.Members[0].InviteRejectedAt, "invite is not rejected")
}
func TestChannel_Moderation(t *testing.T) {
c := initClient(t)
ctx := context.Background()
// init random channel
chanID := randomString(12)
resp, err := c.CreateChannel(ctx, "messaging", chanID, randomUser(t, c).ID, nil)
require.NoError(t, err, "create channel")
ch := resp.Channel
assert.Empty(t, ch.Members, "members are empty")
user := randomUser(t, c)
_, err = ch.AddModeratorsWithMessage(ctx,
[]string{user.ID},
&Message{Text: "accepted", User: &User{ID: user.ID}},
)
require.NoError(t, err, "add moderators")
// refresh channel state
require.NoError(t, ch.refresh(ctx), "refresh channel")
assert.Equal(t, user.ID, ch.Members[0].User.ID, "user exists")
assert.Equal(t, "moderator", ch.Members[0].Role, "user role is moderator")
_, err = ch.DemoteModerators(ctx, user.ID)
require.NoError(t, err, "demote moderators")
// refresh channel state
require.NoError(t, ch.refresh(ctx), "refresh channel")
assert.Equal(t, user.ID, ch.Members[0].User.ID, "user exists")
assert.Equal(t, "member", ch.Members[0].Role, "user role is member")
}
func TestChannel_Delete(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
_, err := ch.Delete(ctx)
require.NoError(t, err, "delete channel")
}
func TestChannel_GetReplies(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
msg := &Message{Text: "test message"}
resp, err := ch.SendMessage(ctx, msg, randomUser(t, c).ID, MessageSkipPush)
require.NoError(t, err, "send message")
msg = resp.Message
reply := &Message{Text: "test reply", ParentID: msg.ID}
resp, err = ch.SendMessage(ctx, reply, randomUser(t, c).ID)
require.NoError(t, err, "send reply")
require.Equal(t, MessageTypeReply, resp.Message.Type, "message type is reply")
repliesResp, err := ch.GetReplies(ctx, msg.ID, nil)
require.NoError(t, err, "get replies")
assert.Len(t, repliesResp.Messages, 1)
}
func TestChannel_RemoveMembers(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
user := randomUser(t, c)
_, err := ch.RemoveMembers(ctx,
[]string{user.ID},
&Message{Text: "some members", User: &User{ID: user.ID}},
)
require.NoError(t, err, "remove members")
for _, member := range ch.Members {
assert.NotEqual(t, member.User.ID, user.ID, "member is not present")
}
}
func TestChannel_SendEvent(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
u := randomUser(t, c)
ctx := context.Background()
t.Cleanup(func() {
_, _ = ch.Delete(ctx)
_, _ = c.DeleteUser(ctx, u.ID)
})
_, err := ch.SendEvent(ctx, &Event{
Type: "typing.start",
}, u.ID)
require.NoError(t, err)
}
func TestChannel_SendMessage(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
user1 := randomUser(t, c)
user2 := randomUser(t, c)
msg := &Message{
Text: "test message",
User: user1,
}
resp, err := ch.SendMessage(ctx, msg, user2.ID)
require.NoError(t, err, "send message")
// check that message was updated
msg = resp.Message
assert.NotEmpty(t, msg.ID, "message has ID")
assert.NotEmpty(t, msg.HTML, "message has HTML body")
msg2 := &Message{
Text: "text message 2",
User: user1,
Silent: true,
}
resp, err = ch.SendMessage(ctx, msg2, user2.ID)
require.NoError(t, err, "send message 2")
// check that message was updated
msg2 = resp.Message
assert.NotEmpty(t, msg2.ID, "message has ID")
assert.NotEmpty(t, msg2.HTML, "message has HTML body")
assert.True(t, msg2.Silent, "message silent flag is set")
}
func TestChannel_SendSystemMessage(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
user := randomUser(t, c)
msg := &Message{
Text: "test message",
Type: MessageTypeSystem,
}
resp, err := ch.SendMessage(ctx, msg, user.ID)
require.NoError(t, err, "send message")
// check that message was updated
msg = resp.Message
assert.NotEmpty(t, msg.ID, "message has ID")
assert.Equal(t, MessageTypeSystem, msg.Type, "message type is system")
}
func TestChannel_Truncate(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
user := randomUser(t, c)
msg := &Message{
Text: "test message",
User: user,
}
// Make sure we have one message in the channel
resp, err := ch.SendMessage(ctx, msg, user.ID)
require.NoError(t, err, "send message")
require.NoError(t, ch.refresh(ctx), "refresh channel")
assert.Equal(t, ch.Messages[0].ID, resp.Message.ID, "message exists")
// Now truncate it
_, err = ch.Truncate(ctx)
require.NoError(t, err, "truncate channel")
require.NoError(t, ch.refresh(ctx), "refresh channel")
assert.Empty(t, ch.Messages, "channel is empty")
}
func TestChannel_TruncateWithOptions(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
user := randomUser(t, c)
truncaterUser := randomUser(t, c)
ctx := context.Background()
msg := &Message{
Text: "test message",
User: user,
}
// Make sure we have one message in the channel
resp, err := ch.SendMessage(ctx, msg, user.ID)
require.NoError(t, err, "send message")
require.NoError(t, ch.refresh(ctx), "refresh channel")
assert.Equal(t, ch.Messages[0].ID, resp.Message.ID, "message exists")
// Now truncate it
_, err = ch.Truncate(ctx,
TruncateWithSkipPush(),
TruncateWithMessage(&Message{Text: "truncated channel", User: &User{ID: user.ID}}),
TruncateWithUser(&User{ID: truncaterUser.ID}),
)
require.NoError(t, err, "truncate channel")
require.NoError(t, ch.refresh(ctx), "refresh channel")
require.Len(t, ch.Messages, 1, "channel has one message")
require.Equal(t, "truncated channel", ch.Messages[0].Text)
require.NotNil(t, ch.TruncatedBy)
require.Equal(t, truncaterUser.ID, ch.TruncatedBy.ID)
require.NotNil(t, ch.TruncatedAt)
}
func TestChannel_Update(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
_, err := ch.Update(ctx, map[string]interface{}{"color": "blue"},
&Message{Text: "color is blue", User: &User{ID: randomUser(t, c).ID}})
require.NoError(t, err)
}
func TestChannel_PartialUpdate(t *testing.T) {
c := initClient(t)
users := randomUsers(t, c, 5)
ctx := context.Background()
members := make([]string, 0, len(users))
for i := range users {
members = append(members, users[i].ID)
}
req := &ChannelRequest{Members: members, ExtraData: map[string]interface{}{"color": "blue", "age": 30}}
resp, err := c.CreateChannel(ctx, "team", randomString(12), randomUser(t, c).ID, req)
require.NoError(t, err)
ch := resp.Channel
_, err = ch.PartialUpdate(ctx, PartialUpdate{
Set: map[string]interface{}{
"color": "red",
"config_override": map[string]interface{}{
"typing_events": false,
},
},
Unset: []string{"age"},
})
require.NoError(t, err)
err = ch.refresh(ctx)
require.NoError(t, err)
require.Equal(t, "red", ch.ExtraData["color"])
require.Nil(t, ch.ExtraData["age"])
}
func TestChannel_SendFile(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
var url string
t.Run("Send file", func(t *testing.T) {
file, err := os.Open(path.Join("testdata", "helloworld.txt"))
if err != nil {
t.Fatal(err)
}
resp, err := ch.SendFile(ctx, SendFileRequest{
Reader: file,
FileName: "HelloWorld.txt",
User: randomUser(t, c),
})
url = resp.File
if err != nil {
t.Fatalf("send file failed: %s", err)
}
if url == "" {
t.Fatal("upload file returned empty url")
}
})
t.Run("Delete file", func(t *testing.T) {
_, err := ch.DeleteFile(ctx, url)
if err != nil {
t.Fatalf("delete file failed: %s", err.Error())
}
})
}
func TestChannel_SendImage(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
ctx := context.Background()
var url string
t.Run("Send image", func(t *testing.T) {
file, err := os.Open(path.Join("testdata", "helloworld.jpg"))
if err != nil {
t.Fatal(err)
}
resp, err := ch.SendImage(ctx, SendFileRequest{
Reader: file,
FileName: "HelloWorld.jpg",
User: randomUser(t, c),
})
if err != nil {
t.Fatalf("Send image failed: %s", err.Error())
}
url = resp.File
if url == "" {
t.Fatal("upload image returned empty url")
}
})
t.Run("Delete image", func(t *testing.T) {
_, err := ch.DeleteImage(ctx, url)
if err != nil {
t.Fatalf("delete image failed: %s", err.Error())
}
})
}
func TestChannel_AcceptInvite(t *testing.T) {
c := initClient(t)
ctx := context.Background()
users := randomUsers(t, c, 5)
members := make([]string, 0, len(users))
for i := range users {
members = append(members, users[i].ID)
}
req := &ChannelRequest{Members: members, Invites: []string{members[0]}}
resp, err := c.CreateChannel(ctx, "team", randomString(12), randomUser(t, c).ID, req)
require.NoError(t, err, "create channel")
_, err = resp.Channel.AcceptInvite(ctx, members[0], &Message{Text: "accepted", User: &User{ID: members[0]}})
require.NoError(t, err, "accept invite")
}
func TestChannel_RejectInvite(t *testing.T) {
c := initClient(t)
ctx := context.Background()
users := randomUsers(t, c, 5)
members := make([]string, 0, len(users))
for i := range users {
members = append(members, users[i].ID)
}
req := &ChannelRequest{Members: members, Invites: []string{members[0]}}
resp, err := c.CreateChannel(ctx, "team", randomString(12), randomUser(t, c).ID, req)
require.NoError(t, err, "create channel")
_, err = resp.Channel.RejectInvite(ctx, members[0], &Message{Text: "rejected", User: &User{ID: members[0]}})
require.NoError(t, err, "reject invite")
}
func TestChannel_Mute_Unmute(t *testing.T) {
c := initClient(t)
ctx := context.Background()
users := randomUsers(t, c, 5)
members := make([]string, 0, len(users))
for i := range users {
members = append(members, users[i].ID)
}
ch := initChannel(t, c, members...)
// mute the channel
mute, err := ch.Mute(ctx, members[0], nil)
require.NoError(t, err, "mute channel")
require.Equal(t, ch.CID, mute.ChannelMute.Channel.CID)
require.Equal(t, members[0], mute.ChannelMute.User.ID)
// query for muted the channel
queryChannResp, err := c.QueryChannels(ctx, &QueryOption{
UserID: members[0],
Filter: map[string]interface{}{
"muted": true,
"cid": ch.CID,
},
})
channels := queryChannResp.Channels
require.NoError(t, err, "query muted channel")
require.Len(t, channels, 1)
require.Equal(t, channels[0].CID, ch.CID)
// unmute the channel
_, err = ch.Unmute(ctx, members[0])
require.NoError(t, err, "mute channel")
// query for unmuted the channel should return 1 results
queryChannResp, err = c.QueryChannels(ctx, &QueryOption{
UserID: members[0],
Filter: map[string]interface{}{
"muted": false,
"cid": ch.CID,
},
})
require.NoError(t, err, "query muted channel")
require.Len(t, queryChannResp.Channels, 1)
}
func ExampleChannel_Update() {
client := &Client{}
ctx := context.Background()
data := map[string]interface{}{
"image": "https://path/to/image",
"created_by": "elon",
"roles": map[string]string{"elon": "admin", "gwynne": "moderator"},
}
spacexChannel := client.Channel("team", "spacex")
if _, err := spacexChannel.Update(ctx, data, nil); err != nil {
log.Fatalf("Error: %v", err)
}
}
func (c *Client) ExampleClient_CreateChannel() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
resp, _ := client.CreateChannel(ctx, "team", "stream", "tommaso", nil)
_, _ = resp.Channel.SendMessage(ctx, &Message{
User: &User{ID: "tomosso"},
Text: "hi there!",
}, "tomosso")
}
func ExampleChannel_Query() {
ctx := context.Background()
channel := &Channel{}
msg, _ := channel.SendMessage(ctx, &Message{Text: "test message", Pinned: true}, channel.CreatedBy.ID)
q := &QueryRequest{
Messages: &MessagePaginationParamsRequest{PaginationParamsRequest: PaginationParamsRequest{Limit: 1, IDLT: msg.Message.ID}},
Members: &PaginationParamsRequest{Limit: 1, Offset: 0},
Watchers: &PaginationParamsRequest{Limit: 1, Offset: 0},
}
_, _ = channel.Query(ctx, q)
}