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.TestSalesInvoicesService_Create   F
last analyzed

Complexity

Conditions 17

Size

Total Lines 296
Code Lines 210

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 210
nop 1
dl 0
loc 296
rs 1.2599
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like mollie.TestSalesInvoicesService_Create often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package mollie
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"fmt"
7
	"net/http"
8
	"testing"
9
10
	"github.com/VictorAvelar/mollie-api-go/v4/testdata"
11
	"github.com/stretchr/testify/assert"
12
)
13
14
func TestSalesInvoicesService_Create(t *testing.T) {
15
	setEnv()
16
	defer unsetEnv()
17
18
	type args struct {
19
		ctx context.Context
20
		req CreateSalesInvoice
21
	}
22
23
	recipient := SalesInvoiceRecipient{
24
		Type:  ConsumerSalesInvoiceRecipientType,
25
		Email: "[email protected]",
26
		Address: Address{
27
			StreetAndNumber: "Keizersgracht 313",
28
			PostalCode:      "1016 EE",
29
			City:            "Amsterdam",
30
			Country:         "NL",
31
		},
32
		Locale: Dutch,
33
	}
34
35
	lines := []SalesInvoiceLineItem{
36
		{
37
			Description: "Product A",
38
			Quantity:    2,
39
			UnitPrice: Amount{
40
				Currency: "EUR",
41
				Value:    "50.00",
42
			},
43
			VATRate: "21.00",
44
		},
45
	}
46
47
	issuedCreateHandler := func(w http.ResponseWriter, r *http.Request) {
48
		testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
49
		testMethod(t, r, "POST")
50
51
		if _, ok := r.Header[AuthHeader]; !ok {
52
			w.WriteHeader(http.StatusUnauthorized)
53
			return
54
		}
55
56
		var payload map[string]any
57
		if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
58
			w.WriteHeader(http.StatusBadRequest)
59
			return
60
		}
61
62
		status, _ := payload["status"].(string)
63
		paymentDetails, hasPaymentDetails := payload["paymentDetails"].(map[string]any)
64
65
		if hasPaymentDetails {
66
			if source, ok := paymentDetails["source"].(string); !ok || source == "" {
67
				w.Header().Set("Content-Type", "application/json")
68
				w.WriteHeader(http.StatusUnprocessableEntity)
69
				_, _ = w.Write([]byte(`{"status":422,"title":"Unprocessable Entity","detail":"The 'source' field is required when providing paymentDetails.","field":"paymentDetails.source"}`))
70
				return
71
			}
72
73
			if status == string(IssuedSalesInvoiceStatus) {
74
				w.Header().Set("Content-Type", "application/json")
75
				w.WriteHeader(http.StatusUnprocessableEntity)
76
				_, _ = w.Write([]byte(`{"status":422,"title":"Unprocessable Entity","detail":"The 'paymentDetails' field is not allowed when status is 'issued'.","field":"paymentDetails"}`))
77
				return
78
			}
79
		}
80
81
		w.Header().Set("Content-Type", "application/json")
82
		w.WriteHeader(http.StatusCreated)
83
		_, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse))
84
	}
85
86
	cases := []struct {
87
		name    string
88
		args    args
89
		wantErr bool
90
		err     error
91
		pre     func()
92
		handler http.HandlerFunc
93
	}{
94
		{
95
			name: "create sales invoice successfully",
96
			args: args{
97
				ctx: context.Background(),
98
				req: CreateSalesInvoice{
99
					Status:              DraftSalesInvoiceStatus,
100
					RecipientIdentifier: "customer_123456789",
101
					Recipient:           recipient,
102
					Lines:               lines,
103
				},
104
			},
105
			wantErr: false,
106
			pre:     noPre,
107
			handler: func(w http.ResponseWriter, r *http.Request) {
108
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
109
				testMethod(t, r, "POST")
110
111
				if _, ok := r.Header[AuthHeader]; !ok {
112
					w.WriteHeader(http.StatusUnauthorized)
113
				}
114
115
				w.Header().Set("Content-Type", "application/json")
116
				w.WriteHeader(http.StatusCreated)
117
				_, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse))
118
			},
