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
/
merchants_test.go
173 lines (142 loc) · 4.21 KB
/
merchants_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
package starling
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path"
"reflect"
"testing"
)
var merchantCases = []struct {
name string
uid string
mock string
}{
{
name: "sample merchant",
uid: "c052f76f-e919-427d-85fc-f46a75a3ff26",
mock: `{
"merchantUid": "c052f76f-e919-427d-85fc-f46a75a3ff26",
"name": "Mastercard",
"website": "http://mastercard.co.uk",
"twitterUsername": "@mastercard"
}`,
},
}
func TestMerchant(t *testing.T) {
for _, tc := range merchantCases {
t.Run(tc.name, func(st *testing.T) {
testMerchant(st, tc.name, tc.uid, tc.mock)
})
}
}
func testMerchant(t *testing.T, name, uid, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/merchants/", 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 request with the correct UID", cross, reqUID)
}
fmt.Fprint(w, mock)
})
got, _, err := client.Merchant(context.Background(), uid)
checkNoError(t, err)
mer := &Merchant{}
json.Unmarshal([]byte(mock), mer)
if !reflect.DeepEqual(got, mer) {
t.Error("should return a merchant matching the mock response", cross)
}
}
func TestMerchantForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/merchants/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.Merchant(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 merchant")
}
}
var merchantLocationCases = []struct {
name string
mUID string
lUID string
mock string
}{
{
name: "sample merchant location",
mUID: "c052f76f-e919-427d-85fc-f46a75a3ff26",
lUID: "371c62bc-dcfc-4799-8b23-b070626772f7",
mock: `{
"merchantUid": "c052f76f-e919-427d-85fc-f46a75a3ff26",
"merchant": {
"href": "/api/v1/merchants/c052f76f-e919-427d-85fc-f46a75a3ff26",
"templated": false
},
"merchantLocationUid": "371c62bc-dcfc-4799-8b23-b070626772f7",
"merchantName": "Mastercard",
"locationName": "Mastercard UK",
"googlePlaceId": "ChIJJ9ZEdgG4h0gRqJZP_Z5tgCc",
"mastercardMerchantCategoryCode": 3619
}`,
},
}
func TestMerchantLocation(t *testing.T) {
for _, tc := range merchantLocationCases {
t.Run(tc.name, func(st *testing.T) {
testMerchantLocation(st, tc.name, tc.mUID, tc.lUID, tc.mock)
})
}
}
func testMerchantLocation(t *testing.T, name, mUID, lUID, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/merchants/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
reqMerUID := path.Base(path.Dir(path.Dir(r.URL.Path)))
if reqMerUID != mUID {
t.Error("should send a request with the correct merchant UID", cross, reqMerUID)
}
resource := path.Base(path.Dir(r.URL.Path))
if resource != "locations" {
t.Error("should send a request for the locations resource", cross, resource)
}
reqLocUID := path.Base(r.URL.Path)
if reqLocUID != lUID {
t.Error("should send a request with the correct location UID", cross, reqLocUID)
}
fmt.Fprint(w, mock)
})
got, _, err := client.MerchantLocation(context.Background(), mUID, lUID)
checkNoError(t, err)
merLoc := &MerchantLocation{}
json.Unmarshal([]byte(mock), merLoc)
if !reflect.DeepEqual(got, merLoc) {
t.Error("should return a merchant matching the mock response", cross)
}
}
func TestMerchantLocationForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/merchants/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.MerchantLocation(context.Background(), "949404bd-d32e-4f1e-9759-4d6caee3137c", "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 merchant location")
}
}