-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbootstrap_test.go
More file actions
349 lines (305 loc) · 10 KB
/
bootstrap_test.go
File metadata and controls
349 lines (305 loc) · 10 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
package easypost
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
"github.com/stretchr/testify/suite"
)
var (
// TestAPIKey is used for most tests.
TestAPIKey string
// ProdAPIKey is used for tests that require a prod key (like the user API).
ProdAPIKey string
// PartnerAPIKey is used for tests that require a partner key (like the white label API).
PartnerAPIKey string
// ReferralAPIKey is used for tests that require a referral key (like the white label API) (internal, access via ReferralAPIKey()).
ReferralAPIKey string
)
type ErrorRoundTripper struct{}
func (ErrorRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("no cassette found for request %s", req.URL)
}
type ClientTests struct {
suite.Suite
recorder *recorder.Recorder
fixture *Fixture
}
// contains returns true if the string is in the list.
func contains(elements []string, target string) bool {
for _, element := range elements {
if target == element {
return true
}
}
return false
}
// applyCensorsToJsonList applies the censors to a JSON list.
func (c *ClientTests) applyCensorsToJsonList(list []interface{}, elementsToCensor map[string]interface{}) []interface{} {
if len(list) == 0 {
// short circuit and return the list if it is empty
return list
}
for index, value := range list {
if value == nil {
// don't need to worry about censoring nil values
continue
}
// nolint:gosimple
switch value.(type) {
case map[string]interface{}:
// value is a dictionary
list[index] = c.applyCensorsToJsonDictionary(value.(map[string]interface{}), elementsToCensor)
case []map[string]interface{}:
// value is a list of dictionaries
list[index] = c.applyCensorsToJsonList(value.([]interface{}), elementsToCensor)
default:
// value is a single value or a normal list, nothing to censor
}
}
return list
}
// applyCensorsToJsonDictionary applies the censors to a JSON dictionary.
func (c *ClientTests) applyCensorsToJsonDictionary(dictionary map[string]interface{}, elementsToCensor map[string]interface{}) map[string]interface{} {
if len(dictionary) == 0 {
// short circuit and return the dictionary if it is empty
return dictionary
}
for key, value := range dictionary {
if value == nil {
// don't need to worry about censoring nil values
continue
}
var censorKeys []string
for k := range elementsToCensor {
censorKeys = append(censorKeys, k)
}
if contains(censorKeys, key) {
// element should be censored
// replace value with corresponding censor value
dictionary[key] = elementsToCensor[key]
} else {
// element doesn't need to be censored
// nolint:gosimple
switch value.(type) {
case map[string]interface{}:
// value is a dictionary
dictionary[key] = c.applyCensorsToJsonDictionary(value.(map[string]interface{}), elementsToCensor)
case []interface{}:
// value is a list
dictionary[key] = c.applyCensorsToJsonList(value.([]interface{}), elementsToCensor)
default:
// value is a single value, nothing to censor
}
}
}
return dictionary
}
// censorJsonData censors data in cassette files before they persist to disk
func (c *ClientTests) censorJsonData(data string, elementsToCensor map[string]interface{}) string {
var jsonMap map[string]interface{}
mapErr := json.Unmarshal([]byte(data), &jsonMap)
if mapErr != nil {
// data is not a JSON dictionary
var jsonList []interface{}
listErr := json.Unmarshal([]byte(data), &jsonList)
if listErr != nil {
// data is not a JSON list either
// short circuit and return the data
return data
}
// data is a JSON list
censoredList := c.applyCensorsToJsonList(jsonList, elementsToCensor)
censoredListBytes, _ := json.Marshal(censoredList)
return string(censoredListBytes)
}
// data is a JSON dictionary
censoredDictionary := c.applyCensorsToJsonDictionary(jsonMap, elementsToCensor)
censoredDictionaryBytes, _ := json.Marshal(censoredDictionary)
return string(censoredDictionaryBytes)
}
// SetupTest runs all the logic required for a test to run
func (c *ClientTests) SetupTest() {
pathComponents := append(
[]string{"cassettes/"}, strings.Split(c.T().Name(), "/")[1],
)
r, err := recorder.New(filepath.Join(pathComponents...))
c.Require().NoError(err)
c.checkExpiredCassette()
// Filter sensitive data from cassettes
// Replace value has to be type specific to its corresponding struct
redactedString := "REDACTED"
redactedStringMap := map[string]string{}
responseBodyElementsToCensor := map[string]interface{}{
"client_ip": redactedString,
"credentials": redactedStringMap,
"email": redactedString,
"fields": redactedStringMap,
"key": redactedString,
"phone_number": redactedString,
"phone": redactedString,
"test_credentials": redactedStringMap,
}
// Strictly match the URL, method, and body of the requests
r.SetMatcher(func(r *http.Request, i cassette.Request) bool {
if r.Body == nil {
return cassette.DefaultMatcher(r, i)
}
var b bytes.Buffer
if _, err := b.ReadFrom(r.Body); err != nil {
return false
}
r.Body = io.NopCloser(&b)
bString := b.String()
if bString == "" && i.Body == "" {
// short circuit and return true if the body is empty as it should be
return true
}
// run the request body through the same censors before comparing to the recording
bStringCensored := c.censorJsonData(bString, responseBodyElementsToCensor)
return cassette.DefaultMatcher(r, i) && (bStringCensored == i.Body)
})
r.AddSaveFilter(func(i *cassette.Interaction) error {
// Filter headers
redactedStringList := []string{"REDACTED"}
if i.Request.Headers["Authorization"] != nil {
i.Request.Headers["Authorization"] = redactedStringList
}
if i.Request.Headers["User-Agent"] != nil {
i.Request.Headers["User-Agent"] = redactedStringList
}
if i.Request.Headers["X-Client-User-Agent"] != nil {
i.Request.Headers["X-Client-User-Agent"] = redactedStringList
}
// Censor request data
var requestBody = i.Request.Body
var censoredRequestBody = c.censorJsonData(requestBody, responseBodyElementsToCensor)
i.Request.Body = censoredRequestBody
// Censor response data
var responseBody = i.Response.Body
var censoredResponseBody = c.censorJsonData(responseBody, responseBodyElementsToCensor)
i.Response.Body = censoredResponseBody
return nil
})
if TestAPIKey == "" {
r.SetTransport(ErrorRoundTripper{})
}
c.recorder = r
}
// checkExpiredCassette checks for an expired cassette and warns if it is too old and must be re-recorded
func (c *ClientTests) checkExpiredCassette() {
fullCassettePath := "cassettes/" + strings.Split(c.T().Name(), "/")[1] + ".yaml"
const expirationDays = 365
expirationHours := expirationDays * 24 * time.Hour
if _, err := os.Stat(fullCassettePath); err == nil {
cassette, _ := os.Stat(fullCassettePath)
cassetteTimestamp := cassette.ModTime()
expirationTimestamp := cassetteTimestamp.Add(expirationHours)
currentTimestamp := time.Now()
if currentTimestamp.After(expirationTimestamp) {
c.T().Logf("%s is older than %d days and has expired. Please re-record the cassette", fullCassettePath, expirationDays)
}
}
}
// TearDownTest runs all the logic required to teardown a test
func (c *ClientTests) TearDownTest() {
_ = c.recorder.Stop()
}
// TestClient sets up the test mode client object to be used in the test
func (c *ClientTests) TestClient() *Client {
return &Client{
APIKey: TestAPIKey,
Client: &http.Client{Transport: c.recorder},
}
}
// ProdClient sets up the prod mode client object to be used in the test
func (c *ClientTests) ProdClient() *Client {
return &Client{
APIKey: ProdAPIKey,
Client: &http.Client{Transport: c.recorder},
}
}
// PartnerClient sets up the partner user client object to be used in the test
func (c *ClientTests) PartnerClient() *Client {
if len(PartnerAPIKey) == 0 {
PartnerAPIKey = "123"
}
return &Client{
APIKey: PartnerAPIKey,
Client: &http.Client{Transport: c.recorder},
}
}
// ReferralClient sets up the referral customer client object to be used in the test
func (c *ClientTests) ReferralClient() *Client {
if len(ReferralAPIKey) == 0 {
ReferralAPIKey = "123"
}
return &Client{
APIKey: ReferralAPIKey,
Client: &http.Client{Transport: c.recorder},
}
}
// ReferralAPIKey returns the referral api key or a fallback if the environment variable is not set
func (c *ClientTests) ReferralAPIKey() string {
if len(ReferralAPIKey) == 0 {
ReferralAPIKey = "123"
}
return ReferralAPIKey
}
// MockClient sets up the mock client object to be used in the test
func (c *ClientTests) MockClient(requests []MockRequest) *Client {
return &Client{
APIKey: "cannot_be_blank",
Client: &http.Client{Transport: c.recorder},
MockRequests: requests,
}
}
// TestClient runs the entire test suite
func TestClient(t *testing.T) {
suite.Run(t, new(ClientTests))
}
// TestMain is the entrypoint when running tests via the CLI
func TestMain(m *testing.M) {
// Create a separate FlagSet just so we can print help specific to our
// flags--don't need to dump help for all of Go's internal test.* flags.
fs := flag.NewFlagSet("test", flag.ExitOnError)
fs.StringVar(
&TestAPIKey,
"test-api-key",
os.Getenv("EASYPOST_TEST_API_KEY"),
"test key to use against EasyPost API",
)
fs.StringVar(
&ProdAPIKey,
"prod-api-key",
os.Getenv("EASYPOST_PROD_API_KEY"),
"production key to use against EasyPost API",
)
fs.StringVar(
&PartnerAPIKey,
"partner-api-key",
os.Getenv("PARTNER_USER_PROD_API_KEY"),
"production key for partner account to use against EasyPost API",
)
fs.StringVar(
&ReferralAPIKey,
"referral-api-key",
os.Getenv("REFERRAL_CUSTOMER_PROD_API_KEY"),
"production key for referral account to use against EasyPost API",
)
// Add flags to CommandLine (default) FlagSet:
fs.VisitAll(func(f *flag.Flag) { flag.Var(f.Value, f.Name, f.Usage) })
flag.Parse()
testSuite := m.Run()
// Exit once tests complete
os.Exit(testSuite)
}