-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignature_test.go
54 lines (46 loc) · 1.78 KB
/
signature_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
package appserver_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
appserver "github.com/janbuecker/shopware-appserver-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
// Signature for payload below.
verifyPayloadSignatureTestSignature = "f88bce849a86f16b9740eceb9190bff7d2a58c0a930d3afad5abcdb2162abacb"
verifyPayloadSignatureTestPayload = `{"data":{"event":"foo"},"source":{"shopId":"123"}}`
)
func TestVerifyPayloadSignature(t *testing.T) {
store := appserver.NewMemoryCredentialStore()
require.NoError(t, store.Store(context.Background(), appserver.Credentials{
ShopID: "123",
ShopSecret: "mysecret",
}))
srv := appserver.NewServer("", "mysecret", "", appserver.WithCredentialStore(store))
srv.Event("foo", func(_ context.Context, webhook appserver.WebhookRequest, api *appserver.APIClient) error {
return nil
})
t.Run("without signature", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", strings.NewReader(verifyPayloadSignatureTestPayload))
err := srv.HandleWebhook(req)
assert.EqualError(t, err, "invalid signature")
})
// test with invalid signature
t.Run("invalid signature", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/signature", strings.NewReader(verifyPayloadSignatureTestPayload))
req.Header.Set(appserver.ShopSignatureKey, "foo")
err := srv.HandleWebhook(req)
assert.EqualError(t, err, "invalid signature")
})
// test with valid signature
t.Run("valid signature", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/signature", strings.NewReader(verifyPayloadSignatureTestPayload))
req.Header.Set(appserver.ShopSignatureKey, verifyPayloadSignatureTestSignature)
err := srv.HandleWebhook(req)
assert.NoError(t, err)
})
}