forked from decred/politeia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds_test.go
499 lines (464 loc) · 11 KB
/
cmds_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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package pi
import (
"encoding/hex"
"encoding/json"
"errors"
"strconv"
"testing"
"time"
"github.com/decred/politeia/politeiad/api/v1/identity"
backend "github.com/decred/politeia/politeiad/backendv2"
"github.com/decred/politeia/politeiad/plugins/pi"
"github.com/decred/politeia/politeiad/plugins/ticketvote"
)
func TestCmdBillingStatus(t *testing.T) {
// Setup pi plugin
p, cleanup := newTestPiPlugin(t)
defer cleanup()
// Setup an identity that will be used to create the payload
// signatures.
fid, err := identity.New()
if err != nil {
t.Fatal(err)
}
// Setup test data
var (
// Valid input
token = "45154fb45664714b"
status = pi.BillingStatusCompleted
publicKey = fid.Public.String()
msg = token + strconv.Itoa(int(status))
signatureb = fid.SignMessage([]byte(msg))
signature = hex.EncodeToString(signatureb[:])
msgWithClosed = token + strconv.Itoa(int(pi.BillingStatusClosed))
signaturebWithClosed = fid.SignMessage([]byte(msgWithClosed))
signatureWithClosed = hex.EncodeToString(signaturebWithClosed[:])
// signatureIsWrong is a valid hex encoded, ed25519 signature,
// but that does not correspond to the valid input parameters
// listed above.
signatureIsWrong = "b387f678e1236ca1784c4bc77912c754c6b122dd8b" +
"3e499617706dd0bd09167a113e59339d2ce4b3570af37a092ba88f39e7f" +
"c93a5ac7513e52dca3e5e13f705"
)
tokenb, err := hex.DecodeString(token)
if err != nil {
t.Fatal(err)
}
// Setup tests
var tests = []struct {
name string // Test name
token []byte
sbs pi.SetBillingStatus
err error // Expected error output
}{
{
"payload token invalid",
tokenb,
setBillingStatus(t, fid,
pi.SetBillingStatus{
Token: "zzz",
Status: status,
Reason: "",
}),
pluginError(pi.ErrorCodeTokenInvalid),
},
{
"payload token does not match cmd token",
tokenb,
setBillingStatus(t, fid,
pi.SetBillingStatus{
Token: "da70d0766348340c",
Status: status,
Reason: "",
}),
pluginError(pi.ErrorCodeTokenInvalid),
},
{
"invalid billing status",
tokenb,
pi.SetBillingStatus{
Token: token,
Status: pi.BillingStatusT(9),
Reason: "",
PublicKey: publicKey,
Signature: signature,
},
pluginError(pi.ErrorCodeBillingStatusInvalid),
},
{
"signature is not hex",
tokenb,
pi.SetBillingStatus{
Token: token,
Status: status,
Reason: "",
PublicKey: publicKey,
Signature: "zzz",
},
pluginError(pi.ErrorCodeSignatureInvalid),
},
{
"signature is the wrong size",
tokenb,
pi.SetBillingStatus{
Token: token,
Status: status,
Reason: "",
PublicKey: publicKey,
Signature: "123456",
},
pluginError(pi.ErrorCodeSignatureInvalid),
},
{
"signature is wrong",
tokenb,
pi.SetBillingStatus{
Token: token,
Status: status,
Reason: "",
PublicKey: publicKey,
Signature: signatureIsWrong,
},
pluginError(pi.ErrorCodeSignatureInvalid),
},
{
"public key is not a hex",
tokenb,
pi.SetBillingStatus{
Token: token,
Status: status,
Reason: "",
PublicKey: "",
Signature: signature,
},
pluginError(pi.ErrorCodePublicKeyInvalid),
},
{
"public key is the wrong length",
tokenb,
pi.SetBillingStatus{
Token: token,
Status: status,
Reason: "",
PublicKey: "123456",
Signature: signature,
},
pluginError(pi.ErrorCodePublicKeyInvalid),
},
{
"set billing status to close without a reason",
tokenb,
pi.SetBillingStatus{
Token: token,
Status: pi.BillingStatusClosed,
Reason: "",
PublicKey: publicKey,
Signature: signatureWithClosed,
},
pluginError(pi.ErrorCodeBillingStatusChangeNotAllowed),
},
}
// Run tests
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Setup command payload
b, err := json.Marshal(tc.sbs)
if err != nil {
t.Fatal(err)
}
payload := string(b)
// Decode the expected error into a PluginError. If
// an error is being returned it should always be a
// PluginError.
var wantErrorCode pi.ErrorCodeT
if tc.err != nil {
var pe backend.PluginError
if !errors.As(tc.err, &pe) {
t.Fatalf("error is not a plugin error '%v'", tc.err)
}
wantErrorCode = pi.ErrorCodeT(pe.ErrorCode)
}
// Run test
_, err = p.cmdSetBillingStatus(tc.token, payload)
switch {
case tc.err != nil && err == nil:
// Wanted an error but didn't get one
t.Errorf("want error '%v', got nil",
pi.ErrorCodes[wantErrorCode])
return
case tc.err == nil && err != nil:
// Wanted success but got an error
t.Errorf("want error nil, got '%v'", err)
return
case tc.err != nil && err != nil:
// Wanted an error and got an error. Verify that it's
// the correct error. All errors should be backend
// plugin errors.
var gotErr backend.PluginError
if !errors.As(err, &gotErr) {
t.Errorf("want plugin error, got '%v'", err)
return
}
if pi.PluginID != gotErr.PluginID {
t.Errorf("want plugin error with plugin ID '%v', got '%v'",
pi.PluginID, gotErr.PluginID)
return
}
gotErrorCode := pi.ErrorCodeT(gotErr.ErrorCode)
if wantErrorCode != gotErrorCode {
t.Errorf("want error '%v', got '%v'",
pi.ErrorCodes[wantErrorCode],
pi.ErrorCodes[gotErrorCode])
}
// Success; continue to next test
return
case tc.err == nil && err == nil:
// Success; continue to next test
return
}
})
}
}
func TestCmdSummary(t *testing.T) {
// Setup pi plugin
p, cleanup := newTestPiPlugin(t)
defer cleanup()
// Setup test data
var tokens = [6]string{"45154fb45664714b", "45154fb45664714a",
"45154fb45664714c", "45154fb45664714d", "45154fb45664714e",
"45154fb45664714f"}
// List of all final proposal statuses
var statuses = [6]pi.PropStatusT{pi.PropStatusUnvettedAbandoned,
pi.PropStatusUnvettedCensored, pi.PropStatusAbandoned,
pi.PropStatusCensored, pi.PropStatusApproved, pi.PropStatusRejected}
// Setup tests and pre-load final statuses in cache
type test struct {
name string // Test name
token []byte
propStatus pi.PropStatusT // expected proposal status
}
var tests [6]test
for i, token := range tokens {
// Decode string token
b, err := hex.DecodeString(token)
if err != nil {
t.Fatal(err)
}
// Cache final status
p.statuses.set(token, statusEntry{
propStatus: statuses[i],
})
// Add test
tests[i] = test{
name: string(statuses[i]),
token: b,
propStatus: statuses[i],
}
}
// Run tests
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Run test
r, err := p.cmdSummary(tc.token)
if err != nil {
// Unexpected error
t.Fatal(err)
}
// Unmarshal command reply
var sr pi.SummaryReply
err = json.Unmarshal([]byte(r), &sr)
if err != nil {
t.Fatal(err)
}
// Check if received proposal status euqal to the expected.
if sr.Summary.Status != tc.propStatus {
t.Errorf("want proposal status %v, got '%v'", tc.propStatus,
sr.Summary.Status)
}
})
}
}
func TestProposalStatus(t *testing.T) {
// Setup tests
var tests = []struct {
name string // Test name
state backend.StateT
status backend.StatusT
voteStatus ticketvote.VoteStatusT
voteMD *ticketvote.VoteMetadata
bscs []pi.BillingStatusChange
proposalStatus pi.PropStatusT // Expected proposal status
}{
{
"unvetted",
backend.StateUnvetted,
backend.StatusUnreviewed,
ticketvote.VoteStatusInvalid,
nil,
nil,
pi.PropStatusUnvetted,
},
{
"unvetted-censored",
backend.StateUnvetted,
backend.StatusCensored,
ticketvote.VoteStatusInvalid,
nil,
nil,
pi.PropStatusUnvettedCensored,
},
{
"unvetted-abandoned",
backend.StateUnvetted,
backend.StatusArchived,
ticketvote.VoteStatusInvalid,
nil,
nil,
pi.PropStatusUnvettedAbandoned,
},
{
"abandoned",
backend.StateVetted,
backend.StatusArchived,
ticketvote.VoteStatusInvalid,
nil,
nil,
pi.PropStatusAbandoned,
},
{
"censored",
backend.StateVetted,
backend.StatusCensored,
ticketvote.VoteStatusInvalid,
nil,
nil,
pi.PropStatusCensored,
},
{
"under-review",
backend.StateVetted,
backend.StatusPublic,
ticketvote.VoteStatusUnauthorized,
nil,
nil,
pi.PropStatusUnderReview,
},
{
"vote-authorized",
backend.StateVetted,
backend.StatusPublic,
ticketvote.VoteStatusAuthorized,
nil,
nil,
pi.PropStatusVoteAuthorized,
},
{
"vote-started",
backend.StateVetted,
backend.StatusPublic,
ticketvote.VoteStatusStarted,
nil,
nil,
pi.PropStatusVoteStarted,
},
{
"approved",
backend.StateVetted,
backend.StatusPublic,
ticketvote.VoteStatusApproved,
&ticketvote.VoteMetadata{
LinkBy: time.Now().Unix() + 600, // 10m in the future
},
nil,
pi.PropStatusApproved,
},
{
"closed",
backend.StateVetted,
backend.StatusPublic,
ticketvote.VoteStatusApproved,
nil,
[]pi.BillingStatusChange{
{
Status: pi.BillingStatusClosed,
},
},
pi.PropStatusClosed,
},
{
"completed",
backend.StateVetted,
backend.StatusPublic,
ticketvote.VoteStatusApproved,
nil,
[]pi.BillingStatusChange{
{
Status: pi.BillingStatusCompleted,
},
},
pi.PropStatusCompleted,
},
{
"multi_active",
backend.StateVetted,
backend.StatusPublic,
ticketvote.VoteStatusApproved,
nil,
[]pi.BillingStatusChange{
{
Status: pi.BillingStatusCompleted,
},
{
Status: pi.BillingStatusActive,
},
},
pi.PropStatusActive,
},
{
"invalid",
backend.StateUnvetted,
backend.StatusPublic,
ticketvote.VoteStatusApproved,
nil,
nil,
pi.PropStatusInvalid,
},
}
// Run tests
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Run test
status, _ := proposalStatus(tc.state, tc.status,
tc.voteStatus, tc.voteMD, tc.bscs)
// Check if received proposal status euqal to the expected.
if tc.proposalStatus != status {
t.Errorf("want proposal status %v, got '%v'", tc.proposalStatus,
status)
}
})
}
}
// setBillingStatus uses the provided arguments to return a SetBillingStatus
// with a valid PublicKey and Signature.
func setBillingStatus(t *testing.T, fid *identity.FullIdentity, sbs pi.SetBillingStatus) pi.SetBillingStatus {
t.Helper()
msg := sbs.Token + strconv.Itoa(int(sbs.Status)) + sbs.Reason
sig := fid.SignMessage([]byte(msg))
return pi.SetBillingStatus{
Token: sbs.Token,
Status: sbs.Status,
Reason: sbs.Reason,
PublicKey: fid.Public.String(),
Signature: hex.EncodeToString(sig[:]),
}
}
// pluginError returns a backend PluginError for the provided pi ErrorCodeT.
func pluginError(e pi.ErrorCodeT) error {
return backend.PluginError{
PluginID: pi.PluginID,
ErrorCode: uint32(e),
}
}