This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_test.go
224 lines (165 loc) · 5.23 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
package stream_chat
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
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 {
_type string
id string
userID string
data map[string]interface{}
wantErr bool
}{
{"messaging", randomString(12), serverUser.ID, nil, false},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("create new channel %s:%s", tt._type, tt.id), 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)
} else {
mustNoError(t, err, "create channel", tt)
}
assert.Equal(t, tt._type, got.Type, "channel type")
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 ch.Delete()
assert.Empty(t, ch.Members, "members are empty")
user := randomUser()
err = ch.AddMembers(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")
}
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 ch.Delete()
assert.Empty(t, ch.Members, "members are empty")
user := randomUser()
err = ch.AddModerators(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 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)
err := ch.Delete()
mustNoError(t, err, "delete channel")
}
func TestChannel_GetReplies(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer 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}
reply, 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 ch.Delete()
user := randomUser()
err := ch.RemoveMembers(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 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 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) {
}