-
Notifications
You must be signed in to change notification settings - Fork 44
/
credit_payments_test.go
121 lines (106 loc) · 3.7 KB
/
credit_payments_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
package recurly_test
import (
"context"
"encoding/xml"
"net/http"
"testing"
"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
)
func TestCreditPayments_List(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
var invocations int
s.HandleFunc("GET", "/v2/credit_payments", func(w http.ResponseWriter, r *http.Request) {
invocations++
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("credit_payments.xml"))
}, t)
pager := client.CreditPayments.List(nil)
for pager.Next() {
var pmts []recurly.CreditPayment
if err := pager.Fetch(context.Background(), &pmts); err != nil {
t.Fatal(err)
} else if !s.Invoked {
t.Fatal("expected s to be invoked")
} else if diff := cmp.Diff(pmts, []recurly.CreditPayment{*NewTestCreditPayment()}); diff != "" {
t.Fatal(diff)
}
}
if invocations != 1 {
t.Fatalf("unexpected number of invocations: %d", invocations)
}
}
func TestCreditPayments_ListAccount(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
var invocations int
s.HandleFunc("GET", "/v2/accounts/1/credit_payments", func(w http.ResponseWriter, r *http.Request) {
invocations++
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("credit_payments.xml"))
}, t)
pager := client.CreditPayments.ListAccount("1", nil)
for pager.Next() {
var pmts []recurly.CreditPayment
if err := pager.Fetch(context.Background(), &pmts); err != nil {
t.Fatal(err)
} else if !s.Invoked {
t.Fatal("expected s to be invoked")
} else if diff := cmp.Diff(pmts, []recurly.CreditPayment{*NewTestCreditPayment()}); diff != "" {
t.Fatal(diff)
}
}
if invocations != 1 {
t.Fatalf("unexpected number of invocations: %d", invocations)
}
}
func TestCreditPayments_Get(t *testing.T) {
t.Run("OK", func(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("GET", "/v2/credit_payments/2cc95aa62517e56d5bec3a48afa1b3b9", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("credit_payment.xml"))
}, t)
if pmt, err := client.CreditPayments.Get(context.Background(), "2cc95aa62517e56d5bec3a48afa1b3b9"); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(pmt, NewTestCreditPayment()); diff != "" {
t.Fatal(diff)
} else if !s.Invoked {
t.Fatal("expected fn invocation")
}
})
// Ensure a 404 returns nil values.
t.Run("ErrNotFound", func(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
s.HandleFunc("GET", "/v2/credit_payments/2cc95aa62517e56d5bec3a48afa1b3b9", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}, t)
if pmt, err := client.CreditPayments.Get(context.Background(), "2cc95aa62517e56d5bec3a48afa1b3b9"); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
} else if pmt != nil {
t.Fatalf("expected nil: %#v", pmt)
}
})
}
// Returns a CreditPayment corresponding to testdata/credit_payment.xml.
func NewTestCreditPayment() *recurly.CreditPayment {
return &recurly.CreditPayment{
XMLName: xml.Name{Local: "credit_payment"},
UUID: "3d3f6754c6df41b9d2a32e43029adc55",
AccountCode: "3465345645345",
Action: recurly.CreditPaymentActionRefund,
Currency: "USD",
AmountInCents: 1000,
OriginalInvoiceNumber: 1000,
AppliedToInvoice: 1000,
OriginalCreditPaymentUUID: "3d3f6754c6df41b9d2a32e43029adc55",
RefundTransactionUUID: "3e823e405e7f752988536947c08349ae",
CreatedAt: recurly.NewTime(MustParseTime("2017-07-06T15:51:38Z")),
UpdatedAt: recurly.NewTime(MustParseTime("2017-07-06T15:51:38Z")),
}
}