-
Notifications
You must be signed in to change notification settings - Fork 1
/
authenticator_test.go
69 lines (58 loc) · 2.27 KB
/
authenticator_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package webhook_test
import (
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/require"
"github.com/rockset/webhook"
)
func TestNoopAuthorization(t *testing.T) {
auth := &webhook.NoopAuthenticator{}
err := auth.Authenticate(events.LambdaFunctionURLRequest{})
require.NoError(t, err)
}
func TestFailAuthorization(t *testing.T) {
auth := &webhook.FailAuthenticator{}
err := auth.Authenticate(events.LambdaFunctionURLRequest{})
require.Error(t, err)
}
func TestHeaderAuthorization(t *testing.T) {
tests := map[string]struct {
request events.LambdaFunctionURLRequest
err require.ErrorAssertionFunc
}{
"pass": {events.LambdaFunctionURLRequest{Headers: map[string]string{"X-Secret": "secret"}}, require.NoError},
"fail": {events.LambdaFunctionURLRequest{Headers: map[string]string{"X-Secret": "incorrect"}}, require.Error},
"no header": {events.LambdaFunctionURLRequest{Headers: map[string]string{}}, require.Error},
}
auth := &webhook.HeaderAuthenticator{
Header: "X-Secret",
Secret: "secret",
}
for name, tst := range tests {
t.Run(name, func(t *testing.T) {
tst.err(t, auth.Authenticate(tst.request))
})
}
}
func TestSignatureAuthorization(t *testing.T) {
body := "body"
tests := map[string]struct {
request events.LambdaFunctionURLRequest
err require.ErrorAssertionFunc
}{
// x-hub-signature-256: sha256=dc46983557fea127b43af721467eb9b3fde2338fe3e14f51952aa8478c13d355
"pass": {events.LambdaFunctionURLRequest{Headers: map[string]string{"x-secret": "dc46983557fea127b43af721467eb9b3fde2338fe3e14f51952aa8478c13d355"}, Body: body}, require.NoError},
"pass with =": {events.LambdaFunctionURLRequest{Headers: map[string]string{"x-secret": "sha256=dc46983557fea127b43af721467eb9b3fde2338fe3e14f51952aa8478c13d355"}, Body: body}, require.NoError},
"fail": {events.LambdaFunctionURLRequest{Headers: map[string]string{"x-secret": "incorrect"}, Body: body}, require.Error},
"no header": {events.LambdaFunctionURLRequest{Headers: map[string]string{}, Body: body}, require.Error},
}
auth := &webhook.SignatureAuthenticator{
Header: "X-Secret",
SigningSecret: "secret",
}
for name, tst := range tests {
t.Run(name, func(t *testing.T) {
tst.err(t, auth.Authenticate(tst.request))
})
}
}