forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_test.go
450 lines (346 loc) · 10.6 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
package stream_chat // nolint: golint
import (
"log"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_CreateChannel(t *testing.T) {
c := initClient(t)
t.Run("get existing channel", func(t *testing.T) {
ch := initChannel(t, c)
got, err := c.CreateChannel(ch.Type, ch.ID, serverUser.ID, nil)
mustNoError(t, err, "create channel", ch)
assert.Equal(t, c, got.client, "client link")
assert.Equal(t, ch.Type, got.Type, "channel type")
assert.Equal(t, ch.ID, got.ID, "channel id")
assert.Equal(t, got.MemberCount, ch.MemberCount, "member count")
assert.Len(t, got.Members, got.MemberCount, "members length")
})
tests := []struct {
name string
_type string
id string
userID string
data map[string]interface{}
wantErr bool
}{
{"create channel with ID", "messaging", randomString(12), serverUser.ID, nil, false},
{"create channel without ID and members", "messaging", "", serverUser.ID, nil, true},
{"create channel without ID but with members", "messaging", "", serverUser.ID,
map[string]interface{}{"members": []string{testUsers[0].ID, testUsers[1].ID}}, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := c.CreateChannel(tt._type, tt.id, tt.userID, tt.data)
if tt.wantErr {
mustError(t, err, "create channel", tt)
return
}
mustNoError(t, err, "create channel", tt)
assert.Equal(t, tt._type, got.Type, "channel type")
assert.NotEmpty(t, got.ID)
if tt.id != "" {
assert.Equal(t, tt.id, got.ID, "channel id")
}
assert.Equal(t, tt.userID, got.CreatedBy.ID, "channel created by")
})
}
}
func TestChannel_AddMembers(t *testing.T) {
c := initClient(t)
chanID := randomString(12)
ch, err := c.CreateChannel("messaging", chanID, serverUser.ID, nil)
mustNoError(t, err, "create channel")
defer func() {
_ = ch.Delete()
}()
assert.Empty(t, ch.Members, "members are empty")
user := randomUser()
err = ch.AddMembers(
[]string{user.ID},
&Message{Text: "some members", User: &User{ID: user.ID}},
)
mustNoError(t, err, "add members")
// refresh channel state
mustNoError(t, ch.refresh(), "refresh channel")
assert.Equal(t, user.ID, ch.Members[0].User.ID, "members contain user id")
}
// See https://getstream.io/chat/docs/channel_members/ for more details.
func ExampleChannel_AddModerators() {
channel := &Channel{}
newModerators := []string{"bob", "sue"}
_ = channel.AddModerators("thierry", "josh")
_ = channel.AddModerators(newModerators...)
_ = channel.DemoteModerators(newModerators...)
}
func TestChannel_InviteMembers(t *testing.T) {
c := initClient(t)
chanID := randomString(12)
ch, err := c.CreateChannel("messaging", chanID, serverUser.ID, nil)
mustNoError(t, err, "create channel")
defer func() {
_ = ch.Delete()
}()
assert.Empty(t, ch.Members, "members are empty")
user := randomUser()
err = ch.InviteMembers(user.ID)
mustNoError(t, err, "invite members")
// refresh channel state
mustNoError(t, ch.refresh(), "refresh channel")
assert.Equal(t, user.ID, ch.Members[0].User.ID, "members contain user id")
assert.Equal(t, true, 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)
// init random channel
chanID := randomString(12)
ch, err := c.CreateChannel("messaging", chanID, serverUser.ID, nil)
mustNoError(t, err, "create channel")
defer func() {
_ = ch.Delete()
}()
assert.Empty(t, ch.Members, "members are empty")
user := randomUser()
err = ch.AddModeratorsWithMessage(
[]string{user.ID},
&Message{Text: "accepted", User: &User{ID: user.ID}},
)
mustNoError(t, err, "add moderators")
// refresh channel state
mustNoError(t, ch.refresh(), "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(user.ID)
mustNoError(t, err, "demote moderators")
// refresh channel state
mustNoError(t, ch.refresh(), "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_BanUser(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
_ = ch.Delete()
}()
user := randomUser()
err := ch.BanUser(user.ID, serverUser.ID, nil)
mustNoError(t, err, "ban user")
err = ch.BanUser(user.ID, serverUser.ID, map[string]interface{}{
"timeout": 3600,
"reason": "offensive language is not allowed here",
})
mustNoError(t, err, "ban user")
err = ch.UnBanUser(user.ID, nil)
mustNoError(t, err, "unban user")
}
func TestChannel_Delete(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
mustNoError(t, ch.Delete(), "delete channel")
}
func TestChannel_GetReplies(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
_ = ch.Delete()
}()
user := randomUser()
msg := &Message{Text: "test message"}
msg, err := ch.SendMessage(msg, user.ID)
mustNoError(t, err, "send message")
reply := &Message{Text: "test reply", ParentID: msg.ID, Type: MessageTypeReply}
_, err = ch.SendMessage(reply, serverUser.ID)
mustNoError(t, err, "send reply")
replies, err := ch.GetReplies(msg.ID, nil)
mustNoError(t, err, "get replies")
assert.Len(t, replies, 1)
}
func TestChannel_MarkRead(t *testing.T) {
}
func TestChannel_RemoveMembers(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
_ = ch.Delete()
}()
user := randomUser()
err := ch.RemoveMembers(
[]string{user.ID},
&Message{Text: "some members", User: &User{ID: user.ID}},
)
mustNoError(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) {
}
func TestChannel_SendMessage(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
_ = ch.Delete()
}()
user := randomUser()
msg := &Message{
Text: "test message",
User: user,
}
msg, err := ch.SendMessage(msg, serverUser.ID)
mustNoError(t, err, "send message")
// check that message was updated
assert.NotEmpty(t, msg.ID, "message has ID")
assert.NotEmpty(t, msg.HTML, "message has HTML body")
}
func TestChannel_Truncate(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
_ = ch.Delete()
}()
user := randomUser()
msg := &Message{
Text: "test message",
User: user,
}
msg, err := ch.SendMessage(msg, serverUser.ID)
mustNoError(t, err, "send message")
// refresh channel state
mustNoError(t, ch.refresh(), "refresh channel")
assert.Equal(t, ch.Messages[0].ID, msg.ID, "message exists")
err = ch.Truncate()
mustNoError(t, err, "truncate channel")
// refresh channel state
mustNoError(t, ch.refresh(), "refresh channel")
assert.Empty(t, ch.Messages, "message not exists")
}
func TestChannel_Update(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
err := ch.Update(map[string]interface{}{"color": "blue"},
&Message{Text: "color is blue", User: &User{ID: testUsers[0].ID}})
require.NoError(t, err)
}
func TestChannel_AddModerators(t *testing.T) {
}
func TestChannel_DemoteModerators(t *testing.T) {
}
func TestChannel_UnBanUser(t *testing.T) {
}
func TestChannel_SendFile(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
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)
}
url, err = ch.SendFile(SendFileRequest{
Reader: file,
FileName: "HelloWorld.txt",
User: randomUser(),
})
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(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)
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)
}
url, err = ch.SendImage(SendFileRequest{
Reader: file,
FileName: "HelloWorld.jpg",
User: randomUser(),
ContentType: "image/jpeg",
})
if err != nil {
t.Fatalf("Send image failed: %s", err.Error())
}
if url == "" {
t.Fatal("upload image returned empty url")
}
})
t.Run("Delete image", func(t *testing.T) {
err := ch.DeleteImage(url)
if err != nil {
t.Fatalf("delete image failed: %s", err.Error())
}
})
}
func TestChannel_AcceptInvite(t *testing.T) {
c := initClient(t)
_, err := c.UpdateUsers(testUsers...)
mustNoError(t, err, "update users")
members := make([]string, 0, len(testUsers))
for i := range testUsers {
members = append(members, testUsers[i].ID)
}
ch, err := c.CreateChannel("team", randomString(12), serverUser.ID, map[string]interface{}{
"members": members,
"invites": []string{members[0]},
})
require.NoError(t, err, "create channel")
err = ch.AcceptInvite(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)
_, err := c.UpdateUsers(testUsers...)
mustNoError(t, err, "update users")
members := make([]string, 0, len(testUsers))
for i := range testUsers {
members = append(members, testUsers[i].ID)
}
ch, err := c.CreateChannel("team", randomString(12), serverUser.ID, map[string]interface{}{
"members": members,
"invites": []string{members[0]},
})
require.NoError(t, err, "create channel")
err = ch.RejectInvite(members[0], &Message{Text: "rejected", User: &User{ID: members[0]}})
require.NoError(t, err, "reject invite")
}
func ExampleChannel_Update() {
// https://getstream.io/chat/docs/channel_permissions/?language=python
client := &Client{}
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(data, nil); err != nil {
log.Fatalf("Error: %v", err)
}
}
func (c *Client) ExampleClient_CreateChannel() {
client, _ := NewClient("XXXX", []byte("XXXX"))
channel, _ := client.CreateChannel("team", "stream", "tommaso", nil)
_, _ = channel.SendMessage(&Message{
User: &User{ID: "tomosso"},
Text: "hi there!",
}, "tomosso")
}