-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook_test.go
49 lines (38 loc) · 1.06 KB
/
webhook_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
package elation
import (
"bytes"
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWebhook(t *testing.T) {
assert := assert.New(t)
publicKey, privateKey, err := ed25519.GenerateKey(nil)
assert.NoError(err)
event := &Event{
Data: []byte(`{"foo":"bar"}`),
Action: "action",
EventID: 1,
ApplicationID: "application-id",
Resource: "resource",
}
b, err := json.Marshal(event)
assert.NoError(err)
sig := ed25519.Sign(privateKey, b)
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(b))
req.Header.Set(WebhookSignatureHeader, base64.StdEncoding.EncodeToString(sig))
actualEvent, err := VerifyWebhook(req, publicKey)
assert.Equal(event, actualEvent)
assert.NoError(err)
}
func TestWebhook_incorrect_public_key_len(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest(http.MethodPost, "/", nil)
event, err := VerifyWebhook(req, []byte("foo"))
assert.Nil(event)
assert.ErrorIs(err, ErrPublicKeyLength)
}