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 pathcustomers_test.go
111 lines (98 loc) · 2.42 KB
/
customers_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
package starling
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
var customersTestCases = []struct {
name string
mock string
}{
{
name: "sample customer",
mock: `{
"_links": {
"accounts": {
"href": "api/v1/accounts",
"templated": false
},
"addresses": {
"href": "api/v1/addresses",
"templated": false
},
"contacts": {
"href": "api/v1/contacts",
"templated": false
},
"self": {
"href": "api/v1/customers",
"templated": false
},
"transactions": {
"href": "api/v1/transactions?from={fromDate}&to={toDate}",
"templated": false
}
},
"customerUid": "6d2aa528-b9d1-4083-ae7c-53d460cd8d88",
"firstName": "Vincent",
"lastName": "Adultman",
"dateOfBirth": "1960-01-01",
"email": "6d2aa528-b9d1-4083-ae7c-53d460cd8d88@starlingbank.com",
"phone": "+447886725871",
"accountHolderType": "INDIVIDUAL"
}`,
},
{
name: "sample customer without HAL wrapper",
mock: `{
"customerUid": "6d2aa528-b9d1-4083-ae7c-53d460cd8d88",
"firstName": "Vincent",
"lastName": "Adultman",
"dateOfBirth": "1960-01-01",
"email": "6d2aa528-b9d1-4083-ae7c-53d460cd8d88@starlingbank.com",
"phone": "+447886725871",
"accountHolderType": "INDIVIDUAL"
}`,
},
}
func TestCustomer(t *testing.T) {
for _, tc := range customersTestCases {
t.Run(tc.name, func(st *testing.T) {
testCustomer(st, tc.name, tc.mock)
})
}
}
func testCustomer(t *testing.T, name, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/customers", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
fmt.Fprint(w, mock)
})
got, _, err := client.Customer(context.Background())
checkNoError(t, err)
want := &Customer{}
json.Unmarshal([]byte(mock), want)
if !reflect.DeepEqual(got, want) {
t.Error("should return an identity matching the mock response", cross)
}
}
func TestCustomerForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/customers", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.Customer(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 a customer")
}
}