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.
Passed
Push — master ( be3b80...df3f10 )
by
unknown
01:29
created

mollie.TestWebhookService_Delete   B

Complexity

Conditions 5

Size

Total Lines 72
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 54
nop 1
dl 0
loc 72
rs 8.0387
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 TestWebhookService_Get(t *testing.T) {
14
	setEnv()
15
	defer unsetEnv()
16
17
	type args struct {
18
		ctx     context.Context
19
		webhook 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 webhooks works as expected with access token",
32
			args{
33
				context.Background(),
34
				"hook_B2EyhTH5N4KWUnoYPcgiH",
35
			},
36
			false,
37
			nil,
38
			setAccessToken,
39
			func(w http.ResponseWriter, r *http.Request) {
40
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
41
				testMethod(t, r, "GET")
42
43
				w.Header().Set("Content-Type", "application/json")
44
				w.WriteHeader(http.StatusOK)
45
				_, _ = w.Write([]byte(testdata.GetWebhookExample))
46
			},
47
		},
48
		{
49
			"get webhooks returns an error when server responds with error",
50
			args{
51
				context.Background(),
52
				"hook_B2EyhTH5N4KWUnoYPcgiH",
53
			},
54
			true,
55
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
56
			setAccessToken,
57
			errorHandler,
58
		},
59
		{
60
			"get webhook, an error returned when parsing JSON",
61
			args{
62
				context.Background(),
63
				"hook_B2EyhTH5N4KWUnoYPcgiH",
64
			},
65
			true,
66
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
67
			setAccessToken,
68
			encodingHandler,
69
		},
70
		{
71
			"get webhooks fails when building request with invalid url",
72
			args{
73
				context.Background(),
74
				"hook_B2EyhTH5N4KWUnoYPcgiH",
75
			},
76
			true,
77
			errBadBaseURL,
78
			crashSrv,
79
			errorHandler,
80
		},
81
	}
82
83
	for _, c := range cases {
84
		setup()
85
		defer teardown()
86
87
		t.Run(c.name, func(t *testing.T) {
88
			c.pre()
89
			tMux.HandleFunc("/v2/webhooks/"+c.args.webhook, c.handler)
90
91
			res, m, err := tClient.Webhooks.Get(c.args.ctx, c.args.webhook)
92
			if c.wantErr {
93
				assert.NotNil(t, err)
94
				assert.EqualError(t, err, c.err.Error())
95
			} else {
96
				assert.Nil(t, err)
97
				assert.IsType(t, &Webhook{}, m)
98
				assert.IsType(t, &http.Response{}, res.Response)
99
			}
100
		})
101
	}
