1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
"net/http" |
7
|
|
|
"testing" |
8
|
|
|
|
9
|
|
|
"github.com/VictorAvelar/mollie-api-go/v4/testdata" |
10
|
|
|
"github.com/stretchr/testify/assert" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
func TestPaymentLinkService_Get(t *testing.T) { |
14
|
|
|
setEnv() |
15
|
|
|
defer unsetEnv() |
16
|
|
|
|
17
|
|
|
type args struct { |
18
|
|
|
ctx context.Context |
19
|
|
|
paymentLink string |
20
|
|
|
opts *PaymentLinkOptions |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
cases := []struct { |
24
|
|
|
name string |
25
|
|
|
args args |
26
|
|
|
wantErr bool |
27
|
|
|
err error |
28
|
|
|
pre func() |
29
|
|
|
handler http.HandlerFunc |
30
|
|
|
}{ |
31
|
|
|
{ |
32
|
|
|
"get payment links works as expected.", |
33
|
|
|
args{ |
34
|
|
|
context.Background(), |
35
|
|
|
"pl_ka21123129", |
36
|
|
|
nil, |
37
|
|
|
}, |
38
|
|
|
false, |
39
|
|
|
nil, |
40
|
|
|
noPre, |
41
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
42
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
43
|
|
|
testMethod(t, r, "GET") |
44
|
|
|
|
45
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
46
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
47
|
|
|
} |
48
|
|
|
_, _ = w.Write([]byte(testdata.GetPaymentLinkResponse)) |
49
|
|
|
}, |
50
|
|
|
}, |
51
|
|
|
{ |
52
|
|
|
"get payment links, an error is returned from the server", |
53
|
|
|
args{ |
54
|
|
|
context.Background(), |
55
|
|
|
"pl_ka21123129", |
56
|
|
|
nil, |
57
|
|
|
}, |
58
|
|
|
true, |
59
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
60
|
|
|
noPre, |
61
|
|
|
errorHandler, |
62
|
|
|
}, |
63
|
|
|
{ |
64
|
|
|
"get payment links, an error occurs when parsing json", |
65
|
|
|
args{ |
66
|
|
|
context.Background(), |
67
|
|
|
"pl_ka21123129", |
68
|
|
|
nil, |
69
|
|
|
}, |
70
|
|
|
true, |
71
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
72
|
|
|
noPre, |
73
|
|
|
encodingHandler, |
74
|
|
|
}, |
75
|
|
|
{ |
76
|
|
|
"get payment links, invalid url when building request", |
77
|
|
|
args{ |
78
|
|
|
context.Background(), |
79
|
|
|
"pl_ka21123129", |
80
|
|
|
nil, |
81
|
|
|
}, |
82
|
|
|
true, |
83
|
|
|
errBadBaseURL, |
84
|
|
|
crashSrv, |
85
|
|
|
errorHandler, |
86
|
|
|
}, |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
for _, c := range cases { |
90
|
|
|
setup() |
91
|
|
|
defer teardown() |
92
|
|
|
|
93
|
|
|
t.Run(c.name, func(t *testing.T) { |
94
|
|
|
c.pre() |
95
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/payment-links/%s", c.args.paymentLink), c.handler) |
96
|
|
|
|
97
|
|
|
res, m, err := tClient.PaymentLinks.Get(c.args.ctx, c.args.paymentLink) |
98
|
|
|
if c.wantErr { |
99
|
|
|
assert.NotNil(t, err) |
100
|
|
|
assert.EqualError(t, err, c.err.Error()) |
101
|
|
|
} else { |
102
|
|
|
assert.Nil(t, err) |
103
|
|
|
assert.IsType(t, &PaymentLink{}, m) |
104
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
105
|
|
|
} |
106
|
|
|
}) |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
func TestPaymentLinkService_Create(t *testing.T) { |
111
|
|
|
setEnv() |
112
|
|
|
defer unsetEnv() |
113
|
|
|
|
114
|
|
|
type args struct { |
115
|
|
|
ctx context.Context |
116
|
|
|
paymentLink PaymentLink |
117
|
|
|
options *PaymentLinkOptions |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
cases := []struct { |
121
|
|
|
name string |
122
|
|
|
args args |
123
|
|
|
wantErr bool |
124
|
|
|
err error |
125
|
|
|
pre func() |
126
|
|
|
handler http.HandlerFunc |
127
|
|
|
}{ |
128
|
|
|
{ |
129
|
|
|
"create payment links works as expected.", |
130
|
|
|
args{ |
131
|
|
|
context.Background(), |
132
|
|
|
PaymentLink{ |
133
|
|
|
Amount: Amount{Value: "20.00", Currency: "EUR"}, |
134
|
|
|
}, |
135
|
|
|
&PaymentLinkOptions{ProfileID: "prf_12312312"}, |
136
|
|
|
}, |
137
|
|
|
false, |
138
|
|
|
nil, |
139
|
|
|
noPre, |
140
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
141
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
142
|
|
|
testMethod(t, r, "POST") |
143
|
|
|
testQuery(t, r, "profileId=prf_12312312") |
144
|
|
|
|
145
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
146
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
147
|
|
|
} |
148
|
|
|
_, _ = w.Write([]byte(testdata.CreatePaymentLinkResponse)) |
149
|
|
|
}, |
150
|
|
|
}, |
151
|
|
|
{ |
152
|
|
|
"create payment links, an error is returned from the server", |
153
|
|
|
args{ |
154
|
|
|
context.Background(), |
155
|
|
|
PaymentLink{}, |
156
|
|
|
nil, |
157
|
|
|
}, |
158
|
|
|
true, |
159
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
160
|
|
|
noPre, |
161
|
|
|
errorHandler, |
162
|
|
|
}, |
163
|
|
|
{ |
164
|
|
|
"create payment links, an error occurs when parsing json", |
165
|
|
|
args{ |
166
|
|
|
context.Background(), |
167
|
|
|
PaymentLink{}, |
168
|
|
|
nil, |
169
|
|
|
}, |
170
|
|
|
true, |
171
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
172
|
|
|
noPre, |
173
|
|
|
encodingHandler, |
174
|
|
|
}, |
175
|
|
|
{ |
176
|
|
|
"create payment links, invalid url when building request", |
177
|
|
|
args{ |
178
|
|
|
context.Background(), |
179
|
|
|
PaymentLink{}, |
180
|
|
|
nil, |
181
|
|
|
}, |
182
|
|
|
true, |
183
|
|
|
errBadBaseURL, |
184
|
|
|
crashSrv, |
185
|
|
|
errorHandler, |
186
|
|
|
}, |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
for _, c := range cases { |
190
|
|
|
setup() |
191
|
|
|
defer teardown() |
192
|
|
|
|
193
|
|
|
t.Run(c.name, func(t *testing.T) { |
194
|
|
|
c.pre() |
195
|
|
|
tMux.HandleFunc("/v2/payment-links", c.handler) |
196
|
|
|
|
197
|
|
|
res, m, err := tClient.PaymentLinks.Create(c.args.ctx, c.args.paymentLink, c.args.options) |
198
|
|
|
if c.wantErr { |
199
|
|
|
assert.NotNil(t, err) |
200
|
|
|
assert.EqualError(t, err, c.err.Error()) |
201
|
|
|
} else { |
202
|
|
|
assert.Nil(t, err) |
203
|
|
|
assert.IsType(t, &PaymentLink{}, m) |
204
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
205
|
|
|
} |
206
|
|
|
}) |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
func TestPaymentLinkService_List(t *testing.T) { |
211
|
|
|
setEnv() |
212
|
|
|
defer unsetEnv() |
213
|
|
|
|
214
|
|
|
type args struct { |
215
|
|
|
ctx context.Context |
216
|
|
|
opts *PaymentLinkOptions |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
cases := []struct { |
220
|
|
|
name string |
221
|
|
|
args args |
222
|
|
|
wantErr bool |
223
|
|
|
err error |
224
|
|
|
pre func() |
225
|
|
|
handler http.HandlerFunc |
226
|
|
|
}{ |
227
|
|
|
{ |
228
|
|
|
"list payment links works as expected.", |
229
|
|
|
args{ |
230
|
|
|
context.Background(), |
231
|
|
|
nil, |
232
|
|
|
}, |
233
|
|
|
false, |
234
|
|
|
nil, |
235
|
|
|
noPre, |
236
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
237
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
238
|
|
|
testMethod(t, r, "GET") |
239
|
|
|
|
240
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
241
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
242
|
|
|
} |
243
|
|
|
_, _ = w.Write([]byte(testdata.ListPaymentLinksResponse)) |
244
|
|
|
}, |
245
|
|
|
}, |
246
|
|
|
{ |
247
|
|
|
"list payment links with options works as expected.", |
248
|
|
|
args{ |
249
|
|
|
context.Background(), |
250
|
|
|
&PaymentLinkOptions{ |
251
|
|
|
ProfileID: "pfl_11211", |
252
|
|
|
}, |
253
|
|
|
}, |
254
|
|
|
false, |
255
|
|
|
nil, |
256
|
|
|
noPre, |
257
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
258
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
259
|
|
|
testMethod(t, r, "GET") |
260
|
|
|
testQuery(t, r, "profileId=pfl_11211") |
261
|
|
|
|
262
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
263
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
264
|
|
|
} |
265
|
|
|
_, _ = w.Write([]byte(testdata.ListPaymentLinksResponse)) |
266
|
|
|
}, |
267
|
|
|
}, |
268
|
|
|
{ |
269
|
|
|
"list payment links, an error is returned from the server", |
270
|
|
|
args{ |
271
|
|
|
context.Background(), |
272
|
|
|
nil, |
273
|
|
|
}, |
274
|
|
|
true, |
275
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
276
|
|
|
noPre, |
277
|
|
|
errorHandler, |
278
|
|
|
}, |
279
|
|
|
{ |
280
|
|
|
"list payment links, an error occurs when parsing json", |
281
|
|
|
args{ |
282
|
|
|
context.Background(), |
283
|
|
|
nil, |
284
|
|
|
}, |
285
|
|
|
true, |
286
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
287
|
|
|
noPre, |
288
|
|
|
encodingHandler, |
289
|
|
|
}, |
290
|
|
|
{ |
291
|
|
|
"list payment links, invalid url when building request", |
292
|
|
|
args{ |
293
|
|
|
context.Background(), |
294
|
|
|
nil, |
295
|
|
|
}, |
296
|
|
|
true, |
297
|
|
|
errBadBaseURL, |
298
|
|
|
crashSrv, |
299
|
|
|
errorHandler, |
300
|
|
|
}, |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
for _, c := range cases { |
304
|
|
|
setup() |
305
|
|
|
defer teardown() |
306
|
|
|
|
307
|
|
|
t.Run(c.name, func(t *testing.T) { |
308
|
|
|
c.pre() |
309
|
|
|
tMux.HandleFunc("/v2/payment-links", c.handler) |
310
|
|
|
|
311
|
|
|
res, m, err := tClient.PaymentLinks.List(c.args.ctx, c.args.opts) |
312
|
|
|
if c.wantErr { |
313
|
|
|
assert.NotNil(t, err) |
314
|
|
|
assert.EqualError(t, err, c.err.Error()) |
315
|
|
|
} else { |
316
|
|
|
assert.Nil(t, err) |
317
|
|
|
assert.IsType(t, &PaymentLinksList{}, m) |
318
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
319
|
|
|
} |
320
|
|
|
}) |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
func TestPaymentLinkService_Update(t *testing.T) { |
325
|
|
|
setEnv() |
326
|
|
|
defer unsetEnv() |
327
|
|
|
|
328
|
|
|
type args struct { |
329
|
|
|
ctx context.Context |
330
|
|
|
paymentLink string |
331
|
|
|
pl UpdatePaymentLinks |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
cases := []struct { |
335
|
|
|
name string |
336
|
|
|
args args |
337
|
|
|
wantErr bool |
338
|
|
|
err error |
339
|
|
|
pre func() |
340
|
|
|
handler http.HandlerFunc |
341
|
|
|
}{ |
342
|
|
|
{ |
343
|
|
|
"update payment links works as expected.", |
344
|
|
|
args{ |
345
|
|
|
context.Background(), |
346
|
|
|
"pl_ka21123129", |
347
|
|
|
UpdatePaymentLinks{ |
348
|
|
|
Archived: true, |
349
|
|
|
}, |
350
|
|
|
}, |
351
|
|
|
false, |
352
|
|
|
nil, |
353
|
|
|
noPre, |
354
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
355
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
356
|
|
|
testMethod(t, r, "PATCH") |
357
|
|
|
|
358
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
359
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
360
|
|
|
} |
361
|
|
|
_, _ = w.Write([]byte(testdata.UpdatePaymentLinksResponse)) |
362
|
|
|
}, |
363
|
|
|
}, |
364
|
|
|
{ |
365
|
|
|
"update payment links, an error is returned from the server", |
366
|
|
|
args{ |
367
|
|
|
context.Background(), |
368
|
|
|
"pl_ka21123129", |
369
|
|
|
UpdatePaymentLinks{}, |
370
|
|
|
}, |
371
|
|
|
true, |
372
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
373
|
|
|
noPre, |
374
|
|
|
errorHandler, |
375
|
|
|
}, |
376
|
|
|
{ |
377
|
|
|
"update payment links, an error occurs when parsing json", |
378
|
|
|
args{ |
379
|
|
|
context.Background(), |
380
|
|
|
"pl_ka21123129", |
381
|
|
|
UpdatePaymentLinks{}, |
382
|
|
|
}, |
383
|
|
|
true, |
384
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
385
|
|
|
noPre, |
386
|
|
|
encodingHandler, |
387
|
|
|
}, |
388
|
|
|
{ |
389
|
|
|
"update payment links, invalid url when building request", |
390
|
|
|
args{ |
391
|
|
|
context.Background(), |
392
|
|
|
"pl_ka21123129", |
393
|
|
|
UpdatePaymentLinks{}, |
394
|
|
|
}, |
395
|
|
|
true, |
396
|
|
|
errBadBaseURL, |
397
|
|
|
crashSrv, |
398
|
|
|
errorHandler, |
399
|
|
|
}, |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
for _, c := range cases { |
403
|
|
|
setup() |
404
|
|
|
defer teardown() |
405
|
|
|
|
406
|
|
|
t.Run(c.name, func(t *testing.T) { |
407
|
|
|
c.pre() |
408
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/payment-links/%s", c.args.paymentLink), c.handler) |
409
|
|
|
|
410
|
|
|
res, m, err := tClient.PaymentLinks.Update(c.args.ctx, c.args.paymentLink, c.args.pl) |
411
|
|
|
if c.wantErr { |
412
|
|
|
assert.NotNil(t, err) |
413
|
|
|
assert.EqualError(t, err, c.err.Error()) |
414
|
|
|
} else { |
415
|
|
|
assert.Nil(t, err) |
416
|
|
|
assert.IsType(t, &PaymentLink{}, m) |
417
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
418
|
|
|
} |
419
|
|
|
}) |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
func TestPaymentLinkService_Delete(t *testing.T) { |
424
|
|
|
setEnv() |
425
|
|
|
defer unsetEnv() |
426
|
|
|
|
427
|
|
|
type args struct { |
428
|
|
|
ctx context.Context |
429
|
|
|
paymentLink string |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
cases := []struct { |
433
|
|
|
name string |
434
|
|
|
args args |
435
|
|
|
wantErr bool |
436
|
|
|
err error |
437
|
|
|
pre func() |
438
|
|
|
handler http.HandlerFunc |
439
|
|
|
}{ |
440
|
|
|
{ |
441
|
|
|
"delete payment links works as expected.", |
442
|
|
|
args{ |
443
|
|
|
context.Background(), |
444
|
|
|
"pl_ka21123129", |
445
|
|
|
}, |
446
|
|
|
false, |
447
|
|
|
nil, |
448
|
|
|
noPre, |
449
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
450
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
451
|
|
|
testMethod(t, r, "DELETE") |
452
|
|
|
|
453
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
454
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
455
|
|
|
} |
456
|
|
|
w.WriteHeader(http.StatusNoContent) |
457
|
|
|
}, |
458
|
|
|
}, |
459
|
|
|
{ |
460
|
|
|
"delete payment links, an error is returned from the server", |
461
|
|
|
args{ |
462
|
|
|
context.Background(), |
463
|
|
|
"pl_ka21123129", |
464
|
|
|
}, |
465
|
|
|
true, |
466
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
467
|
|
|
noPre, |
468
|
|
|
errorHandler, |
469
|
|
|
}, |
470
|
|
|
{ |
471
|
|
|
"delete payment links, invalid url when building request", |
472
|
|
|
args{ |
473
|
|
|
context.Background(), |
474
|
|
|
"pl_ka21123129", |
475
|
|
|
}, |
476
|
|
|
true, |
477
|
|
|
errBadBaseURL, |
478
|
|
|
crashSrv, |
479
|
|
|
errorHandler, |
480
|
|
|
}, |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
for _, c := range cases { |
484
|
|
|
setup() |
485
|
|
|
defer teardown() |
486
|
|
|
|
487
|
|
|
t.Run(c.name, func(t *testing.T) { |
488
|
|
|
c.pre() |
489
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/payment-links/%s", c.args.paymentLink), c.handler) |
490
|
|
|
|
491
|
|
|
res, err := tClient.PaymentLinks.Delete(c.args.ctx, c.args.paymentLink) |
492
|
|
|
if c.wantErr { |
493
|
|
|
assert.NotNil(t, err) |
494
|
|
|
assert.EqualError(t, err, c.err.Error()) |
495
|
|
|
} else { |
496
|
|
|
assert.Nil(t, err) |
497
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
498
|
|
|
} |
499
|
|
|
}) |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|