Skip to content

Commit

Permalink
[FAB-11059] Add fabric-client mocha tests
Browse files Browse the repository at this point in the history
Changes include:
- new test folder in fabric-client
- single test in fabric-client package
- addition of npm ignores so that tests are not published
- modification of build test so that mocha tests are run from both fabric-ca and fabric-client directories
- removal of tape test the mocha test replaces

Change-Id: I6f8f1fa0ba00d21593d4c31cfdf36d9c56f1ea4a
Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
nklincoln committed Jul 26, 2018
1 parent b7e528d commit a38bbda
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ coverage
docs/gen
node_modules/*
fabric-client/node_modules/*
fabric-client/.nyc_output
fabric-ca-client/node_modules/*
fabric-ca-client/.nyc_output
fabric-ca-client/lib/api.js
Expand Down
22 changes: 18 additions & 4 deletions build/tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,30 @@ gulp.task('test-headless', shell.task(
'./node_modules/nyc/bin/nyc.js gulp run-test-headless'
));

gulp.task('run-test', ['run-full'],
gulp.task('test-mocha', ['mocha-fabric-client'],
() => {
return gulp.src(['./fabric-ca-client/test/**/*.js', './fabric-client/test/**/*.js'], { read: false })
return gulp.src(['./fabric-ca-client/test/**/*.js'], { read: false })
.pipe(mocha({ reporter: 'list', exit: true }));
}
);

gulp.task('run-test-headless', ['run-headless'],
gulp.task('mocha-fabric-client',
() => {
return gulp.src(['./fabric-ca-client/test/**/*.js', './fabric-client/test/**/*.js'], { read: false })
return gulp.src(['./fabric-client/test/**/*.js'], { read: false })
.pipe(mocha({ reporter: 'list', exit: true }));
}
);

gulp.task('run-test', ['run-full', 'mocha-fabric-client'],
() => {
return gulp.src(['./fabric-ca-client/test/**/*.js'], { read: false })
.pipe(mocha({ reporter: 'list', exit: true }));
}
);

gulp.task('run-test-headless', ['run-headless', 'mocha-fabric-client'],
() => {
return gulp.src(['./fabric-ca-client/test/**/*.js'], { read: false })
.pipe(mocha({ reporter: 'list', exit: true }));
}
);
Expand Down
26 changes: 26 additions & 0 deletions fabric-client/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# OS generated files #
.DS_Store

# Node Files #
/node_modules
npm-debug.log

# Coverage #
/coverage

# Test Files #
/test
6 changes: 3 additions & 3 deletions fabric-client/lib/TransactionID.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

'use strict';

const sdkUtils = require('./utils.js');
const logger = sdkUtils.getLogger('TransactionID.js');
const utils = require('./utils.js');
const logger = utils.getLogger('TransactionID.js');
const User = require('./User.js');
const hashPrimitives = require('./hash.js');

Expand Down Expand Up @@ -41,7 +41,7 @@ class TransactionID {
signer = signer_or_userContext;
}

this._nonce = sdkUtils.getNonce(); //nonce is in bytes
this._nonce = utils.getNonce(); //nonce is in bytes
const creator_bytes = signer.serialize();//same as signatureHeader.Creator
const trans_bytes = Buffer.concat([this._nonce, creator_bytes]);
const trans_hash = hashPrimitives.SHA2_256(trans_bytes);
Expand Down
34 changes: 30 additions & 4 deletions fabric-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "1.3.0-snapshot",
"thirdparty": "0.4.10",
"main": "index.js",
"scripts": {
"test": "nyc mocha --recursive -t 10000"
},
"repository": {
"type": "github (read mirror)",
"url": "https://github.com/hyperledger/fabric-sdk-node"
},
"scripts": {
"test": "node test/unit/headless-tests.js"
},
"engines": {
"node": "^8.9.0",
"npm": "^5.5.1"
Expand All @@ -19,6 +19,8 @@
"@types/bytebuffer": "^5.0.34",
"bn.js": "^4.11.3",
"callsite": "^1.0.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"elliptic": "^6.2.3",
"fs": "0.0.2",
"fs-extra": "^6.0.1",
Expand All @@ -31,12 +33,16 @@
"jssha": "^2.1.0",
"klaw": "^2.0.0",
"long": "^4.0.0",
"mocha": "^5.2.0",
"nano": "^6.4.4",
"nconf": "^0.10.0",
"nyc": "^12.0.2",
"path": "^0.12.7",
"pkcs11js": "^1.0.6",
"promise-settle": "^0.3.0",
"restify": "7.2.0",
"rewire": "^4.0.1",
"sinon": "^6.1.3",
"sjcl": "1.0.7",
"stream-buffers": "3.0.1",
"tar-stream": "1.6.1",
Expand All @@ -50,5 +56,25 @@
"type": "Apache-2.0",
"url": "https://github.com/hyperledger/fabric/blob/master/LICENSE"
}
]
],
"nyc": {
"exclude": [
"index.js",
"coverage/**",
"lib/impl",
"lib/msp",
"lib/protos",
"test/**"
],
"reporter": [
"text-summary",
"html"
],
"all": true,
"check-coverage": true,
"statements": 3,
"branches": 1,
"functions": 5,
"lines": 3.5
}
}
191 changes: 191 additions & 0 deletions fabric-client/test/TransactionID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const rewire = require('rewire');
const TransactionID = require('../lib/TransactionID');
const TransactionIDRewire = rewire('../lib/TransactionID');

const Identity = require('../lib/msp/identity').Identity;
const User = require('../lib/User');
const Utils = require('../lib/utils');

require('chai').should();
const sinon = require('sinon');

