forked from customerio/go-customerio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customerio_test.go
344 lines (310 loc) · 8.04 KB
/
customerio_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package customerio_test
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"github.com/customerio/go-customerio/v3"
)
var cio *customerio.CustomerIO
func TestMain(m *testing.M) {
srv := httptest.NewServer(http.HandlerFunc(handler))
defer srv.Close()
cio = customerio.NewCustomerIO("siteid", "apikey")
cio.URL = srv.URL
os.Exit(m.Run())
}
type testCase struct {
id string
method string
path string
body interface{}
}
func runCases(t *testing.T, cases []testCase, do func(c testCase) error) {
for i, c := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
expect(c.method, c.path, c.body)
if err := do(c); err != nil {
t.Error(err.Error())
}
})
}
}
func checkParamError(t *testing.T, err error, param string) {
if err == nil {
t.Error("expected error")
return
}
pe, ok := err.(customerio.ParamError)
if !ok {
t.Error("expected ParamError")
}
if pe.Param != param {
t.Errorf("expected %s got %s", param, pe.Param)
}
}
func TestIdentify(t *testing.T) {
attributes := map[string]interface{}{
"a": "1",
}
err := cio.Identify("", attributes)
checkParamError(t, err, "customerID")
runCases(t,
[]testCase{
{"1", "PUT", "/api/v1/customers/1", attributes},
{"1 ", "PUT", "/api/v1/customers/1%20", attributes},
{"1/", "PUT", "/api/v1/customers/1%2F", attributes},
},
func(c testCase) error {
return cio.Identify(c.id, attributes)
})
}
func TestTrack(t *testing.T) {
data := map[string]interface{}{
"a": "1",
}
body := map[string]interface{}{
"name": "test",
"data": map[string]interface{}{
"a": "1",
},
}
err := cio.Track("", "test", data)
checkParamError(t, err, "customerID")
err = cio.Track("1", "", data)
checkParamError(t, err, "eventName")
runCases(t,
[]testCase{
{"1", "POST", "/api/v1/customers/1/events", body},
{"1 ", "POST", "/api/v1/customers/1%20/events", body},
{"1/", "POST", "/api/v1/customers/1%2F/events", body},
},
func(c testCase) error {
return cio.Track(c.id, "test", data)
})
}
func TestTrackAnonymous(t *testing.T) {
data := map[string]interface{}{
"a": "1",
}
body := map[string]interface{}{
"name": "test",
"anonymous_id": "anon123",
"data": map[string]interface{}{
"a": "1",
},
}
expect("POST", "/api/v1/events", body)
if err := cio.TrackAnonymous("anon123", "test", data); err != nil {
t.Error(err.Error())
}
}
func TestDelete(t *testing.T) {
err := cio.Delete("")
checkParamError(t, err, "customerID")
runCases(t,
[]testCase{
{"1", "DELETE", "/api/v1/customers/1", nil},
{"1 ", "DELETE", "/api/v1/customers/1%20", nil},
{"1/", "DELETE", "/api/v1/customers/1%2F", nil},
},
func(c testCase) error {
return cio.Delete(c.id)
})
}
func TestAddDevice(t *testing.T) {
err := cio.AddDevice("", "d1", "ios", nil)
checkParamError(t, err, "customerID")
err = cio.AddDevice("1", "", "ios", nil)
checkParamError(t, err, "deviceID")
err = cio.AddDevice("1", "d1", "", nil)
checkParamError(t, err, "platform")
body := map[string]map[string]interface{}{
"device": {
"id": "d1",
"platform": "ios",
"last_used": 1606511962,
},
}
runCases(t,
[]testCase{
{"1", "PUT", "/api/v1/customers/1/devices", body},
{"1 ", "PUT", "/api/v1/customers/1%20/devices", body},
{"1/", "PUT", "/api/v1/customers/1%2F/devices", body},
},
func(c testCase) error {
return cio.AddDevice(c.id, "d1", "ios", map[string]interface{}{
"last_used": 1606511962,
})
})
}
func TestDeleteDevice(t *testing.T) {
err := cio.DeleteDevice("", "d1")
checkParamError(t, err, "customerID")
err = cio.DeleteDevice("1", "")
checkParamError(t, err, "deviceID")
runCases(t,
[]testCase{
{"1", "DELETE", "/api/v1/customers/1/devices/d1", nil},
{"1 ", "DELETE", "/api/v1/customers/1%20/devices/d1", nil},
{"1/", "DELETE", "/api/v1/customers/1%2F/devices/d1", nil},
{"2", "DELETE", "/api/v1/customers/d1/devices/2", nil},
{"2 ", "DELETE", "/api/v1/customers/d1/devices/2%20", nil},
{"2/", "DELETE", "/api/v1/customers/d1/devices/2%2F", nil},
},
func(c testCase) error {
if c.id[0] == '2' {
return cio.DeleteDevice("d1", c.id)
} else {
return cio.DeleteDevice(c.id, "d1")
}
})
}
var (
expectedMethod string
expectedPath string
expectedBody interface{}
)
func handler(w http.ResponseWriter, req *http.Request) {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer req.Body.Close()
s := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
if len(s) != 2 || s[0] != "Basic" {
w.WriteHeader(http.StatusUnauthorized)
return
}
decoded, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
pair := strings.SplitN(string(decoded), ":", 2)
if len(pair) != 2 {
w.WriteHeader(http.StatusUnauthorized)
return
}
if pair[0] != "siteid" && pair[1] != "apikey" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if req.Method != "DELETE" && req.Header.Get("Content-Type") != "application/json" {
http.Error(w, "expected Content-Type application/json", http.StatusBadRequest)
}
var data map[string]interface{}
if len(b) > 0 {
dec := json.NewDecoder(bytes.NewReader(b))
dec.UseNumber()
if err := dec.Decode(&data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
validate := func(method, path string, body interface{}) error {
if method != expectedMethod {
return fmt.Errorf("expected %s got %s", expectedMethod, method)
}
if path != expectedPath {
return fmt.Errorf("expected %s got %s", expectedPath, path)
}
expected, err := json.Marshal(body)
if err != nil {
return err
}
got, err := json.Marshal(data)
if err != nil {
return err
}
if bytes.Compare(expected, got) != 0 {
return fmt.Errorf("expected %v got %v", expected, got)
}
return nil
}
if err := validate(req.Method, req.RequestURI, data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
func expect(method, path string, body interface{}) {
expectedMethod = method
expectedPath = path
expectedBody = body
}
func TestMergeCustomers(t *testing.T) {
err1 := cio.MergeCustomers(customerio.Identifier{
Type: "",
Value: "id1",
}, customerio.Identifier{
Type: "id",
Value: "id2",
})
checkParamError(t, err1, "primary")
err2 := cio.MergeCustomers(customerio.Identifier{
Type: "id",
Value: "",
}, customerio.Identifier{
Type: "id",
Value: "id2",
})
checkParamError(t, err2, "primary")
err3 := cio.MergeCustomers(customerio.Identifier{
Type: "email",
Value: "id1",
}, customerio.Identifier{
Type: "",
Value: "id2",
})
checkParamError(t, err3, "secondary")
err4 := cio.MergeCustomers(customerio.Identifier{
Type: "cio_id",
Value: "id1",
}, customerio.Identifier{
Type: "email",
Value: "",
})
checkParamError(t, err4, "secondary")
runCases(t,
[]testCase{
{"1", "POST", "/api/v1/merge_customers", `{"primary":{"email":"[email protected]"},"secondary":{"email":"[email protected]"}}`},
{"2", "POST", "/api/v1/merge_customers", `{"primary":{"id":"[email protected]"},"secondary":{"cio_id":"person2"}}`},
{"3", "POST", "/api/v1/merge_customers", `{"primary":{"cio_id":"CIO123"},"secondary":{"id":"person1"}}`},
},
func(c testCase) error {
if c.id == "1" {
return cio.MergeCustomers(customerio.Identifier{
Type: "email",
Value: "[email protected]",
}, customerio.Identifier{
Type: "email",
Value: "[email protected]",
})
} else if c.id == "2" {
return cio.MergeCustomers(customerio.Identifier{
Type: "id",
Value: "[email protected]",
}, customerio.Identifier{
Type: "cio_id",
Value: "person2",
})
} else {
return cio.MergeCustomers(customerio.Identifier{
Type: customerio.IdentifierTypeCioID,
Value: "CIO123",
}, customerio.Identifier{
Type: customerio.IdentifierTypeID,
Value: "person1",
})
}
})
}