/
uxout_test.go
280 lines (252 loc) · 7.33 KB
/
uxout_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
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"errors"
"github.com/stretchr/testify/require"
"github.com/skycoin/skycoin/src/cipher"
"github.com/skycoin/skycoin/src/readable"
"github.com/skycoin/skycoin/src/testutil"
"github.com/skycoin/skycoin/src/visor/historydb"
)
func TestGetUxOutByID(t *testing.T) {
invalidHash := "carccb"
oddHash := "caccb"
validHash := "79216473e8f2c17095c6887cc9edca6c023afedfac2e0c5460e8b6f359684f8b"
type httpBody struct {
uxid string
}
tt := []struct {
name string
method string
status int
err string
httpBody *httpBody
uxid string
getGetUxOutByIDArg cipher.SHA256
getGetUxOutByIDResponse *historydb.UxOut
getGetUxOutByIDError error
httpResponse readable.SpentOutput
csrfDisabled bool
}{
{
name: "405",
method: http.MethodPost,
status: http.StatusMethodNotAllowed,
err: "405 Method Not Allowed",
},
{
name: "400 - empty uxin value",
method: http.MethodGet,
status: http.StatusBadRequest,
err: "400 Bad Request - uxid is empty",
httpBody: &httpBody{
uxid: "",
},
},
{
name: "400 - odd length uxin value",
method: http.MethodGet,
status: http.StatusBadRequest,
err: "400 Bad Request - encoding/hex: odd length hex string",
httpBody: &httpBody{
uxid: oddHash,
},
uxid: oddHash,
},
{
name: "400 - invalid uxin value",
method: http.MethodGet,
status: http.StatusBadRequest,
err: "400 Bad Request - encoding/hex: invalid byte: U+0072 'r'",
httpBody: &httpBody{
uxid: invalidHash,
},
uxid: invalidHash,
},
{
name: "400 - getGetUxOutByIDError",
method: http.MethodGet,
status: http.StatusBadRequest,
err: "400 Bad Request - getGetUxOutByIDError",
httpBody: &httpBody{
uxid: validHash,
},
uxid: validHash,
getGetUxOutByIDArg: testutil.SHA256FromHex(t, validHash),
getGetUxOutByIDError: errors.New("getGetUxOutByIDError"),
},
{
name: "404 - uxout == nil",
method: http.MethodGet,
status: http.StatusNotFound,
err: "404 Not Found",
httpBody: &httpBody{
uxid: validHash,
},
uxid: validHash,
getGetUxOutByIDArg: testutil.SHA256FromHex(t, validHash),
},
{
name: "200",
method: http.MethodGet,
status: http.StatusOK,
err: "404 Not Found",
httpBody: &httpBody{
uxid: validHash,
},
uxid: validHash,
getGetUxOutByIDArg: testutil.SHA256FromHex(t, validHash),
getGetUxOutByIDResponse: &historydb.UxOut{},
httpResponse: readable.NewSpentOutput(&historydb.UxOut{}),
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
gateway := &MockGatewayer{}
endpoint := "/api/v1/uxout"
gateway.On("GetUxOutByID", tc.getGetUxOutByIDArg).Return(tc.getGetUxOutByIDResponse, tc.getGetUxOutByIDError)
v := url.Values{}
if tc.httpBody != nil {
if tc.httpBody.uxid != "" {
v.Add("uxid", tc.httpBody.uxid)
}
}
if len(v) > 0 {
endpoint += "?" + v.Encode()
}
req, err := http.NewRequest(tc.method, endpoint, nil)
require.NoError(t, err)
if tc.csrfDisabled {
setCSRFParameters(t, tokenInvalid, req)
} else {
setCSRFParameters(t, tokenValid, req)
}
rr := httptest.NewRecorder()
cfg := defaultMuxConfig()
cfg.disableCSRF = tc.csrfDisabled
handler := newServerMux(cfg, gateway)
handler.ServeHTTP(rr, req)
status := rr.Code
require.Equal(t, tc.status, status, "got `%v` want `%v`", status, tc.status)
if status != http.StatusOK {
require.Equal(t, tc.err, strings.TrimSpace(rr.Body.String()), "got `%v`| %d, want `%v`",
strings.TrimSpace(rr.Body.String()), status, tc.err)
} else {
var msg readable.SpentOutput
err = json.Unmarshal(rr.Body.Bytes(), &msg)
require.NoError(t, err)
require.Equal(t, tc.httpResponse, msg, tc.name)
}
})
}
}
func TestGetAddrUxOuts(t *testing.T) {
addressForGwError := testutil.MakeAddress()
addressForGwResponse := testutil.MakeAddress()
type httpBody struct {
address string
}
tt := []struct {
name string
method string
status int
err string
httpBody *httpBody
getSpentOutputsForAddressesArg []cipher.Address
getSpentOutputsForAddressesResponse [][]historydb.UxOut
getSpentOutputsForAddressesError error
httpResponse []readable.SpentOutput
csrfDisabled bool
}{
{
name: "405",
method: http.MethodPost,
status: http.StatusMethodNotAllowed,
err: "405 Method Not Allowed",
},
{
name: "400 - address is empty",
method: http.MethodGet,
status: http.StatusBadRequest,
err: "400 Bad Request - address is empty",
httpBody: &httpBody{
address: "",
},
},
{
name: "400 - cipher.DecodeBase58Address error",
method: http.MethodGet,
status: http.StatusBadRequest,
err: "400 Bad Request - Invalid address length",
httpBody: &httpBody{
address: "abcd",
},
},
{
name: "400 - gateway.GetSpentOutputsForAddresses error",
method: http.MethodGet,
status: http.StatusBadRequest,
err: "400 Bad Request - getSpentOutputsForAddressesError",
httpBody: &httpBody{
address: addressForGwError.String(),
},
getSpentOutputsForAddressesArg: []cipher.Address{addressForGwError},
getSpentOutputsForAddressesError: errors.New("getSpentOutputsForAddressesError"),
},
{
name: "200",
method: http.MethodGet,
status: http.StatusOK,
httpBody: &httpBody{
address: addressForGwResponse.String(),
},
getSpentOutputsForAddressesArg: []cipher.Address{addressForGwResponse},
getSpentOutputsForAddressesResponse: [][]historydb.UxOut{{}},
httpResponse: []readable.SpentOutput{},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
endpoint := "/api/v1/address_uxouts"
gateway := &MockGatewayer{}
gateway.On("GetSpentOutputsForAddresses", tc.getSpentOutputsForAddressesArg).Return(tc.getSpentOutputsForAddressesResponse, tc.getSpentOutputsForAddressesError)
v := url.Values{}
if tc.httpBody != nil {
if tc.httpBody.address != "" {
v.Add("address", tc.httpBody.address)
}
}
if len(v) > 0 {
endpoint += "?" + v.Encode()
}
req, err := http.NewRequest(tc.method, endpoint, nil)
require.NoError(t, err)
if tc.csrfDisabled {
setCSRFParameters(t, tokenInvalid, req)
} else {
setCSRFParameters(t, tokenValid, req)
}
rr := httptest.NewRecorder()
cfg := defaultMuxConfig()
cfg.disableCSRF = tc.csrfDisabled
handler := newServerMux(cfg, gateway)
handler.ServeHTTP(rr, req)
status := rr.Code
require.Equal(t, tc.status, status, "got `%v` want `%v`", status, tc.status)
if status != http.StatusOK {
require.Equal(t, tc.err, strings.TrimSpace(rr.Body.String()), "got `%v`| %d, want `%v`",
strings.TrimSpace(rr.Body.String()), status, tc.err)
} else {
var msg []readable.SpentOutput
err = json.Unmarshal(rr.Body.Bytes(), &msg)
require.NoError(t, err)
require.Equal(t, tc.httpResponse, msg, tc.name)
}
})
}
}