-
Notifications
You must be signed in to change notification settings - Fork 1
/
signature_test.go
159 lines (153 loc) · 6.02 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
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
package bca
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func Test_generateSignature(t *testing.T) {
// Test 1 & 2 based on Authentication > Signature section in https://developer.bca.co.id/documentation/#signature
// Test 3 & 4 based on testGenerateSign() & testGenerateSign2() in https://github.com/odenktools/php-bca/blob/develop/test/unit/bcaConstructorTest.php
type args struct {
apiSecret string
method string
path string
accessToken string
requestBody string
timestamp string
}
tests := []struct {
name string
args args
wantSign string
wantStrToSign string
wantErr bool
}{
{name: "official bca/POST Example", args: args{
apiSecret: "22a2d25e-765d-41e1-8d29-da68dcb5698b",
method: http.MethodPost,
path: "/banking/corporates/transfers",
accessToken: "lIWOt2p29grUo59bedBUrBY3pnzqQX544LzYPohcGHOuwn8AUEdUKS",
requestBody: `
{
"CorporateID" : "BCAAPI2016",
"SourceAccountNumber" : "0201245680",
"TransactionID" : "00000001",
"TransactionDate" : "2016-01-30",
"ReferenceID" : "12345/PO/2016",
"CurrencyCode" : "IDR",
"Amount" : "100000.00",
"BeneficiaryAccountNumber" : "0201245681",
"Remark1" : "Transfer Test",
"Remark2" : "Online Transfer"
}
`,
timestamp: "2016-02-03T10:00:00.000+07:00",
},
wantSign: "69ad66589ade078a30922a0848725cf153aecfcca82eba94e3270285b4a9c604",
wantStrToSign: "POST:/banking/corporates/transfers:lIWOt2p29grUo59bedBUrBY3pnzqQX544LzYPohcGHOuwn8AUEdUKS:e3cf5797ac4ac02f7dad89ed2c5f5615c9884b2d802a504e4aebb76f45b8bdfb:2016-02-03T10:00:00.000+07:00",
wantErr: false,
},
{name: "official bca/GET Example", args: args{
apiSecret: "22a2d25e-765d-41e1-8d29-da68dcb5698b",
method: http.MethodGet,
path: "/banking/v2/corporates/BCAAPI2016/accounts/0201245680/statements?StartDate=2016-09-01&EndDate=2016-09-01",
accessToken: "lIWOt2p29grUo59bedBUrBY3pnzqQX544LzYPohcGHOuwn8AUEdUKS",
requestBody: "",
timestamp: "2016-02-03T10:00:00.000+07:00",
},
wantSign: "3ac124303746d222387d4398dddf33201a384aa22137aa08f4d9843c6f467a48",
wantStrToSign: "GET:/banking/v2/corporates/BCAAPI2016/accounts/0201245680/statements?EndDate=2016-09-01&StartDate=2016-09-01:lIWOt2p29grUo59bedBUrBY3pnzqQX544LzYPohcGHOuwn8AUEdUKS:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855:2016-02-03T10:00:00.000+07:00",
wantErr: false,
},
{name: "php-bca/testGenerateSign/GET Example", args: args{
apiSecret: "9db65b91-01ff-46ec-9274-3f234b677450",
method: http.MethodGet,
path: "/banking/v2/corporates/corpid/accounts/0063001004",
accessToken: "NopUsBuSbT3eNrQTfcEZN2aAL52JT1SlRgoL1MIslsX5gGIgv4YUf",
requestBody: "",
timestamp: "2017-09-30T22:03:35.800+07:00",
},
wantSign: "761eaec0e544c9cf5010b406ade39228ab182401e57f17fc54b9daa5ad99d0d6",
wantStrToSign: "GET:/banking/v2/corporates/corpid/accounts/0063001004:NopUsBuSbT3eNrQTfcEZN2aAL52JT1SlRgoL1MIslsX5gGIgv4YUf:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855:2017-09-30T22:03:35.800+07:00",
wantErr: false,
},
{name: "go/testGenerateSign/POST VA", args: args{
apiSecret: "4adfd936-61ca-4602-9b4b-4dd6b19abb0f",
method: "POST",
path: "/webhook/bca/inquiry-bills",
accessToken: "uAG5NYeecImOykoCAQbL6Nl0emC5yJ2M",
requestBody: `{
"CompanyCode": "20114",
"CustomerNumber": "087888857307",
"RequestID": "202002241437331226900030239310",
"ChannelType": "6014",
"TransactionDate": "24/02/202014:52:10",
"AdditionalData": "test"
}`,
timestamp: "2020-02-24T14:53:43.000+07:00",
},
wantSign: "91d10d5405fafb311f617872ad796fe4fb26cc763e6dc2a988dcee71b926d3c4",
wantStrToSign: "POST:/webhook/bca/inquiry-bills:uAG5NYeecImOykoCAQbL6Nl0emC5yJ2M:ad4fa9e73f907d0924a274776ef8064377d52b9edda38c330d49f66978a13b8e:2020-02-24T14:53:43.000+07:00",
wantErr: false,
},
// TODO: STILL FAILED
// {name: "php-bca/testGenerateSign2/GET Example", args: args{
// apiSecret: "9db65b91-01ff-46ec-9274-3f234b677450",
// method: http.MethodGet,
// path: "/banking/v2/corporates/corpid/accounts/0063001004",
// accessToken: "NopUsBuSbT3eNrQTfcEZN2aAL52JT1SlRgoL1MIslsX5gGIgv4YUf",
// requestBody: `
// {
// "Amount" : "100000.00",
// "BeneficiaryAccountNumber" : "8329389",
// "CorporateID" : "8293489283499",
// "CurrencyCode" : "idr",
// "ReferenceID" : "",
// "Remark1" : "Ini adalah remark1",
// "Remark2" : "Ini adalah remark2",
// "SourceAccountNumber" : "09202990",
// "TransactionDate" : "2019-02-30T22:03:35.800+07:00",
// "TransactionID" : "0020292"
// }
// `,
// timestamp: "2017-09-30T22:03:35.800+07:00",
// },
// wantSign: "1878f0eedcd93ff53054c8fc9ea271a29c99ea2f752f636c1cc765948009a90b",
// wantStrToSign: "GET:/banking/v2/corporates/corpid/accounts/0063001004:NopUsBuSbT3eNrQTfcEZN2aAL52JT1SlRgoL1MIslsX5gGIgv4YUf:4a24a20ceb436d69bd344902a71e9bdf3a45d11efac1754b48015fb2a291b3df:2017-09-30T22:03:35.800+07:00",
// wantErr: false,
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSign, gotStrToSign, err := GenerateSignature(tt.args.apiSecret, tt.args.method, tt.args.path, tt.args.accessToken, tt.args.requestBody, tt.args.timestamp)
require.NoError(t, err)
require.Equal(t, tt.wantStrToSign, gotStrToSign)
require.Equal(t, tt.wantSign, gotSign)
})
}
}
func Test_canonicalize(t *testing.T) {
// Based on Authentication > Signature section in https://developer.bca.co.id/documentation/#signature
type args struct {
data string
}
tests := []struct {
name string
args args
want string
}{
{name: "Canonicalization Example", args: args{
data: `{
"Test1" : "str Val",
"Test2" : 1
}`},
want: `{"Test1":"strVal","Test2":1}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := canonicalize(tt.args.data)
require.Equal(t, tt.want, got)
})
}
}