Skip to content

Commit

Permalink
chore: update webhook validation fixtures (#236)
Browse files Browse the repository at this point in the history
* chore: update webhook validation fixtures

* Update webhook_test.go
  • Loading branch information
Justintime50 committed Aug 15, 2024
1 parent 7bd2358 commit d68244a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@ jobs:
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Install dependencies
run: make install
# curl installs must occur in the same step as the linter to run properly on CI
# install must occur in the same step as the linter to run properly on CI
- name: Lint project
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
curl -sSfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
make lint
run: make install lint

build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.16', '1.17', '1.18', '1.19', '1.20', '1.21', '1.22']
go-version: ['1.16', '1.17', '1.18', '1.19', '1.20', '1.21', '1.22', '1.23']
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: use golang ${{ matrix.go-version }}
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: install
Expand All @@ -44,7 +42,7 @@ jobs:
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.19' # TODO: There is a bug in 1.20 that isn't calculating coverage correctly
- name: Install dependencies
Expand All @@ -57,7 +55,7 @@ jobs:
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.19' # TODO: There is a bug in 1.20 that isn't calculating coverage correctly
- name: Install dependencies
Expand Down
16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PROJECT_NAME := easypost-go
PROJECT_PATH := $$(go env GOPATH)/bin/$(PROJECT_NAME)
GO_BIN := $(shell go env GOPATH)/bin
DIST_PATH := dist

## help - Display help about make targets for this Makefile
Expand All @@ -13,7 +13,7 @@ build:
## clean - Clean the project
clean:
rm -rf $(DIST_PATH)
rm $(PROJECT_PATH)
rm $(GO_BIN)/$(PROJECT_NAME)

## coverage - Get test coverage and open it in a browser
coverage:
Expand All @@ -26,17 +26,21 @@ init-examples-submodule:

## install - Install and vendor dependencies
install: | init-examples-submodule
brew install golangci-lint || exit 0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GO_BIN) v1.59.1
curl -sSfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(GO_BIN)
go mod vendor
go build -o $(PROJECT_PATH)

## golangci - Lint the project
golangci:
golangci-lint run --config examples/style_guides/golang/.golangci.yml
$(GO_BIN)/golangci-lint run --config examples/style_guides/golang/.golangci.yml

## lint - Lint the project
lint: golangci scan

## lint-fix - Runs formatting tools against the project
lint-fix:
$(GO_BIN)/golangci-lint run --config examples/style_guides/golang/.golangci.yml --fix

## release - Cuts a release for the project on GitHub (requires GitHub CLI)
# tag = The associated tag title of the release
# target = Target branch or full commit SHA
Expand All @@ -45,7 +49,7 @@ release:

## scan - Run gosec to scan for security issues
scan:
gosec -tests --exclude-dir=examples ./...
$(GO_BIN)/gosec -tests --exclude-dir=examples ./...

## test - Test the project
test:
Expand Down
56 changes: 33 additions & 23 deletions tests/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@ import (
)

