-
Notifications
You must be signed in to change notification settings - Fork 2
/
secret_test.go
57 lines (41 loc) · 1.46 KB
/
secret_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
package octohooks
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSecretValidation(t *testing.T) {
secret := Secret("super-secret")
t.Run("A valid signature returns no error", func(t *testing.T) {
signature := "sha1=8942fc54602322464f81a966cbd09a17077d4702"
body := []byte("hello-world")
err := secret.Validate(signature, body)
assert.Nil(t, err)
})
t.Run("An invalid signature returns an signatureInvalid error", func(t *testing.T) {
signature := "sha1=bunk"
body := []byte("hello-world")
err := secret.Validate(signature, body)
assert.IsType(t, signatureInvalid(""), err, "type matches signatureInvalid")
})
t.Run("An empty signature returns a signatureInvalid error", func(t *testing.T) {
signature := ""
body := []byte("hello-world")
err := secret.Validate(signature, body)
assert.IsType(t, signatureInvalid(""), err, "type matches signatureInvalid")
assert.Equal(t, "signature not found", err.Error(), "error is descriptive")
})
t.Run("An empty secret returns a no error", func(t *testing.T) {
secret = Secret("")
signature := "bunk"
body := []byte("hello-world")
err := secret.Validate(signature, body)
assert.Nil(t, err, "empty error")
})
}
func TestStaticResolver(t *testing.T) {
resolver := StaticResolver{Secret: "super-secret"}
secret, err := resolver.Resolve(&http.Request{})
assert.Nil(t, err)
assert.Equal(t, Secret("super-secret"), secret, "secret matches static one provided")
}