-
Notifications
You must be signed in to change notification settings - Fork 21
/
webhook_test.go
112 lines (97 loc) · 2.94 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package onfido_test
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/uw-labs/go-onfido"
)
func TestNewWebhookFromEnv_MissingToken(t *testing.T) {
_, err := onfido.NewWebhookFromEnv()
if err == nil {
t.Fatal()
}
if err != onfido.ErrMissingWebhookToken {
t.Fatal("expected error to match ErrMissingWebhookToken")
}
}
func TestNewWebhookFromEnv_TokenSet(t *testing.T) {
expected := "808yup"
os.Setenv(onfido.WebhookTokenEnv, expected)
defer os.Setenv(onfido.WebhookTokenEnv, "")
wh, err := onfido.NewWebhookFromEnv()
if err != nil {
t.Fatal()
}
if wh.Token != expected {
t.Fatalf("expected to see `%s` token but got `%s`", expected, wh.Token)
}
}
func TestValidateSignature_InvalidSignature(t *testing.T) {
wh := onfido.Webhook{Token: "abc123"}
err := wh.ValidateSignature([]byte("hello world"), "invalid")
if err == nil {
t.Fatal()
}
if err != onfido.ErrInvalidWebhookSignature {
t.Fatal("expected error to match ErrInvalidWebhookSignature")
}
}
func TestValidateSignature_ValidSignature(t *testing.T) {
wh := onfido.Webhook{Token: "abc123"}
err := wh.ValidateSignature([]byte("hello world"), "fcc98c5b4f306cfe6b5b8fcce03ddb33fc13ae6b")
if err != nil {
t.Fatal()
}
}
func TestParseFromRequest_InvalidSignature(t *testing.T) {
req := &http.Request{
Header: make(map[string][]string),
}
req.Header.Add(onfido.WebhookSignatureHeader, "123")
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("{\"msg\": \"hello world\"}")))
wh := onfido.Webhook{Token: "abc123"}
_, err := wh.ParseFromRequest(req)
if err == nil {
t.Fatal()
}
if err != onfido.ErrInvalidWebhookSignature {
t.Fatal("expected error to match ErrInvalidWebhookSignature")
}
}
func TestParseFromRequest_SkipSignatureValidation(t *testing.T) {
req := &http.Request{
Header: make(map[string][]string),
}
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("{\"msg\": \"hello world\"}")))
wh := onfido.Webhook{Token: "abc123", SkipSignatureValidation: true}
_, err := wh.ParseFromRequest(req)
if err != nil {
t.Errorf("expected no error as signature validation should have been skipped: %s", err.Error())
}
}
func TestParseFromRequest_InvalidJson(t *testing.T) {
req := &http.Request{
Header: make(map[string][]string),
}
req.Header.Add(onfido.WebhookSignatureHeader, "d4163f7af2256fae6ab72cb595d3f9d1dfc6fecc")
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("{\"msg\": \"hello world")))
wh := onfido.Webhook{Token: "abc123"}
_, err := wh.ParseFromRequest(req)
if err == nil {
t.Fatal("expected invalid json to raise an error")
}
}
func TestParseFromRequest_ValidSignature(t *testing.T) {
req := &http.Request{
Header: make(map[string][]string),
}
req.Header.Add(onfido.WebhookSignatureHeader, "d2ef30601350308c1f1c25c5fbf359badb95cbfb")
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("{\"msg\": \"hello world\"}")))
wh := onfido.Webhook{Token: "abc123"}
_, err := wh.ParseFromRequest(req)
if err != nil {
t.Fatal()
}
}