119
		},
120
		{
121
			"create sales invoice works as expected with access tokens",
122
			args{
123
				context.Background(),
124
				CreateSalesInvoice{
125
					Status:              DraftSalesInvoiceStatus,
126
					RecipientIdentifier: "customer_123456789",
127
					Recipient:           recipient,
128
					Lines:               lines,
129
				},
130
			},
131
			false,
132
			nil,
133
			setAccessToken,
134
			func(w http.ResponseWriter, r *http.Request) {
135
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
136
				testMethod(t, r, "POST")
137
138
				if _, ok := r.Header[AuthHeader]; !ok {
139
					w.WriteHeader(http.StatusUnauthorized)
140
				}
141
142
				w.WriteHeader(http.StatusCreated)
143
				_, _ = w.Write([]byte(testdata.CreateSalesInvoicesResponse))
144
			},
145
		},
146
		{
147
			"create sales invoices error handler",
148
			args{
149
				ctx: context.Background(),
150
				req: CreateSalesInvoice{
151
					Status:              DraftSalesInvoiceStatus,
152
					RecipientIdentifier: "customer_123456789",
153
					Recipient:           recipient,
154
					Lines:               lines,
155
				},
156
			},
157
			true,
158
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
159
			noPre,
160
			errorHandler,
161
		},
162
		{
163
			"create sales invoice, an error occurs when parsing json",
164
			args{
165
				ctx: context.Background(),
166
				req: CreateSalesInvoice{
167
					Status:              DraftSalesInvoiceStatus,
168
					RecipientIdentifier: "customer_123456789",
169
					Recipient:           recipient,
170
					Lines:               lines,
171
				},
172
			},
173
			true,
174
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
175
			noPre,
176
			encodingHandler,
177
		},
178
		{
179
			"create sales invoice, invalid url when building request",
180
			args{
181
				ctx: context.Background(),
182
				req: CreateSalesInvoice{
183
					Status:              DraftSalesInvoiceStatus,
184
					RecipientIdentifier: "customer_123456789",
185
					Recipient:           recipient,
186
					Lines:               lines,
187
				},
188
			},
189
			true,
190
			errBadBaseURL,
191
			crashSrv,
192
			errorHandler,
193
		},
194
		{
195
			name: "create sales invoice, payment details list in response",
196
			args: args{
197
				ctx: context.Background(),
198
				req: CreateSalesInvoice{
199
					Status:              DraftSalesInvoiceStatus,
200
					RecipientIdentifier: "customer_123456789",
201
					Recipient:           recipient,
202
					Lines:               lines,
203
				},
204
			},
205
			wantErr: false,
206
			err:     nil,
207
			pre:     noPre,
208
			handler: func(w http.ResponseWriter, r *http.Request) {
209
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
210
				testMethod(t, r, "POST")
211
212
				if _, ok := r.Header[AuthHeader]; !ok {
213
					w.WriteHeader(http.StatusUnauthorized)
214
					return
215
				}
216
217
				w.Header().Set("Content-Type", "application/json")
218
				w.WriteHeader(http.StatusCreated)
219
				_, _ = w.Write([]byte(`{
220
  "resource": "sales-invoice",
221
  "id": "invoice_4Y0eZitmBnQ6IDoMqZQKh",
222
  "status": "paid",
223
  "paymentDetails": [
224
    { "source": "manual", "sourceReference": "ref_1" },
225
    { "source": "payment", "sourceReference": "tr_2" }
226
  ]
227
}`))
228
			},
229
		},
230
		{
231
			name: "create issued sales invoice, without payment details",
232
			args: args{
233
				ctx: context.Background(),
234
				req: CreateSalesInvoice{
235
					Status:              IssuedSalesInvoiceStatus,
236
					RecipientIdentifier: "customer_123456789",
237
					Recipient:           recipient,
238
					Lines:               lines,
239
				},
240
			},
241
			wantErr: false,
242
			err:     nil,
243
			pre:     noPre,
244
			handler: issuedCreateHandler,
245
		},
