-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
98 lines (84 loc) · 2.38 KB
/
integration_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
// +build integration
package parsepush_test
import (
"crypto/rand"
"encoding/json"
"fmt"
"net/url"
"os"
"testing"
"time"
"github.com/daaku/parsepush"
"github.com/facebookgo/ensure"
"github.com/facebookgo/parse"
)
var (
integrationApplicationID = defaultEnv(
"PARSE_APP_ID", "spAVcBmdREXEk9IiDwXzlwe0p4pO7t18KFsHyk7j")
integrationRestAPIKey = defaultEnv(
"PARSE_REST_API_KEY", "t6ON64DfTrTL4QJC322HpWbhN6fzGYo8cnjVttap")
)
func defaultEnv(name, def string) string {
if v := os.Getenv(name); v != "" {
return v
}
return def
}
func uuid(t testing.TB) string {
u := make([]byte, 16)
_, err := rand.Read(u[:])
ensure.Nil(t, err)
return fmt.Sprintf("%x-%x-%x-%x-%x", u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
}
func TestIntegrate(t *testing.T) {
// we' use github.com/facebookgo/parse for the API interactions
client := parse.Client{
Credentials: parse.RestAPIKey{
ApplicationID: integrationApplicationID,
RestAPIKey: integrationRestAPIKey,
},
}
// push data is described at
// https://www.parse.com/docs/push_guide#options/REST
givenPushData := map[string]interface{}{"answer": "42"}
// create new installation
installationID := uuid(t)
ir := map[string]string{
"installationId": installationID,
"deviceType": "embedded",
}
res := make(map[string]string)
_, err := client.Post(&url.URL{Path: "/1/installations"}, ir, &res)
ensure.Nil(t, err, "creating installation")
installationObjectID := res["objectId"]
// start push receiver connection
pushes := make(chan []byte)
conn, err := parsepush.NewConn(
parsepush.ConnApplicationID(integrationApplicationID),
parsepush.ConnInstallationID(installationID),
parsepush.ConnPushHandler(func(raw []byte) {
pushes <- raw
}),
parsepush.ConnErrorHandler(func(err error) {
t.Fatal("unexpected error:", err)
}),
)
ensure.Nil(t, err)
// obvious integration test downside
time.Sleep(2 * time.Second)
// send push
pushReq := map[string]interface{}{
"where": map[string]string{"objectId": installationObjectID},
"data": givenPushData,
}
_, err = client.Post(&url.URL{Path: "/1/push"}, pushReq, nil)
ensure.Nil(t, err, "sending push")
// wait for push to arrive
raw := <-pushes
close(pushes)
push := make(map[string]interface{})
ensure.Nil(t, json.Unmarshal(raw, &push))
ensure.Subset(t, push["data"], givenPushData)
// close our push receiver and clean-up associated resources
conn.Close()
}