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 TestCustomerService_Get(t *testing.T) { |
14
|
|
|
setEnv() |
15
|
|
|
defer unsetEnv() |
16
|
|
|
|
17
|
|
|
type args struct { |
18
|
|
|
ctx context.Context |
19
|
|
|
customer string |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
cases := []struct { |
23
|
|
|
name string |
24
|
|
|
args args |
25
|
|
|
wantErr bool |
26
|
|
|
err error |
27
|
|
|
pre func() |
28
|
|
|
handler http.HandlerFunc |
29
|
|
|
}{ |
30
|
|
|
{ |
31
|
|
|
"get mollie customers works as expected", |
32
|
|
|
args{ |
33
|
|
|
context.Background(), |
34
|
|
|
"cst_kEn1PlbGa", |
35
|
|
|
}, |
36
|
|
|
false, |
37
|
|
|
nil, |
38
|
|
|
noPre, |
39
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
40
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
41
|
|
|
testMethod(t, r, "GET") |
42
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
43
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
_, _ = w.Write([]byte(testdata.GetCustomerResponse)) |
47
|
|
|
}, |
48
|
|
|
}, |
49
|
|
|
{ |
50
|
|
|
"get mollie customers, an error is returned from the server", |
51
|
|
|
args{ |
52
|
|
|
context.Background(), |
53
|
|
|
"cst_kEn1PlbGa", |
54
|
|
|
}, |
55
|
|
|
true, |
56
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
57
|
|
|
noPre, |
58
|
|
|
errorHandler, |
59
|
|
|
}, |
60
|
|
|
{ |
61
|
|
|
"get mollie customers, an error occurs when parsing json", |
62
|
|
|
args{ |
63
|
|
|
context.Background(), |
64
|
|
|
"cst_kEn1PlbGa", |
65
|
|
|
}, |
66
|
|
|
true, |
67
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
68
|
|
|
noPre, |
69
|
|
|
encodingHandler, |
70
|
|
|
}, |
71
|
|
|
{ |
72
|
|
|
"get mollie customers, invalid url when building request", |
73
|
|
|
args{ |
74
|
|
|
context.Background(), |
75
|
|
|
"cst_kEn1PlbGa", |
76
|
|
|
}, |
77
|
|
|
true, |
78
|
|
|
errBadBaseURL, |
79
|
|
|
crashSrv, |
80
|
|
|
errorHandler, |
81
|
|
|
}, |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
for _, c := range cases { |
85
|
|
|
t.Run(c.name, func(t *testing.T) { |
86
|
|
|
setup() |
87
|
|
|
defer teardown() |
88
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s", c.args.customer), c.handler) |
89
|
|
|
c.pre() |
90
|
|
|
res, cc, err := tClient.Customers.Get(c.args.ctx, c.args.customer) |
91
|
|
|
if c.wantErr { |
92
|
|
|
assert.NotNil(t, err) |
93
|
|
|
assert.EqualError(t, err, c.err.Error()) |
94
|
|
|
} else { |
95
|
|
|
assert.Nil(t, err) |
96
|
|
|
assert.IsType(t, &Customer{}, cc) |
97
|
|
|
assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
98
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
99
|
|
|
} |
100
|
|
|
}) |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
func TestCustomersService_Create(t *testing.T) { |
105
|
|
|
setEnv() |
106
|
|
|
defer unsetEnv() |
107
|
|
|
type args struct { |
108
|
|
|
ctx context.Context |
109
|
|
|
customer CreateCustomer |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
cases := []struct { |
113
|
|
|
name string |
114
|
|
|
status int |
115
|
|
|
args args |
116
|
|
|
wantErr bool |
117
|
|
|
err error |
118
|
|
|
pre func() |
119
|
|
|
handler http.HandlerFunc |
120
|
|
|
}{ |
121
|
|
|
{ |
122
|
|
|
"create mollie customers works as expected", |
123
|
|
|
http.StatusAccepted, |
124
|
|
|
args{ |
125
|
|
|
context.Background(), |
126
|
|
|
CreateCustomer{Locale: German}, |
127
|
|
|
}, |
128
|
|
|
false, |
129
|
|
|
nil, |
130
|
|
|
noPre, |
131
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
132
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
133
|
|
|
testMethod(t, r, "POST") |
134
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
135
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
_, _ = w.Write([]byte(testdata.CreateCustomerResponse)) |
139
|
|
|
}, |
140
|
|
|
}, |
141
|
|
|
{ |
142
|
|
|
"create mollie customers, an error is returned from the server", |
143
|
|
|
http.StatusInternalServerError, |
144
|
|
|
args{ |
145
|
|
|
context.Background(), |
146
|
|
|
CreateCustomer{}, |
147
|
|
|
}, |
148
|
|
|
true, |
149
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
150
|
|
|
noPre, |
151
|
|
|
errorHandler, |
152
|
|
|
}, |
153
|
|
|
{ |
154
|
|
|
"create mollie customers, an error occurs when parsing json", |
155
|
|
|
http.StatusInternalServerError, |
156
|
|
|
args{ |
157
|
|
|
context.Background(), |
158
|
|
|
CreateCustomer{}, |
159
|
|
|
}, |
160
|
|
|
true, |
161
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
162
|
|
|
noPre, |
163
|
|
|
encodingHandler, |
164
|
|
|
}, |
165
|
|
|
{ |
166
|
|
|
"create mollie customers, invalid url when building request", |
167
|
|
|
http.StatusInternalServerError, |
168
|
|
|
args{ |
169
|
|
|
context.Background(), |
170
|
|
|
CreateCustomer{}, |
171
|
|
|
}, |
172
|
|
|
true, |
173
|
|
|
errBadBaseURL, |
174
|
|
|
crashSrv, |
175
|
|
|
errorHandler, |
176
|
|
|
}, |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
for _, c := range cases { |
180
|
|
|
t.Run(c.name, func(t *testing.T) { |
181
|
|
|
setup() |
182
|
|
|
defer teardown() |
183
|
|
|
tMux.HandleFunc("/v2/customers", c.handler) |
184
|
|
|
c.pre() |
185
|
|
|
res, cc, err := tClient.Customers.Create(c.args.ctx, c.args.customer) |
186
|
|
|
if c.wantErr { |
187
|
|
|
assert.NotNil(t, err) |
188
|
|
|
assert.EqualError(t, err, c.err.Error()) |
189
|
|
|
} else { |
190
|
|
|
assert.Nil(t, err) |
191
|
|
|
assert.IsType(t, &Customer{}, cc) |
192
|
|
|
assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
193
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
194
|
|
|
} |
195
|
|
|
}) |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
func TestCustomersService_Update(t *testing.T) { |
200
|
|
|
setEnv() |
201
|
|
|
defer unsetEnv() |
202
|
|
|
|
203
|
|
|
type args struct { |
204
|
|
|
ctx context.Context |
205
|
|
|
customerID string |
206
|
|
|
customer UpdateCustomer |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
cases := []struct { |
210
|
|
|
name string |
211
|
|
|
status int |
212
|
|
|
args args |
213
|
|
|
wantErr bool |
214
|
|
|
err error |
215
|
|
|
pre func() |
216
|
|
|
handler http.HandlerFunc |
217
|
|
|
}{ |
218
|
|
|
{ |
219
|
|
|
"update mollie customers works as expected", |
220
|
|
|
http.StatusAccepted, |
221
|
|
|
args{ |
222
|
|
|
context.Background(), |
223
|
|
|
"cst_kEn1PlbGa", |
224
|
|
|
UpdateCustomer{Locale: French}, |
225
|
|
|
}, |
226
|
|
|
false, |
227
|
|
|
nil, |
228
|
|
|
noPre, |
229
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
230
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
231
|
|
|
testMethod(t, r, "PATCH") |
232
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
233
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
_, _ = w.Write([]byte(testdata.UpdateCustomerResponse)) |
237
|
|
|
}, |
238
|
|
|
}, |
239
|
|
|
{ |
240
|
|
|
"update mollie customers, an error is returned from the server", |
241
|
|
|
http.StatusInternalServerError, |
242
|
|
|
args{ |
243
|
|
|
context.Background(), |
244
|
|
|
"cst_kEn1PlbGa", |
245
|
|
|
UpdateCustomer{}, |
246
|
|
|
}, |
247
|
|
|
true, |
248
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
249
|
|
|
noPre, |
250
|
|
|
errorHandler, |
251
|
|
|
}, |
252
|
|
|
{ |
253
|
|
|
"update mollie customers, an error occurs when parsing json", |
254
|
|
|
http.StatusInternalServerError, |
255
|
|
|
args{ |
256
|
|
|
context.Background(), |
257
|
|
|
"cst_kEn1PlbGa", |
258
|
|
|
UpdateCustomer{}, |
259
|
|
|
}, |
260
|
|
|
true, |
261
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
262
|
|
|
noPre, |
263
|
|
|
encodingHandler, |
264
|
|
|
}, |
265
|
|
|
{ |
266
|
|
|
"update mollie customers, invalid url when building request", |
267
|
|
|
http.StatusInternalServerError, |
268
|
|
|
args{ |
269
|
|
|
context.Background(), |
270
|
|
|
"cst_kEn1PlbGa", |
271
|
|
|
UpdateCustomer{}, |
272
|
|
|
}, |
273
|
|
|
true, |
274
|
|
|
errBadBaseURL, |
275
|
|
|
crashSrv, |
276
|
|
|
errorHandler, |
277
|
|
|
}, |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
for _, c := range cases { |
281
|
|
|
t.Run(c.name, func(t *testing.T) { |
282
|
|
|
setup() |
283
|
|
|
defer teardown() |
284
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s", c.args.customerID), c.handler) |
285
|
|
|
c.pre() |
286
|
|
|
res, cc, err := tClient.Customers.Update(c.args.ctx, c.args.customerID, c.args.customer) |
287
|
|
|
if c.wantErr { |
288
|
|
|
assert.NotNil(t, err) |
289
|
|
|
assert.EqualError(t, err, c.err.Error()) |
290
|
|
|
} else { |
291
|
|
|
assert.Nil(t, err) |
292
|
|
|
assert.IsType(t, &Customer{}, cc) |
293
|
|
|
assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
294
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
295
|
|
|
} |
296
|
|
|
}) |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
func TestCustomersService_List(t *testing.T) { |
301
|
|
|
setEnv() |
302
|
|
|
defer unsetEnv() |
303
|
|
|
type args struct { |
304
|
|
|
ctx context.Context |
305
|
|
|
options *ListCustomersOptions |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
cases := []struct { |
309
|
|
|
name string |
310
|
|
|
status int |
311
|
|
|
args args |
312
|
|
|
wantErr bool |
313
|
|
|
err error |
314
|
|
|
pre func() |
315
|
|
|
handler http.HandlerFunc |
316
|
|
|
}{ |
317
|
|
|
{ |
318
|
|
|
"list mollie customers with options works as expected", |
319
|
|
|
http.StatusAccepted, |
320
|
|
|
args{ |
321
|
|
|
context.Background(), |
322
|
|
|
&ListCustomersOptions{ |
323
|
|
|
SequenceType: OneOffSequence, |
324
|
|
|
}, |
325
|
|
|
}, |
326
|
|
|
false, |
327
|
|
|
nil, |
328
|
|
|
noPre, |
329
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
330
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
331
|
|
|
testMethod(t, r, "GET") |
332
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
333
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
_, _ = w.Write([]byte(testdata.ListCustomersResponse)) |
337
|
|
|
}, |
338
|
|
|
}, |
339
|
|
|
{ |
340
|
|
|
"list mollie customers works as expected", |
341
|
|
|
http.StatusAccepted, |
342
|
|
|
args{ |
343
|
|
|
context.Background(), |
344
|
|
|
nil, |
345
|
|
|
}, |
346
|
|
|
false, |
347
|
|
|
nil, |
348
|
|
|
noPre, |
349
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
350
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
351
|
|
|
testMethod(t, r, "GET") |
352
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
353
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
_, _ = w.Write([]byte(testdata.ListCustomersResponse)) |
357
|
|
|
}, |
358
|
|
|
}, |
359
|
|
|
{ |
360
|
|
|
"list mollie customers, an error is returned from the server", |
361
|
|
|
http.StatusInternalServerError, |
362
|
|
|
args{ |
363
|
|
|
context.Background(), |
364
|
|
|
nil, |
365
|
|
|
}, |
366
|
|
|
true, |
367
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
368
|
|
|
noPre, |
369
|
|
|
errorHandler, |
370
|
|
|
}, |
371
|
|
|
{ |
372
|
|
|
"list mollie customers, an error occurs when parsing json", |
373
|
|
|
http.StatusInternalServerError, |
374
|
|
|
args{ |
375
|
|
|
context.Background(), |
376
|
|
|
&ListCustomersOptions{ |
377
|
|
|
SequenceType: OneOffSequence, |
378
|
|
|
}, |
379
|
|
|
}, |
380
|
|
|
true, |
381
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
382
|
|
|
noPre, |
383
|
|
|
encodingHandler, |
384
|
|
|
}, |
385
|
|
|
{ |
386
|
|
|
"list mollie customers, invalid url when building request", |
387
|
|
|
http.StatusInternalServerError, |
388
|
|
|
args{ |
389
|
|
|
context.Background(), |
390
|
|
|
&ListCustomersOptions{ |
391
|
|
|
SequenceType: OneOffSequence, |
392
|
|
|
}, |
393
|
|
|
}, |
394
|
|
|
true, |
395
|
|
|
errBadBaseURL, |
396
|
|
|
crashSrv, |
397
|
|
|
errorHandler, |
398
|
|
|
}, |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
for _, c := range cases { |
402
|
|
|
t.Run(c.name, func(t *testing.T) { |
403
|
|
|
setup() |
404
|
|
|
defer teardown() |
405
|
|
|
tMux.HandleFunc("/v2/customers", c.handler) |
406
|
|
|
c.pre() |
407
|
|
|
res, cc, err := tClient.Customers.List(c.args.ctx, c.args.options) |
408
|
|
|
if c.wantErr { |
409
|
|
|
assert.NotNil(t, err) |
410
|
|
|
assert.EqualError(t, err, c.err.Error()) |
411
|
|
|
} else { |
412
|
|
|
assert.Nil(t, err) |
413
|
|
|
assert.IsType(t, &CustomersList{}, cc) |
414
|
|
|
assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
415
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
416
|
|
|
} |
417
|
|
|
}) |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
func TestCustomersService_Delete(t *testing.T) { |
422
|
|
|
setEnv() |
423
|
|
|
defer unsetEnv() |
424
|
|
|
type args struct { |
425
|
|
|
ctx context.Context |
426
|
|
|
customer string |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
cases := []struct { |
430
|
|
|
name string |
431
|
|
|
status int |
432
|
|
|
args args |
433
|
|
|
wantErr bool |
434
|
|
|
err error |
435
|
|
|
pre func() |
436
|
|
|
handler http.HandlerFunc |
437
|
|
|
}{ |
438
|
|
|
{ |
439
|
|
|
"delete mollie customers with options works as expected", |
440
|
|
|
http.StatusNoContent, |
441
|
|
|
args{ |
442
|
|
|
context.Background(), |
443
|
|
|
"cst_kEn1PlbGa", |
444
|
|
|
}, |
445
|
|
|
false, |
446
|
|
|
nil, |
447
|
|
|
noPre, |
448
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
449
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
450
|
|
|
testMethod(t, r, "DELETE") |
451
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
452
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
w.WriteHeader(http.StatusNoContent) |
456
|
|
|
}, |
457
|
|
|
}, |
458
|
|
|
{ |
459
|
|
|
"delete mollie customers, an error is returned from the server", |
460
|
|
|
http.StatusInternalServerError, |
461
|
|
|
args{ |
462
|
|
|
context.Background(), |
463
|
|
|
"cst_kEn1PlbGa", |
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 mollie customers, invalid url when building request", |
472
|
|
|
http.StatusInternalServerError, |
473
|
|
|
args{ |
474
|
|
|
context.Background(), |
475
|
|
|
"cst_kEn1PlbGa", |
476
|
|
|
}, |
477
|
|
|
true, |
478
|
|
|
errBadBaseURL, |
479
|
|
|
crashSrv, |
480
|
|
|
errorHandler, |
481
|
|
|
}, |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
for _, c := range cases { |
485
|
|
|
t.Run(c.name, func(t *testing.T) { |
486
|
|
|
setup() |
487
|
|
|
defer teardown() |
488
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s", c.args.customer), c.handler) |
489
|
|
|
c.pre() |
490
|
|
|
res, err := tClient.Customers.Delete(c.args.ctx, c.args.customer) |
491
|
|
|
if c.wantErr { |
492
|
|
|
assert.NotNil(t, err) |
493
|
|
|
assert.EqualError(t, err, c.err.Error()) |
494
|
|
|
} else { |
495
|
|
|
assert.Nil(t, err) |
496
|
|
|
assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
497
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
498
|
|
|
} |
499
|
|
|
}) |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
func TestCustomerService_GetPayments(t *testing.T) { |
504
|
|
|
setEnv() |
505
|
|
|
defer unsetEnv() |
506
|
|
|
type args struct { |
507
|
|
|
ctx context.Context |
508
|
|
|
customer string |
509
|
|
|
options *ListCustomersOptions |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
cases := []struct { |
513
|
|
|
name string |
514
|
|
|
args args |
515
|
|
|
wantErr bool |
516
|
|
|
err error |
517
|
|
|
pre func() |
518
|
|
|
handler http.HandlerFunc |
519
|
|
|
}{ |
520
|
|
|
{ |
521
|
|
|
"get mollie customers payments works as expected", |
522
|
|
|
args{ |
523
|
|
|
context.Background(), |
524
|
|
|
"cst_kEn1PlbGa", |
525
|
|
|
nil, |
526
|
|
|
}, |
527
|
|
|
false, |
528
|
|
|
nil, |
529
|
|
|
noPre, |
530
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
531
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
532
|
|
|
testMethod(t, r, "GET") |
533
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
534
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
_, _ = w.Write([]byte(testdata.ListPaymentsResponse)) |
538
|
|
|
}, |
539
|
|
|
}, |
540
|
|
|
{ |
541
|
|
|
"get mollie customers payments with options works as expected", |
542
|
|
|
args{ |
543
|
|
|
context.Background(), |
544
|
|
|
"cst_kEn1PlbGa", |
545
|
|
|
&ListCustomersOptions{Limit: 100}, |
546
|
|
|
}, |
547
|
|
|
false, |
548
|
|
|
nil, |
549
|
|
|
noPre, |
550
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
551
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
552
|
|
|
testMethod(t, r, "GET") |
553
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
554
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
_, _ = w.Write([]byte(testdata.ListPaymentsResponse)) |
558
|
|
|
}, |
559
|
|
|
}, |
560
|
|
|
{ |
561
|
|
|
"get mollie customers payments, an error is returned from the server", |
562
|
|
|
args{ |
563
|
|
|
context.Background(), |
564
|
|
|
"cst_kEn1PlbGa", |
565
|
|
|
&ListCustomersOptions{SequenceType: RecurringSequence}, |
566
|
|
|
}, |
567
|
|
|
true, |
568
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
569
|
|
|
noPre, |
570
|
|
|
errorHandler, |
571
|
|
|
}, |
572
|
|
|
{ |
573
|
|
|
"get mollie customers payments, an error occurs when parsing json", |
574
|
|
|
args{ |
575
|
|
|
context.Background(), |
576
|
|
|
"cst_kEn1PlbGa", |
577
|
|
|
nil, |
578
|
|
|
}, |
579
|
|
|
true, |
580
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
581
|
|
|
noPre, |
582
|
|
|
encodingHandler, |
583
|
|
|
}, |
584
|
|
|
{ |
585
|
|
|
"get mollie customers payments, invalid url when building request", |
586
|
|
|
args{ |
587
|
|
|
context.Background(), |
588
|
|
|
"cst_kEn1PlbGa", |
589
|
|
|
nil, |
590
|
|
|
}, |
591
|
|
|
true, |
592
|
|
|
errBadBaseURL, |
593
|
|
|
crashSrv, |
594
|
|
|
errorHandler, |
595
|
|
|
}, |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
for _, c := range cases { |
599
|
|
|
t.Run(c.name, func(t *testing.T) { |
600
|
|
|
setup() |
601
|
|
|
defer teardown() |
602
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/payments", c.args.customer), c.handler) |
603
|
|
|
c.pre() |
604
|
|
|
res, cc, err := tClient.Customers.GetPayments(c.args.ctx, c.args.customer, c.args.options) |
605
|
|
|
if c.wantErr { |
606
|
|
|
assert.NotNil(t, err) |
607
|
|
|
assert.EqualError(t, err, c.err.Error()) |
608
|
|
|
} else { |
609
|
|
|
assert.Nil(t, err) |
610
|
|
|
assert.IsType(t, &PaymentList{}, cc) |
611
|
|
|
assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
612
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
613
|
|
|
} |
614
|
|
|
}) |
615
|
|
|
} |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
func TestCustomerService_CreatePayment(t *testing.T) { |
619
|
|
|
setEnv() |
620
|
|
|
defer unsetEnv() |
621
|
|
|
type args struct { |
622
|
|
|
ctx context.Context |
623
|
|
|
customer string |
624
|
|
|
payment CreatePayment |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
cases := []struct { |
628
|
|
|
name string |
629
|
|
|
args args |
630
|
|
|
wantErr bool |
631
|
|
|
err error |
632
|
|
|
pre func() |
633
|
|
|
handler http.HandlerFunc |
634
|
|
|
}{ |
635
|
|
|
{ |
636
|
|
|
"create mollie customers payments works as expected", |
637
|
|
|
args{ |
638
|
|
|
context.Background(), |
639
|
|
|
"cst_kEn1PlbGa", |
640
|
|
|
CreatePayment{ |
641
|
|
|
CreatePaymentAccessTokenFields: CreatePaymentAccessTokenFields{ |
642
|
|
|
Testmode: true, |
643
|
|
|
}, |
644
|
|
|
}, |
645
|
|
|
}, |
646
|
|
|
false, |
647
|
|
|
nil, |
648
|
|
|
noPre, |
649
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
650
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
651
|
|
|
testMethod(t, r, "POST") |
652
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
653
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
_, _ = w.Write([]byte(testdata.ListPaymentsResponse)) |
657
|
|
|
}, |
658
|
|
|
}, |
659
|
|
|
{ |
660
|
|
|
"create mollie customers payments, an error is returned from the server", |
661
|
|
|
args{ |
662
|
|
|
context.Background(), |
663
|
|
|
"cst_kEn1PlbGa", |
664
|
|
|
CreatePayment{ |
665
|
|
|
CreatePaymentAccessTokenFields: CreatePaymentAccessTokenFields{ |
666
|
|
|
Testmode: true, |
667
|
|
|
}, |
668
|
|
|
}, |
669
|
|
|
}, |
670
|
|
|
true, |
671
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
672
|
|
|
noPre, |
673
|
|
|
errorHandler, |
674
|
|
|
}, |
675
|
|
|
{ |
676
|
|
|
"create mollie customers payments, an error occurs when parsing json", |
677
|
|
|
args{ |
678
|
|
|
context.Background(), |
679
|
|
|
"cst_kEn1PlbGa", |
680
|
|
|
CreatePayment{ |
681
|
|
|
CreatePaymentAccessTokenFields: CreatePaymentAccessTokenFields{ |
682
|
|
|
Testmode: true, |
683
|
|
|
}, |
684
|
|
|
}, |
685
|
|
|
}, |
686
|
|
|
true, |
687
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
688
|
|
|
noPre, |
689
|
|
|
encodingHandler, |
690
|
|
|
}, |
691
|
|
|
{ |
692
|
|
|
"create mollie customers payments, invalid url when building request", |
693
|
|
|
args{ |
694
|
|
|
context.Background(), |
695
|
|
|
"cst_kEn1PlbGa", |
696
|
|
|
CreatePayment{ |
697
|
|
|
CreatePaymentAccessTokenFields: CreatePaymentAccessTokenFields{ |
698
|
|
|
Testmode: true, |
699
|
|
|
}, |
700
|
|
|
}, |
701
|
|
|
}, |
702
|
|
|
true, |
703
|
|
|
errBadBaseURL, |
704
|
|
|
crashSrv, |
705
|
|
|
errorHandler, |
706
|
|
|
}, |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
for _, c := range cases { |
710
|
|
|
t.Run(c.name, func(t *testing.T) { |
711
|
|
|
setup() |
712
|
|
|
defer teardown() |
713
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/payments", c.args.customer), c.handler) |
714
|
|
|
c.pre() |
715
|
|
|
res, cc, err := tClient.Customers.CreatePayment(c.args.ctx, c.args.customer, c.args.payment) |
716
|
|
|
if c.wantErr { |
717
|
|
|
assert.NotNil(t, err) |
718
|
|
|
assert.EqualError(t, err, c.err.Error()) |
719
|
|
|
} else { |
720
|
|
|
assert.Nil(t, err) |
721
|
|
|
assert.Nil(t, err) |
722
|
|
|
assert.IsType(t, &Payment{}, cc) |
723
|
|
|
assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
724
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
725
|
|
|
} |
726
|
|
|
}) |
727
|
|
|
} |
728
|
|
|
} |
729
|
|
|
|