Skip to content

Commit

Permalink
Merge branch 'development' into feature/typescript-matteo
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanever committed Apr 22, 2018
2 parents ee01be5 + 1a1f180 commit b1e5373
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
3 changes: 2 additions & 1 deletion tests/unit/apis/utils/attachPeerHeaders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ describe('apis/utils/attachPeerHeaders', () => {
});

describe('use()', () => {
it('success', () => {
it('should call to response.set() and next()', () => {
instance.use(request, response, next);
expect(responseSpy.calledOnce).to.be.true;
expect(responseSpy.args[0][0]).to.equal(undefined);
expect(next.calledOnce).to.be.true;
});
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/apis/utils/forgingApisWatchGuard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('apis/utils/forgingApisWatchGuard', () => {
});

describe('use()', () => {
it('success', () => {
it('should call to next() without parameters if everything is ok', () => {
checkIpInListStub.returns(true);
instance.use(request as any, response, next);
expect(next.calledOnce).to.be.true;
Expand All @@ -61,7 +61,7 @@ describe('apis/utils/forgingApisWatchGuard', () => {
expect(checkIpInListStub.args[0][1]).to.equal(request.ip);
});

it('error', () => {
it('should call to next() with an APIError() if checkIpInList() returns false', () => {
checkIpInListStub.returns(false);
instance.use(request as any, response, next);
expect(next.calledOnce).to.be.true;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/apis/utils/successInterceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('apis/utils/attachPeerHeaders', () => {
});

describe('intercept()', () => {
it('success', () => {
it('should return an object parameter result with {success: true} object', () => {
result = instance.intercept({foo: 'bar'} as any, {myresult: true});
expect(result).to.deep.equal({ success: true, myresult: true });
});
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/apis/utils/validatePeerHeaders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe('apis/utils/validatePeerHeaders', () => {
.bind(Symbols.api.utils.validatePeerHeadersMiddleware)
.to(ValidatePeerHeaders);


// Instance
instance = container.get(Symbols.api.utils.validatePeerHeadersMiddleware);

Expand Down Expand Up @@ -113,7 +112,7 @@ describe('apis/utils/validatePeerHeaders', () => {
});
});

it('success', () => {
it('should call to next() without parameters if everything is ok', () => {
instance.use(request, false, next);
expect(next.calledOnce).to.be.true;
expect(next.args[0][0]).to.equal(undefined);
Expand Down
22 changes: 19 additions & 3 deletions tests/unit/logic/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,7 @@ describe('logic/transaction', () => {
});

it('should verify multisignatures', async () => {
// sender.multisignatures = [];
tx.signatures = ['a', 'b'];
// tx.requesterPublicKey = 'yz';
tx.asset.multisignature = {
keysgroup: [
'xyz',
Expand Down Expand Up @@ -710,14 +708,32 @@ describe('logic/transaction', () => {

it('should throw if failed to verify multisignature', async () => {
tx.signatures = ['a', 'b'];
// First call is simple validation with requester or sender publio key
// First call is simple validation with requester or sender public key
verifySignatureStub.onCall(0).returns(true);
// Second call is inside tx.signatures loop
verifySignatureStub.onCall(1).returns(false);
await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Failed to verify multisignature');
});

it('should throw Failed to verify multisignature if verifySignature() is skipped', async () => {
sender.multisignatures = ['yz'];
tx.signatures = ['a', 'b'];
tx.requesterPublicKey = 'yz';
tx.asset.multisignature = {
keysgroup: [
'xyz',
'def',
],
};
verifySignatureStub.returns(true);
verifySignatureStub.onCall(2).returns(false);

await expect(instance.verify(tx, sender, requester, 1))
.to.be.rejectedWith('Failed to verify multisignature');

});

it('should call verifySignature if tx.signatures not empty', async () => {
tx.signatures = ['a', 'b'];
sender.multisignatures = ['c', 'd'];
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/logic/transactionPool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,14 @@ describe('logic/transactionPool - TransactionPool', () => {
expect(removeUnconfirmedTransactionSpy.callCount).to.be.equal(unconfirmedIds.length);
expect(removeUnconfirmedTransactionSpy.firstCall.args[0]).to.be.equal(unconfirmedIds[0]);
});

it('should return an empty array if transactions are empty', async () => {
instance['unconfirmed'].list.restore();
const unconfirmedStub = sandbox.stub(instance['unconfirmed'], 'list').returns([undefined, undefined]);
const retVal = await instance.undoUnconfirmedList(txModuleStub);
expect(retVal).to.be.equalTo([]);
expect(unconfirmedStub.calledOnce).to.be.true;
});
});

describe('reindexAllQueues', () => {
Expand Down
29 changes: 24 additions & 5 deletions tests/unit/logic/transactions/vote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { SinonSandbox, SinonStub } from 'sinon';
import { TransactionType } from '../../../../src/helpers';
import { VoteTransaction } from '../../../../src/logic/transactions';
import voteSchema from '../../../../src/schema/logic/transactions/vote';
import { AccountLogicStub, DelegatesModuleStub, RoundsLogicStub, SystemModuleStub } from '../../../stubs';
import txSQL from '../../../../src/sql/logic/transactions';
import { AccountLogicStub, DbStub, DelegatesModuleStub, RoundsLogicStub, SystemModuleStub } from '../../../stubs';

// tslint:disable-next-line no-var-requires
const assertArrays = require('chai-arrays');
Expand All @@ -22,6 +23,7 @@ describe('logic/transactions/vote', () => {
let roundsLogicStub: RoundsLogicStub;
let delegatesModuleStub: DelegatesModuleStub;
let systemModuleStub: SystemModuleStub;
let dbStub: DbStub;

let instance: VoteTransaction;
let tx: any;
Expand All @@ -31,24 +33,25 @@ describe('logic/transactions/vote', () => {
beforeEach(() => {
sandbox = sinon.sandbox.create();
zSchemaStub = {
validate : sandbox.stub(),
getLastErrors: () => [],
validate : sandbox.stub(),
};
accountLogicStub = new AccountLogicStub();
roundsLogicStub = new RoundsLogicStub();
delegatesModuleStub = new DelegatesModuleStub();
systemModuleStub = new SystemModuleStub();
dbStub = new DbStub();

tx = {
amount : 0,
asset : {
votes: [
'-7e58fe36588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b381',
'+05a37e6c6588716f9c9a2bac4bac0a1525e9605abac4153016f95a37e6c6588a',
],
},
type : TransactionType.VOTE,
amount : 0,
fee : 10,
type : TransactionType.VOTE,
timestamp : 0,
senderId : '1233456789012345R',
senderPublicKey: '6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3',
Expand All @@ -59,8 +62,8 @@ describe('logic/transactions/vote', () => {
};

sender = {
balance : 10000000,
address : '1233456789012345R',
balance : 10000000,
publicKey: '6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3',
};

Expand Down Expand Up @@ -430,4 +433,20 @@ describe('logic/transactions/vote', () => {
}).to.throw('Invalid vote publicKey');
});
});

describe('restoreAsset()', () => {
it('success', async () => {
// tslint:disable max-line-length
const myVotes = '-7e58fe36588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b381,+05a37e6c6588716f9c9a2bac4bac0a1525e9605abac4153016f95a37e6c6588a';
dbStub.stubs.one.resolves({votes: myVotes});
const dbReadSpy = sandbox.spy(instance, 'dbRead');
const result = await instance.restoreAsset(tx, dbStub as any);
expect(dbStub.stubs.one.calledOnce).to.be.true;
expect(dbStub.stubs.one.args[0][0]).to.be.equal(txSQL.getVotesById);
expect(dbStub.stubs.one.args[0][1]).to.deep.equal({id: tx.id});
expect(dbReadSpy.calledOnce).to.be.true;
expect(dbReadSpy.args[0][0]).to.deep.equal({v_votes: myVotes});
expect(result).to.deep.equal(tx);
});
});
});
19 changes: 19 additions & 0 deletions tests/unit/modules/peers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,24 @@ describe('modules/peers', () => {
await instR.onBlockchainReady();
expect(busStub.stubs.message.called).is.false;
});
it('should not increase updated if peer fails', async()=>{
peersLogicStub.reset();
peersLogicStub.enqueueResponse('create', {pingAndUpdate: pingStub});
peersLogicStub.enqueueResponse('create', {pingAndUpdate: pingStub});
peersLogicStub.enqueueResponse('create', {pingAndUpdate: () => Promise.reject('booo')});
peersLogicStub.enqueueResponse('exists', false);
await instR.onBlockchainReady();
expect(loggerStub.stubs.info.callCount).to.be.equal(2);
expect(loggerStub.stubs.info.args[0][0]).to.contains('Peers->insertSeeds - Peers discovered');
expect(loggerStub.stubs.info.args[1][0]).to.contains('unresponsive');
expect(loggerStub.stubs.info.callCount).to.be.equal(2);
expect(loggerStub.stubs.trace.callCount).to.be.equal(5);
expect(loggerStub.stubs.trace.lastCall.args.length).to.be.equal(2);
expect(loggerStub.stubs.trace.lastCall.args[0]).to.be.equal('Peers->dbLoad Peers discovered');
expect(loggerStub.stubs.trace.lastCall.args[1]).to.be.deep.equal({
total: 1,
updated: 0
});
});
});
});

0 comments on commit b1e5373

Please sign in to comment.