-
Notifications
You must be signed in to change notification settings - Fork 23
/
account.spec.ts
828 lines (722 loc) · 30.7 KB
/
account.spec.ts
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
import { expect } from 'chai';
import * as jsonSqlCreator from 'json-sql';
import * as path from 'path';
import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';
import { SinonStub } from 'sinon';
import { DbStub, LoggerStub, ZSchemaStub } from '../../stubs';
const pgpStub = { QueryFile : () => { return; }} as any;
const ProxyAccount = proxyquire('../../../src/logic/account', {
'pg-promise': pgpStub,
});
const jsonSql = jsonSqlCreator();
jsonSql.setDialect('postgresql');
const table = 'mem_accounts';
// tslint:disable no-unused-expression
/**
* TODO: Better describe test cases
* TODO: Check if more test cases are needed
*/
describe('logic/account', () => {
let account;
let dbStub: DbStub;
let loggerStub: LoggerStub;
let zSchemaStub: ZSchemaStub;
before(() => {
dbStub = new DbStub();
});
beforeEach(() => {
loggerStub = new LoggerStub();
zSchemaStub = new ZSchemaStub();
account = new ProxyAccount.AccountLogic();
// Inject the dependencies
(account as any).db = dbStub;
(account as any).logger = loggerStub;
(account as any).schema = zSchemaStub;
dbStub.reset();
});
describe('constructor', () => {
it('binary should match', () => {
const binary = ['publicKey', 'secondPublicKey'];
expect(account.binary).to.deep.equal(binary);
});
it('fields should match', () => {
const fields = [
{ field: 'username' },
{ field: 'isDelegate' },
{ field: 'u_isDelegate' },
{ field: 'secondSignature' },
{ field: 'u_secondSignature' },
{ field: 'u_username' },
{ expression: 'UPPER("address")', alias: 'address' },
{ expression: 'ENCODE("publicKey", \'hex\')', alias: 'publicKey' },
{
expression: 'ENCODE("secondPublicKey", \'hex\')',
alias : 'secondPublicKey',
},
{ expression: '("balance")::bigint', alias: 'balance' },
{ expression: '("u_balance")::bigint', alias: 'u_balance' },
{ expression: '("vote")::bigint', alias: 'vote' },
{ expression: '("rate")::bigint', alias: 'rate' },
{
expression:
'(SELECT ARRAY_AGG("dependentId") FROM mem_accounts2delegates WHERE "accountId" = a."address")',
alias : 'delegates',
},
{
expression:
'(SELECT ARRAY_AGG("dependentId") FROM mem_accounts2u_delegates WHERE "accountId" = a."address")',
alias : 'u_delegates',
},
{
expression:
'(SELECT ARRAY_AGG("dependentId") FROM mem_accounts2multisignatures WHERE "accountId" = a."address")',
alias : 'multisignatures',
},
{
expression:
'(SELECT ARRAY_AGG("dependentId") FROM mem_accounts2u_multisignatures WHERE "accountId" = a."address")',
alias : 'u_multisignatures',
},
{ field: 'multimin' },
{ field: 'u_multimin' },
{ field: 'multilifetime' },
{ field: 'u_multilifetime' },
{ field: 'blockId' },
{ field: 'nameexist' },
{ field: 'u_nameexist' },
{ field: 'producedblocks' },
{ field: 'missedblocks' },
{ expression: '("fees")::bigint', alias: 'fees' },
{ expression: '("rewards")::bigint', alias: 'rewards' },
{ field: 'virgin' },
];
expect(account.fields).to.deep.equal(fields);
});
it('filter should match', () => {
const filter = {
username : {
type : 'string',
case : 'lower',
maxLength: 20,
minLength: 1,
},
isDelegate : { type: 'boolean' },
u_isDelegate : { type: 'boolean' },
secondSignature : { type: 'boolean' },
u_secondSignature: { type: 'boolean' },
u_username : {
type : 'string',
case : 'lower',
maxLength: 20,
minLength: 1,
},
address : {
required : true,
type : 'string',
case : 'upper',
minLength: 1,
maxLength: 22,
},
publicKey : { type: 'string', format: 'publicKey' },
secondPublicKey : { type: 'string', format: 'publicKey' },
balance : {
required: true,
type : 'integer',
minimum : 0,
maximum : 10999999991000000,
},
u_balance : {
required: true,
type : 'integer',
minimum : 0,
maximum : 10999999991000000,
},
vote : { type: 'integer' },
rate : { type: 'integer' },
delegates : { type: 'array', uniqueItems: true },
u_delegates : { type: 'array', uniqueItems: true },
multisignatures : { type: 'array', uniqueItems: true },
u_multisignatures: { type: 'array', uniqueItems: true },
multimin : { type: 'integer', minimum: 0, maximum: 17 },
u_multimin : { type: 'integer', minimum: 0, maximum: 17 },
multilifetime : { type: 'integer', minimum: 1, maximum: 72 },
u_multilifetime : { type: 'integer', minimum: 1, maximum: 72 },
blockId : { type: 'string', minLength: 1, maxLength: 20 },
nameexist : { type: 'boolean' },
u_nameexist : { type: 'boolean' },
producedblocks : { type: 'integer', minimum: -1, maximum: 1 },
missedblocks : { type: 'integer', minimum: -1, maximum: 1 },
fees : { type: 'integer' },
rewards : { type: 'integer' },
virgin : { type: 'boolean' },
};
expect(account.filter).to.deep.equal(filter);
});
it('conv should match', () => {
const conv = {
username : String,
isDelegate : Boolean,
u_isDelegate : Boolean,
secondSignature : Boolean,
u_secondSignature: Boolean,
u_username : String,
address : String,
publicKey : String,
secondPublicKey : String,
balance : Number,
u_balance : Number,
vote : Number,
rate : Number,
delegates : Array,
u_delegates : Array,
multisignatures : Array,
u_multisignatures: Array,
multimin : Number,
u_multimin : Number,
multilifetime : Number,
u_multilifetime : Number,
blockId : String,
nameexist : Boolean,
u_nameexist : Boolean,
producedblocks : Number,
missedblocks : Number,
fees : Number,
rewards : Number,
virgin : Boolean,
};
expect(account.conv).to.deep.equal(conv);
});
it('editable', () => {
const editable = [
'isDelegate',
'u_isDelegate',
'secondSignature',
'u_secondSignature',
'balance',
'u_balance',
'vote',
'rate',
'delegates',
'u_delegates',
'multisignatures',
'u_multisignatures',
'multimin',
'u_multimin',
'multilifetime',
'u_multilifetime',
'blockId',
'nameexist',
'u_nameexist',
'producedblocks',
'missedblocks',
'fees',
'rewards',
];
expect(account.editable).to.deep.equal(editable);
});
});
describe('account.createTables', () => {
const sqlPath = path.join(process.cwd(), 'sql', 'memoryTables.sql');
beforeEach(() => {
pgpStub.QueryFile = sinon.stub();
});
it('should handle sql error', async () => {
const error = new Error('error');
dbStub.enqueueResponse('query', Promise.reject(error));
await account.createTables().then(() => {
throw new Error('Expected method to reject.');
}).catch((errorMessage) => {
// It should call QueryFile
expect(pgpStub.QueryFile.called).to.be.true;
expect(pgpStub.QueryFile.getCall(0).args.length).to.equal(2);
expect(pgpStub.QueryFile.getCall(0).args[0]).to.equal(sqlPath);
expect(pgpStub.QueryFile.getCall(0).args[1]).to.deep.equal({ minify: true });
// It should call db.query
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(1);
expect(dbStub.stubs.query.getCall(0).args[0]).to.be.instanceof(pgpStub.QueryFile);
// It should log the error
expect(loggerStub.stubs.error.calledOnce).to.be.true;
expect(loggerStub.stubs.error.getCall(0).args.length).to.equal(1);
expect(loggerStub.stubs.error.getCall(0).args[0]).to.equal(
error.stack
);
expect(errorMessage).to.equal('Account#createTables error');
});
});
it('should create tables', async () => {
dbStub.enqueueResponse('query', Promise.resolve('OK'));
await account.createTables();
// It should call QueryFile with the right path
expect(pgpStub.QueryFile.calledOnce).to.be.true;
expect(pgpStub.QueryFile.getCall(0).args.length).to.equal(2);
expect(pgpStub.QueryFile.getCall(0).args[0]).to.equal(sqlPath);
expect(pgpStub.QueryFile.getCall(0).args[1]).to.deep.equal({ minify: true });
// It should call db.query
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(1);
expect(dbStub.stubs.query.getCall(0).args[0]).to.be.instanceof(pgpStub.QueryFile);
});
});
describe('account.removeTables', () => {
const tables = [
table,
'mem_round',
'mem_accounts2delegates',
'mem_accounts2u_delegates',
'mem_accounts2multisignatures',
'mem_accounts2u_multisignatures',
];
const sqles = tables.map((tbl) => {
const sql = jsonSql.build({
type : 'remove',
table: tbl,
});
return sql.query;
});
it('should handle errors', async () => {
const error = new Error('error');
dbStub.enqueueResponse('query', Promise.reject(error));
await account.removeTables().then(() => {
throw new Error('Expected method to reject.');
}).catch((errorMessage) => {
// It should call db.query with the right query
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(1);
expect(dbStub.stubs.query.getCall(0).args[0]).to.deep.equal(sqles.join(''));
// It should log the error
expect(loggerStub.stubs.error.calledOnce).to.be.true;
expect(loggerStub.stubs.error.getCall(0).args.length).to.equal(1);
expect(loggerStub.stubs.error.getCall(0).args[0]).to.equal(
error.stack
);
expect(errorMessage).to.equal('Account#removeTables error');
});
});
it('should remove tables', async () => {
dbStub.enqueueResponse('query', Promise.resolve('OK'));
await account.removeTables();
// It should call db.query with the right query
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(1);
expect(dbStub.stubs.query.getCall(0).args[0]).to.deep.equal(sqles.join(''));
});
});
describe('account.objectNormalize', () => {
const accountData = {};
it('should handle validation error', () => {
const errors = [{ message: 'error 1' }, { message: 'error 2' }];
const errorMessage =
'Failed to validate account schema: ' +
errors
.map((error) => error.message)
.join(', ');
zSchemaStub.enqueueResponse('validate', false);
zSchemaStub.enqueueResponse('getLastErrors', errors);
// it should throw an error as expected
expect(account.objectNormalize.bind(account, accountData)).to.throw(
errorMessage
);
// it should call validate correctly
expect(zSchemaStub.stubs.validate.calledOnce).to.be.true;
expect(zSchemaStub.stubs.validate.getCall(0).args.length).to.equal(2);
expect(zSchemaStub.stubs.validate.getCall(0).args[0]).to.equal(accountData);
expect(zSchemaStub.stubs.validate.getCall(0).args[1]).to.deep.equal({
id : 'Account',
object : true,
properties: account.filter,
});
// it should call getLastErrors
expect(zSchemaStub.stubs.getLastErrors.calledOnce).to.be.true;
expect(zSchemaStub.stubs.getLastErrors.getCall(0).args.length).to.equal(0);
});
it('should normalize object', () => {
zSchemaStub.enqueueResponse('validate', true);
// it should not modify the object
expect(account.objectNormalize(accountData)).to.equal(accountData);
// it should call validate correctly
expect(zSchemaStub.stubs.validate.calledOnce).to.be.true;
expect(zSchemaStub.stubs.validate.getCall(0).args.length).to.equal(2);
expect(zSchemaStub.stubs.validate.getCall(0).args[0]).to.equal(accountData);
expect(zSchemaStub.stubs.validate.getCall(0).args[1]).to.deep.equal({
id : 'Account',
object : true,
properties: account.filter,
});
});
});
describe('account.assertPublicKey', () => {
it('public key is not a string error', () => {
const error = 'Invalid public key, must be a string';
expect(() => account.assertPublicKey(null)).to.throw(error);
});
it('public key is too short error', () => {
const error = 'Invalid public key, must be 64 characters long';
expect(() => account.assertPublicKey('short string')).to.throw(error);
});
it('should call through schema hex validation.', () => {
zSchemaStub.enqueueResponse('validate', false);
const publicKey = '29cca24dae30655882603ba49edba31d956c2e79a062c9bc33bcae26138b39da';
expect(() => account.assertPublicKey(publicKey)).to.throw('Invalid public key, must be a hex string');
expect(zSchemaStub.stubs.validate.calledOnce).is.true;
expect(zSchemaStub.stubs.validate.firstCall.args[0]).to.be.eq(publicKey);
expect(zSchemaStub.stubs.validate.firstCall.args[1]).to.be.deep.eq({ format: 'hex' });
});
});
describe('account.toDB', () => {
it('should convert correctly', () => {
const raw = {
publicKey:
'29cca24dae30655882603ba49edba31d956c2e79a062c9bc33bcae26138b39da',
address : '2841811297332056155r',
};
const result = account.toDB(raw);
expect(result.publicKey).to.deep.equal(new Buffer(raw.publicKey, 'hex'));
expect(result.address).to.equal(raw.address.toUpperCase());
});
});
describe('account.get', () => {
const data = ['some data'];
const filter = {};
beforeEach(() => {
sinon.stub(account, 'getAll');
});
afterEach(() => {
account.getAll.restore();
});
it('without fields; getAll error', async () => {
const error = 'error';
const fields = account.fields.map((field) => field.alias || field.field);
account.getAll.returns(Promise.reject(error));
await account.get(filter).then(() => {
throw new Error('Should reject');
}).catch((err) => {
expect(err).to.equal(error);
expect(account.getAll.calledOnce).to.be.true;
expect(account.getAll.getCall(0).args.length).to.equal(2);
expect(account.getAll.getCall(0).args[0]).to.equal(filter);
expect(account.getAll.getCall(0).args[1]).to.deep.equal(fields);
});
});
it('with fields; getAll error', async () => {
const error = 'error';
const fields = {};
account.getAll.returns(Promise.reject(error));
await account.get(filter, fields).then(() => {
throw new Error('Should reject');
}).catch((err) => {
expect(err).to.equal(error);
expect(account.getAll.calledOnce).to.be.true;
expect(account.getAll.getCall(0).args.length).to.equal(2);
expect(account.getAll.getCall(0).args[0]).to.equal(filter);
expect(account.getAll.getCall(0).args[1]).to.equal(fields);
});
});
it('without fields', async () => {
const fields = account.fields.map((field) => field.alias || field.field);
account.getAll.returns(Promise.resolve(data));
const retVal = await account.get(filter).catch(() => {
throw new Error('Should resolve');
});
expect(account.getAll.calledOnce).to.be.true;
expect(account.getAll.getCall(0).args.length).to.equal(2);
expect(account.getAll.getCall(0).args[0]).to.equal(filter);
expect(account.getAll.getCall(0).args[1]).to.deep.equal(fields);
expect(retVal).to.equal(data[0]);
});
it('with fields', async () => {
const fields = {};
account.getAll.returns(Promise.resolve(data));
const retVal = await account.get(filter, fields).catch(() => {
throw new Error('Should resolve');
});
expect(account.getAll.calledOnce).to.be.true;
expect(account.getAll.getCall(0).args.length).to.equal(2);
expect(account.getAll.getCall(0).args[0]).to.equal(filter);
expect(account.getAll.getCall(0).args[1]).to.equal(fields);
expect(retVal).to.equal(data[0]);
});
});
describe('account.getAll', () => {
let filter: any;
let fields: any[];
let sql: string;
let shortSql: string;
let rows: any[];
beforeEach(() => {
filter = {
limit : 4,
offset : 2,
sort : 'username',
address: '2841811297332056155r',
};
fields = [];
sql = 'select "username", "isDelegate", "u_isDelegate", "secondSignature", "u_secondSignature", ' +
'"u_username", UPPER("address") as "address", ENCODE("publicKey", \'hex\') as "publicKey", ' +
'ENCODE("secondPublicKey", \'hex\') as "secondPublicKey", ("balance")::bigint as "balance", ' +
'("u_balance")::bigint as "u_balance", ("vote")::bigint as "vote", ("rate")::bigint as "rate", ' +
'(SELECT ARRAY_AGG("dependentId") FROM mem_accounts2delegates WHERE "accountId" = a."address") ' +
'as "delegates", (SELECT ARRAY_AGG("dependentId") FROM mem_accounts2u_delegates WHERE "accountId" = ' +
'a."address") as "u_delegates", (SELECT ARRAY_AGG("dependentId") FROM mem_accounts2multisignatures ' +
'WHERE "accountId" = a."address") as "multisignatures", (SELECT ARRAY_AGG("dependentId") FROM ' +
'mem_accounts2u_multisignatures WHERE "accountId" = a."address") as "u_multisignatures", "multimin",' +
' "u_multimin", "multilifetime", "u_multilifetime", "blockId", "nameexist", "u_nameexist", ' +
'"producedblocks", "missedblocks", ("fees")::bigint as "fees", ("rewards")::bigint as "rewards", ' +
'"virgin" from "mem_accounts" as "a" where upper("address") = upper(${p1}) order by "username" limit' +
' 4 offset 2;';
shortSql = 'select * from "mem_accounts" as "a" where upper("address") = upper(${p1}) order by "username" ' +
'limit 4 offset 2;';
rows = [];
});
it('without fields; error', async () => {
const error = new Error('error');
dbStub.enqueueResponse('query', Promise.reject(error));
await account.getAll(filter).then(() => {
throw new Error('Should reject');
}).catch(() => {
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.query.getCall(0).args[0]).to.equal(sql);
expect(dbStub.stubs.query.getCall(0).args[1]).to.deep.equal({
p1: '2841811297332056155r',
});
expect(loggerStub.stubs.error.calledOnce).to.be.true;
expect(loggerStub.stubs.error.getCall(0).args.length).to.equal(1);
expect(loggerStub.stubs.error.getCall(0).args[0]).to.equal(
error.stack
);
});
});
it('without fields; success', async () => {
dbStub.enqueueResponse('query', Promise.resolve(rows));
const retVal = await account.getAll(filter).catch( () => {
throw new Error('Should rejects');
});
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.query.getCall(0).args[0]).to.equal(sql);
expect(dbStub.stubs.query.getCall(0).args[1]).to.deep.equal({
p1: '2841811297332056155r',
});
expect(retVal).to.be.deep.equal(rows);
});
it('with fields; error', async () => {
const error = new Error('error');
dbStub.enqueueResponse('query', Promise.reject(error));
await account.getAll(filter, fields).then(() => {
throw new Error('Should reject');
}).catch(() => {
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.query.getCall(0).args[0]).to.equal(shortSql);
expect(dbStub.stubs.query.getCall(0).args[1]).to.deep.equal({
p1: '2841811297332056155r',
});
expect(loggerStub.stubs.error.calledOnce).to.be.true;
expect(loggerStub.stubs.error.getCall(0).args.length).to.equal(1);
expect(loggerStub.stubs.error.getCall(0).args[0]).to.equal(
error.stack
);
});
});
it('with fields; success', async () => {
dbStub.enqueueResponse('query', Promise.resolve(rows));
const retVal = await account.getAll(filter, fields);
expect(dbStub.stubs.query.calledOnce).to.be.true;
expect(dbStub.stubs.query.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.query.getCall(0).args[0]).to.equal(shortSql);
expect(dbStub.stubs.query.getCall(0).args[1]).to.deep.equal({
p1: '2841811297332056155r',
});
expect(retVal).to.be.deep.eq(rows);
});
});
describe('account.set', () => {
const address = '2841811297332056155r';
const fields = {};
const sql = 'insert into "mem_accounts" ("address") values (${p1}) on conflict ("address") do update set ' +
'"address" = ${p2};';
const values = { p1: '2841811297332056155R', p2: '2841811297332056155R' };
let callback: SinonStub;
beforeEach(() => {
sinon.stub(account, 'assertPublicKey');
callback = sinon.stub();
});
afterEach(() => {
account.assertPublicKey.restore();
});
it('should handle errors', async () => {
const error = new Error('error');
account.assertPublicKey.returns(true);
dbStub.enqueueResponse('none', Promise.reject(error));
await account.set(address, fields)
.then(() => {
throw new Error('should have failed');
}).catch((err) => {
expect(err).to.be.deep.eq('Account#set error');
expect(dbStub.stubs.none.calledOnce).to.be.true;
expect(dbStub.stubs.none.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.none.getCall(0).args[0]).to.equal(sql);
expect(dbStub.stubs.none.getCall(0).args[1]).to.deep.equal(values);
});
});
it('should set data', async () => {
account.assertPublicKey.returns(true);
dbStub.enqueueResponse('none', Promise.resolve());
await account.set(address, fields, callback);
expect(dbStub.stubs.none.calledOnce).to.be.true;
expect(dbStub.stubs.none.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.none.getCall(0).args[0]).to.equal(sql);
expect(dbStub.stubs.none.getCall(0).args[1]).to.deep.equal(values);
expect(callback.calledOnce).to.be.true;
expect(callback.getCall(0).args[1]).to.not.exist;
});
});
describe('account.merge', () => {
let address: string;
let diff: any;
let queries: string;
let callback: SinonStub;
beforeEach(() => {
address = '2841811297332056155r';
diff = {
publicKey :
'29cca24dae30655882603ba49edba31d956c2e79a062c9bc33bcae26138b39da',
blockId : '11273313233467167051',
round : 2707,
balance : 300,
u_balance : -300,
multisignatures: [
{
action : '+',
dependentId: '11995752116878847490R',
},
{
action : '-',
dependentId: '11995752116878847490R',
},
],
delegates : [
[
'+',
'5d3c3c5cdead64d9fe7bc1bf1404ae1378912d77b0243143edf8aff5dda1dbde',
],
[
'-',
'5d3c3c5cdead64d9fe7bc1bf1404ae1378912d77b0243143edf8aff5dda1dbde',
],
],
};
queries = 'delete from "mem_accounts2delegates" where "dependentId" in ' +
'(5d3c3c5cdead64d9fe7bc1bf1404ae1378912d77b0243143edf8aff5dda1dbde) and "accountId" ' +
'= \'2841811297332056155R\';insert into "mem_accounts2delegates" ("accountId", "dependentId") ' +
'values (\'2841811297332056155R\', 5d3c3c5cdead64d9fe7bc1bf1404ae1378912d77b0243143edf8aff5dda1dbde);' +
'delete from "mem_accounts2multisignatures" where "dependentId" = \'11995752116878847490R\';' +
'insert into "mem_accounts2multisignatures" ("dependentId") values (\'11995752116878847490R\');' +
'update "mem_accounts" set "balance" = "balance" + 300, "u_balance" = "u_balance" - 300, "virgin" ' +
'= 0, "blockId" = \'11273313233467167051\' where "address" = \'2841811297332056155R\';' +
'INSERT INTO mem_round ("address", "amount", "delegate", "blockId", "round") SELECT ' +
'\'2841811297332056155R\', (300)::bigint, "dependentId", \'11273313233467167051\', 2707 FROM ' +
'mem_accounts2delegates WHERE "accountId" = \'2841811297332056155R\';INSERT INTO mem_round ' +
'("address", "amount", "delegate", "blockId", "round") SELECT \'2841811297332056155R\', ' +
'(balance)::bigint, array[\'5d3c3c5cdead64d9fe7bc1bf1404ae1378912d77b0243143edf8aff5dda1dbde\'],' +
' \'11273313233467167051\', 2707 FROM mem_accounts WHERE address = \'2841811297332056155R\';INSERT ' +
'INTO mem_round ("address", "amount", "delegate", "blockId", "round") SELECT ' +
'\'2841811297332056155R\', (-balance)::bigint, ' +
'array[\'5d3c3c5cdead64d9fe7bc1bf1404ae1378912d77b0243143edf8aff5dda1dbde\'],' +
' \'11273313233467167051\', 2707 FROM mem_accounts WHERE address = \'2841811297332056155R\';';
sinon.stub(account, 'assertPublicKey');
account.assertPublicKey.returns(true);
callback = sinon.stub();
});
afterEach(() => {
account.assertPublicKey.restore();
});
it('should throw if verify throws error', () => {
account.assertPublicKey.throws('error');
expect(account.merge.bind(account, address, diff, callback)).to.throw();
});
it('should handle no queries passed', async () => {
sinon.stub(account, 'get');
dbStub.enqueueResponse('query', Promise.resolve([]));
await account.merge(address, {}, callback);
expect(dbStub.stubs.none.called).to.be.false;
expect(account.get.calledOnce).to.be.true;
expect(account.get.getCall(0).args.length).to.equal(1);
expect(account.get.getCall(0).args[0]).to.deep.equal({ address: address.toUpperCase() });
account.get.restore();
});
it('should handle no callback passed', async () => {
const testQueries = await account.merge(address, diff);
expect(testQueries).to.equal(queries);
});
it('should call callback correctly on error', async () => {
const error = new Error('error');
dbStub.enqueueResponse('none', Promise.reject(error));
await account.merge(address, diff, callback)
.then(() => {
throw new Error('Should have failed');
})
.catch((err) => {
expect(err).to.be.eq('Account#merge error');
expect(dbStub.stubs.none.calledOnce).to.be.true;
expect(dbStub.stubs.none.getCall(0).args.length).to.equal(1);
expect(dbStub.stubs.none.getCall(0).args[0]).to.equal(queries);
expect(loggerStub.stubs.error.calledOnce).to.be.true;
expect(loggerStub.stubs.error.getCall(0).args.length).to.equal(1);
expect(loggerStub.stubs.error.getCall(0).args[0]).to.equal(
error.stack
);
expect(callback.calledOnce).to.be.true;
expect(callback.getCall(0).args.length).to.equal(1);
expect(callback.getCall(0).args[0]).to.equal('Account#merge error');
});
});
it('Should callback on success', async () => {
dbStub.enqueueResponse('none', Promise.resolve());
dbStub.enqueueResponse('query', Promise.resolve([]));
await account.merge(address, diff, callback);
expect(dbStub.stubs.none.calledOnce).to.be.true;
expect(dbStub.stubs.none.getCall(0).args.length).to.equal(1);
expect(dbStub.stubs.none.getCall(0).args[0]).to.equal(queries);
expect(callback.calledOnce).to.be.true;
expect(callback.getCall(0).args.length).to.equal(2);
expect(callback.getCall(0).args[0]).to.be.null;
expect(callback.getCall(0).args[1]).to.be.undefined;
});
});
describe('account.remove', () => {
const address = '2841811297332056155R';
const queries = 'delete from "mem_accounts" where "address" = ${p1};';
const values = { p1: '2841811297332056155R' };
it('handle promise rejection', async () => {
const error = new Error('error');
dbStub.enqueueResponse('none', Promise.reject(error));
await account.remove(address).then(() => {
throw new Error('Should reject');
}).catch(() => {
expect(dbStub.stubs.none.calledOnce).to.be.true;
expect(dbStub.stubs.none.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.none.getCall(0).args[0]).to.equal(queries);
expect(dbStub.stubs.none.getCall(0).args[1]).to.deep.equal(values);
expect(loggerStub.stubs.error.calledOnce).to.be.true;
expect(loggerStub.stubs.error.getCall(0).args.length).to.equal(1);
expect(loggerStub.stubs.error.getCall(0).args[0]).to.equal(
error.stack
);
});
});
it('handle promise fulfillment', async () => {
dbStub.enqueueResponse('none', Promise.resolve());
const addr = await account.remove(address).catch(() => {
throw new Error('Should resolve');
});
expect(dbStub.stubs.none.calledOnce).to.be.true;
expect(dbStub.stubs.none.getCall(0).args.length).to.equal(2);
expect(dbStub.stubs.none.getCall(0).args[0]).to.equal(queries);
expect(dbStub.stubs.none.getCall(0).args[1]).to.deep.equal(values);
expect(addr).to.equal(address);
});
});
});