246
		{
247
			name: "create issued sales invoice, payment details without source returns error",
248
			args: args{
249
				ctx: context.Background(),
250
				req: CreateSalesInvoice{
251
					Status:              IssuedSalesInvoiceStatus,
252
					RecipientIdentifier: "customer_123456789",
253
					Recipient:           recipient,
254
					Lines:               lines,
255
					PaymentDetails:      &SalesInvoicePaymentDetails{},
256
				},
257
			},
258
			wantErr: true,
259
			err: &BaseError{
260
				Status: http.StatusUnprocessableEntity,
261
				Title:  "Unprocessable Entity",
262
				Detail: "The 'source' field is required when providing paymentDetails.",
263
				Field:  "paymentDetails.source",
264
			},
265
			pre:     noPre,
266
			handler: issuedCreateHandler,
267
		},
268
		{
269
			name: "create issued sales invoice, payment details with source returns error",
270
			args: args{
271
				ctx: context.Background(),
272
				req: CreateSalesInvoice{
273
					Status:              IssuedSalesInvoiceStatus,
274
					RecipientIdentifier: "customer_123456789",
275
					Recipient:           recipient,
276
					Lines:               lines,
277
					PaymentDetails: &SalesInvoicePaymentDetails{
278
						Source: ManualSalesInvoiceSource,
279
					},
280
				},
281
			},
282
			wantErr: true,
283
			err: &BaseError{
284
				Status: http.StatusUnprocessableEntity,
285
				Title:  "Unprocessable Entity",
286
				Detail: "The 'paymentDetails' field is not allowed when status is 'issued'.",
287
				Field:  "paymentDetails",
288
			},
289
			pre:     noPre,
290
			handler: issuedCreateHandler,
291
		},
292
	}
293
294
	for _, c := range cases {
295
		setup()
296
		defer teardown()
297
298
		t.Run(c.name, func(t *testing.T) {
299
			c.pre()
300
			tMux.HandleFunc("/v2/sales-invoices", c.handler)
301
302
			res, m, err := tClient.SalesInvoices.Create(c.args.ctx, c.args.req)
303
			if c.wantErr {
304
				assert.NotNil(t, err)
305
				assert.EqualError(t, err, c.err.Error())
306
			} else {
307
				assert.Nil(t, err)
308
				assert.IsType(t, &SalesInvoice{}, m)
309
				assert.IsType(t, &http.Response{}, res.Response)
310
			}
311
		})
312
	}
