-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
270 lines (234 loc) · 6.63 KB
/
examples_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
package getstream_test
import (
"context"
"testing"
"time"
"github.com/GetStream/getstream-go"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
client *getstream.Client
ctx context.Context
)
func init() {
ctx = context.Background()
}
func TestCreatingClient(t *testing.T) {
apiKey := "key"
apiSecret := "secret"
client, err := getstream.NewClient(apiKey, apiSecret)
require.NoError(t, err)
require.NotNil(t, client)
{
// optionally you can create a new client with a different timeout
client, err := getstream.NewClient(
apiKey, apiSecret,
getstream.WithTimeout(10_000*time.Millisecond),
)
require.NoError(t, err)
require.NotNil(t, client)
}
}
func TestCreateUserAndToken(t *testing.T) {
client := initClient(t)
// optional values are passed as pointers, you can use `getstream.PtrTo` to get pointers from literals of any type
response, err := client.UpdateUsers(ctx, &getstream.UpdateUsersRequest{
Users: map[string]getstream.UserRequest{
"user-id": {
ID: "user-id",
Name: getstream.PtrTo("tommaso"),
Role: getstream.PtrTo("admin"),
Custom: map[string]any{"country": "NL"},
},
},
})
require.NoError(t, err)
require.NotNil(t, response)
token, err := client.CreateToken("tommaso-id", getstream.WithExpiration(24*time.Hour))
require.NoError(t, err)
require.NotEmpty(t, token)
}
func TestCreateCall(t *testing.T) {
client := initClient(t)
call := client.Video().Call("default", uuid.NewString())
members := []getstream.MemberRequest{
{UserID: "john", Role: getstream.PtrTo("admin")},
{UserID: "jack"},
}
callRequest := getstream.GetOrCreateCallRequest{
Data: &getstream.CallRequest{
CreatedByID: getstream.PtrTo("sacha"),
Members: members,
Custom: map[string]any{
"color": "blue",
},
},
}
response, err := call.GetOrCreate(ctx, &callRequest)
require.NoError(t, err)
require.NotEmpty(t, response)
// pre-create the users
{
response, err := client.UpdateUsers(ctx, &getstream.UpdateUsersRequest{
Users: map[string]getstream.UserRequest{
"sara": {
ID: "sara",
},
"emily": {
ID: "emily",
},
},
})
require.NoError(t, err)
require.NotNil(t, response)
}
// call members example:
{
// Call members need to be existing users (use `client.UpdateUsers` for that)
// You can also update the role of existing members
response, err := call.UpdateCallMembers(ctx, &getstream.UpdateCallMembersRequest{
UpdateMembers: []getstream.MemberRequest{
{UserID: "sara"},
{UserID: "emily", Role: getstream.PtrTo("admin")},
},
})
require.NoError(t, err)
require.NotEmpty(t, response)
}
// call members remove example:
{
response, err := call.UpdateCallMembers(ctx, &getstream.UpdateCallMembersRequest{
RemoveMembers: []string{
"sara",
},
})
require.NoError(t, err)
require.NotEmpty(t, response)
}
// updating a call
{
// update some custom data for this call
response, err := call.Update(ctx, &getstream.UpdateCallRequest{
Custom: map[string]any{"color": "red"},
})
// update settings for this call
response, err = call.Update(ctx, &getstream.UpdateCallRequest{
SettingsOverride: &getstream.CallSettingsRequest{
Screensharing: &getstream.ScreensharingSettingsRequest{
Enabled: getstream.PtrTo(true),
AccessRequestEnabled: getstream.PtrTo(true),
},
},
})
require.NoError(t, err)
require.NotEmpty(t, response)
assert.Equal(t, map[string]any{"color": "red"}, response.Data.Call.Custom)
assert.True(t, response.Data.Call.Settings.Screensharing.Enabled)
assert.True(t, response.Data.Call.Settings.Screensharing.AccessRequestEnabled)
}
}
func TestUsers(t *testing.T) {
client := initClient(t)
response, err := client.UpdateUsers(ctx, &getstream.UpdateUsersRequest{
Users: map[string]getstream.UserRequest{
"user_id": {
ID: "user_id",
Role: getstream.PtrTo("admin"),
Custom: map[string]interface{}{
"color": "red",
},
Name: getstream.PtrTo("This is a test user"),
Image: getstream.PtrTo("link/to/profile/image"),
},
},
})
require.NoError(t, err)
require.NotEmpty(t, response)
{
response, err := client.UpdateUsers(ctx, &getstream.UpdateUsersRequest{
Users: map[string]getstream.UserRequest{
"user_id": {
ID: "user_id",
Role: getstream.PtrTo("user"),
Custom: map[string]interface{}{
"color": "blue",
},
Name: getstream.PtrTo("This is a test user"),
Image: getstream.PtrTo("link/to/profile/image"),
},
},
})
// or
response, err = client.UpdateUsersPartial(ctx, &getstream.UpdateUsersPartialRequest{
Users: []getstream.UpdateUserPartialRequest{
{
ID: "user_id",
Set: map[string]interface{}{
"new-field": "value",
},
Unset: []string{"name"},
},
},
})
require.NoError(t, err)
require.NotEmpty(t, response)
require.Empty(t, response.Data.Users["user_id"].Name)
require.Equal(t, map[string]interface{}{
"color": "blue",
"new-field": "value",
}, response.Data.Users["user_id"].Custom)
}
aliceID := uuid.New().String()
bobID := uuid.New().String()
// pre-create the users
{
response, err := client.UpdateUsers(ctx, &getstream.UpdateUsersRequest{
Users: map[string]getstream.UserRequest{
aliceID: {
ID: aliceID,
},
bobID: {
ID: bobID,
},
},
})
require.NoError(t, err)
require.NotNil(t, response)
}
{
// deactivate one user
response, err := client.DeactivateUser(ctx, aliceID, &getstream.DeactivateUserRequest{})
require.NoError(t, err)
require.NotEmpty(t, response)
// reactivates the user
_, err = client.ReactivateUser(ctx, aliceID, &getstream.ReactivateUserRequest{})
require.NoError(t, err)
// deactivates users in bulk, this is an async operation
_, err = client.DeactivateUsers(ctx, &getstream.DeactivateUsersRequest{
UserIds: []string{aliceID, bobID},
})
require.NoError(t, err)
}
// deleting users
{
t.Skip("this is just an example, don't run it")
response, err := client.DeleteUsers(ctx, &getstream.DeleteUsersRequest{UserIds: []string{"<id>"}})
// restore a soft-deleted user
_, err = client.RestoreUsers(ctx, &getstream.RestoreUsersRequest{UserIds: []string{"<id>"}})
require.NoError(t, err)
require.NotEmpty(t, response)
}
}
func TestCallToken(t *testing.T) {
client := initClient(t)
// the list of call IDs this token applies to
tokenClaims := getstream.Claims{CallCIDs: []string{"default:call1", "livestream:call2"}}
token, err := client.CreateToken("john",
getstream.WithClaims(tokenClaims),
getstream.WithExpiration(24*time.Hour),
)
require.NoError(t, err)
require.NotEmpty(t, token)
}