-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathklaviyo_test.go
129 lines (115 loc) · 3.05 KB
/
klaviyo_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
package klaviyo
import (
"os"
"testing"
"time"
)
var (
testPersonId = os.Getenv("KlaviyoTestPersonId")
testListId = os.Getenv("KlaviyoTestListId")
)
const (
attrIsTest = "IsTest"
attrLikesGold = "LikesGold"
)
func newTestClient() *Client {
return &Client{
PublicKey: os.Getenv("KlaviyoPublicKey"),
PrivateKey: os.Getenv("KlaviyoPrivateKey"),
DefaultTimeout: time.Second,
}
}
func TestClient_Identify(t *testing.T) {
client := newTestClient()
p := newTestPerson()
err := client.Identify(&p)
if err != nil {
t.Fatal(err)
}
}
func TestClient_GetPerson(t *testing.T) {
client := newTestClient()
p, err := client.GetPerson(testPersonId)
if err != nil {
t.Fatal(err)
}
if p == nil {
t.Fatal("Returned person was nil")
}
}
func TestClient_UpdatePerson(t *testing.T) {
client := newTestClient()
p, err := client.GetPerson(testPersonId)
if err != nil {
t.Fatal(err)
}
if p == nil {
t.Fatal("Returned person was nil")
}
t.Log("attr likes gold", p.Attributes[attrLikesGold])
likesGold := p.Attributes.ParseBool(attrLikesGold)
t.Log("parsed likes gold", likesGold)
likesGold = !likesGold
p.Attributes[attrLikesGold] = likesGold
err = client.UpdatePerson(p)
if err != nil {
t.Fatal(err)
}
// Verify update went through
b, err := client.GetPerson(p.Id)
if err != nil {
t.Fatal(err)
}
t.Log("b attr likes gold", b.Attributes[attrLikesGold])
if _, ok := b.Attributes[attrLikesGold]; !ok {
t.Fatalf("Did not find attribute %s", attrLikesGold)
} else if b.Attributes.ParseBool(attrLikesGold) != likesGold {
t.Fatalf("Attribute did not match for %s", attrLikesGold)
}
}
func TestClient_InList(t *testing.T) {
client := newTestClient()
p := newTestPerson()
// This checks to make sure the test user is in the test list
xs, err := client.InList(testListId, []string{p.Email}, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(xs) != 1 {
t.Fatalf("Expected 1 ListPerson in array")
}
if xs[0].Email != p.Email {
t.Fatalf("Returned ListPerson.Email does not match input")
}
// This checks that a real user is not in the test list
xs, err = client.InList(testListId, []string{"dev@monstercat.com"}, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(xs) != 0 {
t.Fatalf("User should not appear in the test list!")
}
}
// This test expects that your list is using single opt-in settings. Double opt-in will not return any results.
func TestClient_Subscribe(t *testing.T) {
email := "dev@monstercat.com"
client := newTestClient()
// TODO get list information on double opt-in status to adapt test checks
res, err := client.Subscribe(testListId, []string{email}, nil)
if err != nil {
t.Fatal(err)
}
if len(res) != 1 {
t.Fatal("Expected 1 result back from Subscribe call, please make sure that you are using single opt-in")
} else if res[0].Email != email {
t.Fatalf("Result email did not match input email")
}
}
func TestClient_Unsubscribe(t *testing.T) {
email := "dev@monstercat.com"
client := newTestClient()
err := client.Unsubscribe(testListId, []string{email}, nil, nil)
if err != nil {
t.Fatal(err)
}
}