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
/
plan_test.go
169 lines (150 loc) · 4.86 KB
/
plan_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
package stripe
import (
"strings"
"testing"
)
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 Plans to use when creating, deleting, updating Plan data.
var (
// Plan with only the required fields
p1 = PlanParams{
ID: "plan1",
Name: "plan 1",
Amount: 1,
Currency: USD,
Interval: IntervalMonth,
}
// Plan with all required + optional fields.
p2 = PlanParams{
ID: "plan9",
Name: "plan 9",
Amount: 9,
Currency: USD,
Interval: IntervalMonth,
TrialPeriodDays: 365,
}
)
// TestCreatePlan will test that we can successfully Create a plan, parse
// the JSON reponse from Stripe, and that all values are populated as expected.
//
// Second, we will test that error handling works correctly by attempting to
// create a duplicate Plan, create a Plan with invalid currency, which should
// throw exceptions.
func TestCreatePlan(t *testing.T) {
// Create the plan, and defer its deletion
plan, err := Plans.Create(&p1)
defer Plans.Delete(p1.ID)
if err != nil {
t.Errorf("Expected Plan %s, got Error %s", p1.ID, err.Error())
}
if plan.ID != plan.ID {
t.Errorf("Expected Plan ID %s, got %s", p1.ID, plan.ID)
}
if plan.Name != p1.Name {
t.Errorf("Expected Plan Name %v, got %v", p1.Name, plan.Name)
}
if plan.Amount != p1.Amount {
t.Errorf("Expected Plan Amount %v, got %v", p1.Amount, plan.Amount)
}
if plan.Currency != p1.Currency {
t.Errorf("Expected Plan Currency %v, got %v", p1.Currency, plan.Currency)
}
// Now try to re-create the existing plan, which should throw an exception
_, err = Plans.Create(&p1)
if err == nil {
t.Error("Expected non-null Error when creating a duplicate Plan.")
} else if err.Error() != "Plan already exists." {
t.Errorf("Expected %s, got %s", "Plan already exists.", err.Error())
}
// Now use an invalid currency, which should throw an exception
var p3 PlanParams
p3 = p1
p3.Currency = "XXX"
_, err = Plans.Create(&p3)
if err == nil {
t.Error("Expected non-null Error when using an Invalid Currency.")
} else if strings.HasPrefix(err.Error(), "Invalid currency: xxx.") == false {
t.Errorf("Expected %s, got %s", "Invalid currency: xxx.", err.Error())
}
}
// TestRetrievePlan will test that we can successfully Retrieve a Plan,
// parse the JSON response, and that all values are populated as expected.
func TestRetrievePlan(t *testing.T) {
// Create the plan, and defer its deletion
Plans.Create(&p2)
defer Plans.Delete(p2.ID)
// Retrieve the Plan by ID
plan, err := Plans.Get(p2.ID)
if err != nil {
t.Errorf("Expected Plan %s, got Error %s", p2.ID, err.Error())
}
if plan.ID != plan.ID {
t.Errorf("Expected Plan ID %s, got %s", p2.ID, plan.ID)
}
if plan.Name != p2.Name {
t.Errorf("Expected Plan Name %v, got %v", p2.Name, plan.Name)
}
if plan.Amount != p2.Amount {
t.Errorf("Expected Plan Amount %v, got %v", p2.Amount, plan.Amount)
}
if plan.Currency != p2.Currency {
t.Errorf("Expected Plan Currency %v, got %v", p2.Currency, plan.Currency)
}
if plan.TrialPeriodDays != p2.TrialPeriodDays {
t.Errorf("Expected Plan Trial Period %v, got %v",
p2.TrialPeriodDays, plan.TrialPeriodDays)
}
}
// TestUpdatePlan will test that we can successfully update a Plan's name, parse
// the JSON reponse, and verify the updated name was returned.
func TestUpdatePlan(t *testing.T) {
// Create the plan, and defer its deletion
Plans.Create(&p1)
defer Plans.Delete(p1.ID)
plan, err := Plans.Update(p1.ID, &PlanParams{Name: "New Name"})
if err != nil {
t.Errorf("Expected Plan update, got Error %s", err.Error())
}
if plan.Name != "New Name" {
t.Errorf("Expected Updated Plan Name %v, got %v", p1.Name, plan.Name)
}
}
// TestDeletePlan will test that we can successfully remove a Plan, parse
// the JSON reponse, and that the deletion flag is captured as a boolean value.
func TestDeletePlan(t *testing.T) {
// create a Plan that we can delete
Plans.Create(&p1)
// let's try to delete the plan
ok, err := Plans.Delete(p1.ID)
if err != nil {
t.Errorf("Expected Plan deletion, got Error %s", err.Error())
}
if !ok {
t.Errorf("Expected Plan deletion true, got false")
}
}
// TestListPlan will test that we can successfully retrieve a list of Plans,
// parse the JSON reponse, and that the length of the coupon array matches our
// expectations.
func TestListPlan(t *testing.T) {
// create 2 dummy plans that we can retrieve
Plans.Create(&p1)
Plans.Create(&p2)
defer Plans.Delete(p1.ID)
defer Plans.Delete(p2.ID)
// get the list from Stripe
plans, _, err := Plans.List(10, "", "")
if err != nil {
t.Errorf("Expected Plan List, got Error %s", err.Error())
}
// since we added 2 dummy plans, we expect the array to be a size of 2
if len(plans) != 2 {
t.Errorf("Expected 2 Plans, got %d", len(plans))
}
}