Skip to content

Commit

Permalink
[FAB-11059] add unit test for policy
Browse files Browse the repository at this point in the history
- Edit Policy.js to fix where needed
- Add Policy.js unit test with 100% coverage
- Remove existing tape unit test

Change-Id: I2ec6c7391110b4c0f2b1ebd7f0361feae09b4b56
Signed-off-by: nkl199@yahoo.co.uk <nkl199@yahoo.co.uk>
  • Loading branch information
nklincoln committed Aug 21, 2018
1 parent f69b76a commit ddcebe8
Show file tree
Hide file tree
Showing 9 changed files with 888 additions and 137 deletions.
12 changes: 6 additions & 6 deletions build/tasks/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ gulp.task('lint', () => {
'fabric-network/**/*.js',
'fabric-client/**/*.js',
'fabric-ca-client/lib/*.js',
'!fabric-client/coverage/**',
'!fabric-ca-client/coverage/**',
'!coverage/**',
'!docs/**',
'!fabric-network/coverage/**',
'!test/typescript/*.js',
'!node_modules/**',
'!fabric-network/node_modules/**',
'!fabric-client/coverage/**',
'!fabric-client/node_modules/**',
'!fabric-ca-client/coverage/**',
'!fabric-ca-client/node_modules/**',
'!docs/**',
'!coverage/**',
'!node_modules/**',
'!test/typescript/*.js',
'!tmp/**',
]).pipe(eslint())
.pipe(eslint.format())
Expand Down
8 changes: 4 additions & 4 deletions fabric-ca-client/lib/FabricCAClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ const FabricCAClient = class {
const payload = responseBody.join('');

if (!payload) {
reject(new Error(
return reject(new Error(
util.format('fabric-ca request %s failed with HTTP status code %s', api_method, JSON.parse(data).statusCode)));
}
//response should be JSON
Expand All @@ -329,7 +329,7 @@ const FabricCAClient = class {
}

} catch (err) {
reject(new Error(
return reject(new Error(
util.format('Could not parse %s response [%s] as JSON due to error [%s]', api_method, payload, err)));
}
});
Expand Down Expand Up @@ -453,7 +453,7 @@ const FabricCAClient = class {
const payload = responseBody.join('');

if (!payload) {
reject(new Error(
return reject(new Error(
util.format('Enrollment failed with HTTP status code', JSON.parse(data).statusCode)));
}
//response should be JSON
Expand All @@ -472,7 +472,7 @@ const FabricCAClient = class {
}

} catch (err) {
reject(new Error(
return reject(new Error(
util.format('Could not parse enrollment response [%s] as JSON due to error [%s]', payload, err)));
}
});
Expand Down
22 changes: 15 additions & 7 deletions fabric-client/lib/CertificateAuthority.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
/*
Copyright 2017, 2018 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
* 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';

var utils = require('./utils.js');
var logger = utils.getLogger('CertificateAuthority.js');
const utils = require('./utils.js');
const logger = utils.getLogger('CertificateAuthority.js');

/**
* The CertificateAuthority class represents a Certificate Authority configuration
Expand All @@ -21,7 +29,7 @@ var logger = utils.getLogger('CertificateAuthority.js');
*
* @class
*/
var CertificateAuthority = class {
const CertificateAuthority = class {

/**
* Construct a CertificateAuthority object
Expand Down
87 changes: 47 additions & 40 deletions fabric-client/lib/Policy.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
/*
Copyright 2017, 2018 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
* 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';

var grpc = require('grpc');
var util = require('util');
const grpc = require('grpc');
const util = require('util');

var _mspPrProto = grpc.load(__dirname + '/protos/msp/msp_principal.proto').common;
var _policiesProto = grpc.load(__dirname + '/protos/common/policies.proto').common;
const _mspPrProto = grpc.load(__dirname + '/protos/msp/msp_principal.proto').common;
const _policiesProto = grpc.load(__dirname + '/protos/common/policies.proto').common;

var IDENTITY_TYPE = {
const IDENTITY_TYPE = {
Role: 'role',
OrganizationUnit: 'organization-unit',
Identity: 'identity'
Expand Down Expand Up @@ -51,7 +58,7 @@ var IDENTITY_TYPE = {
* Governs the constructions of endorsement policies to be passed into the calls to instantiate chaincodes
* @class
*/
var EndorsementPolicy = class {
const EndorsementPolicy = class {
/**
* Constructs an endorsement policy envelope. If the optional "policy" object is not present, a default
* policy of "a signature by any member from any of the organizations corresponding to the array of member
Expand All @@ -65,25 +72,25 @@ var EndorsementPolicy = class {
static buildPolicy(msps, policy) {
const principals = [];
const envelope = new _policiesProto.SignaturePolicyEnvelope();
if (typeof policy === 'undefined' || policy === null) {
if (!policy) {
// no policy was passed in, construct a 'Signed By any member of an organization by mspid' policy
// construct a list of msp principals to select from using the 'n out of' operator
var signedBys = [];
var index = 0;
for (let name in msps) {
const signedBys = [];
let index = 0;
for (const name in msps) {
if (msps.hasOwnProperty(name)) {
let onePrn = new _mspPrProto.MSPPrincipal();
const onePrn = new _mspPrProto.MSPPrincipal();
onePrn.setPrincipalClassification(_mspPrProto.MSPPrincipal.Classification.ROLE);

let memberRole = new _mspPrProto.MSPRole();
const memberRole = new _mspPrProto.MSPRole();
memberRole.setRole(_mspPrProto.MSPRole.MSPRoleType.MEMBER);
memberRole.setMspIdentifier(name);

onePrn.setPrincipal(memberRole.toBuffer());

principals.push(onePrn);

var signedBy = new _policiesProto.SignaturePolicy();
const signedBy = new _policiesProto.SignaturePolicy();
signedBy.set('signed_by', index++);
signedBys.push(signedBy);
}
Expand All @@ -94,11 +101,11 @@ var EndorsementPolicy = class {
}

// construct 'one of any' policy
var oneOfAny = new _policiesProto.SignaturePolicy.NOutOf();
const oneOfAny = new _policiesProto.SignaturePolicy.NOutOf();
oneOfAny.setN(1);
oneOfAny.setRules(signedBys);

var noutof = new _policiesProto.SignaturePolicy();
const noutof = new _policiesProto.SignaturePolicy();
noutof.set('n_out_of', oneOfAny);

envelope.setVersion(0);
Expand All @@ -111,11 +118,11 @@ var EndorsementPolicy = class {
checkPolicy(policy);

policy.identities.forEach((identity) => {
let newPrincipal = buildPrincipal(identity);
const newPrincipal = buildPrincipal(identity);
principals.push(newPrincipal);
});

var thePolicy = new parsePolicy(policy.policy);
const thePolicy = parsePolicy(policy.policy);

envelope.setVersion(0);
envelope.setRule(thePolicy);
Expand Down Expand Up @@ -144,11 +151,11 @@ function buildPrincipal(identity) {
throw new Error(util.format('Invalid role name found: must be one of "peer", "member" or "admin", but found "%s"', roleName));
}

let mspid = identity[principalType].mspId;
const mspid = identity[principalType].mspId;
if (typeof mspid !== 'string' || !mspid) {
throw new Error(util.format('Invalid mspid found: "%j"', mspid));
}
newRole.setMspIdentifier(identity[principalType].mspId);
newRole.setMspIdentifier(mspid);

newPrincipal.setPrincipal(newRole.toBuffer());
} else {
Expand All @@ -159,8 +166,8 @@ function buildPrincipal(identity) {
}

function getIdentityType(obj) {
var invalidTypes = [];
for (let key in obj) {
const invalidTypes = [];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (key === IDENTITY_TYPE.Role || key === IDENTITY_TYPE.OrganizationUnit || key === IDENTITY_TYPE.Identity) {
return key;
Expand All @@ -179,8 +186,8 @@ function getIdentityType(obj) {
}

function getPolicyType(spec) {
var invalidTypes = [];
for (var key in spec) {
const invalidTypes = [];
for (const key in spec) {
if (spec.hasOwnProperty(key)) {
// each policy spec has exactly one property of one of these two forms: 'n-of' or 'signed-by'
if (key === 'signed-by' || key.match(/^\d+-of$/)) {
Expand All @@ -195,27 +202,27 @@ function getPolicyType(spec) {
}

function parsePolicy(spec) {
var type = getPolicyType(spec);
const type = getPolicyType(spec);
if (type === 'signed-by') {
let signedBy = new _policiesProto.SignaturePolicy();
const signedBy = new _policiesProto.SignaturePolicy();
signedBy.set('signed_by', spec[type]);
return signedBy;
} else {
let n = type.match(/^(\d+)-of$/)[1];
let array = spec[type];
const n = type.match(/^(\d+)-of$/)[1];
const array = spec[type];

let nOutOf = new _policiesProto.SignaturePolicy.NOutOf();
const nOutOf = new _policiesProto.SignaturePolicy.NOutOf();
nOutOf.setN(parseInt(n));

let subs = [];
const subs = [];
array.forEach((sub) => {
var subPolicy = parsePolicy(sub);
const subPolicy = parsePolicy(sub);
subs.push(subPolicy);
});

nOutOf.setRules(subs);

let nOf = new _policiesProto.SignaturePolicy();
const nOf = new _policiesProto.SignaturePolicy();
nOf.set('n_out_of', nOutOf);

return nOf;
Expand All @@ -231,8 +238,8 @@ function buildSignaturePolicy(spec) {
} else {
let n = type.match(/^(\d+)-of$/)[1];
n = parseInt(n);
let ruleArray = spec[type];
let rules = [];
const ruleArray = spec[type];
const rules = [];
ruleArray.forEach(rule => {
rules.push(buildSignaturePolicy(rule));
});
Expand All @@ -250,13 +257,13 @@ function checkPolicy(policy){
if (!policy) {
throw new Error('Missing Required Param "policy"');
}
if (typeof policy.identities === 'undefined' || policy.identities === null || policy.identities === '' || policy.identities === {}) {
if (!policy.identities || policy.identities === '' || Object.keys(policy.identities).length === 0 ) {
throw new Error('Invalid policy, missing the "identities" property');
} else if (!Array.isArray(policy.identities)) {
throw new Error('Invalid policy, the "identities" property must be an array');
}

if (typeof policy.policy === 'undefined' || policy.policy === null || policy.policy === '' || policy.policy === {}) {
if (!policy.policy || policy.policy === '' || Object.keys(policy.policy).length === 0) {
throw new Error('Invalid policy, missing the "policy" property');
}
}
Expand Down
Loading

0 comments on commit ddcebe8

Please sign in to comment.