forked from tonicpow/go-handcash-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
307 lines (250 loc) · 9.93 KB
/
api_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package handcash
import (
"encoding/hex"
"net/http"
"testing"
"github.com/bitcoinschema/go-bitcoin"
"github.com/stretchr/testify/assert"
)
const (
testTimestamp = "2020-12-10T16:31:23.304Z"
)
func TestGetSignedRequest(t *testing.T) {
t.Parallel()
t.Run("valid auth - empty body", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{}, EnvironmentBeta)
assert.NotNil(t, client)
// These values are used (GOSEC complains about the token)
token := "68d8fadc95324afa853f00923e0b" + "86f06a76ceb7a6afbb1784e0dde8f43989a0"
method := http.MethodGet
timestamp := testTimestamp
endpoint := endpointProfileCurrent
// privateKey := "5JcTmjpJkfkcRnf3W2qTvauC4mczNsnUY3SLm6EcKQDS3Gj2wGh" // 68d8fadc95324afa853f00923e0b86f06a76ceb7a6afbb1784e0dde8f439
signedRequest, err := client.getSignedRequest(method, endpoint, token, nil, timestamp)
assert.NoError(t, err)
assert.NotNil(t, signedRequest)
assert.Equal(t,
"0275e7081e5b6e73c94998098e075c0ed888d1eb33c721ee38ee741648b108c90d",
signedRequest.Headers.OauthPublicKey,
)
assert.Equal(t,
"30450221009b613aa82657e28471406d3a390688bc2dceece75cf73d89088d447cc9d1f5c502200a1ff4f02f5dfdb7f48b51b6dd2a0f586f591b07a89d5ad846392fca8ae0c856",
signedRequest.Headers.OauthSignature,
)
assert.Equal(t, testTimestamp, signedRequest.Headers.OauthTimestamp)
assert.Equal(t, client.Environment.APIURL+endpointProfileCurrent, signedRequest.URI)
assert.Equal(t, http.MethodGet, signedRequest.Method)
})
t.Run("valid auth - with body", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{}, EnvironmentBeta)
assert.NotNil(t, client)
// These values are used (GOSEC complains about the token)
token := "68d8fadc95324afa853f00923e0b" + "86f06a76ceb7a6afbb1784e0dde8f43989a0"
method := http.MethodGet
timestamp := testTimestamp
endpoint := endpointProfileCurrent
// privateKey := "5JcTmjpJkfkcRnf3W2qTvauC4mczNsnUY3SLm6EcKQDS3Gj2wGh" // 68d8fadc95324afa853f00923e0b86f06a76ceb7a6afbb1784e0dde8f439
var customBodyContents = struct {
Name string `json:"name"`
Number int `json:"number"`
Float float64 `json:"float"`
Boolean bool `json:"boolean"`
}{Name: "TestName", Number: 123, Float: 123.123, Boolean: true}
signedRequest, err := client.getSignedRequest(method, endpoint, token, customBodyContents, timestamp)
assert.NoError(t, err)
assert.NotNil(t, signedRequest)
assert.Equal(t,
"0275e7081e5b6e73c94998098e075c0ed888d1eb33c721ee38ee741648b108c90d",
signedRequest.Headers.OauthPublicKey,
)
assert.Equal(t,
"304402200901cc9d77c01d8ab89583eca396f4b753154cb007173ad212e1175e643cf5e1022026f8ab3e03eb1891bb05a24b742d40192fa8b40d2578c5ffc36fb70cf0fafb6a",
signedRequest.Headers.OauthSignature,
)
assert.Equal(t, testTimestamp, signedRequest.Headers.OauthTimestamp)
assert.Equal(t, client.Environment.APIURL+endpointProfileCurrent, signedRequest.URI)
assert.Equal(t, http.MethodGet, signedRequest.Method)
})
t.Run("invalid auth token", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{}, EnvironmentBeta)
assert.NotNil(t, client)
method := http.MethodGet
endpoint := endpointProfileCurrent
token := "0"
timestamp := testTimestamp
signedRequest, err := client.getSignedRequest(method, endpoint, token, nil, timestamp)
assert.Error(t, err)
assert.Nil(t, signedRequest)
})
t.Run("valid token, invalid json", func(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{}, EnvironmentBeta)
assert.NotNil(t, client)
method := http.MethodGet
endpoint := endpointProfileCurrent
token := "68d8fadc95324afa853f00923e0b" + "86f06a76ceb7a6afbb1784e0dde8f43989a0"
timestamp := testTimestamp
signedRequest, err := client.getSignedRequest(method, endpoint, token, make(chan int), timestamp)
assert.Error(t, err)
assert.Nil(t, signedRequest)
})
}
func TestGetRequestSignature(t *testing.T) {
t.Parallel()
t.Run("valid signature", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := testTimestamp
privateKey, err := bitcoin.CreatePrivateKey()
assert.NoError(t, err)
assert.NotNil(t, privateKey)
var signature []byte
signature, err = getRequestSignature(method, endpoint, nil, timestamp, privateKey)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(hex.EncodeToString(signature)))
})
t.Run("missing method", func(t *testing.T) {
method := ""
endpoint := endpointProfileCurrent
timestamp := testTimestamp
privateKey, err := bitcoin.CreatePrivateKey()
assert.NoError(t, err)
assert.NotNil(t, privateKey)
var signature []byte
signature, err = getRequestSignature(method, endpoint, nil, timestamp, privateKey)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(hex.EncodeToString(signature)))
})
t.Run("missing endpoint", func(t *testing.T) {
method := http.MethodGet
endpoint := ""
timestamp := testTimestamp
privateKey, err := bitcoin.CreatePrivateKey()
assert.NoError(t, err)
assert.NotNil(t, privateKey)
var signature []byte
signature, err = getRequestSignature(method, endpoint, nil, timestamp, privateKey)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(hex.EncodeToString(signature)))
})
t.Run("missing timestamp", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := ""
privateKey, err := bitcoin.CreatePrivateKey()
assert.NoError(t, err)
assert.NotNil(t, privateKey)
var signature []byte
signature, err = getRequestSignature(method, endpoint, nil, timestamp, privateKey)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(hex.EncodeToString(signature)))
})
t.Run("custom struct", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := testTimestamp
privateKey, err := bitcoin.CreatePrivateKey()
assert.NoError(t, err)
assert.NotNil(t, privateKey)
var customBodyContents = struct {
Name string `json:"name"`
Number int `json:"number"`
Float float64 `json:"float"`
Boolean bool `json:"boolean"`
}{Name: "TestName", Number: 123, Float: 123.123, Boolean: true}
var signature []byte
signature, err = getRequestSignature(method, endpoint, customBodyContents, timestamp, privateKey)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(hex.EncodeToString(signature)))
})
t.Run("invalid body / json", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := testTimestamp
privateKey, err := bitcoin.CreatePrivateKey()
assert.NoError(t, err)
assert.NotNil(t, privateKey)
var signature []byte
signature, err = getRequestSignature(method, endpoint, make(chan int), timestamp, privateKey)
assert.Error(t, err)
assert.Equal(t, 0, len(signature))
})
}
func TestGetRequestSignatureHash(t *testing.T) {
t.Parallel()
t.Run("valid signature hash", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := testTimestamp
hash, err := getRequestSignatureHash(method, endpoint, nil, timestamp)
assert.NoError(t, err)
assert.Equal(t, 32, len(hash))
assert.Equal(t, "aaa21242e579564ec36a3c5108cbd215661fb61cfb8cf17dbf00c074f4561378", hex.EncodeToString(hash))
})
t.Run("missing method", func(t *testing.T) {
method := ""
endpoint := endpointProfileCurrent
timestamp := testTimestamp
hash, err := getRequestSignatureHash(method, endpoint, nil, timestamp)
assert.NoError(t, err)
assert.Equal(t, 32, len(hash))
assert.Equal(t, "1f1917cb12ecd2ef0d245ac193b348f91db912e93d692b504a94ca850ac412f8", hex.EncodeToString(hash))
})
t.Run("missing endpoint", func(t *testing.T) {
method := http.MethodGet
endpoint := ""
timestamp := testTimestamp
hash, err := getRequestSignatureHash(method, endpoint, nil, timestamp)
assert.NoError(t, err)
assert.Equal(t, 32, len(hash))
assert.Equal(t, "b7b2f37bcd5d28ebd048f56160787a7a86b23e6233167e638e288332dedbdb3b", hex.EncodeToString(hash))
})
t.Run("missing timestamp", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := ""
hash, err := getRequestSignatureHash(method, endpoint, nil, timestamp)
assert.NoError(t, err)
assert.Equal(t, 32, len(hash))
assert.Equal(t, "e4740d26cbe8defe7842304461e08d311aaba77fa7f22e80283a3d7c4ced26cf", hex.EncodeToString(hash))
})
t.Run("custom body - basic struct - valid json", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := testTimestamp
var customBodyContents = struct {
Name string `json:"name"`
Number int `json:"number"`
Float float64 `json:"float"`
Boolean bool `json:"boolean"`
}{Name: "TestName", Number: 123, Float: 123.123, Boolean: true}
hash, err := getRequestSignatureHash(method, endpoint, customBodyContents, timestamp)
assert.NoError(t, err)
assert.Equal(t, 32, len(hash))
assert.Equal(t, "73a73a79325098f309881d0103e189b73f1a8a247440c93d36852ca036ab7dc5", hex.EncodeToString(hash))
})
t.Run("custom body - advanced struct - valid json", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := testTimestamp
// todo: nested structs, arrays of structs
var customBodyContents = struct {
Name string `json:"name"`
Number int `json:"number"`
Float float64 `json:"float"`
Boolean bool `json:"boolean"`
Parts []string `json:"parts"`
}{Name: "TestName", Number: 123, Float: 123.123, Boolean: true, Parts: []string{"one", "two"}}
hash, err := getRequestSignatureHash(method, endpoint, customBodyContents, timestamp)
assert.NoError(t, err)
assert.Equal(t, 32, len(hash))
assert.Equal(t, "287e66e5b4ffe062015c078895d70120638080bde96f9a48022add1f4c69f78e", hex.EncodeToString(hash))
})
t.Run("invalid body - produces error", func(t *testing.T) {
method := http.MethodGet
endpoint := endpointProfileCurrent
timestamp := testTimestamp
hash, err := getRequestSignatureHash(method, endpoint, make(chan int), timestamp)
assert.Error(t, err)
assert.Equal(t, 0, len(hash))
})
}