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 pathdirectdebit_test.go
250 lines (208 loc) · 5.66 KB
/
directdebit_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
package starling
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"
"reflect"
"testing"
)
var ddTestCases = []struct {
name string
mock string
}{
{
name: "empty dd list",
mock: `{
"_links": {
"self": {
"href": "/api/v1/direct-debit/mandates",
"templated": false
}
},
"_embedded": {
"mandates": []
}
}`,
},
{
name: "single dd",
mock: `{
"_links": {
"self": {
"href": "/api/v1/direct-debit/mandates",
"templated": false
}
},
"_embedded": {
"mandates": [
{
"uid": "fa7998f6-07ce-42a9-ba5b-ce45ea8aff89",
"reference": "VolcanoDisruptions",
"status": "LIVE",
"source": "ELECTRONIC",
"created": "2018-04-17T07:23:59.173Z",
"originatorName": "ANTIQUARIES",
"originatorUid": "949404bd-d32e-4f1e-9759-4d6caee3137c"
}
]
}
}`,
},
}
func TestDirectDebits(t *testing.T) {
for _, tc := range ddTestCases {
t.Run(tc.name, func(st *testing.T) {
testDirectDebits(st, tc.name, tc.mock)
})
}
}
func testDirectDebits(t *testing.T, name, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/direct-debit/mandates", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
fmt.Fprint(w, mock)
})
got, _, err := client.DirectDebitMandates(context.Background())
checkNoError(t, err)
hal := &halDirectDebitMandates{}
json.Unmarshal([]byte(mock), hal)
want := hal.Embedded
if !reflect.DeepEqual(got, want.Mandates) {
t.Error("should return a list of mandates matching the mock response", cross)
}
}
func TestDDMandatesForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/direct-debit/mandates", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.DirectDebitMandates(context.Background())
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != nil {
t.Error("should not return direct-debit mandates")
}
}
var ddMandateCases = []struct {
name string
uid string
mock string
}{
{
name: "direct debit mandate",
uid: "fa7998f6-07ce-42a9-ba5b-ce45ea8aff89",
mock: `{
"uid": "fa7998f6-07ce-42a9-ba5b-ce45ea8aff89",
"reference": "VolcanoDisruptions",
"status": "LIVE",
"source": "ELECTRONIC",
"created": "2018-04-17T07:23:59.173Z",
"originatorName": "ANTIQUARIES",
"originatorUid": "949404bd-d32e-4f1e-9759-4d6caee3137c"
}`,
},
}
func TestDDMandate(t *testing.T) {
for _, tc := range ddMandateCases {
t.Run(tc.name, func(st *testing.T) {
testDDMandate(st, tc.name, tc.uid, tc.mock)
})
}
}
func testDDMandate(t *testing.T, name, uid, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/direct-debit/mandates/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
reqUID := path.Base(r.URL.Path)
if reqUID != uid {
t.Error("should send a requestwith the correct UID", cross, reqUID)
}
fmt.Fprint(w, mock)
})
got, _, err := client.DirectDebitMandate(context.Background(), uid)
checkNoError(t, err)
want := &DirectDebitMandate{}
json.Unmarshal([]byte(mock), want)
if !reflect.DeepEqual(got, want) {
t.Error("should return a single mandate matching the mock response", cross)
}
if got.UID != want.UID {
t.Error("should have the correct UID", cross, got.UID)
}
}
func TestDDMandateForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/direct-debit/mandates/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.DirectDebitMandate(context.Background(), "949404bd-d32e-4f1e-9759-4d6caee3137c")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != nil {
t.Error("should not return a direct-debit mandate")
}
}
var deleteDDMandateCases = []struct {
name string
uid string
}{
{
name: "sample direct debit mandate",
uid: "840e4030-b94c-4e71-a1d3-1319a233dd3c",
},
}
func TestDeleteDDMandate(t *testing.T) {
for _, tc := range deleteDDMandateCases {
t.Run(tc.name, func(st *testing.T) {
testDeleteDDMandate(st, tc.name, tc.uid)
})
}
}
func testDeleteDDMandate(t *testing.T, name, uid string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/direct-debit/mandates/", 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 requestwith the correct UID", cross, reqUID)
}
w.WriteHeader(http.StatusNoContent)
})
resp, err := client.DeleteDirectDebitMandate(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 TestDDMandateDeleteForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/direct-debit/mandates/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusForbidden)
})
resp, err := client.DeleteDirectDebitMandate(context.Background(), "949404bd-d32e-4f1e-9759-4d6caee3137c")
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
}