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 ( 62d1d3...ebeb07 )
by
unknown
01:33
created

mollie.TestSalesInvoicesService_Create   C

Complexity

Conditions 8

Size

Total Lines 159
Code Lines 117

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 117
nop 1
dl 0
loc 159
rs 5.1333
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 TestSalesInvoicesService_Create(t *testing.T) {
14
	setEnv()
15
	defer unsetEnv()
16
17
	type args struct {
18
		ctx context.Context
19
		req CreateSalesInvoice
20
	}
21
22
	recipient := SalesInvoiceRecipient{
23
		Type:  ConsumerSalesInvoiceRecipientType,
24
		Email: "[email protected]",
25
		Address: Address{
26
			StreetAndNumber: "Keizersgracht 313",
27
			PostalCode:      "1016 EE",
28
			City:            "Amsterdam",
29
			Country:         "NL",
30
		},
31
		Locale: Dutch,
32
	}
33
34
	lines := []SalesInvoiceLineItem{
35
		{
36
			Description: "Product A",
37
			Quantity:    2,
38
			UnitPrice: Amount{
39
				Currency: "EUR",
40
				Value:    "50.00",
41
			},
42
			VATRate: "21.00",
43
		},
44
	}
45
46
	cases := []struct {
47
		name    string
48
		args    args
49
		wantErr bool
50
		err     error
51
		pre     func()
52
		handler http.HandlerFunc
53
	}{
54
		{
55
			name: "create sales invoice successfully",
56
			args: args{
57
				ctx: context.Background(),
58
				req: CreateSalesInvoice{
59
					Status:              DraftSalesInvoiceStatus,
60
					RecipientIdentifier: "customer_123456789",
61
					Recipient:           recipient,
62
					Lines:               lines,
63
				},
64
			},
65
			wantErr: false,
66
			pre:     noPre,
67
			handler: func(w http.ResponseWriter, r *http.Request) {
68
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
69
				testMethod(t, r, "POST")
70
71
				if _, ok := r.Header[AuthHeader]; !ok {
72
					w.WriteHeader(http.StatusUnauthorized)
73
				}
74
75
				w.Header().Set("Content-Type", "application/json")
76
				w.WriteHeader(http.StatusCreated)
77
				_, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse))
78
			},
79
		},
80
		{
81
			"create sales invoice works as expected with access tokens",
82
			args{
83
				context.Background(),
84
				CreateSalesInvoice{
85
					Status:              DraftSalesInvoiceStatus,
86
					RecipientIdentifier: "customer_123456789",
87
					Recipient:           recipient,
88
					Lines:               lines,
89
				},
90
			},
91
			false,
92
			nil,
93
			setAccessToken,
94
			func(w http.ResponseWriter, r *http.Request) {
95
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
96
				testMethod(t, r, "POST")
97
98
				if _, ok := r.Header[AuthHeader]; !ok {
99
					w.WriteHeader(http.StatusUnauthorized)
100
				}
101
102
				w.WriteHeader(http.StatusCreated)
103
				_, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse))
104
			},
105
		},
106
		{
107
			"create sales invoices error handler",
108
			args{
109
				ctx: context.Background(),
110
				req: CreateSalesInvoice{
111
					Status:              DraftSalesInvoiceStatus,
112
					RecipientIdentifier: "customer_123456789",
113
					Recipient:           recipient,
114
					Lines:               lines,
115
				},
116
			},
117
			true,
118
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
119
			noPre,
120
			errorHandler,
121
		},
122
		{
123
			"create sales invoice, an error occurs when parsing json",
124
			args{
125
				ctx: context.Background(),
126
				req: CreateSalesInvoice{
127
					Status:              DraftSalesInvoiceStatus,
128
					RecipientIdentifier: "customer_123456789",
129
					Recipient:           recipient,
130
					Lines:               lines,
131
				},
132
			},
133
			true,
134
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
135
			noPre,
136
			encodingHandler,
137
		},
138
		{
139
			"create sales invoice, invalid url when building request",
140
			args{
141
				ctx: context.Background(),
142
				req: CreateSalesInvoice{
143
					Status:              DraftSalesInvoiceStatus,
144
					RecipientIdentifier: "customer_123456789",
145
					Recipient:           recipient,
146
					Lines:               lines,
147
				},
148
			},
149
			true,
150
			errBadBaseURL,
151
			crashSrv,
152
			errorHandler,
153
		},
154
	}
