This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
savings_test.go
921 lines (735 loc) · 33.9 KB
/
savings_test.go
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
package starling
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"
"reflect"
"testing"
"github.com/google/uuid"
"github.com/pkg/errors"
)
// TestSavingsGoals confirms that the client is able to query a list of savings goals.
func TestSavingsGoals(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mock := `{
"savingsGoalList": [
{
"uid": "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b",
"name": "Trip to Paris",
"target": {
"currency": "GBP",
"minorUnits": 11223344
},
"totalSaved": {
"currency": "GBP",
"minorUnits": 11223344
},
"savedPercentage": 50
}
]
}`
mux.HandleFunc("/api/v1/savings-goals", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "GET")
fmt.Fprint(w, mock)
})
got, _, err := client.SavingsGoals(context.Background())
if err != nil {
t.Fatal("should be able to make the request", cross, err)
}
want := &savingsGoals{}
json.Unmarshal([]byte(mock), want)
if got == nil {
t.Fatal("should not return 'nil'", cross)
}
if !reflect.DeepEqual(got, want.SavingsGoals) {
t.Error("should return a list of savings goals matching the mock response", cross)
}
if len(got) == 0 {
t.Errorf("should return a list with at least one savings goal %s %d", cross, len(got))
}
}
// TestGetSavingsGoals confirms that the client is able to query a single savings goal.
func TestGetSavingsGoal(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
// Define our mock response and handler
mock := `{
"uid": "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b",
"name": "Trip to Paris",
"target": {
"currency": "GBP",
"minorUnits": 11223344
},
"totalSaved": {
"currency": "GBP",
"minorUnits": 11223344
},
"savedPercentage": 50
}`
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "GET")
fmt.Fprint(w, mock)
})
got, _, err := client.SavingsGoal(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b")
if err != nil {
t.Fatal("should be able to make the request", cross, err)
}
want := &SavingsGoal{}
json.Unmarshal([]byte(mock), want)
if got == nil {
t.Fatal("should not return 'nil'", cross)
}
if !reflect.DeepEqual(got, want) {
t.Error("should return a savings goal that matches the mock response", cross)
}
if len(got.UID) == 0 {
t.Errorf("should return a savings goal that has a UID %s %d", cross, len(got.UID))
}
}
func TestSavingsGoalForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
goal, resp, err := client.SavingsGoal(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if goal != nil {
t.Error("should not return a savings goal")
}
}
// TestPutSavingsGoal confirms that the client is able to create a savings goal.
func TestPutSavingsGoal(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
uid := "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b"
mockReq := SavingsGoalRequest{
Name: "test",
Currency: "GBP",
Target: Amount{
Currency: "GBP",
MinorUnits: 10000,
},
Base64EncodedPhoto: "",
}
mockResp := `{
"savingsGoalUid": "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b",
"success": true,
"errors": []
}`
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "PUT")
var sg = SavingsGoalRequest{}
err := json.NewDecoder(r.Body).Decode(&sg)
if err != nil {
t.Fatal("should send a request that the API can parse", cross, err)
}
if !reflect.DeepEqual(mockReq, sg) {
t.Error("should send a savings goal that matches the mock", cross)
}
reqUID := path.Base(r.URL.Path)
if reqUID != uid {
t.Error("should send a savings goal with the correct UID", cross, reqUID)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, mockResp)
})
resp, err := client.CreateSavingsGoal(context.Background(), uid, mockReq)
if err != nil {
t.Fatal("should be able to make the request", cross, err)
}
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("should receive a %d status code %s %d", want, cross, got)
}
}
// TestPutSavingsGoal_ValidateError confirms that the client is able to handle validation
// errors when creating savings goals.
func TestPutSavingsGoal_ValidateError(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
uid := "d8770f9d-4ee9-4cc1-86e1-83c26bcfcc4f"
mockReq := SavingsGoalRequest{
Name: "test",
Currency: "GBP",
Target: Amount{
Currency: "GBP",
MinorUnits: 10000,
},
Base64EncodedPhoto: "",
}
mockResp := `{
"savingsGoalUid": "d8770f9d-4ee9-4cc1-86e1-83c26bcfcc4f",
"success": true,
"errors": [
{
"message": "Something about the validation error"
}
]
}`
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "PUT")
var sg = SavingsGoalRequest{}
err := json.NewDecoder(r.Body).Decode(&sg)
if err != nil {
t.Fatal("should send a request that the API can parse", cross, err)
}
if !reflect.DeepEqual(mockReq, sg) {
t.Error("should send a savings goal that matches the mock", cross)
}
reqUID := path.Base(r.URL.Path)
if reqUID != uid {
t.Error("should send a savings goal with the correct UID", cross, reqUID)
}
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, mockResp)
})
resp, err := client.CreateSavingsGoal(context.Background(), uid, mockReq)
if err == nil {
t.Fatal("expected an error to be returned", cross)
}
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("should receive a %d status code %s %d", want, cross, got)
}
}
func TestPutSavingsGoalForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodPut)
w.WriteHeader(http.StatusForbidden)
})
goal := SavingsGoalRequest{
Name: "test",
Currency: "GBP",
Target: Amount{
Currency: "GBP",
MinorUnits: 10000,
},
Base64EncodedPhoto: "",
}
resp, err := client.CreateSavingsGoal(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b", goal)
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
}
// TestTransferToSavingsGoal confirms that the client is able to make a request to add money to a savings goal and parse
// the successful response from the API.
func TestTransferToSavingsGoal(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
goalUID := "d8770f9d-4ee9-4cc1-86e1-83c26bcfcc4f"
txnUID := "28dff346-dd48-426f-96df-d7f33d29c379"
mockResp := `{"transferUid":"28dff346-dd48-426f-96df-d7f33d29c379","success":true,"errors":[]}`
mockAmount := Amount{Currency: "GBP", MinorUnits: 1050}
mockReq := topUpRequest{Amount: mockAmount}
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "PUT")
var tu = topUpRequest{}
err := json.NewDecoder(r.Body).Decode(&tu)
if err != nil {
t.Fatal("should send a request that the API can parse", cross, err)
}
if !reflect.DeepEqual(mockReq, tu) {
t.Error("should send a top-up request that matches the mock", cross)
}
reqUID, err := uuid.Parse(path.Base(r.URL.Path))
if err != nil {
t.Error("should send a top-up request with a valid UID", cross, reqUID)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, mockResp)
})
id, resp, err := client.TransferToSavingsGoal(context.Background(), goalUID, mockAmount)
if err != nil {
t.Fatal("should be able to make the request", cross, err)
}
want := &savingsGoalTransferResponse{}
json.Unmarshal([]byte(mockResp), want)
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("should receive a %d status code %s %d", want, cross, got)
}
if got, want := id, txnUID; got != want {
t.Fatal("should be receive the UID assigned to the transaction", cross, got)
}
}
func TestTransferToSavingsGoal_BadRequest(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
goalUID := "d8770f9d-4ee9-4cc1-86e1-83c26bcfcc4f"
mockAmount := Amount{Currency: "GBP", MinorUnits: 1050}
mockResp := `[
"UNKNOWN_CATEGORY"
]`
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "PUT")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, mockResp)
})
_, resp, err := client.TransferToSavingsGoal(context.Background(), goalUID, mockAmount)
if err == nil {
t.Fatal("should return an error")
}
errWant := errors.New("UNKNOWN_CATEGORY")
errWant = errors.Wrap(errWant, http.StatusText(http.StatusBadRequest))
if err.Error() != errWant.Error() {
t.Error("should indicate the error detail")
}
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("should receive a %d status code", http.StatusBadRequest)
}
}
func TestTransferToSavingsGoalForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodPut)
w.WriteHeader(http.StatusForbidden)
})
amt := Amount{Currency: "GBP", MinorUnits: 1050}
id, resp, err := client.TransferToSavingsGoal(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b", amt)
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if id != "" {
t.Error("should not return a transaction id")
}
}
var deleteSavingsGoalCases = []struct {
name string
uid string
mock string
}{
{
name: "sample savings goal",
uid: "840e4030-b94c-4e71-a1d3-1319a233dd3c",
},
}
func TestDeleteSavingsGoal(t *testing.T) {
for _, tc := range deleteSavingsGoalCases {
t.Run(tc.name, func(st *testing.T) {
testDeleteSavingsGoal(st, tc.name, tc.uid)
})
}
}
func testDeleteSavingsGoal(t *testing.T, name, uid string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodDelete)
reqUID := path.Base(r.URL.Path)
if reqUID != uid {
t.Error("should send a request with the correct UID", cross, reqUID)
}
w.WriteHeader(http.StatusNoContent)
})
resp, err := client.DeleteSavingsGoal(context.Background(), uid)
checkNoError(t, err)
if resp.StatusCode != http.StatusNoContent {
t.Error("should return an HTTP 204 status", cross, resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
checkNoError(t, err)
if len(body) != 0 {
t.Error("should return an empty body", cross, len(body))
}
}
func TestDeleteGoalForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusForbidden)
})
resp, err := client.DeleteSavingsGoal(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
}
var savingsGoalPhotoCases = []struct {
name string
uid string
mock string
}{
{
name: "sample savings goal photo",
uid: "72011c4b-d42b-4709-8511-b7f01669e46f",
mock: `{
"_links": {
"self": {
"href": "api/v1/savings-goals/72011c4b-d42b-4709-8511-b7f01669e46f/photo",
"templated": false
}
},
"base64EncodedPhoto": "iVBORw0KGgoAAAANSUhEUgAAAPoAAABkCAYAAACvgC0OAAAAAXNSR0IArs4c6QAAHgpJREFUeAHtnQnYXdO5gBFEYo55iCSk0RiLmueZmpWr+tR8W1WUqg7aKlqlykWj3Kqhpj4t7VVjzVFJ0KiZCjVkFImEEERiyn3fOOf0/Off+5y199nn5P9jfc/zZu+99re+tda317z3+TPffFGiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB6IHogeiB+Y1D8yePXsBWBh6w2KweBVe94KF5rVyt6I8+Gl+6AeXw2j4GMoyg5ORcBws2or0P4s28eVCcBQMh+lQFn0/Dq6DL8L87fJP2xJKKxCFXZh7S5ZYjmN/WLWE10tATyjLR5y8Ca/CKHgRxsGk+eef/wOOUao8gH/7cPk7+HJVcNLpN/HfpUk3Ylg2D+DzPYjxZ+hVJ+bT3NsOn0+ro1PYrQULs5TBEI5YEfX+MBDWgcGwJqwCi0EWmYGyTnsAu8M4jsB507MYmMd1l6Z8GwWU8fMBOlGlgQeogw6e1ul6jVwrq4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) {
testSavingsGoalPhoto(st, tc.name, tc.uid, tc.mock)
})
}
}
func testSavingsGoalPhoto(t *testing.T, name, uid string, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
resource := path.Base(r.URL.Path)
reqUID := path.Base(path.Dir(r.URL.Path))
if reqUID != uid {
t.Error("should send a request with the correct UID", cross, reqUID)
}
if resource != "photo" {
t.Error("should request the photo", cross, resource)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, mock)
})
photo, resp, err := client.SavingsGoalPhoto(context.Background(), uid)
checkNoError(t, err)
if resp.StatusCode != http.StatusOK {
t.Error("should return an HTTP 200 status", cross, resp.Status)
}
want := &Photo{}
json.Unmarshal([]byte(mock), want)
if !reflect.DeepEqual(photo, want) {
t.Error("should return a savings goal photo that matches the mock", cross)
}
if len(photo.Base64EncodedPhoto) == 0 {
t.Error("should return a base64 encoded photo", cross)
}
}
func TestSavingsGoalPhotoForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.SavingsGoalPhoto(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != nil {
t.Error("should not return a savings goal photo")
}
}
// TestTransferFromSavingsGoal confirms that the client is able to make a request to withdraw money from a savings goal.
func TestTransferFromSavingsGoal(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
goalUID := "d8770f9d-4ee9-4cc1-86e1-83c26bcfcc4f"
txnUID := "28dff346-dd48-426f-96df-d7f33d29c379"
mockResp := `{"transferUid":"28dff346-dd48-426f-96df-d7f33d29c379","success":true,"errors":[]}`
mockAmount := Amount{Currency: "GBP", MinorUnits: 1050}
mockReq := withdrawalRequest{Amount: mockAmount}
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "PUT")
var wr = withdrawalRequest{}
err := json.NewDecoder(r.Body).Decode(&wr)
if err != nil {
t.Fatal("should send a request that the API can parse", cross, err)
}
if !reflect.DeepEqual(mockReq, wr) {
t.Error("should send a top-up request that matches the mock", cross)
}
resource := path.Base(path.Dir(r.URL.Path))
if resource != "withdraw-money" {
t.Error("should make a request to withdraw-money", cross, resource)
}
reqUID, err := uuid.Parse(path.Base(r.URL.Path))
if err != nil {
t.Error("should send a top-up request with a valid UID", cross, reqUID)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, mockResp)
})
id, resp, err := client.TransferFromSavingsGoal(context.Background(), goalUID, mockAmount)
if err != nil {
t.Fatal("should be able to make the request", cross, err)
}
want := &savingsGoalTransferResponse{}
json.Unmarshal([]byte(mockResp), want)
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("should receive a %d status code %s %d", want, cross, got)
}
if got, want := id, txnUID; got != want {
t.Fatal("should be receive the UID assigned to the transaction", cross, got)
}
}
func TestTransferFromSavingsGoal_InsufficientFunds(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
goalUID := "d8770f9d-4ee9-4cc1-86e1-83c26bcfcc4f"
mockResp := `["INSUFFICIENT_FUNDS"]`
mockAmount := Amount{Currency: "GBP", MinorUnits: 10000000}
mockReq := withdrawalRequest{Amount: mockAmount}
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "PUT")
var wr = withdrawalRequest{}
err := json.NewDecoder(r.Body).Decode(&wr)
if err != nil {
t.Fatal("should send a request that the API can parse", cross, err)
}
if !reflect.DeepEqual(mockReq, wr) {
t.Error("should send a top-up request that matches the mock", cross)
}
resource := path.Base(path.Dir(r.URL.Path))
if resource != "withdraw-money" {
t.Error("should make a request to withdraw-money", cross, resource)
}
reqUID, err := uuid.Parse(path.Base(r.URL.Path))
if err != nil {
t.Error("should send a top-up request with a valid UID", cross, reqUID)
}
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, mockResp)
})
_, resp, err := client.TransferFromSavingsGoal(context.Background(), goalUID, mockAmount)
if err == nil {
t.Fatal("should return an error when making the request", cross)
}
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
t.Errorf("should receive a %d status code %s %d", want, cross, got)
}
}
func TestTransferFromSavingsGoalForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodPut)
w.WriteHeader(http.StatusForbidden)
})
amt := Amount{Currency: "GBP", MinorUnits: 1050}
got, resp, err := client.TransferFromSavingsGoal(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b", amt)
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != "" {
t.Error("should not return a transaction id")
}
}
var recurringSavingsGoalCases = []struct {
name string
uid string
mock string
}{
{
name: "sample recurring savings goal",
uid: "840e4030-b94c-4e71-a1d3-1319a233dd3c",
mock: `{
"transferUid": "28dff346-dd48-426f-96df-d7f33d29c379",
"recurrenceRule": {
"startDate": "2017-09-23",
"frequency": "WEEKLY"
},
"currencyAndAmount": {
"currency": "GBP",
"minorUnits": 1000
}
}`,
},
{
name: "confusing recurring savings goal",
uid: "840e4030-b94c-4e71-a1d3-1319a233dd3c",
mock: `{
"transferUid": "28dff346-dd48-426f-96df-d7f33d29c379",
"recurrenceRule": {
"startDate": "2017-09-23",
"frequency": "WEEKLY",
"interval": 2,
"count": 2,
"untilDate": "2019-09-23",
"weekStart": "WEDNESDAY",
"days": ["MONDAY", "TUESDAY", "WEDNESDAY"],
"monthDay": 10,
"monthWeek": 3
},
"currencyAndAmount": {
"currency": "GBP",
"minorUnits": 1000
}
}`,
},
}
func RecurringTransfer(t *testing.T) {
for _, tc := range recurringSavingsGoalCases {
t.Run(tc.name, func(st *testing.T) {
recurringTransfer(st, tc.name, tc.uid, tc.mock)
})
}
}
func recurringTransfer(t *testing.T, name, uid string, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
resource := path.Base(r.URL.Path)
reqUID := path.Base(path.Dir(r.URL.Path))
if reqUID != uid {
t.Error("should send a request with the correct UID", cross, reqUID)
}
if resource != "recurring-transfer" {
t.Error("should request the recurring transfer", cross, resource)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, mock)
})
recSav, resp, err := client.RecurringTransfer(context.Background(), uid)
checkNoError(t, err)
if resp.StatusCode != http.StatusOK {
t.Error("should return an HTTP 200 status", cross, resp.Status)
}
want := &RecurringTransferRequest{}
json.Unmarshal([]byte(mock), want)
if !reflect.DeepEqual(recSav, want) {
t.Error("should return a recurring savings goal that matches the mock", cross)
}
if recSav.UID == "" {
t.Error("should return a UID", cross)
}
}
func TestRecurringTransferForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.RecurringTransfer(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != nil {
t.Error("should not return a recurring transfer")
}
}
func TestCreateRecurringTransfer(t *testing.T) {
for _, tc := range recurringSavingsGoalCases {
t.Run(tc.name, func(st *testing.T) {
testCreateRecurringTransfer(st, tc.name, tc.uid, tc.mock)
})
}
}
func testCreateRecurringTransfer(t *testing.T, name, uid string, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodPut)
resource := path.Base(r.URL.Path)
reqUID := path.Base(path.Dir(r.URL.Path))
if reqUID != uid {
t.Error("should send a request with the correct UID", cross, reqUID)
}
if resource != "recurring-transfer" {
t.Error("should request the recurring transfer", cross, resource)
}
mockResp := `{
"transferUid": "28dff346-dd48-426f-96df-d7f33d29c379",
"success": true,
"errors": []
}`
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, mockResp)
})
rtrReq := RecurringTransferRequest{
UID: "123",
Amount: Amount{Currency: "GBP", MinorUnits: 1234},
RecurrenceRule: RecurrenceRule{
Frequency: "DAILY",
Interval: 2,
Count: 4,
},
}
id, resp, err := client.CreateRecurringTransfer(context.Background(), uid, rtrReq)
checkNoError(t, err)
if resp.StatusCode != http.StatusOK {
t.Error("should return an HTTP 200 status", cross, resp.Status)
}
if id == "28dff346-dd48-426f-96df-d7f33d29c379" {
t.Error("should return a UID", cross)
}
}
func TestCreateRecurringTransferForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodPut)
w.WriteHeader(http.StatusForbidden)
})
rtrReq := RecurringTransferRequest{
UID: "123",
Amount: Amount{Currency: "GBP", MinorUnits: 1234},
RecurrenceRule: RecurrenceRule{Frequency: "DAILY", Interval: 2, Count: 4},
}
got, resp, err := client.CreateRecurringTransfer(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b", rtrReq)
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != "" {
t.Error("should not return a recurring transfer id")
}
}
func TestDeleteRecurringTransfer(t *testing.T) {
for _, tc := range recurringSavingsGoalCases {
t.Run(tc.name, func(st *testing.T) {
testDeleteRecurringTransfer(st, tc.name, tc.uid)
})
}
}
func testDeleteRecurringTransfer(t *testing.T, name, uid string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodDelete)
resource := path.Base(r.URL.Path)
reqUID := path.Base(path.Dir(r.URL.Path))
if reqUID != uid {
t.Error("should send a request with the correct UID", cross, reqUID)
}
if resource != "recurring-transfer" {
t.Error("should request the recurring transfer", cross, resource)
}
w.WriteHeader(http.StatusNoContent)
})
resp, err := client.DeleteRecurringTransfer(context.Background(), uid)
checkNoError(t, err)
if resp.StatusCode != http.StatusNoContent {
t.Error("should return an HTTP 204 status", cross, resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
checkNoError(t, err)
if len(body) != 0 {
t.Error("should return an empty body", cross, len(body))
}
}
func TestDeleteRecurringTransferForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/savings-goals/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusForbidden)
})
resp, err := client.DeleteRecurringTransfer(context.Background(), "e43d3060-2c83-4bb9-ac8c-c627b9c45f8b")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
}