GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

mollie.TestMandatesService_List   C
last analyzed

Complexity

Conditions 8

Size

Total Lines 119
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 90
nop 1
dl 0
loc 119
rs 5.4606
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 TestMandatesService_Get(t *testing.T) {
14
	setEnv()
15
	defer unsetEnv()
16
17
	type args struct {
18
		ctx      context.Context
19
		mandate  string
20
		customer string
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 mandates works as expected.",
33
			args{
34
				context.Background(),
35
				"mdt_h3gAaD5zP",
36
				"cst_4qqhO89gsT",
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
				if _, ok := r.Header[AuthHeader]; !ok {
45
					w.WriteHeader(http.StatusUnauthorized)
46
				}
47
				_, _ = w.Write([]byte(testdata.GetMandateResponse))
48
			},
49
		},
50
		{
51
			"get mandate, an error is returned from the server",
52
			args{
53
				context.Background(),
54
				"mdt_h3gAaD5zP",
55
				"cst_4qqhO89gsT",
56
			},
57
			true,
58
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
59
			noPre,
60
			errorHandler,
61
		},
62
		{
63
			"get mandate, an error occurs when parsing json",
64
			args{
65
				context.Background(),
66
				"mdt_h3gAaD5zP",
67
				"cst_4qqhO89gsT",
68
			},
69
			true,
70
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
71
			noPre,
72
			encodingHandler,
73
		},
74
		{
75
			"get mandate, invalid url when building request",
76
			args{
77
				context.Background(),
78
				"mdt_h3gAaD5zP",
79
				"cst_4qqhO89gsT",
80
			},
81
			true,
82
			errBadBaseURL,
83
			crashSrv,
84
			errorHandler,
85
		},
86
	}
87
88
	for _, c := range cases {
89
		setup()
90
		defer teardown()
91
92
		t.Run(c.name, func(t *testing.T) {
93
			c.pre()
94
			tMux.HandleFunc(
95
				fmt.Sprintf(
96
					"/v2/customers/%s/mandates/%s",
97
					c.args.customer,
98
					c.args.mandate,
99
				),
100
				c.handler,
101
			)
102
103
			res, m, err := tClient.Mandates.Get(c.args.ctx, c.args.customer, c.args.mandate)
104
			if c.wantErr {
105
				assert.NotNil(t, err)
106
				assert.EqualError(t, err, c.err.Error())
107
			} else {
108
				assert.Nil(t, err)
109
				assert.IsType(t, &Mandate{}, m)
110
				assert.EqualValues(t, c.args.ctx, res.Request.Context())
111
				assert.IsType(t, &http.Response{}, res.Response)
112
			}
113
		})
114
	}