155
156
	for _, c := range cases {
157
		setup()
158
		defer teardown()
159
160
		t.Run(c.name, func(t *testing.T) {
161
			c.pre()
162
			tMux.HandleFunc("/v2/sales-invoices", c.handler)
163
164
			res, m, err := tClient.SalesInvoices.Create(c.args.ctx, c.args.req)
165
			if c.wantErr {
166
				assert.NotNil(t, err)
167
				assert.EqualError(t, err, c.err.Error())
168
			} else {
169
				assert.Nil(t, err)
170
				assert.IsType(t, &SalesInvoice{}, m)
171
				assert.IsType(t, &http.Response{}, res.Response)
172
			}
173
		})
174
	}
175
}
176
177
func TestSalesInvoicesService_Get(t *testing.T) {
178
	setEnv()
179
	defer unsetEnv()
180
181
	type args struct {
182
		ctx context.Context
183
		id  string
184
	}
185
186
	cases := []struct {
187
		name    string
188
		args    args
189
		wantErr bool
190
		err     error
191
		pre     func()
192
		handler http.HandlerFunc
193
	}{
194
		{
195
			name: "get sales invoice successfully",
196
			args: args{
197
				ctx: context.Background(),
198
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
199
			},
200
			wantErr: false,
201
			pre:     noPre,
202
			handler: func(w http.ResponseWriter, r *http.Request) {
203
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
204
				testMethod(t, r, "GET")
205
206
				if _, ok := r.Header[AuthHeader]; !ok {
207
					w.WriteHeader(http.StatusUnauthorized)
208
				}
209
210
				w.Header().Set("Content-Type", "application/json")
211
				w.WriteHeader(http.StatusOK)
212
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
213
			},
214
		},
215
		{
216
			"get sales invoice works as expected with access tokens",
217
			args{
218
				context.Background(),
219
				"invoice_4Y0eZitmBnQ6IDoMqZQKh",
220
			},
221
			false,
222
			nil,
223
			setAccessToken,
224
			func(w http.ResponseWriter, r *http.Request) {
225
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
226
				testMethod(t, r, "GET")
227
228
				if _, ok := r.Header[AuthHeader]; !ok {
229
					w.WriteHeader(http.StatusUnauthorized)
230
				}
231
232
				w.WriteHeader(http.StatusOK)
233
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
234
			},
235
		},
236
		{
237
			"get sales invoice error handler",
238
			args{
239
				ctx: context.Background(),
240
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
241
			},
242
			true,
243
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
244
			noPre,
245
			errorHandler,
246
		},
247
		{
248
			"get sales invoice, an error occurs when parsing json",
249
			args{
250
				ctx: context.Background(),
251
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
252
			},
253
			true,
254
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
255
			noPre,
256
			encodingHandler,
257
		},
258
		{
259
			"get sales invoice, invalid url when building request",
260
			args{
261
				ctx: context.Background(),
262
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
263
			},
264
			true,
265
			errBadBaseURL,
266
			crashSrv,
267
			errorHandler,
268
		},
269
	}
270
271
	for _, c := range cases {
272
		setup()
273
		defer teardown()
274
275
		t.Run(c.name, func(t *testing.T) {
276
			c.pre()
277
			tMux.HandleFunc("/v2/sales-invoices/"+c.args.id, c.handler)
278
279
			res, m, err := tClient.SalesInvoices.Get(c.args.ctx, c.args.id)
280
			if c.wantErr {
281
				assert.NotNil(t, err)
282
				assert.EqualError(t, err, c.err.Error())
283
			} else {
284
				assert.Nil(t, err)
285
				assert.IsType(t, &SalesInvoice{}, m)
286
				assert.IsType(t, &http.Response{}, res.Response)
287
			}
288
		})
289
	}
290
}
291
292
func TestSalesInvoicesService_List(t *testing.T) {
293
	setEnv()
294
	defer unsetEnv()
295
296
	type args struct {
297
		ctx  context.Context
298
		opts ListSalesInvoicesOptions
299
	}
300
301
	cases := []struct {
302
		name    string
303
		args    args
304
		wantErr bool
305
		err     error
306
		pre     func()
307
		handler http.HandlerFunc
308
	}{
309
		{
310
			name: "list sales invoices successfully",
311
			args: args{
312
				ctx: context.Background(),
313
				opts: ListSalesInvoicesOptions{
314
					Limit: 5,
315
				},
316
			},
317
			wantErr: false,
318
			pre:     noPre,
319
			handler: func(w http.ResponseWriter, r *http.Request) {
320
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
321
				testMethod(t, r, "GET")
322
323
				if _, ok := r.Header[AuthHeader]; !ok {
324
					w.WriteHeader(http.StatusUnauthorized)
325
				}
326
327
				w.Header().Set("Content-Type", "application/json")
328
				w.WriteHeader(http.StatusOK)
329
				_, _ = w.Write([]byte(testdata.ListSalesInvoicesResponse))
330
			},
331
		},
332
		{
333
			"list sales invoices works as expected with access tokens",
334
			args{
335
				context.Background(),
336
				ListSalesInvoicesOptions{
337
					Limit: 5,
338
				},
339
			},
340
			false,
341
			nil,
342
			setAccessToken,
343
			func(w http.ResponseWriter, r *http.Request) {
344
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
345
				testMethod(t, r, "GET")
346
347
				if _, ok := r.Header[AuthHeader]; !ok {
348
					w.WriteHeader(http.StatusUnauthorized)
349
				}
350
351
				w.WriteHeader(http.StatusOK)
352
				_, _ = w.Write([]byte(testdata.ListSalesInvoicesResponse))
353
			},
354
		},
355
		{
356
			"list sales invoices error handler",
357
			args{
358
				ctx:  context.Background(),
359
				opts: ListSalesInvoicesOptions{},
360
			},
361
			true,
362
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
363
			noPre,
364
			errorHandler,
365
		},
366
		{
367
			"list sales invoices, an error occurs when parsing json",
368
			args{
369
				ctx:  context.Background(),
370
				opts: ListSalesInvoicesOptions{},
371
			},
372
			true,
373
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
374
			noPre,
375
			encodingHandler,
376
		},
377
		{
378
			"list sales invoices, invalid url when building request",
379
			args{
380
				ctx:  context.Background(),
381
				opts: ListSalesInvoicesOptions{},
382
			},
383
			true,
384
			errBadBaseURL,
385
			crashSrv,
386
			errorHandler,
387
		},
388
	}
389
390
	for _, c := range cases {
391
		setup()
392
		defer teardown()
393
394
		t.Run(c.name, func(t *testing.T) {
395
			c.pre()
396
			tMux.HandleFunc("/v2/sales-invoices", c.handler)
397
398
			res, m, err := tClient.SalesInvoices.List(c.args.ctx, &c.args.opts)
399
			if c.wantErr {
400
				assert.NotNil(t, err)
401
				assert.EqualError(t, err, c.err.Error())
402
			} else {
403
				assert.Nil(t, err)
404
				assert.IsType(t, &SalesInvoiceList{}, m)
405
				assert.IsType(t, &http.Response{}, res.Response)
406
			}
407
		})
408
	}
409
}
410
411
func TestSalesInvoicesService_Delete(t *testing.T) {
412
	setEnv()
413
	defer unsetEnv()
414
415
	type args struct {
416
		ctx context.Context
417
		id  string
418
	}
419
420
	cases := []struct {
421
		name    string
422
		args    args
423
		wantErr bool
424
		err     error
425
		pre     func()
426
		handler http.HandlerFunc
427
	}{
428
		{
429
			name: "delete sales invoice successfully",
430
			args: args{
431
				ctx: context.Background(),
432
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
433
			},
434
			wantErr: false,
435
			pre:     noPre,
436
			handler: func(w http.ResponseWriter, r *http.Request) {
437
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
438
				testMethod(t, r, "DELETE")
439
440
				if _, ok := r.Header[AuthHeader]; !ok {
441
					w.WriteHeader(http.StatusUnauthorized)
442
				}
443
444
				w.WriteHeader(http.StatusNoContent)
445
			},
446
		},
447
		{
448
			"delete sales invoice works as expected with access tokens",
449
			args{
450
				context.Background(),
451
				"invoice_4Y0eZitmBnQ6IDoMqZQKh",
452
			},
453
			false,
454
			nil,
455
			setAccessToken,
456
			func(w http.ResponseWriter, r *http.Request) {
457
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
458
				testMethod(t, r, "DELETE")
459
460
				if _, ok := r.Header[AuthHeader]; !ok {
461
					w.WriteHeader(http.StatusUnauthorized)
462
				}
463
464
				w.WriteHeader(http.StatusNoContent)
465
			},
466
		},
467
		{
468
			"delete sales invoice error handler",
469
			args{
470
				ctx: context.Background(),
471
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
472
			},
473
			true,
474
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
475
			noPre,
476
			errorHandler,
477
		},
478
		{
479
			"delete sales invoice, invalid url when building request",
480
			args{
481
				ctx: context.Background(),
482
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
483
			},
484
			true,
485
			errBadBaseURL,
486
			crashSrv,
487
			errorHandler,
488
		},
489
	}
490
491
	for _, c := range cases {
492
		setup()
493
		defer teardown()
494
495
		t.Run(c.name, func(t *testing.T) {
496
			c.pre()
497
			tMux.HandleFunc("/v2/sales-invoices/"+c.args.id, c.handler)
498
499
			res, err := tClient.SalesInvoices.Delete(c.args.ctx, c.args.id)
500
			if c.wantErr {
501
				assert.NotNil(t, err)
502
				assert.EqualError(t, err, c.err.Error())
503
			} else {
504
				assert.Nil(t, err)
505
				assert.IsType(t, &http.Response{}, res.Response)
506
			}
507
		})
508
	}
509
}
510
511
func TestSalesInvoicesService_Update(t *testing.T) {
512
	setEnv()
513
	defer unsetEnv()
514
515
	type args struct {
516
		ctx context.Context
517
		id  string
518
		req UpdateSalesInvoice
519
	}
520
521
	cases := []struct {
522
		name    string
523
		args    args
524
		wantErr bool
525
		err     error
526
		pre     func()
527
		handler http.HandlerFunc
528
	}{
529
		{
530
			name: "update sales invoice successfully",
531
			args: args{
532
				ctx: context.Background(),
533
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
534
				req: UpdateSalesInvoice{
535
					Status: PaidSalesInvoiceStatus,
536
				},
537
			},
538
			wantErr: false,
539
			pre:     noPre,
540
			handler: func(w http.ResponseWriter, r *http.Request) {
541
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
542
				testMethod(t, r, "PATCH")
543
544
				if _, ok := r.Header[AuthHeader]; !ok {
545
					w.WriteHeader(http.StatusUnauthorized)
546
				}
547
548
				w.Header().Set("Content-Type", "application/json")
549
				w.WriteHeader(http.StatusOK)
550
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
551
			},
552
		},
553
		{
554
			"update sales invoice works as expected with access tokens",
555
			args{
556
				context.Background(),
557
				"invoice_4Y0eZitmBnQ6IDoMqZQKh",
558
				UpdateSalesInvoice{
559
					Status: PaidSalesInvoiceStatus,
560
				},
561
			},
562
			false,
563
			nil,
564
			setAccessToken,
565
			func(w http.ResponseWriter, r *http.Request) {
566
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
567
				testMethod(t, r, "PATCH")
568
569
				if _, ok := r.Header[AuthHeader]; !ok {
570
					w.WriteHeader(http.StatusUnauthorized)
571
				}
572
573
				w.WriteHeader(http.StatusOK)
574
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
575
			},
576
		},
577
		{
578
			"update sales invoice error handler",
579
			args{
580
				ctx: context.Background(),
581
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
582
				req: UpdateSalesInvoice{
583
					Status: PaidSalesInvoiceStatus,
584
				},
585
			},
586
			true,
587
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
588
			noPre,
589
			errorHandler,
590
		},
591
		{
592
			"update sales invoice, an error occurs when parsing json",
593
			args{
594
				ctx: context.Background(),
595
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
596
				req: UpdateSalesInvoice{
597
					Status: PaidSalesInvoiceStatus,
598
				},
599
			},
600
			true,
601
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
602
			noPre,
603
			encodingHandler,
604
		},
605
		{
606
			"update sales invoice, invalid url when building request",
607
			args{
608
				ctx: context.Background(),
609
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
610
				req: UpdateSalesInvoice{
611
					Status: PaidSalesInvoiceStatus,
612
				},
613
			},
614
			true,
615
			errBadBaseURL,
616
			crashSrv,
617
			errorHandler,
618
		},
619
	}
620
621
	for _, c := range cases {
622
		setup()
623
		defer teardown()
624
625
		t.Run(c.name, func(t *testing.T) {
626
			c.pre()
627
			tMux.HandleFunc("/v2/sales-invoices/"+c.args.id, c.handler)
628
629
			res, m, err := tClient.SalesInvoices.Update(c.args.ctx, c.args.id, c.args.req)
630
			if c.wantErr {
631
				assert.NotNil(t, err)
632
				assert.EqualError(t, err, c.err.Error())
633
			} else {
634
				assert.Nil(t, err)
635
				assert.IsType(t, &SalesInvoice{}, m)
636
				assert.IsType(t, &http.Response{}, res.Response)
637
			}
638
		})
639
	}
640
}
641