Skip to content

Commit

Permalink
test webhook verify
Browse files Browse the repository at this point in the history
  • Loading branch information
oussama4 committed Sep 26, 2021
1 parent e7d758a commit cfd457a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gopify

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)

func TestVerifyWebhook(t *testing.T) {
gopify := Gopify{
ApiKey: "key",
ApiSecret: "hush",
RedirectUrl: "https://example.com/auth",
Scopes: []string{"read_products"},
}

cases := []struct {
payload []byte
mac string
expected bool
}{
{[]byte("webhook request body"), "MzE4OWFmOThjYmIyODA2ZmZmZWFmMjdmYzQ2ZTg3MTM1M2FmZTNlYmMzMGYzNTNkMDA0ZjQyNjkxMGZjNzEzNA==", true},
{[]byte("webhook request body"), "wronghash", false},
}

for _, c := range cases {
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(c.payload))
req.Header.Add("X-Shopify-Hmac-SHA256", c.mac)
valid := gopify.VerifyWebhook(req)

if valid != c.expected {
t.Errorf("webhook verification expected %v got %v", c.expected, valid)
}
}
}

0 comments on commit cfd457a

Please sign in to comment.