313
}
314
315
func TestSalesInvoicesService_Get(t *testing.T) {
316
	setEnv()
317
	defer unsetEnv()
318
319
	type args struct {
320
		ctx context.Context
321
		id  string
322
	}
323
324
	cases := []struct {
325
		name    string
326
		args    args
327
		wantErr bool
328
		err     error
329
		pre     func()
330
		handler http.HandlerFunc
331
	}{
332
		{
333
			name: "get sales invoice successfully",
334
			args: args{
335
				ctx: context.Background(),
336
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
337
			},
338
			wantErr: false,
339
			pre:     noPre,
340
			handler: func(w http.ResponseWriter, r *http.Request) {
341
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
342
				testMethod(t, r, "GET")
343
344
				if _, ok := r.Header[AuthHeader]; !ok {
345
					w.WriteHeader(http.StatusUnauthorized)
346
				}
347
348
				w.Header().Set("Content-Type", "application/json")
349
				w.WriteHeader(http.StatusOK)
350
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
351
			},
352
		},
353
		{
354
			"get sales invoice works as expected with access tokens",
355
			args{
356
				context.Background(),
357
				"invoice_4Y0eZitmBnQ6IDoMqZQKh",
358
			},
359
			false,
360
			nil,
361
			setAccessToken,
362
			func(w http.ResponseWriter, r *http.Request) {
363
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
364
				testMethod(t, r, "GET")
365
366
				if _, ok := r.Header[AuthHeader]; !ok {
367
					w.WriteHeader(http.StatusUnauthorized)
368
				}
369
370
				w.WriteHeader(http.StatusOK)
371
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
372
			},
373
		},
374
		{
375
			"get sales invoice error handler",
376
			args{
377
				ctx: context.Background(),
378
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
379
			},
380
			true,
381
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
382
			noPre,
383
			errorHandler,
384
		},
385
		{
386
			"get sales invoice, an error occurs when parsing json",
387
			args{
388
				ctx: context.Background(),
389
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
390
			},
391
			true,
392
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
393
			noPre,
394
			encodingHandler,
395
		},
396
		{
397
			"get sales invoice, invalid url when building request",
398
			args{
399
				ctx: context.Background(),
400
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
401
			},
402
			true,
403
			errBadBaseURL,
404
			crashSrv,
405
			errorHandler,
406
		},
407
	}
408
409
	for _, c := range cases {
410
		setup()
411
		defer teardown()
412
413
		t.Run(c.name, func(t *testing.T) {
414
			c.pre()
415
			tMux.HandleFunc("/v2/sales-invoices/"+c.args.id, c.handler)
416
417
			res, m, err := tClient.SalesInvoices.Get(c.args.ctx, c.args.id)
418
			if c.wantErr {
419
				assert.NotNil(t, err)
420
				assert.EqualError(t, err, c.err.Error())
421
			} else {
422
				assert.Nil(t, err)
423
				assert.IsType(t, &SalesInvoice{}, m)
424
				assert.IsType(t, &http.Response{}, res.Response)
425
			}
426
		})
427
	}
428
}
429
430
func TestSalesInvoicesService_List(t *testing.T) {
431
	setEnv()
432
	defer unsetEnv()
433
434
	type args struct {
435
		ctx  context.Context
436
		opts ListSalesInvoicesOptions
437
	}
438
439
	cases := []struct {
440
		name    string
441
		args    args
442
		wantErr bool
443
		err     error
444
		pre     func()
445
		handler http.HandlerFunc
446
	}{
447
		{
448
			name: "list sales invoices successfully",
449
			args: args{
450
				ctx: context.Background(),
451
				opts: ListSalesInvoicesOptions{
452
					Limit: 5,
453
				},
454
			},
455
			wantErr: false,
456
			pre:     noPre,
457
			handler: func(w http.ResponseWriter, r *http.Request) {
458
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
459
				testMethod(t, r, "GET")
460
461
				if _, ok := r.Header[AuthHeader]; !ok {
462
					w.WriteHeader(http.StatusUnauthorized)
463
				}
464
465
				w.Header().Set("Content-Type", "application/json")
466
				w.WriteHeader(http.StatusOK)
467
				_, _ = w.Write([]byte(testdata.ListSalesInvoicesResponse))
468
			},
469
		},
470
		{
471
			"list sales invoices works as expected with access tokens",
472
			args{
473
				context.Background(),
474
				ListSalesInvoicesOptions{
475
					Limit: 5,
476
				},
477
			},
478
			false,
479
			nil,
480
			setAccessToken,
481
			func(w http.ResponseWriter, r *http.Request) {
482
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
483
				testMethod(t, r, "GET")
484
485
				if _, ok := r.Header[AuthHeader]; !ok {
486
					w.WriteHeader(http.StatusUnauthorized)
487
				}
488
489
				w.WriteHeader(http.StatusOK)
490
				_, _ = w.Write([]byte(testdata.ListSalesInvoicesResponse))
491
			},
492
		},
493
		{
494
			"list sales invoices error handler",
495
			args{
496
				ctx:  context.Background(),
497
				opts: ListSalesInvoicesOptions{},
498
			},
499
			true,
500
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
501
			noPre,
502
			errorHandler,
503
		},
504
		{
505
			"list sales invoices, an error occurs when parsing json",
506
			args{
507
				ctx:  context.Background(),
508
				opts: ListSalesInvoicesOptions{},
509
			},
510
			true,
511
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
512
			noPre,
513
			encodingHandler,
514
		},
515
		{
516
			"list sales invoices, invalid url when building request",
517
			args{
518
				ctx:  context.Background(),
519
				opts: ListSalesInvoicesOptions{},
520
			},
521
			true,
522
			errBadBaseURL,
523
			crashSrv,
524
			errorHandler,
525
		},
526
	}
527
528
	for _, c := range cases {
529
		setup()
530
		defer teardown()
531
532
		t.Run(c.name, func(t *testing.T) {
533
			c.pre()
534
			tMux.HandleFunc("/v2/sales-invoices", c.handler)
535
536
			res, m, err := tClient.SalesInvoices.List(c.args.ctx, &c.args.opts)
537
			if c.wantErr {
538
				assert.NotNil(t, err)
539
				assert.EqualError(t, err, c.err.Error())
540
			} else {
541
				assert.Nil(t, err)
542
				assert.IsType(t, &SalesInvoiceList{}, m)
543
				assert.IsType(t, &http.Response{}, res.Response)
544
			}
545
		})
546
	}
547
}
548
549
func TestSalesInvoicesService_Delete(t *testing.T) {
550
	setEnv()
551
	defer unsetEnv()
552
553
	type args struct {
554
		ctx context.Context
555
		id  string
556
	}
557
558
	cases := []struct {
559
		name    string
560
		args    args
561
		wantErr bool
562
		err     error
563
		pre     func()
564
		handler http.HandlerFunc
565
	}{
566
		{
567
			name: "delete sales invoice successfully",
568
			args: args{
569
				ctx: context.Background(),
570
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
571
			},
572
			wantErr: false,
573
			pre:     noPre,
574
			handler: func(w http.ResponseWriter, r *http.Request) {
575
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
576
				testMethod(t, r, "DELETE")
577
578
				if _, ok := r.Header[AuthHeader]; !ok {
579
					w.WriteHeader(http.StatusUnauthorized)
580
				}
581
582
				w.WriteHeader(http.StatusNoContent)
583
			},
584
		},
585
		{
586
			"delete sales invoice works as expected with access tokens",
587
			args{
588
				context.Background(),
589
				"invoice_4Y0eZitmBnQ6IDoMqZQKh",
590
			},
591
			false,
592
			nil,
593
			setAccessToken,
594
			func(w http.ResponseWriter, r *http.Request) {
595
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
596
				testMethod(t, r, "DELETE")
597
598
				if _, ok := r.Header[AuthHeader]; !ok {
599
					w.WriteHeader(http.StatusUnauthorized)
600
				}
601
602
				w.WriteHeader(http.StatusNoContent)
603
			},
604
		},
605
		{
606
			"delete sales invoice error handler",
607
			args{
608
				ctx: context.Background(),
609
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
610
			},
611
			true,
612
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
613
			noPre,
614
			errorHandler,
615
		},
616
		{
617
			"delete sales invoice, invalid url when building request",
618
			args{
619
				ctx: context.Background(),
620
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
621
			},
622
			true,
623
			errBadBaseURL,
624
			crashSrv,
625
			errorHandler,
626
		},
627
	}
628
629
	for _, c := range cases {
630
		setup()
631
		defer teardown()
632
633
		t.Run(c.name, func(t *testing.T) {
634
			c.pre()
635
			tMux.HandleFunc("/v2/sales-invoices/"+c.args.id, c.handler)
636
637
			res, err := tClient.SalesInvoices.Delete(c.args.ctx, c.args.id)
638
			if c.wantErr {
639
				assert.NotNil(t, err)
640
				assert.EqualError(t, err, c.err.Error())
641
			} else {
642
				assert.Nil(t, err)
643
				assert.IsType(t, &http.Response{}, res.Response)
644
			}
645
		})
646
	}
647
}
648
649
func TestSalesInvoicesService_Update(t *testing.T) {
650
	setEnv()
651
	defer unsetEnv()
652
653
	type args struct {
654
		ctx context.Context
655
		id  string
656
		req UpdateSalesInvoice
657
	}
658
659
	cases := []struct {
660
		name    string
661
		args    args
662
		wantErr bool
663
		err     error
664
		pre     func()
665
		handler http.HandlerFunc
666
	}{
667
		{
668
			name: "update sales invoice successfully",
669
			args: args{
670
				ctx: context.Background(),
671
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
672
				req: UpdateSalesInvoice{
673
					Status: PaidSalesInvoiceStatus,
674
				},
675
			},
676
			wantErr: false,
677
			pre:     noPre,
678
			handler: func(w http.ResponseWriter, r *http.Request) {
679
				testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
680
				testMethod(t, r, "PATCH")
681
682
				if _, ok := r.Header[AuthHeader]; !ok {
683
					w.WriteHeader(http.StatusUnauthorized)
684
				}
685
686
				w.Header().Set("Content-Type", "application/json")
687
				w.WriteHeader(http.StatusOK)
688
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
689
			},
690
		},
691
		{
692
			"update sales invoice works as expected with access tokens",
693
			args{
694
				context.Background(),
695
				"invoice_4Y0eZitmBnQ6IDoMqZQKh",
696
				UpdateSalesInvoice{
697
					Status: PaidSalesInvoiceStatus,
698
				},
699
			},
700
			false,
701
			nil,
702
			setAccessToken,
703
			func(w http.ResponseWriter, r *http.Request) {
704
				testHeader(t, r, AuthHeader, "Bearer access_token_test")
705
				testMethod(t, r, "PATCH")
706
707
				if _, ok := r.Header[AuthHeader]; !ok {
708
					w.WriteHeader(http.StatusUnauthorized)
709
				}
710
711
				w.WriteHeader(http.StatusOK)
712
				_, _ = w.Write([]byte(testdata.GetSalesInvoicesResponse))
713
			},
714
		},
715
		{
716
			"update sales invoice error handler",
717
			args{
718
				ctx: context.Background(),
719
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
720
				req: UpdateSalesInvoice{
721
					Status: PaidSalesInvoiceStatus,
722
				},
723
			},
724
			true,
725
			fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"),
726
			noPre,
727
			errorHandler,
728
		},
729
		{
730
			"update sales invoice, an error occurs when parsing json",
731
			args{
732
				ctx: context.Background(),
733
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
734
				req: UpdateSalesInvoice{
735
					Status: PaidSalesInvoiceStatus,
736
				},
737
			},
738
			true,
739
			fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
740
			noPre,
741
			encodingHandler,
742
		},
743
		{
744
			"update sales invoice, invalid url when building request",
745
			args{
746
				ctx: context.Background(),
747
				id:  "invoice_4Y0eZitmBnQ6IDoMqZQKh",
748
				req: UpdateSalesInvoice{
749
					Status: PaidSalesInvoiceStatus,
750
				},
751
			},
752
			true,
753
			errBadBaseURL,
754
			crashSrv,
755
			errorHandler,
756
		},
757
	}
758
759
	for _, c := range cases {
760
		setup()
761
		defer teardown()
762
763
		t.Run(c.name, func(t *testing.T) {
764
			c.pre()
765
			tMux.HandleFunc("/v2/sales-invoices/"+c.args.id, c.handler)
766
767
			res, m, err := tClient.SalesInvoices.Update(c.args.ctx, c.args.id, c.args.req)
768
			if c.wantErr {
769
				assert.NotNil(t, err)
770
				assert.EqualError(t, err, c.err.Error())
771
			} else {
772
				assert.Nil(t, err)
773
				assert.IsType(t, &SalesInvoice{}, m)
774
				assert.IsType(t, &http.Response{}, res.Response)
775
			}
776
		})
777
	}
778
}
779