describe.only('TransactionID', () => {

let revert;
let bufferSpy;
let utilsMock;
const nonceBuffer = Buffer.from('myNonce');

beforeEach(() => {
bufferSpy = sinon.spy(Buffer, 'concat');
utilsMock = sinon.stub(Utils, 'getNonce').returns(nonceBuffer);
revert = [];
});

afterEach(() => {
bufferSpy.restore();
utilsMock.restore();
if (revert.length) {
revert.forEach(Function.prototype.call, Function.prototype.call);
}
});

describe('#constructor', () => {

it('should throw if missing Identity parameter', async () => {
(() => {
new TransactionID();
}).should.throw(/Missing userContext or signing identity parameter/);
});

it('should set signer to be SigningIdentity if passed Identity parameter is of class User', () => {

const signingIdentity = sinon.createStubInstance(Identity);
const bufferResponse = Buffer.from('serializeMyID');
signingIdentity.serialize = sinon.stub().returns(bufferResponse);

const signer = new User('bob');
signer._signingIdentity = signingIdentity;

new TransactionID(signer);

sinon.assert.calledOnce(bufferSpy);
const args = bufferSpy.getCall(0).args;
args[0][1].should.equal(bufferResponse);

});

it('should set signer to be passed Identity parameter is not of class User', () => {
const signer = sinon.createStubInstance(User);

signer.getSigningIdentity = sinon.stub().returns('MyID');
const bufferResponse = Buffer.from('serializeMyNewID');
signer.serialize = sinon.stub().returns(bufferResponse);

new TransactionID(signer);

sinon.assert.notCalled(signer.getSigningIdentity);
sinon.assert.calledOnce(bufferSpy);
const args = bufferSpy.getCall(0).args;
args[0][1].should.equal(bufferResponse);

});

it('should set nonce from the sdkutils', () => {
const signer = sinon.createStubInstance(User);
revert.push(TransactionIDRewire.__set__('utils', utilsMock));

signer.getSigningIdentity = sinon.stub().returns('MyID');
const bufferResponse = Buffer.from('serializeMyNewID');
signer.serialize = sinon.stub().returns(bufferResponse);

new TransactionID(signer);
sinon.assert.calledOnce(bufferSpy);
const args = bufferSpy.getCall(0).args;
args[0][0].should.equal(nonceBuffer);
});

it('should set admin to be true if the passed boolean value is true', () => {
const signingIdentity = sinon.createStubInstance(Identity);
const bufferResponse = Buffer.from('serializeMyID');
signingIdentity.serialize = sinon.stub().returns(bufferResponse);

const signer = new User('bob');
signer._signingIdentity = signingIdentity;

const myTransID = new TransactionID(signer, true);
myTransID._admin.should.be.equal(true);
});

it('should set admin to be false if the passed boolean value is false', () => {
const signingIdentity = sinon.createStubInstance(Identity);
const bufferResponse = Buffer.from('serializeMyID');
signingIdentity.serialize = sinon.stub().returns(bufferResponse);

const signer = new User('bob');
signer._signingIdentity = signingIdentity;

const myTransID = new TransactionID(signer, false);
myTransID._admin.should.be.equal(false);

});
});

describe('#getTransactionID', () => {

it('should return transactionId', () => {
const signer = sinon.createStubInstance(User);
revert.push(TransactionIDRewire.__set__('utils', utilsMock));

signer.getSigningIdentity = sinon.stub().returns('MyID');
const bufferResponse = Buffer.from('serializeMyNewID');
signer.serialize = sinon.stub().returns(bufferResponse);

const myTransID = new TransactionID(signer);

myTransID.getTransactionID().should.be.equal('bb20c43d9dd1e4b5b62bf5be5cc23debf198c86a19c6adcb6c6c912337a35e39');
});
});


describe('#getNonce', () => {

it('should return _nonce', () => {
const signer = sinon.createStubInstance(User);

revert.push(TransactionIDRewire.__set__('utils', utilsMock));

signer.getSigningIdentity = sinon.stub().returns('MyID');
const bufferResponse = Buffer.from('serializeMyNewID');
signer.serialize = sinon.stub().returns(bufferResponse);

const myTransID = new TransactionID(signer);

myTransID.getNonce().should.be.equal(nonceBuffer);
});
});

describe('#isAdmin', () => {

it('should return boolean true if admin', () => {
const signingIdentity = sinon.createStubInstance(Identity);
const bufferResponse = Buffer.from('serializeMyID');
signingIdentity.serialize = sinon.stub().returns(bufferResponse);

const signer = new User('bob');
signer._signingIdentity = signingIdentity;

const myTransID = new TransactionID(signer, true);
myTransID.isAdmin().should.be.equal(true);
});

it('should return boolean false if not admin', () => {
const signingIdentity = sinon.createStubInstance(Identity);
const bufferResponse = Buffer.from('serializeMyID');
signingIdentity.serialize = sinon.stub().returns(bufferResponse);

const signer = new User('bob');
signer._signingIdentity = signingIdentity;

const myTransID = new TransactionID(signer, false);
myTransID.isAdmin().should.be.equal(false);
});
});

});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"gulp-add-src": "^1.0.0",
"gulp-debug": "^4.0.0",
"gulp-eslint": "^3.0.1",
"gulp-mocha": "^6.0.0",
"gulp-jsdoc3": "^2.0.0",
"gulp-mocha": "^6.0.0",
"gulp-shell": "^0.6.3",
"gulp-tape": "0.0.9",
"gulp-watch": "^5.0.0",
Expand Down
Loading

0 comments on commit a38bbda

Please sign in to comment.