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
/
Copy pathfeed_test.go
337 lines (302 loc) · 8.71 KB
/
feed_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
package starling
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
var feedTC = []struct {
name string
act string
cat string
since time.Time
mock string
}{
{
name: "no transactions",
act: "30aa7ab8-4389-4658-a4f8-0bc6d0015ba0",
cat: "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
mock: `{
"feedItems": []
}`,
},
{
name: "single transaction",
act: "30aa7ab8-4389-4658-a4f8-0bc6d0015ba0",
cat: "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
mock: `{
"feedItems": [
{
"feedItemUid": "dbb59f1c-39e6-4558-87ba-11c142965393",
"categoryUid": "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
"amount": {
"currency": "GBP",
"minorUnits": 32
},
"sourceAmount": {
"currency": "GBP",
"minorUnits": 32
},
"direction": "OUT",
"transactionTime": "2018-06-28T07:16:28.364Z",
"source": "MASTER_CARD",
"sourceSubType": "CHIP_AND_PIN",
"status": "SETTLED",
"counterPartyType": "MERCHANT",
"counterPartyUid": "e6dbe57e-7c23-4015-97a4-4afbbf7faa23",
"reference": "ATM 111072\\35 REGENT ST), LONDON\\LONDON\\SW1Y 4ND 00 GBR",
"country": "GB",
"spendingCategory": "HOLIDAYS"
}
]
}`,
},
{
name: "multiple transactions",
act: "30aa7ab8-4389-4658-a4f8-0bc6d0015ba0",
cat: "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
mock: `{
"feedItems": [
{
"feedItemUid": "dbb59f1c-39e6-4558-87ba-11c142965393",
"categoryUid": "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
"amount": {
"currency": "GBP",
"minorUnits": 32
},
"sourceAmount": {
"currency": "GBP",
"minorUnits": 32
},
"direction": "OUT",
"transactionTime": "2018-06-28T07:16:28.364Z",
"source": "MASTER_CARD",
"sourceSubType": "CHIP_AND_PIN",
"status": "SETTLED",
"counterPartyType": "MERCHANT",
"counterPartyUid": "e6dbe57e-7c23-4015-97a4-4afbbf7faa23",
"reference": "ATM 111072\\35 REGENT ST), LONDON\\LONDON\\SW1Y 4ND 00 GBR",
"country": "GB",
"spendingCategory": "HOLIDAYS"
},
{
"feedItemUid": "199c2bba-9f4d-4b20-b5df-4de440411b03",
"categoryUid": "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
"amount": {
"currency": "GBP",
"minorUnits": 7
},
"sourceAmount": {
"currency": "GBP",
"minorUnits": 7
},
"direction": "OUT",
"transactionTime": "2018-06-28T07:16:28.361Z",
"source": "MASTER_CARD",
"sourceSubType": "CHIP_AND_PIN",
"status": "SETTLED",
"counterPartyType": "MERCHANT",
"counterPartyUid": "c052f76f-e919-427d-85fc-f46a75a3ff26",
"reference": "MASTERCARD EUROPE WATERLOO BEL",
"country": "GB",
"spendingCategory": "HOLIDAYS"
},
{
"feedItemUid": "32f8ffc4-d12c-43fe-9d1b-61faf7243143",
"categoryUid": "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
"amount": {
"currency": "GBP",
"minorUnits": 24
},
"sourceAmount": {
"currency": "GBP",
"minorUnits": 24
},
"direction": "OUT",
"transactionTime": "2018-06-28T07:16:28.359Z",
"source": "MASTER_CARD",
"sourceSubType": "CHIP_AND_PIN",
"status": "SETTLED",
"counterPartyType": "MERCHANT",
"counterPartyUid": "c052f76f-e919-427d-85fc-f46a75a3ff26",
"reference": "MASTERCARD UK MANA\\19TH FLOOR\\LONDON E14\\E14 5NP GBR",
"country": "GB",
"spendingCategory": "HOLIDAYS"
}
]
}`,
},
{
name: "transaction since",
act: "30aa7ab8-4389-4658-a4f8-0bc6d0015ba0",
cat: "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
since: time.Now(),
mock: `{
"feedItems": [
{
"feedItemUid": "dbb59f1c-39e6-4558-87ba-11c142965393",
"categoryUid": "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
"amount": {
"currency": "GBP",
"minorUnits": 32
},
"sourceAmount": {
"currency": "GBP",
"minorUnits": 32
},
"direction": "OUT",
"transactionTime": "2018-06-28T07:16:28.364Z",
"source": "MASTER_CARD",
"sourceSubType": "CHIP_AND_PIN",
"status": "SETTLED",
"counterPartyType": "MERCHANT",
"counterPartyUid": "e6dbe57e-7c23-4015-97a4-4afbbf7faa23",
"reference": "ATM 111072\\35 REGENT ST), LONDON\\LONDON\\SW1Y 4ND 00 GBR",
"country": "GB",
"spendingCategory": "HOLIDAYS"
}
]
}`,
},
}
func TestFeed(t *testing.T) {
for _, tc := range feedTC {
t.Run(tc.name, func(t *testing.T) {
testFeed(t, tc.name, tc.act, tc.cat, tc.mock, tc.since)
})
}
}
func testFeed(t *testing.T, name, act, cat, mock string, since time.Time) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v2/feed/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
if r.URL.Path != "/api/v2/feed/account/"+act+"/category/"+cat {
t.Error("should sent a request to the correct path")
}
params := r.URL.Query()
if time.Time.IsZero(since) == false {
if got, want := params.Get("changesSince"), since.Format(time.RFC3339Nano); got != want {
t.Errorf("should include 'changesSince=%s' query string parameter %s 'changesSince=%s'", want, cross, got)
}
}
fmt.Fprint(w, mock)
})
opts := &FeedOpts{}
if time.Time.IsZero(since) == false {
opts.Since = since
}
got, _, err := client.Feed(context.Background(), act, cat, opts)
checkNoError(t, err)
want := &feed{}
json.Unmarshal([]byte(mock), want)
if got == nil {
t.Fatal("should not return 'nil'", cross)
}
if len(got) != len(want.Items) {
t.Error("should return the correct number of items")
}
if !reflect.DeepEqual(got, want.Items) {
t.Error("should return a slice of feed items matching the mock response")
}
}
func TestFeedForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v2/feed/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.Feed(context.Background(), "30aa7ab8-4389-4658-a4f8-0bc6d0015ba0", "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0", nil)
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != nil {
t.Error("should not return a slice of items")
}
}
var itemTC = []struct {
name string
act string
cat string
itm string
mock string
}{
{
name: "card transaction",
act: "30aa7ab8-4389-4658-a4f8-0bc6d0015ba0",
cat: "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
itm: "dbb59f1c-39e6-4558-87ba-11c142965393",
mock: `{
"feedItemUid": "dbb59f1c-39e6-4558-87ba-11c142965393",
"categoryUid": "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0",
"amount": {
"currency": "GBP",
"minorUnits": 32
},
"sourceAmount": {
"currency": "GBP",
"minorUnits": 32
},
"direction": "OUT",
"transactionTime": "2018-06-28T07:16:28.364Z",
"source": "MASTER_CARD",
"sourceSubType": "CHIP_AND_PIN",
"status": "SETTLED",
"counterPartyType": "MERCHANT",
"counterPartyUid": "e6dbe57e-7c23-4015-97a4-4afbbf7faa23",
"reference": "ATM 111072\\35 REGENT ST), LONDON\\LONDON\\SW1Y 4ND 00 GBR",
"country": "GB",
"spendingCategory": "HOLIDAYS"
}`,
},
}
func TestFeedItem(t *testing.T) {
for _, tc := range itemTC {
t.Run(tc.name, func(t *testing.T) {
testFeedItem(t, tc.name, tc.act, tc.cat, tc.itm, tc.mock)
})
}
}
func testFeedItem(t *testing.T, name, act, cat, itm, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v2/feed/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
if r.URL.Path != "/api/v2/feed/account/"+act+"/category/"+cat+"/"+itm {
t.Error("should sent a request to the correct path")
}
fmt.Fprint(w, mock)
})
got, _, err := client.FeedItem(context.Background(), act, cat, itm)
checkNoError(t, err)
want := new(Item)
json.Unmarshal([]byte(mock), want)
if got == nil {
t.Fatal("should not return 'nil'", cross)
}
if !reflect.DeepEqual(got, want) {
t.Error("should return an item matching the mock response")
}
if got.FeedItemUID != want.FeedItemUID {
t.Error("should return the requested item")
}
}
func TestFeedItemForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v2/feed/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.FeedItem(context.Background(), "30aa7ab8-4389-4658-a4f8-0bc6d0015ba0", "c423ab8d-9a6a-44b2-8db6-ac6000fe58e0", "dbb59f1c-39e6-4558-87ba-11c142965393")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != nil {
t.Error("should not return an item")
}
}