forked from drone/go.stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
84 lines (79 loc) · 2.23 KB
/
error_test.go
File metadata and controls
84 lines (79 loc) · 2.23 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
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)
}
}
var (
// Cards from https://stripe.com/docs/testing
// These cards will be successfully charged.
goodCards = []string{
"4242424242424242",
"4012888888881881",
"5555555555554444",
"5105105105105100",
"378282246310005",
"371449635398431",
"6011111111111117",
"6011000990139424",
"30569309025904",
"38520000023237",
"3530111333300000",
"3566002020360505",
"4000000000000010",
"4000000000000028",
"4000000000000036",
"4000000000000044",
"4000000000000101",
}
// "These cards will produce specific responses that are useful for testing different scenarios"
badCardsAndErrorCodes = map[string]string{
"4000000000000341": ErrCodeCardDeclined,
"4000000000000002": ErrCodeCardDeclined,
"4000000000000127": ErrCodeIncorrectCVC,
"4000000000000069": ErrCodeExpiredCard,
"4000000000000119": ErrCodeProcessingError,
}
// Charge with only the required fields
charge = ChargeParams{
Desc: "Turkish Delight",
Amount: 300,
Currency: USD,
Card: &CardParams{
Name: "Orhan Pamuk",
//Number: "", // This gets changed per-test
ExpYear: time.Now().Year() + 1,
ExpMonth: 5,
},
}
)
// TestGoodCards ensures we can charge all of Stripe's "good" test cards.
func TestGoodCards(t *testing.T) {
for _, cardNumber := range goodCards {
charge.Card.Number = cardNumber
if _, err := Charges.Create(&charge); err != nil {
t.Errorf("Expected Successful Charge, got Error %s", err.Error())
}
}
}
// TestBadCards ensures we can't charge any of Stripe's "bad" test cards,
// and that the resulting error types and codes are correctly mapped.
func TestBadCards(t *testing.T) {
for cardNumber, errCode := range badCardsAndErrorCodes {
charge.Card.Number = cardNumber
_, err := Charges.Create(&charge)
stripeErr := err.(*Error)
if stripeErr.Detail.Type != ErrTypeCard {
t.Errorf("Expected Error Type %s, got %s", ErrTypeCard, stripeErr.Detail.Type)
}
if stripeErr.Detail.Code != errCode {
t.Errorf("Expected Error Code %s, got %s", errCode, stripeErr.Detail.Code)
}
}
}