forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects_get_members_test.go
123 lines (101 loc) · 3.38 KB
/
objects_get_members_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
package pubnub
import (
"fmt"
"strconv"
"testing"
h "github.com/pubnub/go/tests/helpers"
"github.com/pubnub/go/utils"
"github.com/stretchr/testify/assert"
)
func AssertGetMembers(t *testing.T, checkQueryParam, testContext bool) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
incl := []PNMembersInclude{
PNMembersCustom,
}
queryParam := map[string]string{
"q1": "v1",
"q2": "v2",
}
if !checkQueryParam {
queryParam = nil
}
inclStr := EnumArrayToStringArray(incl)
o := newGetMembersBuilder(pn)
if testContext {
o = newGetMembersBuilderWithContext(pn, backgroundContext)
}
spaceID := "id0"
limit := 90
start := "Mxmy"
end := "Nxny"
o.SpaceID(spaceID)
o.Include(incl)
o.Limit(limit)
o.Start(start)
o.End(end)
o.Count(false)
o.QueryParam(queryParam)
path, err := o.opts.buildPath()
assert.Nil(err)
h.AssertPathsEqual(t,
fmt.Sprintf("/v1/objects/%s/spaces/%s/users", pn.Config.SubscribeKey, "id0"),
path, []int{})
body, err := o.opts.buildBody()
assert.Nil(err)
assert.Empty(body)
if checkQueryParam {
u, _ := o.opts.buildQuery()
assert.Equal("v1", u.Get("q1"))
assert.Equal("v2", u.Get("q2"))
assert.Equal(string(utils.JoinChannels(inclStr)), u.Get("include"))
assert.Equal(strconv.Itoa(limit), u.Get("limit"))
assert.Equal(start, u.Get("start"))
assert.Equal(end, u.Get("end"))
assert.Equal("0", u.Get("count"))
}
}
func TestGetMembers(t *testing.T) {
AssertGetMembers(t, true, false)
}
func TestGetMembersContext(t *testing.T) {
AssertGetMembers(t, true, true)
}
func TestGetMembersResponseValueError(t *testing.T) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
opts := &getMembersOpts{
pubnub: pn,
}
jsonBytes := []byte(`s`)
_, _, err := newPNGetMembersResponse(jsonBytes, opts, StatusResponse{})
assert.Equal("pubnub/parsing: Error unmarshalling response: {s}", err.Error())
}
func TestGetMembersResponseValuePass(t *testing.T) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
opts := &getMembersOpts{
pubnub: pn,
}
jsonBytes := []byte(`{"status":200,"data":[{"id":"id0","custom":{"a3":"b3","c3":"d3"},"user":{"id":"id0","name":"name","externalId":"extid","profileUrl":"purl","email":"email","custom":{"a":"b","c":"d"},"created":"2019-08-20T13:26:19.140324Z","updated":"2019-08-20T13:26:19.140324Z","eTag":"AbyT4v2p6K7fpQE"},"created":"2019-08-20T13:26:24.07832Z","updated":"2019-08-20T13:26:24.07832Z","eTag":"AamrnoXdpdmzjwE"}],"totalCount":1,"next":"MQ","prev":"NQ"}`)
r, _, err := newPNGetMembersResponse(jsonBytes, opts, StatusResponse{})
assert.Equal(1, r.TotalCount)
assert.Equal("MQ", r.Next)
assert.Equal("NQ", r.Prev)
assert.Equal("id0", r.Data[0].ID)
assert.Equal("name", r.Data[0].User.Name)
assert.Equal("extid", r.Data[0].User.ExternalID)
assert.Equal("purl", r.Data[0].User.ProfileURL)
assert.Equal("email", r.Data[0].User.Email)
assert.Equal("2019-08-20T13:26:19.140324Z", r.Data[0].User.Created)
assert.Equal("2019-08-20T13:26:19.140324Z", r.Data[0].User.Updated)
assert.Equal("AbyT4v2p6K7fpQE", r.Data[0].User.ETag)
assert.Equal("b", r.Data[0].User.Custom["a"])
assert.Equal("d", r.Data[0].User.Custom["c"])
assert.Equal("2019-08-20T13:26:24.07832Z", r.Data[0].Created)
assert.Equal("2019-08-20T13:26:24.07832Z", r.Data[0].Updated)
assert.Equal("AamrnoXdpdmzjwE", r.Data[0].ETag)
assert.Equal("b3", r.Data[0].Custom["a3"])
assert.Equal("d3", r.Data[0].Custom["c3"])
assert.Nil(err)
}