This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
customer_test.go
214 lines (189 loc) · 6.32 KB
/
customer_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
package stripe
import (
"testing"
"time"
)
func init() {
// In order to execute Unit Test, you must set your Stripe API Key as
// environment variable, STRIPE_API_KEY=xxxx
if err := SetKeyEnv(); err != nil {
panic(err)
}
}
// Sample Customers to use when creating, deleting, updating Customer data.
var (
// Customer with only the required fields
cust1 = CustomerParams{
Email: "[email protected]",
Description: "a test customer",
}
// Customer with all required fields + required credit card fields.
cust2 = CustomerParams{
Email: "[email protected]",
Description: "a 2nd test customer",
Coupon: c1.ID,
Plan: p1.ID,
Card: &CardParams{
Name: "John Smith",
Number: "4242424242424242",
ExpYear: time.Now().Year() + 1,
ExpMonth: 1,
},
}
// Another Customer with only the required fields
cust3 = CustomerParams{
Email: "[email protected]",
Description: "a 3rd test customer",
}
// A customer with the required fields + a credit card
cust4 = CustomerParams{
Email: "[email protected]",
Description: "a 3rd test customer",
Card: &CardParams{
Name: "John Smith",
Number: "4242424242424242",
ExpYear: time.Now().Year() + 1,
ExpMonth: 1,
},
}
)
// TestCreateCustomer will test that we can successfully Create a Customer,
// parse the JSON reponse from Stripe, and that all values are populated as
// expected.
func TestCreateCustomer(t *testing.T) {
// Create the customer, and defer its deletion
cust, err := Customers.Create(&cust1)
defer Customers.Delete(cust.ID)
if err != nil {
t.Errorf("Expected Customer, got Error %s", err.Error())
}
if cust.Email != cust1.Email {
t.Errorf("Expected Customer Email %s, got %v", cust1.Email, cust.Email)
}
if cust.Description != cust1.Description {
t.Errorf("Expected Customer Desc %s, got %v", cust1.Description, cust.Description)
}
}
// TestCreateCustomerToken will test that we can successfully Create a Customer
// using a credit card Token.
func TestCreateCustomerToken(t *testing.T) {
// Create a Token for the credit card
token, _ := Tokens.Create(&token1)
// Create a Charge that uses a Token
cust := CustomerParams{
Token: token.ID,
Description: "Customer for [email protected]",
}
// Create the charge
resp, err := Customers.Create(&cust)
defer Customers.Delete(resp.ID)
if err != nil {
t.Errorf("Expected Create Customer, got Error %s", err.Error())
}
if len(resp.Cards.Data) == 0 {
t.Errorf("Expected Customer Card from Token, got nil")
}
// Sanity check to make sure card was attached to customer
if resp.Cards.Data[0].Name != token.Card.Name {
t.Errorf("Expected Card Name %s, got %v", token.Card.Name, resp.Cards.Data[0].Name)
}
}
// TestRetrieveCustomer will test that we can successfully Retrieve a Customer,
// parse the JSON response, and that all values are populated as expected.
func TestRetrieveCustomer(t *testing.T) {
// setup default plans and coupons, defer deletion
Plans.Create(&p1)
Coupons.Create(&c1)
defer Plans.Delete(p1.ID)
defer Coupons.Delete(c1.ID)
// Create the customer, and defer its deletion
resp, err := Customers.Create(&cust2)
defer Customers.Delete(resp.ID)
if err != nil {
t.Errorf("Expected Customer, got Error %s", err.Error())
return
}
// Retrieve the Customer by ID
cust, err := Customers.Get(resp.ID)
if err != nil {
t.Errorf("Expected Customer, got Error %s", err.Error())
}
if cust.Email != cust2.Email {
t.Errorf("Expected Customer Email %s, got %v", cust2.Email, cust.Email)
}
if cust.Description != cust2.Description {
t.Errorf("Expected Customer Desc %s, got %v", cust2.Description, cust.Description)
}
if len(cust.Cards.Data) == 0 {
t.Errorf("Expected Credit Card %s, got nil", cust2.Card.Number)
return
}
if cust.Cards.Data[0].Name != cust2.Card.Name {
t.Errorf("Expected Card Name %s, got %s", cust2.Card.Name, cust.Cards.Data[0].Name)
}
if cust.Cards.Data[0].Last4 != "4242" {
t.Errorf("Expected Card Last4 %s, got %s", "4242", cust.Cards.Data[0].Last4)
}
if cust.Cards.Data[0].ExpYear != cust2.Card.ExpYear {
t.Errorf("Expected Card ExpYear %d, got %d", cust2.Card.ExpYear, cust.Cards.Data[0].ExpYear)
}
if cust.Cards.Data[0].ExpMonth != cust2.Card.ExpMonth {
t.Errorf("Expected Card ExpMonth %d, got %d", cust2.Card.ExpMonth, cust.Cards.Data[0].ExpMonth)
}
if cust.Cards.Data[0].Type != Visa {
t.Errorf("Expected Card Type %s, got %s", Visa, cust.Cards.Data[0].Type)
}
}
// TestUpdateCustomer will test that we can successfully update a Customer,
// parse the JSON reponse, and verify the updated name was returned.
func TestUpdateCustomer(t *testing.T) {
// Create the Customer, and defer its deletion
resp, _ := Customers.Create(&cust1)
defer Customers.Delete(resp.ID)
balance := -100
cust, err := Customers.Update(resp.ID, &CustomerParams{Email: "[email protected]", Balance: &balance})
if err != nil {
t.Errorf("Expected Customer update, got Error %s", err.Error())
}
if cust.Email != "[email protected]" {
t.Errorf("Expected Updated Customer Email")
}
if cust.Balance != balance {
t.Errorf("Expected Updated Customer Balance")
}
}
// TestDeleteCustomer will test that we can successfully remove a Customer,
// parse the JSON reponse, and that the deletion flag is captured as a boolean
// value.
func TestDeleteCustomer(t *testing.T) {
// Create the Customer, and defer its deletion
resp, _ := Customers.Create(&cust1)
defer Customers.Delete(resp.ID)
// let's try to delete the customer
ok, err := Customers.Delete(resp.ID)
if err != nil {
t.Errorf("Expected Customer deletion, got Error %s", err.Error())
}
if !ok {
t.Errorf("Expected Customer deleted true, got false")
}
}
// TestListCustomers will test that we can successfully retrieve a list of
// Customers, parse the JSON reponse, and that the length of the coupon array
// matches our expectations.
func TestListCustomers(t *testing.T) {
// create 2 dummy customers that we can retrieve
resp1, _ := Customers.Create(&cust1)
resp2, _ := Customers.Create(&cust3)
defer Customers.Delete(resp1.ID)
defer Customers.Delete(resp2.ID)
// get the list from Stripe
customers, _, err := Customers.List(2, "", "")
if err != nil {
t.Errorf("Expected Customer List, got Error %s", err.Error())
}
// since we added 2 dummy customers, we expect the array to be a size of 2
if len(customers) != 2 {
t.Errorf("Expected 2 Customers, got %d", len(customers))
}
}