type Fixture struct {
Addresses map[string]*easypost.Address `json:"addresses,omitempty" url:"addresses,omitempty"`
CarrierAccounts map[string]*easypost.CarrierAccount `json:"carrier_accounts,omitempty" url:"carrier_accounts,omitempty"`
CarrierStrings map[string]string `json:"carrier_strings,omitempty" url:"carrier_strings,omitempty"`
Claims map[string]*easypost.CreateClaimParameters `json:"claims,omitempty" url:"claims,omitempty"`
CustomsInfos map[string]*easypost.CustomsInfo `json:"customs_infos,omitempty" url:"customs_infos,omitempty"`
CustomsItems map[string]*easypost.CustomsItem `json:"customs_items,omitempty" url:"customs_items,omitempty"`
CreditCards map[string]*easypost.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]*easypost.Insurance `json:"insurances,omitempty" url:"insurances,omitempty"`
Orders map[string]*easypost.Order `json:"orders,omitempty" url:"orders,omitempty"`
PageSizes map[string]int `json:"page_sizes,omitempty" url:"page_sizes,omitempty"`
Parcels map[string]*easypost.Parcel `json:"parcels,omitempty" url:"parcels,omitempty"`
Pickups map[string]*easypost.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]*easypost.Shipment `json:"shipments,omitempty" url:"shipments,omitempty"`
TaxIdentifiers map[string]*easypost.TaxIdentifier `json:"tax_identifiers,omitempty" url:"tax_identifiers,omitempty"`
Users map[string]*easypost.UserOptions `json:"users,omitempty" url:"users,omitempty"`
WebhookURL string `json:"webhook_url,omitempty" url:"webhook_url,omitempty"`
Addresses map[string]*easypost.Address `json:"addresses,omitempty" url:"addresses,omitempty"`
CarrierAccounts map[string]*easypost.CarrierAccount `json:"carrier_accounts,omitempty" url:"carrier_accounts,omitempty"`
CarrierStrings map[string]string `json:"carrier_strings,omitempty" url:"carrier_strings,omitempty"`
Claims map[string]*easypost.CreateClaimParameters `json:"claims,omitempty" url:"claims,omitempty"`
CustomsInfos map[string]*easypost.CustomsInfo `json:"customs_infos,omitempty" url:"customs_infos,omitempty"`
CustomsItems map[string]*easypost.CustomsItem `json:"customs_items,omitempty" url:"customs_items,omitempty"`
CreditCards map[string]*easypost.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]*easypost.Insurance `json:"insurances,omitempty" url:"insurances,omitempty"`
Orders map[string]*easypost.Order `json:"orders,omitempty" url:"orders,omitempty"`
PageSizes map[string]int `json:"page_sizes,omitempty" url:"page_sizes,omitempty"`
Parcels map[string]*easypost.Parcel `json:"parcels,omitempty" url:"parcels,omitempty"`
Pickups map[string]*easypost.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]*easypost.Shipment `json:"shipments,omitempty" url:"shipments,omitempty"`
TaxIdentifiers map[string]*easypost.TaxIdentifier `json:"tax_identifiers,omitempty" url:"tax_identifiers,omitempty"`
Users map[string]*easypost.UserOptions `json:"users,omitempty" url:"users,omitempty"`
WebhookHmacSignatureString string `json:"webhook_hmac_signature,omitempty" url:"webhook_hmac_signature,omitempty"`
WebhookSecretString string `json:"webhook_secret,omitempty" url:"webhook_secret,omitempty"`
WebhookURL string `json:"webhook_url,omitempty" url:"webhook_url,omitempty"`
}

// Reads fixture data from the fixtures JSON file
Expand Down Expand Up @@ -94,10 +96,6 @@ func (fixture *Fixture) ReportDate() string {
return "2022-04-11"
}

func (fixture *Fixture) WebhookUrl() string {
return readFixtureData().WebhookURL
}

func (fixture *Fixture) CaAddress1() *easypost.Address {
return readFixtureData().Addresses["ca_address_1"]
}
Expand Down Expand Up @@ -211,6 +209,18 @@ func (fixture *Fixture) EventBody() []byte {
return eventBody
}

func (fixture *Fixture) WebhookHmacSignature() string {
return readFixtureData().WebhookHmacSignatureString
}

func (fixture *Fixture) WebhookSecret() string {
return readFixtureData().WebhookSecretString
}

func (fixture *Fixture) WebhookUrl() string {
return readFixtureData().WebhookURL
}

func (fixture *Fixture) RmaFormOptions() map[string]interface{} {
return readFixtureData().FormOptions["rma"]
}
Expand Down
9 changes: 4 additions & 5 deletions tests/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,15 @@ func (c *ClientTests) TestValidateWebhook() {
client := c.TestClient()
assert, require := c.Assert(), c.Require()

webhookSecret := "sécret"
expectedHmacSignature := "hmac-sha256-hex=e93977c8ccb20363d51a62b3fe1fc402b7829be1152da9e88cf9e8d07115a46b"
headers := map[string]interface{}{
"X-Hmac-Signature": expectedHmacSignature,
"X-Hmac-Signature": c.fixture.WebhookHmacSignature(),
}

webhookBody, err := client.ValidateWebhook(c.fixture.EventBody(), headers, webhookSecret)
webhookBody, err := client.ValidateWebhook(c.fixture.EventBody(), headers, c.fixture.WebhookSecret())
require.NoError(err)

assert.Equal(webhookBody.Description, "batch.created")
assert.Equal(webhookBody.Description, "tracker.updated")
assert.Equal(webhookBody.Result.(*easypost.Tracker).Weight, 614.4) // Ensure we convert floats properly
}

func (c *ClientTests) TestValidateWebhookInvalidSecret() {
Expand Down

0 comments on commit d68244a

Please sign in to comment.