Skip to content
This repository
tree: 12a21f3f44
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 211 lines (185 sloc) 6.16 kb
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
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: "test1@test.com",
Desc: "a test customer",
}

// Customer with all required fields + required credit card fields.
cust2 = CustomerParams{
Email: "test2@test.com",
Desc: "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: "test3@test.com",
Desc: "a 3rd test customer",
}

// A customer with the required fields + a credit card
cust4 = CustomerParams{
Email: "test3@test.com",
Desc: "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 string(cust.Email) != cust1.Email {
t.Errorf("Expected Customer Email %s, got %v", cust1.Email, cust.Email)
}
if string(cust.Desc) != cust1.Desc {
t.Errorf("Expected Customer Desc %s, got %v", cust1.Desc, cust.Desc)
}
}

// 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,
Desc: "Customer for site@stripe.com",
}

// 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 resp.Card == nil {
t.Errorf("Expected Customer Card from Token, got nil")
}
// Sanity check to make sure card was attached to customer
if string(resp.Card.Name) != string(token.Card.Name) {
t.Errorf("Expected Card Name %s, got %v", token.Card.Name, resp.Card.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.Retrieve(resp.Id)
if err != nil {
t.Errorf("Expected Customer, got Error %s", err.Error())
}
if string(cust.Email) != cust2.Email {
t.Errorf("Expected Customer Email %s, got %v", cust2.Email, cust.Email)
}
if string(cust.Desc) != cust2.Desc {
t.Errorf("Expected Customer Desc %s, got %v", cust2.Desc, cust.Desc)
}
if cust.Card == nil {
t.Errorf("Expected Credit Card %s, got nil", cust2.Card.Number)
return
}

if string(cust.Card.Name) != cust2.Card.Name {
t.Errorf("Expected Card Name %s, got %s", cust2.Card.Name, cust.Card.Name)
}
if cust.Card.Last4 != "4242" {
t.Errorf("Expected Card Last4 %d, got %d", "4242", cust.Card.Last4)
}
if cust.Card.ExpYear != cust2.Card.ExpYear {
t.Errorf("Expected Card ExpYear %d, got %d", cust2.Card.ExpYear, cust.Card.ExpYear)
}
if cust.Card.ExpMonth != cust2.Card.ExpMonth {
t.Errorf("Expected Card ExpMonth %d, got %d", cust2.Card.ExpMonth, cust.Card.ExpMonth)
}
if cust.Card.Type != Visa {
t.Errorf("Expected Card Type %s, got %s", Visa, cust.Card.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)

cust, err := Customers.Update(resp.Id, &CustomerParams{Email: "joe@email.com"})
if err != nil {
t.Errorf("Expected Customer update, got Error %s", err.Error())
}
if cust.Email != "joe@email.com" {
t.Errorf("Expected Updated Customer Email")
}
}

// 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.ListN(2, 0)
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 %s", len(customers))
}
}
Something went wrong with that request. Please try again.