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
/
subscription_test.go
178 lines (149 loc) · 4.77 KB
/
subscription_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
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 Subscriptions to use for testing
var (
// Subscriptions with only the required fields
sub1 = SubscriptionParams{
Plan: "plan1",
}
// Subscriptions with all fields, plus new Credit Card
sub2 = SubscriptionParams{
Plan: "plan1",
Coupon: "test coupon 1",
TrialEnd: &UnixTime{time.Now().Add(24 * time.Hour)},
Quantity: 5,
Card: &CardParams{
Name: "George Costanza",
Number: "4242424242424242",
ExpYear: time.Now().Year() + 1,
ExpMonth: 6,
},
}
)
func TestCreateSubscription(t *testing.T) {
// Create the customer, and defer its deletion
cust, _ := Customers.Create(&cust1)
defer Customers.Delete(cust.ID)
// Create the plan, and defer its deletion
Plans.Create(&p1)
defer Customers.Delete(p1.ID)
// Subscribe the Customer to the Plan
resp, err := Subscriptions.Create(cust.ID, &sub1)
if err != nil {
t.Errorf("Expected Subscription, got error %s", err.Error())
}
if resp.Customer != cust.ID {
t.Errorf("Expected Customer %s, got %s", cust.ID, resp.Customer)
}
if resp.Status != SubscriptionActive {
t.Errorf("Expected Active Subscription, got %s", resp.Status)
}
}
func TestCreateSubscriptionCard(t *testing.T) {
// Create the customer, and defer its deletion
cust, _ := Customers.Create(&cust1)
defer Customers.Delete(cust.ID)
if cust.DefaultCard != "" {
t.Errorf("Expected Customer to be created with a nil card")
return
}
// Create the plan, and defer its deletion
Plans.Create(&p1)
defer Customers.Delete(p1.ID)
// Create the coupon, and defer its deletion
Coupons.Create(&c1)
defer Coupons.Delete(c1.ID)
// Subscribe a Customer to a new plan, using a new Credit Card
resp, err := Subscriptions.Create(cust.ID, &sub2)
if err != nil {
t.Errorf("Expected Subscription, got error %s", err.Error())
}
if resp.Quantity != sub2.Quantity {
t.Errorf("Expected Quantity %d, got %d", sub2.Quantity, resp.Quantity)
}
// Check to see if the customer's card was added
cust, _ = Customers.Get(cust.ID)
if cust.DefaultCard == "" {
t.Errorf("Expected Subscription to assign a new active customer card")
}
}
func TestCreateSubscriptionToken(t *testing.T) {
// Create the customer, and defer its deletion
cust, _ := Customers.Create(&cust1)
defer Customers.Delete(cust.ID)
if cust.DefaultCard != "" {
t.Errorf("Expected Customer to be created with a nil card")
return
}
// Create the plan, and defer its deletion
Plans.Create(&p1)
defer Customers.Delete(p1.ID)
// Create a Token for the credit card
token, _ := Tokens.Create(&token1)
// Subscribe the Customer to the Plan, using the Token
params := SubscriptionParams{Plan: "plan1", Token: token.ID}
_, err := Subscriptions.Create(cust.ID, ¶ms)
if err != nil {
t.Errorf("Expected Subscription with Token, got error %s", err.Error())
}
// Check to see if the customer's card was added
cust, _ = Customers.Get(cust.ID)
if cust.DefaultCard == "" {
t.Errorf("Expected Subscription to assign a new active customer card")
}
}
func TestCancelSubscription(t *testing.T) {
// Create the customer, and defer its deletion
cust, _ := Customers.Create(&cust1)
defer Customers.Delete(cust.ID)
// Create the plan, and defer its deletion
Plans.Create(&p1)
defer Customers.Delete(p1.ID)
// Subscribe the Customer to the Plan
sub, err := Subscriptions.Create(cust.ID, &sub1)
if err != nil {
t.Errorf("Expected Subscription, got error %s", err.Error())
}
// Now cancel the subscription
subs, err := Subscriptions.Cancel(cust.ID, sub.ID, false)
if err != nil {
t.Errorf("Expected Subscription Cancellation, got error %s", err.Error())
}
if subs.Status != SubscriptionCanceled {
t.Errorf("Expected Subscription Status %s, got %s", SubscriptionCanceled, subs.Status)
}
}
func TestCancelSubscriptionAtPeriodEnd(t *testing.T) {
// Create the customer, and defer its deletion
cust, _ := Customers.Create(&cust1)
defer Customers.Delete(cust.ID)
// Create the plan, and defer its deletion
Plans.Create(&p1)
defer Customers.Delete(p1.ID)
// Subscribe the Customer to the Plan
sub, err := Subscriptions.Create(cust.ID, &sub1)
if err != nil {
t.Errorf("Expected Subscription, got error %s", err.Error())
}
// Now cancel the subscription
subs, err := Subscriptions.Cancel(cust.ID, sub.ID, true)
if err != nil {
t.Errorf("Expected Subscription Cancellation, got error %s", err.Error())
}
if subs.Status != SubscriptionActive {
t.Errorf("Expected Subscription Status %s, got %s", SubscriptionCanceled, subs.Status)
}
if subs.CancelAtPeriodEnd != true {
t.Errorf("Expected CancelAtPeriodEnd to be %t, got %t", true, subs.CancelAtPeriodEnd)
}
}