forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
108 lines (98 loc) · 3.1 KB
/
errors_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
package braintree
import (
"encoding/xml"
"testing"
)
var errorXML = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<api-error-response>
<errors>
<errors type="array"/>
<transaction>
<errors type="array">
<error>
<code>81502</code>
<attribute type="symbol">amount</attribute>
<message>Amount is required.</message>
</error>
<error>
<code>91526</code>
<attribute type="symbol">custom_fields</attribute>
<message>Custom field is invalid: store_me.</message>
</error>
<error>
<code>91513</code>
<attribute type="symbol">merchant_account_id</attribute>
<message>Merchant account ID is invalid.</message>
</error>
</errors>
<credit-card>
<errors type="array">
<error>
<code>91708</code>
<attribute type="symbol">base</attribute>
<message>Cannot provide expiration_date if you are also providing expiration_month and expiration_year.</message>
</error>
<error>
<code>81714</code>
<attribute type="symbol">number</attribute>
<message>Credit card number is required.</message>
</error>
<error>
<code>81725</code>
<attribute type="symbol">base</attribute>
<message>Credit card must include either number or venmo_sdk_payment_method_code.</message>
</error>
<error>
<code>81703</code>
<attribute type="symbol">number</attribute>
<message>Credit card type is not accepted by this merchant account.</message>
</error>
</errors>
</credit-card>
<customer>
<errors type="array">
<error>
<code>81606</code>
<attribute type="symbol">email</attribute>
<message>Email is an invalid format.</message>
</error>
</errors>
</customer>
</transaction>
</errors>
<message>Everything is broken!</message>
</api-error-response>`)
func TestErrorsUnmarshalEverything(t *testing.T) {
apiErrors := &BraintreeError{}
err := xml.Unmarshal(errorXML, apiErrors)
if err != nil {
t.Fatal("Error unmarshalling: " + err.Error())
}
allErrors := apiErrors.All()
if len(allErrors) != 8 {
t.Fatal("Did not get all errors")
}
}
func TestAccessors(t *testing.T) {
apiErrors := &BraintreeError{}
err := xml.Unmarshal(errorXML, apiErrors)
if err != nil {
t.Fatal("Error unmarshalling: " + err.Error())
}
ccErrors := apiErrors.For("Transaction").On("CreditCard")
if len(ccErrors) != 4 {
t.Fatal("Did not get the right credit card errors")
}
numberErrors := apiErrors.For("Transaction").For("CreditCard").On("Number")
if len(numberErrors) != 2 {
t.Fatal("Did not get the right number errors")
}
customerErrors := apiErrors.For("Transaction").On("Customer")
if len(customerErrors) != 1 {
t.Fatal("Did not get the right customer errors")
}
baseErrors := apiErrors.For("Transaction").On("Base")
if len(baseErrors) != 3 {
t.Fatal("Did not get the right base errors")
}
}