-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsignature_test.go
More file actions
168 lines (148 loc) · 4.53 KB
/
signature_test.go
File metadata and controls
168 lines (148 loc) · 4.53 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package adyen
import (
"encoding/json"
"net/http"
"os"
"testing"
"github.com/google/go-querystring/query"
)
func TestSignatureCalculateSignature(t *testing.T) {
t.Parallel()
instance := getTestInstanceWithHPP()
req := DirectoryLookupRequest{
CurrencyCode: "EUR",
MerchantAccount: os.Getenv("ADYEN_ACCOUNT"),
ShipBeforeDate: "2015-11-31T13:42:40+1:00",
PaymentAmount: 1000,
SkinCode: os.Getenv("ADYEN_SKINCODE"),
MerchantReference: "DE-100100GMWJGS",
SessionsValidity: "2015-11-29T13:42:40+1:00",
}
err := req.CalculateSignature(instance)
if err != nil {
t.Fatal(err)
}
v, _ := query.Values(req)
// there is no automated way to verify full URL, cause adyen webpage require authenication first
// to debug request signature, print URL params, login to https://ca-test.adyen.com and follow full link below
url := "https://ca-test.adyen.com/ca/ca/skin/checkhmac.shtml" + "?" + v.Encode()
if _, err = http.NewRequest(http.MethodGet, url, nil); err != nil {
t.Fatal(err)
}
}
func TestSignatureCalculateSignatureForSkipHppRequest(t *testing.T) {
t.Parallel()
instance := getTestInstanceWithHPP()
req := SkipHppRequest{
MerchantReference: "DE-100100GMWJGS",
PaymentAmount: 1000,
CurrencyCode: instance.Currency,
ShipBeforeDate: "2015-11-31T13:42:40+1:00",
SkinCode: os.Getenv("ADYEN_SKINCODE"),
MerchantAccount: os.Getenv("ADYEN_ACCOUNT"),
ShopperLocale: "en_GB",
SessionsValidity: "2015-11-29T13:42:40+1:00",
CountryCode: "NL",
BrandCode: "ideal",
}
err := req.CalculateSignature(instance)
if err != nil {
t.Fatal(err)
}
v, _ := query.Values(req)
// there is no automated way to verify full URL, cause adyen webpage require authenication first
// to debug request signature, print URL params, login to https://ca-test.adyen.com and follow full link below
url := "https://ca-test.adyen.com/ca/ca/skin/checkhmac.shtml" + "?" + v.Encode()
if _, err = http.NewRequest(http.MethodGet, url, nil); err != nil {
t.Fatal(err)
}
}
func TestSignatureNotification(t *testing.T) {
t.Parallel()
//ref: https://github.com/Adyen/adyen-ruby-api-library/blob/53d9a03ab09d58927ec34e65d3d2acc1c5dc1ea7/spec/utils/hmac_validator_spec.rb
itemDataJSON := `
{
"additionalData": {
"authCode": "1234",
"cardSummary": "7777"
},
"amount": {
"currency": "EUR",
"value": 1130
},
"eventCode": "AUTHORISATION",
"eventDate": "2020-01-01T10:00:00+05:00",
"merchantAccountCode": "TestMerchant",
"merchantReference": "TestPayment-1407325143704",
"operations": ["CANCEL", "CAPTURE", "REFUND"],
"paymentMethod": "visa",
"pspReference": "7914073381342284",
"reason": "1234:7777:12\/2012",
"success": "true"
}
`
validSignature := "coqCmt/IZ4E3CzPvMY8zTjQVL5hYJUiBRg8UU+iCWo0="
cases := []struct {
name string
signatureInMessage string
hmacKey string
exp bool
expErr bool
}{
{
name: "no sig",
signatureInMessage: "",
hmacKey: "",
exp: false,
expErr: true,
},
{
name: "sig, no key",
signatureInMessage: validSignature,
hmacKey: "",
exp: false,
expErr: true,
},
{
name: "sig, wrong key",
signatureInMessage: validSignature,
hmacKey: "DFB1EB5485895CFA84146406857104ABB4CBCABDC8AAF103A624C8F6A3EAAB00",
exp: false,
expErr: false,
},
{
name: "sig, correct key",
signatureInMessage: validSignature,
hmacKey: "44782DEF547AAA06C910C43932B1EB0C71FC68D9D0C057550C48EC2ACF6BA056",
exp: true,
expErr: false,
},
{
name: "sig, malformed key",
signatureInMessage: validSignature,
hmacKey: "invalid_hmac",
exp: false,
expErr: true,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var itemData NotificationRequestItemData
err := json.Unmarshal([]byte(itemDataJSON), &itemData)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if c.hmacKey != "" {
itemData.AdditionalData.HmacSignature = c.signatureInMessage
}
config := NewWithHMAC(Testing, "username", "fake_password", c.hmacKey)
res, err := itemData.ValidateSignature(config)
if (err != nil) != c.expErr {
t.Fatalf("expected error?: %t, actual error: %v", c.expErr, err)
}
if res != c.exp {
t.Fatalf("expected result: %t, actual result: %t", c.exp, res)
}
})
}
}