forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_test.go
84 lines (73 loc) · 2.21 KB
/
users_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
package stream_test
import (
"net/http"
"testing"
stream "github.com/GetStream/stream-go2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUserRefHelpers(t *testing.T) {
client, _ := newClient(t)
ref := client.Users().CreateReference("bar")
assert.Equal(t, "SU:bar", ref)
}
func TestGetUser(t *testing.T) {
client, requester := newClient(t)
_, err := client.Users().Get("id1")
require.NoError(t, err)
testRequest(t, requester.req, http.MethodGet, "https://api.stream-io-api.com/api/v1.0/user/id1/?api_key=key", "")
}
func TestDeleteUser(t *testing.T) {
client, requester := newClient(t)
err := client.Users().Delete("id1")
require.NoError(t, err)
testRequest(t, requester.req, http.MethodDelete, "https://api.stream-io-api.com/api/v1.0/user/id1/?api_key=key", "")
}
func TestAddUser(t *testing.T) {
client, requester := newClient(t)
testCases := []struct {
object stream.User
getOrCreate bool
expectedURL string
expectedBody string
}{
{
object: stream.User{
ID: "user-test",
Data: map[string]interface{}{
"is_admin": true,
"name": "Johnny",
},
},
expectedURL: "https://api.stream-io-api.com/api/v1.0/user/?api_key=key&get_or_create=false",
expectedBody: `{"id":"user-test","data":{"is_admin":true,"name":"Johnny"}}`,
},
{
object: stream.User{
ID: "user-test",
Data: map[string]interface{}{
"is_admin": true,
"name": "Jane",
},
},
getOrCreate: true,
expectedURL: "https://api.stream-io-api.com/api/v1.0/user/?api_key=key&get_or_create=true",
expectedBody: `{"id":"user-test","data":{"is_admin":true,"name":"Jane"}}`,
},
}
for _, tc := range testCases {
_, err := client.Users().Add(tc.object, tc.getOrCreate)
require.NoError(t, err)
testRequest(t, requester.req, http.MethodPost, tc.expectedURL, tc.expectedBody)
}
}
func TestUpdateUser(t *testing.T) {
client, requester := newClient(t)
data := map[string]interface{}{
"name": "Jane",
}
_, err := client.Users().Update("123", data)
require.NoError(t, err)
expectedBody := `{"data":{"name":"Jane"}}`
testRequest(t, requester.req, http.MethodPut, "https://api.stream-io-api.com/api/v1.0/user/123/?api_key=key", expectedBody)
}