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 pathcards_test.go
99 lines (86 loc) · 2.06 KB
/
cards_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
package starling
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
var cardTestCases = []struct {
name string
mock string
}{
{
name: "sample card",
mock: `{
"_links": {
"transactions": {
"href": "/api/v1/transactions/mastercard?from={fromDate}&to={toDate}",
"templated": true
}
},
"id": "8e9c955c-b209-4887-af32-a9e4999e985e",
"nameOnCard": "Vincent Adultman",
"type": "ContactlessDebitMastercard",
"enabled": true,
"cancelled": false,
"activationRequested": true,
"activated": true,
"dispatchDate": "2018-03-13",
"lastFourDigits": "0142"
}`,
},
{
name: "sample card without HAL wrapper",
mock: `{
"id": "8e9c955c-b209-4887-af32-a9e4999e985e",
"nameOnCard": "Vincent Adultman",
"type": "ContactlessDebitMastercard",
"enabled": true,
"cancelled": false,
"activationRequested": true,
"activated": true,
"dispatchDate": "2018-03-13",
"lastFourDigits": "0142"
}`,
},
}
func TestCard(t *testing.T) {
for _, tc := range cardTestCases {
t.Run(tc.name, func(st *testing.T) {
testCard(st, tc.name, tc.mock)
})
}
}
func testCard(t *testing.T, name, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/cards", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
fmt.Fprint(w, mock)
})
got, _, err := client.Card(context.Background())
checkNoError(t, err)
want := &Card{}
json.Unmarshal([]byte(mock), want)
if !reflect.DeepEqual(got, want) {
t.Error("should return a card matching the mock response", cross)
}
}
func TestCardForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/cards", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.Card(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 card")
}
}