-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
278 lines (233 loc) · 5.22 KB
/
integration_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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package pasdk
import (
"strings"
"testing"
"time"
)
// Run the suite of integration tests, if enabled.
func Test_IntegrationTests(t *testing.T) {
if !shouldRunIntegrationTests() {
return
}
Initialise(PAAuth{
APISecret: getTestAPISecret(),
APIKey: getTestAPIKey(),
APIURL: "https://api.demo.payassi.st/",
})
accountResponse := testAccount(t)
testPlan(t, *accountResponse)
testPreapproval(t)
beginResponse := testBegin(t)
testStatus(t, beginResponse.ApplicationToken)
testUpdate(t, beginResponse.ApplicationToken)
testCapture(t, beginResponse.ApplicationToken)
testInvoice(t, beginResponse.ApplicationToken)
}
func testUpdate(t *testing.T, token string) {
// Test only updating some fields.
amount := 80000
request := UpdateRequest{
ApplicationToken: token,
Amount: &amount,
}
response, err := request.Fetch()
if err != nil {
t.Error(err)
}
if *response.Amount != 80000 {
t.Error()
}
if response.ExpiresIn != nil {
t.Error()
}
if response.OrderID != nil {
t.Error()
}
// Test updating most fields. We can't easily test updating order ID
// because that requires a completed application.
amount = 70000
expiresIn := 60 * 10
request = UpdateRequest{
ApplicationToken: token,
Amount: &amount,
ExpiresIn: &expiresIn,
}
response, err = request.Fetch()
if err != nil {
t.Error(err)
}
if *response.Amount != 70000 {
t.Error()
}
if *response.ExpiresIn != 600 {
t.Error()
}
if response.OrderID != nil {
t.Error()
}
}
func testStatus(t *testing.T, token string) {
request := StatusRequest{
ApplicationToken: token,
}
response, err := request.Fetch()
if err != nil {
t.Error(err)
}
if response.Amount != 100000 {
t.Error()
}
if response.ExpiresAt.Before(time.Now()) ||
response.ExpiresAt.After(time.Now().Add(time.Hour*25)) {
t.Error(response.ExpiresAt)
}
if response.ApplicationToken != token {
t.Error()
}
if response.HasInvoice != false {
t.Error()
}
if response.RequriesInvoice != false {
t.Error()
}
if response.Status != "pending" {
t.Error(response.Status)
}
}
func testPlan(t *testing.T, accountInfo AccountResponse) {
request := PlanRequest{
Amount: 50000,
PlanID: &accountInfo.Plans[0].ID,
}
response, err := request.Fetch()
if err != nil {
t.Error(err)
}
if response.Amount != 50000 {
t.Error()
}
if response.Interest != 0 {
t.Error()
}
if response.PlanName != accountInfo.Plans[0].Name {
t.Error()
}
if response.TotalRepayable != 50000 {
t.Error()
}
if len(response.PaymentSchedule) != 4 {
t.Error()
}
if response.PaymentSchedule[3].Amount <= 0 {
t.Error()
}
if response.PaymentSchedule[3].Date.Before(time.Now()) {
t.Error()
}
if response.PaymentSchedule[3].Date.After(time.Now().AddDate(0, 5, 0)) {
t.Error()
}
}
func testPreapproval(t *testing.T) {
request := PreapprovalRequest{
CustomerFirstName: "Test",
CustomerLastName: "Testington",
CustomerAddress1: "Test House",
CustomerPostcode: "TEST TES",
}
response, err := request.Fetch()
if err != nil {
t.Error(err)
}
if !response.Approved {
t.Error()
}
}
func testCapture(t *testing.T, token string) {
request := CaptureRequest{
ApplicationToken: token,
}
_, err := request.Fetch()
// This is the closest we can get to testing it because only a completed application
// can be captured.
if !strings.Contains(err.Error(), `{"status":"error","msg":"Application is not awaiting capture","data":[]}`) {
t.Error()
}
}
func testInvoice(t *testing.T, token string) {
request := InvoiceRequest{
ApplicationToken: token,
FileType: "txt",
FileData: []byte("Test invoice"),
}
response, err := request.Fetch()
// Only a completed application can be invoiced so we are expecting this to fail.
if !strings.Contains(err.Error(), "Application is not yet completed") {
t.Error(err.Error())
}
if response != nil {
t.Error()
}
}
func testBegin(t *testing.T) *BeginResponse {
falseValue := false
request := BeginRequest{
OrderID: getRandomID(),
Amount: 100000,
CustomerFirstName: "Test",
CustomerLastName: "Testington",
CustomerAddress1: "Test House",
CustomerPostcode: "TEST TES",
EnableAutoCapture: &falseValue,
}
response, err := request.Fetch()
if err != nil {
t.Error(err)
}
if len(response.ApplicationToken) != 36 {
t.Error()
}
if len(response.ContinuationURL) < 10 {
t.Error()
}
return response
}
func testAccount(t *testing.T) *AccountResponse {
request := AccountRequest{}
accountResponse, err := request.Fetch()
if err != nil {
t.Error(err)
}
if len(accountResponse.DisplayName) == 0 {
t.Error()
}
if len(accountResponse.LegalName) == 0 {
t.Error()
}
if len(accountResponse.Plans[0].APR) == 0 {
t.Error()
}
if len(accountResponse.Plans[0].CommissionRate) == 0 {
t.Error()
}
if len(accountResponse.Plans[0].Frequency) == 0 {
t.Error()
}
if len(accountResponse.Plans[0].Name) == 0 {
t.Error()
}
if accountResponse.Plans[0].DepositRequired == false {
t.Error()
}
if accountResponse.Plans[0].ID == 0 {
t.Error()
}
if accountResponse.Plans[0].Instalments == 0 {
t.Error()
}
if accountResponse.Plans[0].MaxAmount == nil ||
*accountResponse.Plans[0].MaxAmount <= 0 {
t.Error()
}
return accountResponse
}