forked from getlago/lago-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer_test.go
More file actions
788 lines (716 loc) · 27.1 KB
/
customer_test.go
File metadata and controls
788 lines (716 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package lago_test
import (
"context"
"testing"
qt "github.com/frankban/quicktest"
. "github.com/bentoml/lago-go-client"
lt "github.com/bentoml/lago-go-client/testing"
)
// Mock response for customer list
var mockCustomerListResponse = `{
"customers": [
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"sequential_id": 1,
"slug": "LAG-1234-001",
"external_id": "CUSTOMER_1",
"billing_entity_code": "acme_corp",
"name": "John Doe",
"firstname": "John",
"lastname": "Doe",
"customer_type": "company",
"email": "customer@example.com",
"address_line1": "5230 Penfield Ave",
"address_line2": null,
"city": "Woodland Hills",
"state": "CA",
"zipcode": "91364",
"country": "US",
"legal_name": "Acme Corp",
"legal_number": "123456789",
"net_payment_term": 30,
"tax_identification_number": "US123456789",
"logo_url": "https://getlago.com/logo.png",
"phone": "+1-555-123-4567",
"url": "https://acme.com",
"finalize_zero_amount_invoice": "finalize",
"billing_configuration": {
"invoice_grace_period": 3,
"payment_provider": "stripe",
"payment_provider_code": "stripe_123",
"provider_customer_id": "cus_123456",
"sync_with_provider": true,
"document_locale": "en",
"provider_payment_methods": ["card", "sepa_debit"]
},
"shipping_address": {
"address_line1": "123 Shipping St",
"address_line2": "Suite 456",
"city": "Shipping City",
"zipcode": "12345",
"state": "NY",
"country": "US"
},
"integration_customers": [
{
"lago_id": "2b902b90-2b90-2b90-2b90-2b902b902b90",
"external_customer_id": "netsuite_123",
"type": "netsuite",
"integration_code": "netsuite_integration",
"subsidiary_id": "sub_123",
"sync_with_provider": true
}
],
"metadata": [
{
"lago_id": "3c903c90-3c90-3c90-3c90-3c903c903c90",
"key": "department",
"value": "engineering",
"display_in_invoice": true,
"created_at": "2022-04-29T08:59:51Z"
}
],
"currency": "USD",
"timezone": "America/New_York",
"applicable_timezone": "America/New_York",
"skip_invoice_custom_sections": false,
"taxes": [],
"applicable_invoice_custom_sections": [],
"created_at": "2022-04-29T08:59:51Z",
"updated_at": "2022-04-29T08:59:51Z"
},
{
"lago_id": "2b902b90-2b90-2b90-2b90-2b902b902b90",
"sequential_id": 2,
"slug": "LAG-1234-002",
"external_id": "CUSTOMER_2",
"billing_entity_code": "acme_corp",
"name": "Jane Smith",
"firstname": "Jane",
"lastname": "Smith",
"customer_type": "individual",
"email": "jane@example.com",
"address_line1": "456 Oak Street",
"address_line2": "Apt 789",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102",
"country": "US",
"legal_name": "Jane Smith",
"legal_number": "987654321",
"net_payment_term": 15,
"tax_identification_number": "US987654321",
"logo_url": null,
"phone": "+1-555-987-6543",
"url": "https://janesmith.com",
"finalize_zero_amount_invoice": "skip",
"billing_configuration": {
"invoice_grace_period": 5,
"payment_provider": "adyen",
"payment_provider_code": "adyen_456",
"provider_customer_id": "adyen_customer_789",
"sync_with_provider": false,
"document_locale": "fr",
"provider_payment_methods": ["card", "us_bank_account"]
},
"shipping_address": {
"address_line1": "789 Delivery Ave",
"address_line2": null,
"city": "Delivery City",
"zipcode": "54321",
"state": "TX",
"country": "US"
},
"integration_customers": [],
"metadata": [
{
"lago_id": "4d904d90-4d90-4d90-4d90-4d904d904d90",
"key": "segment",
"value": "enterprise",
"display_in_invoice": false,
"created_at": "2022-04-29T08:59:51Z"
}
],
"currency": "EUR",
"timezone": "Europe/Paris",
"applicable_timezone": "Europe/Paris",
"skip_invoice_custom_sections": true,
"taxes": [],
"applicable_invoice_custom_sections": [],
"created_at": "2022-04-29T08:59:51Z",
"updated_at": "2022-04-29T08:59:51Z"
}
],
"meta": {
"current_page": 1,
"next_page": 0,
"prev_page": 0,
"total_pages": 1,
"total_count": 2
}
}`
// Mock response for customer invoice list
var mockCustomerInvoiceListResponse = map[string]any{
"invoices": []map[string]interface{}{
mockInvoice,
},
"meta": map[string]interface{}{
"current_page": 1,
"next_page": 0,
"prev_page": 0,
"total_pages": 1,
"total_count": 1,
},
}
// Mock response for customer credit note list
var mockCustomerCreditNoteListResponse = map[string]any{
"credit_notes": []map[string]interface{}{
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"sequential_id": 2,
"number": "LAG-1234-CN-001-002",
"lago_invoice_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"invoice_number": "LAG-1234-001-002",
"issuing_date": "2022-04-30",
"credit_status": "available",
"refund_status": "pending",
"reason": "duplicated_charge",
"description": "Duplicated charge",
"currency": "EUR",
"total_amount_cents": 120,
"credit_amount_cents": 100,
"balance_amount_cents": 100,
"refund_amount_cents": 0,
"coupons_adjustment_amount_cents": 0,
"taxes_amount_cents": 20,
"sub_total_excluding_taxes_amount_cents": 100,
"max_creditable_amount_cents": 100,
"max_refundable_amount_cents": 100,
"precise_coupons_adjustment_amount_cents": "0.0",
"created_at": "2022-04-29T08:59:51Z",
"updated_at": "2022-04-29T08:59:51Z",
"file_url": "https://getlago.com/credit_note/file",
"voided_at": nil,
"self_billed": false,
},
},
"meta": map[string]interface{}{
"current_page": 1,
"next_page": 0,
"prev_page": 0,
"total_pages": 1,
"total_count": 1,
},
}
// Mock response for customer payment list
var mockCustomerPaymentListResponse = map[string]any{
"payments": []map[string]interface{}{
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"amount_cents": 1200,
"amount_currency": "EUR",
"payment_status": "succeeded",
"type": "manual",
"reference": "REF-123456",
"external_payment_id": "ext_payment_123",
"created_at": "2022-04-29T08:59:51Z",
"invoice_ids": []string{"1a901a90-1a90-1a90-1a90-1a901a901a90"},
"external_customer_id": "CUSTOMER_1",
},
},
"meta": map[string]interface{}{
"current_page": 1,
"next_page": 0,
"prev_page": 0,
"total_pages": 1,
"total_count": 1,
},
}
// Mock response for customer payment request list
var mockCustomerPaymentRequestListResponse = map[string]any{
"payment_requests": []map[string]interface{}{
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"email": "customer@example.com",
"amount_cents": 1200,
"amount_currency": "EUR",
"payment_status": "pending",
"created_at": "2022-04-29T08:59:51Z",
"customer": map[string]interface{}{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_id": "CUSTOMER_1",
"name": "John Doe",
"email": "customer@example.com",
},
"invoices": []map[string]interface{}{
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"number": "LAG-1234-001-002",
},
},
},
},
"meta": map[string]interface{}{
"current_page": 1,
"next_page": 0,
"prev_page": 0,
"total_pages": 1,
"total_count": 1,
},
}
// Mock response for customer applied coupon list
var mockCustomerAppliedCouponListResponse = map[string]any{
"applied_coupons": []map[string]interface{}{
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"lago_coupon_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"coupon_code": "APPLIED_COUPON",
"coupon_name": "Startup Deal",
"lago_customer_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_customer_id": "CUSTOMER_1",
"status": "active",
"amount_cents": 2000,
"amount_cents_remaining": 50,
"amount_currency": "EUR",
"percentage_rate": nil,
"frequency": "recurring",
"frequency_duration": 3,
"frequency_duration_remaining": 1,
"expiration_at": "2022-04-29T08:59:51Z",
"created_at": "2022-04-29T08:59:51Z",
"terminated_at": "2022-04-29T08:59:51Z",
},
},
"meta": map[string]interface{}{
"current_page": 1,
"next_page": 0,
"prev_page": 0,
"total_pages": 1,
"total_count": 1,
},
}
// Mock response for customer subscription list
var mockCustomerSubscriptionListResponse = map[string]any{
"subscriptions": []map[string]interface{}{
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba",
"lago_customer_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_customer_id": "CUSTOMER_1",
"billing_time": "anniversary",
"name": "Repository A",
"plan_code": "premium",
"status": "active",
"created_at": "2022-08-08T00:00:00Z",
"started_at": "2022-08-08T00:00:00Z",
"subscription_at": "2022-08-08T00:00:00Z",
"current_billing_period_started_at": "2022-08-08T00:00:00Z",
"current_billing_period_ending_at": "2022-09-08T00:00:00Z",
"on_termination_credit_note": "skip",
"on_termination_invoice": "skip",
"plan": map[string]interface{}{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"name": "Premium Plan",
"code": "premium",
"interval": "monthly",
"amount_cents": 10000,
"amount_currency": "USD",
},
},
},
"meta": map[string]interface{}{
"current_page": 1,
"next_page": 0,
"prev_page": 0,
"total_pages": 1,
"total_count": 1,
},
}
func TestCustomerRequest_GetInvoiceList(t *testing.T) {
t.Run("When the server is not reachable", func(t *testing.T) {
c := qt.New(t)
client := New().SetBaseURL("http://localhost:88888").SetApiKey("test_api_key")
result, err := client.Customer().GetInvoiceList(context.Background(), "CUSTOMER_1", &CustomerInvoiceListInput{})
c.Assert(result, qt.IsNil)
c.Assert(err.Error(), qt.Equals, `{"status":0,"error":"","code":"","err":"Get \"http://localhost:88888/api/v1/customers/CUSTOMER_1/invoices\": dial tcp: address 88888: invalid port"}`)
})
t.Run("When no parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/invoices").
MatchQuery("").
MockResponse(mockCustomerInvoiceListResponse)
defer server.Close()
result, err := server.Client().Customer().GetInvoiceList(context.Background(), "CUSTOMER_1", &CustomerInvoiceListInput{})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Invoices, qt.HasLen, 1)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 1)
})
t.Run("When parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/invoices").
MatchQuery("per_page=10&page=1&issuing_date_from=2022-01-01&issuing_date_to=2022-12-31&invoice_type=subscription&status=finalized&payment_status=succeeded&payment_overdue=true&partially_paid=false&self_billed=true&payment_dispute_lost=false&amount_from=100&amount_to=1000&search_term=test").
MockResponse(mockCustomerInvoiceListResponse)
defer server.Close()
perPage := 10
page := 1
paymentOverdue := true
partiallyPaid := false
selfBilled := true
paymentDisputeLost := false
amountFrom := 100
amountTo := 1000
result, err := server.Client().Customer().GetInvoiceList(context.Background(), "CUSTOMER_1", &CustomerInvoiceListInput{
PerPage: &perPage,
Page: &page,
IssuingDateFrom: "2022-01-01",
IssuingDateTo: "2022-12-31",
InvoiceType: SubscriptionInvoiceType,
Status: InvoiceStatusFinalized,
PaymentStatus: InvoicePaymentStatusSucceeded,
PaymentOverdue: &paymentOverdue,
PartiallyPaid: &partiallyPaid,
SelfBilled: &selfBilled,
PaymentDisputeLost: &paymentDisputeLost,
AmountFrom: &amountFrom,
AmountTo: &amountTo,
SearchTerm: "test",
})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Invoices, qt.HasLen, 1)
})
}
func TestCustomerRequest_GetCreditNoteList(t *testing.T) {
t.Run("When the server is not reachable", func(t *testing.T) {
c := qt.New(t)
client := New().SetBaseURL("http://localhost:88888").SetApiKey("test_api_key")
result, err := client.Customer().GetCreditNoteList(context.Background(), "CUSTOMER_1", &CustomerCreditNoteListInput{})
c.Assert(result, qt.IsNil)
c.Assert(err.Error(), qt.Equals, `{"status":0,"error":"","code":"","err":"Get \"http://localhost:88888/api/v1/customers/CUSTOMER_1/credit_notes\": dial tcp: address 88888: invalid port"}`)
})
t.Run("When no parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/credit_notes").
MatchQuery("").
MockResponse(mockCustomerCreditNoteListResponse)
defer server.Close()
result, err := server.Client().Customer().GetCreditNoteList(context.Background(), "CUSTOMER_1", &CustomerCreditNoteListInput{})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.CreditNotes, qt.HasLen, 1)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 1)
})
t.Run("When parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/credit_notes").
MatchQuery("per_page=10" +
"&page=1" +
"&issuing_date_from=2022-01-01" +
"&issuing_date_to=2022-12-31" +
"&amount_from=100" +
"&amount_to=1000" +
"&search_term=test" +
"&credit_status=available" +
"&invoice_number=INV-001" +
"&reason=duplicated_charge" +
"&refund_status=pending" +
"&self_billed=true").
MockResponse(mockCustomerCreditNoteListResponse)
defer server.Close()
perPage := 10
page := 1
selfBilled := true
result, err := server.Client().Customer().GetCreditNoteList(context.Background(), "CUSTOMER_1", &CustomerCreditNoteListInput{
PerPage: &perPage,
Page: &page,
IssuingDateFrom: "2022-01-01",
IssuingDateTo: "2022-12-31",
AmountFrom: 100,
AmountTo: 1000,
SearchTerm: "test",
CreditStatus: CreditNoteCreditStatusAvailable,
InvoiceNumber: "INV-001",
Reason: CreditNoteReasonDuplicatedCharge,
RefundStatus: CreditNoteRefundStatusPending,
SelfBilled: &selfBilled,
})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.CreditNotes, qt.HasLen, 1)
})
}
func TestCustomerRequest_GetPaymentList(t *testing.T) {
t.Run("When the server is not reachable", func(t *testing.T) {
c := qt.New(t)
client := New().SetBaseURL("http://localhost:88888").SetApiKey("test_api_key")
result, err := client.Customer().GetPaymentList(context.Background(), "CUSTOMER_1", &CustomerPaymentListInput{})
c.Assert(result, qt.IsNil)
c.Assert(err.Error(), qt.Equals, `{"status":0,"error":"","code":"","err":"Get \"http://localhost:88888/api/v1/customers/CUSTOMER_1/payments\": dial tcp: address 88888: invalid port"}`)
})
t.Run("When no parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/payments").
MatchQuery("").
MockResponse(mockCustomerPaymentListResponse)
defer server.Close()
result, err := server.Client().Customer().GetPaymentList(context.Background(), "CUSTOMER_1", &CustomerPaymentListInput{})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Payments, qt.HasLen, 1)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 1)
})
t.Run("When parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/payments").
MatchQuery("per_page=10&page=1&invoice_id=invoice_123").
MockResponse(mockCustomerPaymentListResponse)
defer server.Close()
perPage := 10
page := 1
result, err := server.Client().Customer().GetPaymentList(context.Background(), "CUSTOMER_1", &CustomerPaymentListInput{
PerPage: &perPage,
Page: &page,
InvoiceID: "invoice_123",
})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Payments, qt.HasLen, 1)
})
}
func TestCustomerRequest_GetPaymentRequestList(t *testing.T) {
t.Run("When the server is not reachable", func(t *testing.T) {
c := qt.New(t)
client := New().SetBaseURL("http://localhost:88888").SetApiKey("test_api_key")
result, err := client.Customer().GetPaymentRequestList(context.Background(), "CUSTOMER_1", &CustomerPaymentRequestListInput{})
c.Assert(result, qt.IsNil)
c.Assert(err.Error(), qt.Equals, `{"status":0,"error":"","code":"","err":"Get \"http://localhost:88888/api/v1/customers/CUSTOMER_1/payment_requests\": dial tcp: address 88888: invalid port"}`)
})
t.Run("When no parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/payment_requests").
MatchQuery("").
MockResponse(mockCustomerPaymentRequestListResponse)
defer server.Close()
result, err := server.Client().Customer().GetPaymentRequestList(context.Background(), "CUSTOMER_1", &CustomerPaymentRequestListInput{})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.PaymentRequests, qt.HasLen, 1)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 1)
})
t.Run("When parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/payment_requests").
MatchQuery("per_page=10&page=1&payment_status=pending").
MockResponse(mockCustomerPaymentRequestListResponse)
defer server.Close()
perPage := 10
page := 1
result, err := server.Client().Customer().GetPaymentRequestList(context.Background(), "CUSTOMER_1", &CustomerPaymentRequestListInput{
PerPage: &perPage,
Page: &page,
PaymentStatus: "pending",
})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.PaymentRequests, qt.HasLen, 1)
})
}
func TestCustomerRequest_GetAppliedCouponList(t *testing.T) {
t.Run("When the server is not reachable", func(t *testing.T) {
c := qt.New(t)
client := New().SetBaseURL("http://localhost:88888").SetApiKey("test_api_key")
result, err := client.Customer().GetAppliedCouponList(context.Background(), "CUSTOMER_1", &CustomerAppliedCouponListInput{})
c.Assert(result, qt.IsNil)
c.Assert(err.Error(), qt.Equals, `{"status":0,"error":"","code":"","err":"Get \"http://localhost:88888/api/v1/customers/CUSTOMER_1/applied_coupons\": dial tcp: address 88888: invalid port"}`)
})
t.Run("When no parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/applied_coupons").
MatchQuery("").
MockResponse(mockCustomerAppliedCouponListResponse)
defer server.Close()
result, err := server.Client().Customer().GetAppliedCouponList(context.Background(), "CUSTOMER_1", &CustomerAppliedCouponListInput{})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.AppliedCoupons, qt.HasLen, 1)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 1)
})
t.Run("When parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/applied_coupons").
MatchQuery("per_page=10&page=1&status=active&coupon_code[]=COUPON1&coupon_code[]=COUPON2").
MockResponse(mockCustomerAppliedCouponListResponse)
defer server.Close()
perPage := 10
page := 1
result, err := server.Client().Customer().GetAppliedCouponList(context.Background(), "CUSTOMER_1", &CustomerAppliedCouponListInput{
PerPage: &perPage,
Page: &page,
Status: AppliedCouponStatusActive,
CouponCode: []string{"COUPON1", "COUPON2"},
})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.AppliedCoupons, qt.HasLen, 1)
})
}
func TestCustomerRequest_GetSubscriptionList(t *testing.T) {
t.Run("When the server is not reachable", func(t *testing.T) {
c := qt.New(t)
client := New().SetBaseURL("http://localhost:88888").SetApiKey("test_api_key")
result, err := client.Customer().GetSubscriptionList(context.Background(), "CUSTOMER_1", &CustomerSubscriptionListInput{})
c.Assert(result, qt.IsNil)
c.Assert(err.Error(), qt.Equals, `{"status":0,"error":"","code":"","err":"Get \"http://localhost:88888/api/v1/customers/CUSTOMER_1/subscriptions\": dial tcp: address 88888: invalid port"}`)
})
t.Run("When no parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/subscriptions").
MatchQuery("").
MockResponse(mockCustomerSubscriptionListResponse)
defer server.Close()
result, err := server.Client().Customer().GetSubscriptionList(context.Background(), "CUSTOMER_1", &CustomerSubscriptionListInput{})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Subscriptions, qt.HasLen, 1)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 1)
})
t.Run("When parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers/CUSTOMER_1/subscriptions").
MatchQuery("per_page=10&page=1&plan_code=premium&status[]=active&status[]=terminated").
MockResponse(mockCustomerSubscriptionListResponse)
defer server.Close()
perPage := 10
page := 1
result, err := server.Client().Customer().GetSubscriptionList(context.Background(), "CUSTOMER_1", &CustomerSubscriptionListInput{
PerPage: &perPage,
Page: &page,
PlanCode: "premium",
Status: []SubscriptionStatus{SubscriptionStatusActive, SubscriptionStatusTerminated},
})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Subscriptions, qt.HasLen, 1)
})
}
func TestCustomerRequest_GetList(t *testing.T) {
t.Run("When the server is not reachable", func(t *testing.T) {
c := qt.New(t)
client := New().SetBaseURL("http://localhost:88888").SetApiKey("test_api_key")
result, err := client.Customer().GetList(context.Background(), &CustomerListInput{})
c.Assert(result, qt.IsNil)
c.Assert(err.Error(), qt.Equals, `{"status":0,"error":"","code":"","err":"Get \"http://localhost:88888/api/v1/customers\": dial tcp: address 88888: invalid port"}`)
})
t.Run("When no parameters are provided", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers").
MatchQuery("").
MockResponse(mockCustomerListResponse)
defer server.Close()
result, err := server.Client().Customer().GetList(context.Background(), &CustomerListInput{})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Customers, qt.HasLen, 2)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 2)
// Verify first customer data
firstCustomer := result.Customers[0]
c.Assert(firstCustomer.ExternalID, qt.Equals, "CUSTOMER_1")
c.Assert(firstCustomer.Name, qt.Equals, "John Doe")
c.Assert(firstCustomer.Email, qt.Equals, "customer@example.com")
c.Assert(firstCustomer.CustomerType, qt.Equals, "company")
// Verify second customer data
secondCustomer := result.Customers[1]
c.Assert(secondCustomer.ExternalID, qt.Equals, "CUSTOMER_2")
c.Assert(secondCustomer.Name, qt.Equals, "Jane Smith")
c.Assert(secondCustomer.Email, qt.Equals, "jane@example.com")
c.Assert(secondCustomer.CustomerType, qt.Equals, "individual")
})
t.Run("When all parameters are provided including search_term", func(t *testing.T) {
c := qt.New(t)
server := lt.NewMockServer(c).
MatchMethod("GET").
MatchPath("/api/v1/customers").
MatchQuery("per_page=5" +
"&page=1" +
"&search_term=acme" +
"&countries[]=US" +
"&countries[]=CA" +
"&states[]=CA" +
"&states[]=NY" +
"&zipcodes[]=91364" +
"&zipcodes[]=94102" +
"¤cies[]=USD" +
"¤cies[]=EUR" +
"&has_tax_identification_number=true" +
"&customer_type=company" +
"&has_customer_type=false" +
"&metadata[department]=engineering" +
"&metadata[segment]=enterprise").
MockResponse(mockCustomerListResponse)
defer server.Close()
perPage := 5
page := 1
hasTaxId := true
hasCustomerType := false
metadata := CustomerListInputMetadata{
"department": "engineering",
"segment": "enterprise",
}
result, err := server.Client().Customer().GetList(context.Background(), &CustomerListInput{
PerPage: &perPage,
Page: &page,
SearchTerm: "acme",
Countries: []string{"US", "CA"},
States: []string{"CA", "NY"},
Zipcodes: []string{"91364", "94102"},
Currencies: []Currency{USD, EUR},
HasTaxIdentificationNumber: &hasTaxId,
CustomerType: CompanyCustomerType,
HasCustomerType: &hasCustomerType,
Metadata: metadata,
})
c.Assert(err == nil, qt.IsTrue)
c.Assert(result.Customers, qt.HasLen, 2)
c.Assert(result.Meta.CurrentPage, qt.Equals, 1)
c.Assert(result.Meta.TotalCount, qt.Equals, 2)
// Verify first customer data
firstCustomer := result.Customers[0]
c.Assert(firstCustomer.ExternalID, qt.Equals, "CUSTOMER_1")
c.Assert(firstCustomer.Name, qt.Equals, "John Doe")
c.Assert(firstCustomer.Email, qt.Equals, "customer@example.com")
c.Assert(firstCustomer.CustomerType, qt.Equals, "company")
// Verify second customer data
secondCustomer := result.Customers[1]
c.Assert(secondCustomer.ExternalID, qt.Equals, "CUSTOMER_2")
c.Assert(secondCustomer.Name, qt.Equals, "Jane Smith")
c.Assert(secondCustomer.Email, qt.Equals, "jane@example.com")
c.Assert(secondCustomer.CustomerType, qt.Equals, "individual")
// Verify the response structure
for _, customer := range result.Customers {
c.Assert(customer.LagoID, qt.Not(qt.Equals), "")
c.Assert(customer.ExternalID, qt.Not(qt.Equals), "")
c.Assert(customer.Name, qt.Not(qt.Equals), "")
}
})
}