-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfixture_test.go
More file actions
268 lines (209 loc) · 8.99 KB
/
fixture_test.go
File metadata and controls
268 lines (209 loc) · 8.99 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
package easypost
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"time"
)
type Fixture struct {
Addresses map[string]*Address `json:"addresses,omitempty" url:"addresses,omitempty"`
Billing billingFixture `json:"billing,omitempty" url:"billing,omitempty"`
CarrierAccounts map[string]*CarrierAccount `json:"carrier_accounts,omitempty" url:"carrier_accounts,omitempty"`
CarrierStrings map[string]string `json:"carrier_strings,omitempty" url:"carrier_strings,omitempty"`
Claims map[string]*CreateClaimParameters `json:"claims,omitempty" url:"claims,omitempty"`
CustomsInfos map[string]*CustomsInfo `json:"customs_infos,omitempty" url:"customs_infos,omitempty"`
CustomsItems map[string]*CustomsItem `json:"customs_items,omitempty" url:"customs_items,omitempty"`
CreditCards map[string]*CreditCardOptions `json:"credit_cards,omitempty" url:"credit_cards,omitempty"`
FormOptions map[string]map[string]interface{} `json:"form_options,omitempty" url:"form_options,omitempty"`
Insurances map[string]*Insurance `json:"insurances,omitempty" url:"insurances,omitempty"`
Luma map[string]interface{} `json:"luma,omitempty" url:"luma,omitempty"`
Orders map[string]*Order `json:"orders,omitempty" url:"orders,omitempty"`
PageSizes map[string]int `json:"page_sizes,omitempty" url:"page_sizes,omitempty"`
Parcels map[string]*Parcel `json:"parcels,omitempty" url:"parcels,omitempty"`
Pickups map[string]*Pickup `json:"pickups,omitempty" url:"pickups,omitempty"`
ReportTypes map[string]string `json:"report_types,omitempty" url:"report_types,omitempty"`
ServiceNames map[string]map[string]string `json:"service_names,omitempty" url:"service_names,omitempty"`
Shipments map[string]*Shipment `json:"shipments,omitempty" url:"shipments,omitempty"`
TaxIdentifiers map[string]*TaxIdentifier `json:"tax_identifiers,omitempty" url:"tax_identifiers,omitempty"`
Users map[string]*UserOptions `json:"users,omitempty" url:"users,omitempty"`
Webhooks map[string]interface{} `json:"webhooks,omitempty" url:"webhooks,omitempty"`
}
type billingFixture struct {
PaymentMethodID string `json:"payment_method_id,omitempty" url:"payment_method_id,omitempty"`
FinancialConnectionsID string `json:"financial_connections_id,omitempty" url:"financial_connections_id,omitempty"`
MandateData *MandateData `json:"mandate_data,omitempty" url:"mandate_data,omitempty"`
Priority string `json:"priority,omitempty" url:"priority,omitempty"`
}
// Reads fixture data from the fixtures JSON file
func readFixtureData() Fixture {
filePath := "examples/official/fixtures/client-library-fixtures.json"
/* #nosec */
data, err := os.Open(filePath)
if err != nil {
fmt.Fprintln(os.Stderr, "error opening fixture file:", err)
os.Exit(1)
}
defer func() { _ = data.Close() }()
byteData, _ := io.ReadAll(data)
var fixtures Fixture
_ = json.Unmarshal([]byte(byteData), &fixtures)
return fixtures
}
// We keep the page_size of retrieving `all` records small so cassettes stay small
func (fixture *Fixture) pageSize() int {
return readFixtureData().PageSizes["five_results"]
}
// This is the USPS carrier account ID that comes with your EasyPost account by default and should be used for all tests
func (fixture *Fixture) USPSCarrierAccountID() string {
uspsCarrierAccountID := os.Getenv("USPS_CARRIER_ACCOUNT_ID")
// Fallback to the EasyPost Go Client Library Test User USPS carrier account ID
if len(uspsCarrierAccountID) == 0 {
return "ca_e606176ddb314afe896733636dba2f3b"
}
return uspsCarrierAccountID
}
func (fixture *Fixture) USPS() string {
return readFixtureData().CarrierStrings["usps"]
}
func (fixture *Fixture) USPSService() string {
return readFixtureData().ServiceNames["usps"]["first_service"]
}
func (fixture *Fixture) PickupService() string {
return readFixtureData().ServiceNames["usps"]["pickup_service"]
}
func (fixture *Fixture) ReportType() string {
return readFixtureData().ReportTypes["shipment"]
}
func (fixture *Fixture) ReportDate() string {
return "2022-04-11"
}
func (fixture *Fixture) CaAddress1() *Address {
return readFixtureData().Addresses["ca_address_1"]
}
func (fixture *Fixture) CaAddress2() *Address {
return readFixtureData().Addresses["ca_address_2"]
}
func (fixture *Fixture) IncorrectAddress() *Address {
return readFixtureData().Addresses["incorrect"]
}
func (fixture *Fixture) BasicParcel() *Parcel {
return readFixtureData().Parcels["basic"]
}
func (fixture *Fixture) BasicCustomsItem() *CustomsItem {
customsItem := readFixtureData().CustomsItems["basic"]
// Json unmarshalling doesn't handle float64 well, need to manually set the value
customsItem.Value = 23.25
return customsItem
}
func (fixture *Fixture) BasicCustomsInfo() *CustomsInfo {
customsInfo := readFixtureData().CustomsInfos["basic"]
// Json unmarshalling doesn't handle float64 well, need to manually set the value
for _, customsItem := range customsInfo.CustomsItems {
customsItem.Value = 23.25
}
return customsInfo
}
func (fixture *Fixture) TaxIdentifier() *TaxIdentifier {
return readFixtureData().TaxIdentifiers["basic"]
}
func (fixture *Fixture) BasicShipment() *Shipment {
return readFixtureData().Shipments["basic_domestic"]
}
func (fixture *Fixture) FullShipment() *Shipment {
return readFixtureData().Shipments["full"]
}
func (fixture *Fixture) OneCallBuyShipment() *Shipment {
return &Shipment{
ToAddress: fixture.CaAddress1(),
FromAddress: fixture.CaAddress2(),
Parcel: fixture.BasicParcel(),
Service: fixture.USPSService(),
CarrierAccountIDs: []string{fixture.USPSCarrierAccountID()},
Carrier: fixture.USPS(),
}
}
// This fixture will require you to add a `shipment` key with a Shipment object from a test.
// If you need to re-record cassettes, increment the date below and ensure it is one day in the future,
// USPS only does "next-day" pickups including Saturday but not Sunday or Holidays.
func (fixture *Fixture) BasicPickup() *Pickup {
pickupDate := NewDateTime(2024, time.August, 14, 0, 0, 0, 0, time.UTC)
pickupData := readFixtureData().Pickups["basic"]
pickupData.MinDatetime = &pickupDate
pickupData.MaxDatetime = &pickupDate
return pickupData
}
func (fixture *Fixture) BasicCarrierAccount() *CarrierAccount {
return readFixtureData().CarrierAccounts["basic"]
}
// This fixture will require you to add a `tracking_code` key with a tracking code from a shipment
func (fixture *Fixture) BasicInsurance() *Insurance {
return readFixtureData().Insurances["basic"]
}
// This fixture will require you to add a `tracking_code` key with a tracking code from a shipment and an `amount` key with a float value
func (fixture *Fixture) BasicClaim() *CreateClaimParameters {
return readFixtureData().Claims["basic"]
}
func (fixture *Fixture) BasicOrder() *Order {
return readFixtureData().Orders["basic"]
}
func (fixture *Fixture) EventBody() []byte {
filePath := "examples/official/fixtures/event-body.json"
/* #nosec */
data, err := os.Open(filePath)
if err != nil {
fmt.Fprintln(os.Stderr, "error opening fixture file:", err)
os.Exit(1)
}
defer func() { _ = data.Close() }()
scanner := bufio.NewScanner(data)
var eventBody []byte
for scanner.Scan() {
eventBody = []byte(scanner.Text())
}
return eventBody
}
func (fixture *Fixture) WebhookHmacSignature() string {
return readFixtureData().Webhooks["hmac_signature"].(string)
}
func (fixture *Fixture) WebhookSecret() string {
return readFixtureData().Webhooks["secret"].(string)
}
func (fixture *Fixture) WebhookUrl() string {
return readFixtureData().Webhooks["url"].(string)
}
func (fixture *Fixture) WebhookCustomHeaders() []WebhookCustomHeader {
var header WebhookCustomHeader
inputCustomHeaders := readFixtureData().Webhooks["custom_headers"]
customHeaders := inputCustomHeaders.([]interface{})
headerMap := customHeaders[0].(map[string]interface{})
header.Name = headerMap["name"].(string)
header.Value = headerMap["value"].(string)
webhookCustomHeaders := []WebhookCustomHeader{header}
return webhookCustomHeaders
}
func (fixture *Fixture) RmaFormOptions() map[string]interface{} {
return readFixtureData().FormOptions["rma"]
}
func (fixture *Fixture) ReferralUser() *UserOptions {
return readFixtureData().Users["referral"]
}
func (fixture *Fixture) TestCreditCard() *CreditCardOptions {
return readFixtureData().CreditCards["test"]
}
func (fixture *Fixture) PlannedShipDate() string {
return "2024-08-14"
}
func (fixture *Fixture) DesiredDeliveryDate() string {
return "2024-08-14"
}
func (fixture *Fixture) BillingData() billingFixture {
return readFixtureData().Billing
}
func (fixture *Fixture) LumaRulesetName() string {
return readFixtureData().Luma["ruleset_name"].(string)
}
func (fixture *Fixture) LumaPlannedShipDate() string {
return "2025-06-16"
}