Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2682 from LiskHQ/2445-Add_unit_test_coverage_for_…
Browse files Browse the repository at this point in the history
…signatures_module

Add unit tests for signatures module - Closes #2445
  • Loading branch information
MaciejBaj authored Jan 9, 2019
2 parents 58a53a0 + fc9ce23 commit 1f04d9a
Showing 1 changed file with 183 additions and 31 deletions.
214 changes: 183 additions & 31 deletions test/unit/modules/signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,218 @@

'use strict';

const application = require('../../common/application');
const transactionTypes = require('../../../helpers/transaction_types');
const ApiError = require('../../../helpers/api_error');

/* eslint-disable mocha/no-pending-tests */
describe('signatures', () => {
let library;

before(done => {
application.init(
{ sandbox: { name: 'lisk_test_modules_signatures' } },
(err, scope) => {
library = scope;
done(err);
}
);
});

describe('isLoaded', () => {
it('should return true if modules exists');
let revert;

afterEach(() => {
return revert();
});

it('should return true if modules exists', () => {
// Arrange
revert = library.rewiredModules.signatures.__set__('modules', {
accounts: library.modules.accounts,
transactions: library.modules.transactions,
transport: library.modules.transport,
});

it('should return true if modules does not exist');
// Act & assert
return expect(library.modules.signatures.isLoaded()).to.be.true;
});

it('should return true if modules does not exist', () => {
// Arrange
revert = library.rewiredModules.signatures.__set__('modules', undefined);

// Act & assert
return expect(library.modules.signatures.isLoaded()).to.be.false;
});
});

describe('onBind', () => {
let privateModules;

beforeEach(() => {
privateModules = library.rewiredModules.signatures.__get__('modules');
return library.modules.signatures.onBind(library.modules);
});

describe('modules', () => {
it('should assign accounts');
it('should assign accounts', () => {
return expect(privateModules).to.have.property(
'accounts',
library.modules.accounts
);
});

it('should assign transactions');
it('should assign transactions', () => {
return expect(privateModules).to.have.property(
'transactions',
library.modules.transactions
);
});

it('should assign transport');
it('should assign transport', () => {
return expect(privateModules).to.have.property(
'transport',
library.modules.transport
);
});
});

describe('assetTypes', () => {
it('should call bind on signature logic with scope.accounts');
let signatureLogicSpy;

before(done => {
signatureLogicSpy = sinonSandbox.spy(
library.rewiredModules.signatures.__get__('__private').assetTypes[
transactionTypes.SIGNATURE
],
'bind'
);
done();
});

after(() => {
return signatureLogicSpy.restore();
});

it('should call bind on signature logic with scope.accounts', () => {
return expect(signatureLogicSpy).to.be.calledWith(
library.modules.accounts
);
});
});
});

describe('shared', () => {
describe('postSignatures', () => {
describe('when modules not are loaded', () => {
it('should call callback with ApiError');
describe('shared.postSignature', () => {
const req = {
body: {
signature: 'aSignature',
transactionId: 'aTransactionId',
publicKey: 'aPublicKey',
},
};

let postSignatureStub;

beforeEach(done => {
postSignatureStub = sinonSandbox.stub(
library.modules.transport.shared,
'postSignature'
);
library.modules.signatures.shared.postSignature(req.body, null);
done();
});

it(
'should call callback with ApiError containing message = "Blockchain is loading"'
);
afterEach(() => {
return sinonSandbox.restore();
});

it('should call callback with ApiError containing code = 500');
it('should call modules.transport.shared.postSignature with req.body', () => {
return expect(postSignatureStub).to.be.calledWith({
signature: req.body,
});
});

describe('when modules are loaded', () => {
it('should call modules.transport.shared.postSignatures with req.body');

describe('when modules.transport.shared.postSignatures fails with result', () => {
it('should call callback with ApiError');
describe('when modules.transport.shared.postSignature fails with result', () => {
it('should call callback with ApiError', done => {
// Arrange
postSignatureStub.yields(null, { success: false });

it(
'should call callback with ApiError containing message = "Blockchain is loading"'
);
// Act
library.modules.signatures.shared.postSignature(req.body, error => {
// Assert
expect(error).to.be.instanceof(ApiError);
done();
});
});

describe('when result.message = "Invalid signatures body"', () => {
it('should call callback with ApiError containing code = 400');
describe('when result.message = "Invalid signature body"', () => {
it('should call callback with ApiError containing code = 400', done => {
// Force library.modules.transport.shared.postSignature to fail with custom message
// Arrange
postSignatureStub.yields(null, {
success: false,
message: 'Invalid signature body',
});
// Act
library.modules.signatures.shared.postSignature(req.body, error => {
// Assert
expect(error.code).to.equal(400);
done();
});
});
});

describe('when result.message != "Invalid signatures body"', () => {
it('should call callback with ApiError containing code = 500');
describe('when result.message != "Invalid signature body"', () => {
it('should call callback with ApiError containing code = 500', done => {
// Force library.modules.transport.shared.postSignature to fail with custom message
// Arrange
postSignatureStub.yields(null, {
success: false,
message: 'A different message',
});
// Act
library.modules.signatures.shared.postSignature(req.body, error => {
// Assert
expect(error.code).to.equal(500);
done();
});
});
});
});

describe('when modules.transport.shared.postSignatures succeeds with result', () => {
it('should call callback with error = null');
describe('when modules.transport.shared.postSignature succeeds with result', () => {
let postSignatureError;
let postSignatureResult;

it(
'should call callback with result containing status = "Signature Accepted"'
);
beforeEach(done => {
// Force library.modules.transport.shared.postSignature to succeed
postSignatureStub.yields(null, {
success: true,
});

library.modules.signatures.shared.postSignature(
req.body,
(error, result) => {
postSignatureError = error;
postSignatureResult = result;
done();
}
);
});

afterEach(() => {
return postSignatureStub.restore();
});

it('should call callback with error = null', () => {
return expect(postSignatureError).to.be.null;
});

it('should call callback with result containing status = "Signature Accepted"', () => {
return expect(postSignatureResult.status).to.equal(
'Signature Accepted'
);
});
});
});
Expand Down

0 comments on commit 1f04d9a

Please sign in to comment.