diff --git a/hfc/lib/Member.js b/hfc/lib/Member.js index b46e749ce8..a94a6d0609 100644 --- a/hfc/lib/Member.js +++ b/hfc/lib/Member.js @@ -129,6 +129,30 @@ var Member = class { this._affiliation = affiliation; } + /** + * Get the enrollment object for this User instance + * @returns {Enrollment} the enrollment object + */ + getEnrollment() { + return this._enrollment; + } + + /** + * Set the enrollment object for this User instance + * @param {Enrollment} the enrollment object + */ + setEnrollment(enrollment) { + if (typeof enrollment.privateKey === 'undefined' || enrollment.privateKey === null || enrollment.privateKey === '') { + throw new Error('Invalid enrollment object. Must have a valid private key.'); + } + + if (typeof enrollment.certificate === 'undefined' || enrollment.certificate === null || enrollment.certificate === '') { + throw new Error('Invalid enrollment object. Must have a valid certificate.'); + } + + this._enrollment = enrollment; + } + /** * Get the transaction certificate (tcert) batch size, which is the number of tcerts retrieved * from member services each time (i.e. in a single batch). diff --git a/test/unit/chain-fabriccop-tests.js b/test/unit/chain-fabriccop-tests.js index 580a3120ca..f2eb969fd8 100644 --- a/test/unit/chain-fabriccop-tests.js +++ b/test/unit/chain-fabriccop-tests.js @@ -53,7 +53,7 @@ test('Attempt to use FabricCOPServices',function(t){ t.pass('Successfully enrolled admin with COP server'); var member = new Member('admin', chain); - member._enrollment = admin; + member.setEnrollment(admin); return member.saveState(); }, function(err){ diff --git a/test/unit/headless-tests.js b/test/unit/headless-tests.js index ed46710006..f6aa5f84e6 100644 --- a/test/unit/headless-tests.js +++ b/test/unit/headless-tests.js @@ -507,7 +507,31 @@ test('\n\n ** Member - constructor set get tests **\n\n', function (t) { if (member1.getAffiliation() === affiliation) t.pass('Member constructor get set tests 1: setAffiliation getAffiliation was successful'); else - t.pass('Member constructor get set tests 1: setAffiliation getAffiliation was not successful'); + t.fail('Member constructor get set tests 1: setAffiliation getAffiliation was not successful'); + + t.throws(function() { + member1.setEnrollment( {privateKey: undefined} ); + }, + /Invalid enrollment object. Must have a valid private key/, + 'Test invalid enrollment without private key'); + + t.throws(function() { + member1.setEnrollment( {privateKey: ''} ); + }, + /Invalid enrollment object. Must have a valid private key/, + 'Test invalid enrollment with empty private key'); + + t.throws(function() { + member1.setEnrollment( {privateKey: 'dummy', certificate: undefined} ); + }, + /Invalid enrollment object. Must have a valid certificate/, + 'Test invalid enrollment without certificate'); + + t.throws(function() { + member1.setEnrollment( {privateKey: 'dummy', certificate: ''} ); + }, + /Invalid enrollment object. Must have a valid certificate/, + 'Test invalid enrollment with empty certificate'); var member2 = new Member(memberCfg, _chain); if (member2.getName() === enrollmentID) @@ -525,7 +549,7 @@ test('\n\n ** Member - constructor set get tests **\n\n', function (t) { if (member1.getAffiliation() === affiliation) t.pass('Member constructor get set tests 1: new Member cfg getAffiliation was successful'); else - t.pass('Member constructor get set tests 1: new Member cfg getAffiliation was not successful'); + t.fail('Member constructor get set tests 1: new Member cfg getAffiliation was not successful'); if (member2.getChain().getName() === chainName) t.pass('Member constructor get set tests 2: getChain new Member cfg getName was successful'); diff --git a/test/unit/util.js b/test/unit/util.js index 935f8ba02a..66afc34ea2 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -28,7 +28,7 @@ function getSubmitter(username, password, chain, t) { t.pass('Successfully enrolled user \'' + username + '\''); var member = new Member(username, chain); - member._enrollment = enrollment; + member.setEnrollment(enrollment); return member.saveState(); } ).then(