-
Notifications
You must be signed in to change notification settings - Fork 27
/
user_test.go
225 lines (178 loc) · 6.07 KB
/
user_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
package stream_chat
import (
"context"
"log"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_MuteUser(t *testing.T) {
c := initClient(t)
ctx := context.Background()
user := randomUser(t, c)
_, err := c.MuteUser(ctx, randomUser(t, c).ID, user.ID)
require.NoError(t, err, "MuteUser should not return an error")
resp, err := c.QueryUsers(ctx, &QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": user.ID},
},
})
users := resp.Users
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.NotEmptyf(t, users[0].Mutes, "user should have Mutes: %+v", users[0])
mute := users[0].Mutes[0]
assert.NotEmpty(t, mute.User, "mute should have a User")
assert.NotEmpty(t, mute.Target, "mute should have a Target")
assert.Empty(t, mute.Expires, "mute should have no Expires")
user = randomUser(t, c)
// when timeout is given, expiration field should be set on mute
_, err = c.MuteUser(ctx, randomUser(t, c).ID, user.ID, MuteWithExpiration(60))
require.NoError(t, err, "MuteUser should not return an error")
resp, err = c.QueryUsers(ctx, &QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": user.ID},
},
})
users = resp.Users
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.NotEmptyf(t, users[0].Mutes, "user should have Mutes: %+v", users[0])
mute = users[0].Mutes[0]
assert.NotEmpty(t, mute.User, "mute should have a User")
assert.NotEmpty(t, mute.Target, "mute should have a Target")
assert.NotEmpty(t, mute.Expires, "mute should have Expires")
}
func TestClient_MuteUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
user := randomUser(t, c)
targetIDs := randomUsersID(t, c, 2)
_, err := c.MuteUsers(ctx, targetIDs, user.ID, MuteWithExpiration(60))
require.NoError(t, err, "MuteUsers should not return an error")
resp, err := c.QueryUsers(ctx, &QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": user.ID},
},
})
users := resp.Users
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.NotEmptyf(t, users[0].Mutes, "user should have Mutes: %+v", users[0])
for _, mute := range users[0].Mutes {
assert.NotEmpty(t, mute.Expires, "mute should have Expires")
}
}
func TestClient_UnmuteUser(t *testing.T) {
c := initClient(t)
ctx := context.Background()
user := randomUser(t, c)
mutedUser := randomUser(t, c)
_, err := c.MuteUser(ctx, mutedUser.ID, user.ID)
require.NoError(t, err, "MuteUser should not return an error")
_, err = c.UnmuteUser(ctx, mutedUser.ID, user.ID)
assert.NoError(t, err)
}
func TestClient_CreateGuestUser(t *testing.T) {
c := initClient(t)
ctx := context.Background()
u := &User{ID: randomString(10)}
resp, err := c.CreateGuestUser(ctx, u)
if err != nil {
// Sometimes the guest user access is disabled on app level
// so let's ignore errors here
return
}
require.NotNil(t, resp.AccessToken)
require.NotNil(t, resp.User)
}
func TestClient_UnmuteUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
user := randomUser(t, c)
targetIDs := []string{randomUser(t, c).ID, randomUser(t, c).ID}
_, err := c.MuteUsers(ctx, targetIDs, user.ID)
require.NoError(t, err, "MuteUsers should not return an error")
_, err = c.UnmuteUsers(ctx, targetIDs, user.ID)
assert.NoError(t, err, "unmute users")
}
func TestClient_UpsertUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
user := &User{ID: randomString(10)}
resp, err := c.UpsertUsers(ctx, user)
require.NoError(t, err, "update users")
assert.Contains(t, resp.Users, user.ID)
assert.NotEmpty(t, resp.Users[user.ID].CreatedAt)
assert.NotEmpty(t, resp.Users[user.ID].UpdatedAt)
}
func TestClient_PartialUpdateUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
user := randomUser(t, c)
update := PartialUserUpdate{
ID: user.ID,
Set: map[string]interface{}{
"test": map[string]interface{}{
"passed": true,
},
},
}
resp, err := c.PartialUpdateUsers(ctx, []PartialUserUpdate{update})
require.NoError(t, err, "partial update user")
got := resp.Users
assert.Contains(t, got, user.ID)
assert.Contains(t, got[user.ID].ExtraData, "test", "extra data contains: %v", got[user.ID].ExtraData)
assert.Equal(t, map[string]interface{}{"passed": true}, got[user.ID].ExtraData["test"])
update = PartialUserUpdate{
ID: user.ID,
Unset: []string{"test.passed"},
}
resp, err = c.PartialUpdateUsers(ctx, []PartialUserUpdate{update})
require.NoError(t, err, "partial update user")
got = resp.Users
assert.Contains(t, got, user.ID)
assert.Contains(t, got[user.ID].ExtraData, "test", "extra data contains", got[user.ID].ExtraData)
assert.Empty(t, got[user.ID].ExtraData["test"], "extra data field removed")
}
func ExampleClient_UpsertUser() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
_, err := client.UpsertUser(ctx, &User{
ID: "tommaso",
Name: "Tommaso",
Role: "Admin",
})
if err != nil {
log.Fatalf("Err: %v", err)
}
}
func ExampleClient_ExportUser() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
user, _ := client.ExportUser(ctx, "userID")
log.Printf("%#v", user)
}
func ExampleClient_DeactivateUser() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
_, _ = client.DeactivateUser(ctx, "userID")
}
func ExampleClient_ReactivateUser() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
_, _ = client.ReactivateUser(ctx, "userID")
}
func ExampleClient_DeleteUser() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
_, _ = client.DeleteUser(ctx, "userID")
}
func ExampleClient_DeleteUser_hard() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
_, _ = client.DeleteUser(ctx, "userID",
DeleteUserWithHardDelete(),
DeleteUserWithMarkMessagesDeleted(),
)
}