Skip to content

Commit

Permalink
FAB-1235 add setEnrollment() to Member
Browse files Browse the repository at this point in the history
this is needed so that applications can use the Enrollment
object returned by the new COP client to construct a new
User instance and persist in KVS, now that the COP client
has been separated from Chain and Member (gerrit 2725)

Change-Id: I8c6daf92a368c325990d5f94935cb151975c8f71
Signed-off-by: Jim Zhang <jzhang@us.ibm.com>
  • Loading branch information
jimthematrix committed Nov 30, 2016
1 parent 1f08e84 commit 2a6987f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
24 changes: 24 additions & 0 deletions hfc/lib/Member.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion test/unit/chain-fabriccop-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
28 changes: 26 additions & 2 deletions test/unit/headless-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/unit/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2a6987f

Please sign in to comment.