-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature_test.go
72 lines (62 loc) · 3.09 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package dusupay
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"io/ioutil"
"testing"
)
type SignatureTestSuite struct {
suite.Suite
}
func (suite *SignatureTestSuite) TestValidateSignatureCollectionWebhookSuccess() {
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem")
webhook := &CollectionWebhook{ID: 226, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"}
validator, _ := NewSignatureValidator(rawBytes)
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature)
assert.NoError(suite.T(), err)
}
func (suite *SignatureTestSuite) TestValidateSignatureCollectionWebhookWrongPayload() {
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem")
webhook := &CollectionWebhook{ID: 225, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"}
validator, _ := NewSignatureValidator(rawBytes)
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature)
assert.Error(suite.T(), err)
assert.Equal(suite.T(), "crypto/rsa: verification error", err.Error())
}
func (suite *SignatureTestSuite) TestValidateSignatureCollectionWebhookNonBase64Signature() {
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem")
webhook := &CollectionWebhook{ID: 225, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"}
validator, _ := NewSignatureValidator(rawBytes)
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", "qwerty")
assert.Error(suite.T(), err)
assert.Equal(suite.T(), "illegal base64 data at input byte 4", err.Error())
}
func (suite *SignatureTestSuite) TestValidateSignaturePayoutWebhookSuccess() {
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem")
webhook := &PayoutWebhook{ID: 226, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"}
validator, _ := NewSignatureValidator(rawBytes)
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature)
assert.NoError(suite.T(), err)
}
func (suite *SignatureTestSuite) TestValidateSignatureRefundWebhookSuccess() {
rawBytes, _ := ioutil.ReadFile("stubs/rsa/public-key.pem")
webhook := &RefundWebhook{ID: 226, InternalReference: "DUSUPAY405GZM1G5JXGA71IK", TransactionStatus: "COMPLETED"}
validator, _ := NewSignatureValidator(rawBytes)
err := validator.ValidateSignature(webhook, "https://www.sample-url.com/callback", stubSignature)
assert.NoError(suite.T(), err)
}
func (suite *SignatureTestSuite) TestParsePublicKeyError() {
pk, err := parsePublicKey([]byte(`foo`))
assert.Error(suite.T(), err)
assert.Contains(suite.T(), err.Error(), "parsePublicKey wrong parse PKIX: asn1: structure error: tags don't match")
assert.Nil(suite.T(), pk)
}
func (suite *SignatureTestSuite) TestNewSignatureValidatorError() {
validator, err := NewSignatureValidator([]byte(`foo`))
assert.Error(suite.T(), err)
assert.Contains(suite.T(), err.Error(), "wrong public key data")
assert.Nil(suite.T(), validator)
}
func TestSignatureTestSuite(t *testing.T) {
suite.Run(t, new(SignatureTestSuite))
}