115
}
116
117
func TestMandatesService_Create(t *testing.T) {
118
	setEnv()
119
	defer unsetEnv()
120
121
	type args struct {
122
		ctx      context.Context
123
		mandate  CreateMandate
124
		customer string
125
	}
126
127
	cases := []struct {
128
		name    string
129
		args    args
130
		wantErr bool
131
		err     error
132
		pre     func()
133
		handler http.HandlerFunc
134
	}{
135
		{
136
			"create mandates works as expected.",
137
			args{
138
				context.Background(),
139
				CreateMandate{
140
					Method: PayPal,
141
				},
142
				"cst_4qqhO89gsT",
143
			},
144
			false,
145
			nil,
146
			noPre,
147
			func(w http.ResponseWriter, r *http.Request) {
148
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
149
				testMethod(t, r, "POST")
150
				if _, ok := r.Header[AuthHeader]; !ok {
151
					w.WriteHeader(http.StatusUnauthorized)
152
				}
153
				w.WriteHeader(http.StatusCreated)
154
				_, _ = w.Write([]byte(testdata.CreateMandateResponse))
155
			},
156
		},
157
		{
158
			"create mandates works as expected when using access tokens",
159
			args{
160
				context.Background(),
161
				CreateMandate{
162
					Method: PayPal,
163
				},
164
				"cst_4qqhO89gsT",
165
			},
166
			false,
167
			nil,
168
			setAccessToken,
169
			func(w http.ResponseWriter, r *http.Request) {
170
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
171
				testMethod(t, r, "POST")
172
				if _, ok := r.Header[AuthHeader]; !ok {
173
					w.WriteHeader(http.StatusUnauthorized)
174
				}
175
				w.WriteHeader(http.StatusCreated)
176
				_, _ = w.Write([]byte(testdata.CreateMandateResponse))
177
			},
178
		},
179
		{
180
			"create mandate, an error is returned from the server",
181
			args{
182
				context.Background(),
183
				CreateMandate{
184
					Method: PayPal,
185
				},
186
				"cst_4qqhO89gsT",
187
			},
188
			true,
189
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
190
			noPre,
191
			errorHandler,
192
		},
193
		{
194
			"create mandate, an error occurs when parsing json",
195
			args{
196
				context.Background(),
197
				CreateMandate{
198
					Method: PayPal,
199
				},
200
				"cst_4qqhO89gsT",
201
			},
202
			true,
203
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
204
			noPre,
205
			encodingHandler,
206
		},
207
		{
208
			"create mandate, invalid url when building request",
209
			args{
210
				context.Background(),
211
				CreateMandate{
212
					Method: PayPal,
213
				},
214
				"cst_4qqhO89gsT",
215
			},
216
			true,
217
			errBadBaseURL,
218
			crashSrv,
219
			errorHandler,
220
		},
221
	}
222
223
	for _, c := range cases {
224
		setup()
225
		defer teardown()
226
227
		t.Run(c.name, func(t *testing.T) {
228
			c.pre()
229
			tMux.HandleFunc(
230
				fmt.Sprintf(
231
					"/v2/customers/%s/mandates",
232
					c.args.customer,
233
				),
234
				c.handler,
235
			)
236
237
			res, m, err := tClient.Mandates.Create(c.args.ctx, c.args.customer, c.args.mandate)
238
			if c.wantErr {
239
				assert.NotNil(t, err)
240
				assert.EqualError(t, err, c.err.Error())
241
			} else {
242
				assert.Nil(t, err)
243
				assert.IsType(t, &Mandate{}, m)
244
				assert.EqualValues(t, c.args.ctx, res.Request.Context())
245
				assert.IsType(t, &http.Response{}, res.Response)
246
			}
247
		})
248
	}
249
}
250
251
func TestMandatesService_Revoke(t *testing.T) {
252
	setEnv()
253
	defer unsetEnv()
254
255
	type args struct {
256
		ctx      context.Context
257
		mandate  string
258
		customer string
259
	}
260
261
	cases := []struct {
262
		name    string
263
		args    args
264
		wantErr bool
265
		err     error
266
		pre     func()
267
		handler http.HandlerFunc
268
	}{
269
		{
270
			"revoke mandates works as expected.",
271
			args{
272
				context.Background(),
273
				"mdt_h3gAaD5zP",
274
				"cst_4qqhO89gsT",
275
			},
276
			false,
277
			nil,
278
			noPre,
279
			func(w http.ResponseWriter, r *http.Request) {
280
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
281
				testMethod(t, r, "DELETE")
282
				if _, ok := r.Header[AuthHeader]; !ok {
283
					w.WriteHeader(http.StatusUnauthorized)
284
				}
285
286
				w.WriteHeader(http.StatusNoContent)
287
			},
288
		},
289
		{
290
			"revoke mandate, an error is returned from the server",
291
			args{
292
				context.Background(),
293
				"mdt_h3gAaD5zP",
294
				"cst_4qqhO89gsT",
295
			},
296
			true,
297
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
298
			noPre,
299
			errorHandler,
300
		},
301
		{
302
			"revoke mandate, invalid url when building request",
303
			args{
304
				context.Background(),
305
				"mdt_h3gAaD5zP",
306
				"cst_4qqhO89gsT",
307
			},
308
			true,
309
			errBadBaseURL,
310
			crashSrv,
311
			errorHandler,
312
		},
313
	}
314
315
	for _, c := range cases {
316
		setup()
317
		defer teardown()
318
319
		t.Run(c.name, func(t *testing.T) {
320
			c.pre()
321
			tMux.HandleFunc(
322
				fmt.Sprintf(
323
					"/v2/customers/%s/mandates/%s",
324
					c.args.customer,
325
					c.args.mandate,
326
				),
327
				c.handler,
328
			)
329
330
			res, err := tClient.Mandates.Revoke(c.args.ctx, c.args.customer, c.args.mandate)
331
			if c.wantErr {
332
				assert.NotNil(t, err)
333
				assert.EqualError(t, err, c.err.Error())
334
			} else {
335
				assert.Nil(t, err)
336
				assert.EqualValues(t, c.args.ctx, res.Request.Context())
337
				assert.IsType(t, &http.Response{}, res.Response)
338
			}
339
		})
340
	}
341
}
342
343
func TestMandatesService_List(t *testing.T) {
344
	setEnv()
345
	defer unsetEnv()
346
347
	type args struct {
348
		ctx      context.Context
349
		options  *ListMandatesOptions
350
		customer string
351
	}
352
353
	cases := []struct {
354
		name    string
355
		args    args
356
		wantErr bool
357
		err     error
358
		pre     func()
359
		handler http.HandlerFunc
360
	}{
361
		{
362
			"list mandates works as expected.",
363
			args{
364
				context.Background(),
365
				nil,
366
				"cst_4qqhO89gsT",
367
			},
368
			false,
369
			nil,
370
			noPre,
371
			func(w http.ResponseWriter, r *http.Request) {
372
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
373
				testMethod(t, r, "GET")
374
				if _, ok := r.Header[AuthHeader]; !ok {
375
					w.WriteHeader(http.StatusUnauthorized)
376
				}
377
				_, _ = w.Write([]byte(testdata.GetMandateResponse))
378
			},
379
		},
380
		{
381
			"list mandates with options works as expected.",
382
			args{
383
				context.Background(),
384
				&ListMandatesOptions{
385
					Limit: 10,
386
				},
387
				"cst_4qqhO89gsT",
388
			},
389
			false,
390
			nil,
391
			noPre,
392
			func(w http.ResponseWriter, r *http.Request) {
393
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
394
				testMethod(t, r, "GET")
395
				if _, ok := r.Header[AuthHeader]; !ok {
396
					w.WriteHeader(http.StatusUnauthorized)
397
				}
398
				_, _ = w.Write([]byte(testdata.GetMandateResponse))
399
			},
400
		},
401
		{
402
			"list mandates, an error is returned from the server",
403
			args{
404
				context.Background(),
405
				nil,
406
				"cst_4qqhO89gsT",
407
			},
408
			true,
409
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
410
			noPre,
411
			errorHandler,
412
		},
413
		{
414
			"list mandates, an error occurs when parsing json",
415
			args{
416
				context.Background(),
417
				nil,
418
				"cst_4qqhO89gsT",
419
			},
420
			true,
421
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
422
			noPre,
423
			encodingHandler,
424
		},
425
		{
426
			"list mandates, invalid url when building request",
427
			args{
428
				context.Background(),
429
				nil,
430
				"cst_4qqhO89gsT",
431
			},
432
			true,
433
			errBadBaseURL,
434
			crashSrv,
435
			errorHandler,
436
		},
437
	}
438
439
	for _, c := range cases {
440
		setup()
441
		defer teardown()
442
443
		t.Run(c.name, func(t *testing.T) {
444
			c.pre()
445
			tMux.HandleFunc(
446
				fmt.Sprintf(
447
					"/v2/customers/%s/mandates",
448
					c.args.customer,
449
				),
450
				c.handler,
451
			)
452
453
			res, m, err := tClient.Mandates.List(c.args.ctx, c.args.customer, c.args.options)
454
			if c.wantErr {
455
				assert.NotNil(t, err)
456
				assert.EqualError(t, err, c.err.Error())
457
			} else {
458
				assert.Nil(t, err)
459
				assert.IsType(t, &MandatesList{}, m)
460
				assert.EqualValues(t, c.args.ctx, res.Request.Context())
461
				assert.IsType(t, &http.Response{}, res.Response)
462
			}
463
		})
464
	}
465
}
466