102
}
103
104
func TestWebhookService_Create(t *testing.T) {
105
	setEnv()
106
	defer unsetEnv()
107
108
	type args struct {
109
		ctx context.Context
110
		wh  CreateWebhook
111
	}
112
113
	cases := []struct {
114
		name    string
115
		args    args
116
		wantErr bool
117
		err     error
118
		pre     func()
119
		handler http.HandlerFunc
120
	}{
121
		{
122
			"create webhooks works as expected with access token",
123
			args{
124
				context.Background(),
125
				CreateWebhook{
126
					URL: "https://example.com/webhook",
127
				},
128
			},
129
			false,
130
			nil,
131
			setAccessToken,
132
			func(w http.ResponseWriter, r *http.Request) {
133
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
134
				testMethod(t, r, "POST")
135
136
				w.Header().Set("Content-Type", "application/json")
137
				w.WriteHeader(http.StatusCreated)
138
				_, _ = w.Write([]byte(testdata.CreateWebhookExample))
139
			},
140
		},
141
		{
142
			"create webhooks returns an error when server responds with error",
143
			args{
144
				context.Background(),
145
				CreateWebhook{
146
					URL: "https://example.com/webhook",
147
				},
148
			},
149
			true,
150
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
151
			setAccessToken,
152
			errorHandler,
153
		},
154
		{
155
			"create webhook, an error returned when parsing JSON",
156
			args{
157
				context.Background(),
158
				CreateWebhook{
159
					URL: "https://example.com/webhook",
160
				},
161
			},
162
			true,
163
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
164
			setAccessToken,
165
			encodingHandler,
166
		},
167
		{
168
			"create webhooks fails when building request with invalid url",
169
			args{
170
				context.Background(),
171
				CreateWebhook{
172
					URL: "https://example.com/webhook",
173
				},
174
			},
175
			true,
176
			errBadBaseURL,
177
			crashSrv,
178
			errorHandler,
179
		},
180
	}
181
182
	for _, c := range cases {
183
		setup()
184
		defer teardown()
185
186
		t.Run(c.name, func(t *testing.T) {
187
			c.pre()
188
			tMux.HandleFunc("/v2/webhooks", c.handler)
189
190
			res, m, err := tClient.Webhooks.Create(c.args.ctx, c.args.wh)
191
			if c.wantErr {
192
				assert.NotNil(t, err)
193
				assert.EqualError(t, err, c.err.Error())
194
			} else {
195
				assert.Nil(t, err)
196
				assert.IsType(t, &Webhook{}, m)
197
				assert.IsType(t, &http.Response{}, res.Response)
198
			}
199
		})
200
	}
201
}
202
203
func TestWebhookService_Delete(t *testing.T) {
204
	setEnv()
205
	defer unsetEnv()
206
207
	type args struct {
208
		ctx     context.Context
209
		webhook string
210
	}
211
212
	cases := []struct {
213
		name    string
214
		args    args
215
		wantErr bool
216
		err     error
217
		pre     func()
218
		handler http.HandlerFunc
219
	}{
220
		{
221
			"delete webhooks works as expected with access token",
222
			args{
223
				context.Background(),
224
				"hook_B2EyhTH5N4KWUnoYPcgiH",
225
			},
226
			false,
227
			nil,
228
			setAccessToken,
229
			func(w http.ResponseWriter, r *http.Request) {
230
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
231
				testMethod(t, r, "DELETE")
232
233
				w.WriteHeader(http.StatusNoContent)
234
			},
235
		},
236
		{
237
			"delete webhooks returns an error when server responds with error",
238
			args{
239
				context.Background(),
240
				"hook_B2EyhTH5N4KWUnoYPcgiH",
241
			},
242
			true,
243
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
244
			setAccessToken,
245
			errorHandler,
246
		},
247
		{
248
			"delete webhooks fails when building request with invalid url",
249
			args{
250
				context.Background(),
251
				"hook_B2EyhTH5N4KWUnoYPcgiH",
252
			},
253
			true,
254
			errBadBaseURL,
255
			crashSrv,
256
			errorHandler,
257
		},
258
	}
259
260
	for _, c := range cases {
261
		setup()
262
		defer teardown()
263
264
		t.Run(c.name, func(t *testing.T) {
265
			c.pre()
266
			tMux.HandleFunc("/v2/webhooks/"+c.args.webhook, c.handler)
267
268
			res, err := tClient.Webhooks.Delete(c.args.ctx, c.args.webhook)
269
			if c.wantErr {
270
				assert.NotNil(t, err)
271
				assert.EqualError(t, err, c.err.Error())
272
			} else {
273
				assert.Nil(t, err)
274
				assert.IsType(t, &http.Response{}, res.Response)
275
			}
276
		})
277
	}
278
}
279
280
func TestWebhookService_List(t *testing.T) {
281
	setEnv()
282
	defer unsetEnv()
283
284
	type args struct {
285
		ctx    context.Context
286
		params *WebhooksListOptions
287
	}
288
289
	cases := []struct {
290
		name    string
291
		args    args
292
		wantErr bool
293
		err     error
294
		pre     func()
295
		handler http.HandlerFunc
296
	}{
297
		{
298
			"list webhooks works as expected with access token",
299
			args{
300
				context.Background(),
301
				&WebhooksListOptions{},
302
			},
303
			false,
304
			nil,
305
			setAccessToken,
306
			func(w http.ResponseWriter, r *http.Request) {
307
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
308
				testMethod(t, r, "GET")
309
310
				w.Header().Set("Content-Type", "application/json")
311
				w.WriteHeader(http.StatusOK)
312
				_, _ = w.Write([]byte(testdata.ListWebhooksExample))
313
			},
314
		},
315
		{
316
			"list webhooks returns an error when server responds with error",
317
			args{
318
				context.Background(),
319
				&WebhooksListOptions{},
320
			},
321
			true,
322
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
323
			setAccessToken,
324
			errorHandler,
325
		},
326
		{
327
			"list webhooks, an error returned when parsing JSON",
328
			args{
329
				context.Background(),
330
				&WebhooksListOptions{},
331
			},
332
			true,
333
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
334
			setAccessToken,
335
			encodingHandler,
336
		},
337
		{
338
			"list webhooks fails when building request with invalid url",
339
			args{
340
				context.Background(),
341
				&WebhooksListOptions{},
342
			},
343
			true,
344
			errBadBaseURL,
345
			crashSrv,
346
			errorHandler,
347
		},
348
	}
349
350
	for _, c := range cases {
351
		setup()
352
		defer teardown()
353
354
		t.Run(c.name, func(t *testing.T) {
355
			c.pre()
356
			tMux.HandleFunc("/v2/webhooks", c.handler)
357
358
			res, m, err := tClient.Webhooks.List(c.args.ctx, c.args.params)
359
			if c.wantErr {
360
				assert.NotNil(t, err)
361
				assert.EqualError(t, err, c.err.Error())
362
			} else {
363
				assert.Nil(t, err)
364
				assert.IsType(t, &WebhookList{}, m)
365
				assert.IsType(t, &http.Response{}, res.Response)
366
			}
367
		})
368
	}
369
}
370
371
func TestWebhookService_Update(t *testing.T) {
372
	setEnv()
373
	defer unsetEnv()
374
375
	type args struct {
376
		ctx     context.Context
377
		webhook string
378
		uw      UpdateWebhook
379
	}
380
381
	cases := []struct {
382
		name    string
383
		args    args
384
		wantErr bool
385
		err     error
386
		pre     func()
387
		handler http.HandlerFunc
388
	}{
389
		{
390
			"update webhooks works as expected with access token",
391
			args{
392
				context.Background(),
393
				"hook_B2EyhTH5N4KWUnoYPcgiH",
394
				UpdateWebhook{
395
					URL: "https://example.com/updated-webhook",
396
				},
397
			},
398
			false,
399
			nil,
400
			setAccessToken,
401
			func(w http.ResponseWriter, r *http.Request) {
402
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
403
				testMethod(t, r, "PATCH")
404
405
				w.Header().Set("Content-Type", "application/json")
406
				w.WriteHeader(http.StatusOK)
407
				_, _ = w.Write([]byte(testdata.GetWebhookExample))
408
			},
409
		},
410
		{
411
			"update webhooks returns an error when server responds with error",
412
			args{
413
				context.Background(),
414
				"hook_B2EyhTH5N4KWUnoYPcgiH",
415
				UpdateWebhook{
416
					URL: "https://example.com/updated-webhook",
417
				},
418
			},
419
			true,
420
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
421
			setAccessToken,
422
			errorHandler,
423
		},
424
		{
425
			"update webhook, an error returned when parsing JSON",
426
			args{
427
				context.Background(),
428
				"hook_B2EyhTH5N4KWUnoYPcgiH",
429
				UpdateWebhook{
430
					URL: "https://example.com/updated-webhook",
431
				},
432
			},
433
			true,
434
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
435
			setAccessToken,
436
			encodingHandler,
437
		},
438
		{
439
			"update webhooks fails when building request with invalid url",
440
			args{
441
				context.Background(),
442
				"hook_B2EyhTH5N4KWUnoYPcgiH",
443
				UpdateWebhook{
444
					URL: "https://example.com/updated-webhook",
445
				},
446
			},
447
			true,
448
			errBadBaseURL,
449
			crashSrv,
450
			errorHandler,
451
		},
452
	}
453
454
	for _, c := range cases {
455
		setup()
456
		defer teardown()
457
458
		t.Run(c.name, func(t *testing.T) {
459
			c.pre()
460
			tMux.HandleFunc("/v2/webhooks/"+c.args.webhook, c.handler)
461
462
			res, m, err := tClient.Webhooks.Update(c.args.ctx, c.args.webhook, c.args.uw)
463
			if c.wantErr {
464
				assert.NotNil(t, err)
465
				assert.EqualError(t, err, c.err.Error())
466
			} else {
467
				assert.Nil(t, err)
468
				assert.IsType(t, &Webhook{}, m)
469
				assert.IsType(t, &http.Response{}, res.Response)
470
			}
471
		})
472
	}
473
}
474
475
func TestWebhookService_Test(t *testing.T) {
476
	setEnv()
477
	defer unsetEnv()
478
479
	type args struct {
480
		ctx     context.Context
481
		webhook string
482
	}
483
484
	cases := []struct {
485
		name    string
486
		args    args
487
		wantErr bool
488
		err     error
489
		pre     func()
490
		handler http.HandlerFunc
491
	}{
492
		{
493
			"test webhooks works as expected with access token",
494
			args{
495
				context.Background(),
496
				"hook_B2EyhTH5N4KWUnoYPcgiH",
497
			},
498
			false,
499
			nil,
500
			setAccessToken,
501
			func(w http.ResponseWriter, r *http.Request) {
502
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
503
				testMethod(t, r, "POST")
504
505
				w.WriteHeader(http.StatusNoContent)
506
			},
507
		},
508
		{
509
			"test webhooks returns an error when server responds with error",
510
			args{
511
				context.Background(),
512
				"hook_B2EyhTH5N4KWUnoYPcgiH",
513
			},
514
			true,
515
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
516
			setAccessToken,
517
			errorHandler,
518
		},
519
		{
520
			"test webhooks fails when building request with invalid url",
521
			args{
522
				context.Background(),
523
				"hook_B2EyhTH5N4KWUnoYPcgiH",
524
			},
525
			true,
526
			errBadBaseURL,
527
			crashSrv,
528
			errorHandler,
529
		},
530
	}
531
532
	for _, c := range cases {
533
		setup()
534
		defer teardown()
535
536
		t.Run(c.name, func(t *testing.T) {
537
			c.pre()
538
			tMux.HandleFunc("/v2/webhooks/"+c.args.webhook+"/ping", c.handler)
539
540
			res, err := tClient.Webhooks.Test(c.args.ctx, c.args.webhook)
541
			if c.wantErr {
542
				assert.NotNil(t, err)
543
				assert.EqualError(t, err, c.err.Error())
544
			} else {
545
				assert.Nil(t, err)
546
				assert.IsType(t, &http.Response{}, res.Response)
547
			}
548
		})
549
	}
550
}
551