diff --git a/hfc/lib/Chain.js b/hfc/lib/Chain.js index afc6045f9d..a864318ef9 100644 --- a/hfc/lib/Chain.js +++ b/hfc/lib/Chain.js @@ -72,8 +72,6 @@ var Chain = class { throw new Error('Failed to create Chain. Missing requirement "clientContext" parameter.'); } - // naming for the chain will be enforced later by the orderer when - // "initialize()" gets called this._name = name; // Security enabled flag @@ -214,7 +212,6 @@ var Chain = class { return; } } - logger.debug('Did not find a peer to remove with url "%s".', url); } /** @@ -262,7 +259,6 @@ var Chain = class { return; } } - logger.debug('Did not find an orderer to remove with url "%s".', url); } /** @@ -405,12 +401,6 @@ var Chain = class { return Promise.reject(new Error('no user defined')); } - // verify that we have a name configured - if(!this._name) { - logger.error('initializeChain - no chain id defined'); - return Promise.reject(new Error('Chain name is not defined')); - } - // verify that we have a transactionid configured if(!this._initial_transaction_id) { logger.error('initializeChain - no transaction id defined'); diff --git a/hfc/lib/Client.js b/hfc/lib/Client.js index e0a5bb3cd7..895cd7d497 100644 --- a/hfc/lib/Client.js +++ b/hfc/lib/Client.js @@ -148,7 +148,7 @@ var Client = class { var self = this; logger.debug('saveUserToStateStore, userContext: ' + self._userContext); return new Promise(function(resolve, reject) { - if (self._userContext && self._userContext._name) { + if (self._userContext && self._userContext._name && self._stateStore) { logger.debug('saveUserToStateStore, begin promise stateStore.setValue'); self._stateStore.setValue(self._userContext._name, self._userContext.toString()) .then( @@ -177,8 +177,16 @@ var Client = class { } ); } else { - logger.debug('saveUserToStateStore Promise rejected'); - reject(new Error('Cannot save null userContext name to stateStore.')); + if (self._userContext == null) { + logger.debug('saveUserToStateStore Promise rejected, Cannot save user to state store when userContext is null.'); + reject(new Error('Cannot save user to state store when userContext is null.')); + } else if (self._userContext._name == null) { + logger.debug('saveUserToStateStore Promise rejected, Cannot save user to state store when userContext has no name.'); + reject(new Error('Cannot save user to state store when userContext has no name.')); + } else { + logger.debug('saveUserToStateStore Promise rejected, Cannot save user to state store when stateStore is null.'); + reject(new Error('Cannot save user to state store when stateStore is null.')); + } } }); } diff --git a/test/unit/headless-tests.js b/test/unit/headless-tests.js index 9de5395bac..4516dea6d4 100644 --- a/test/unit/headless-tests.js +++ b/test/unit/headless-tests.js @@ -133,7 +133,81 @@ test('\n\n ** index.js **\n\n', function (t) { var Client = hfc; var client = new Client(); var chainKeyValStorePath = 'tmp/chainKeyValStorePath'; + test('\n\n ** lib/Client.js **\n\n', function (t) { + + t.equals(client.getCryptoSuite(), null, 'Client getCryptoSuite should initially be null'); + client.setCryptoSuite(utils.getCryptoSuite()); + if (client.getCryptoSuite() != null) t.pass('Client getCryptoSuite should not be null after setCryptoSuite'); + + client.getUserContext() + .then(function(response){ + if (response === null) + t.pass('Client tests: getUserContext successful null user name.'); + else t.fail('Client tests: getUserContext failed null name check'); + }, function(error){ + t.fail('Client tests: Unexpected error, getUserContext null name check. ' + error); + }); + + client.saveUserToStateStore() + .then(function(response){ + t.fail('Client tests: got response, but should throw "Cannot save user to state store when userContext is null."'); + }, function(error){ + if (error.message === 'Cannot save user to state store when userContext is null.') + t.pass('Client tests: Should throw "Cannot save user to state store when userContext is null."'); + else t.fail('Client tests: Unexpected error message thrown, should throw "Cannot save user to state store when userContext is null." ' + error); + }); + + client.setUserContext(null) + .then(function(response){ + t.fail('Client tests: got response, but should throw "Cannot save null userContext."'); + }, function(error){ + if (error.message === 'Cannot save null userContext.') + t.pass('Client tests: Should throw "Cannot save null userContext."'); + else t.fail('Client tests: Unexpected error message thrown, should throw "Cannot save null userContext." ' + error); + }); + + client.getUserContext('someUser') + .then(function(response){ + if (response == null) + t.pass('Client tests: getUserContext with no context in memory or persisted returns null'); + else t.fail('Client tests: getUserContext with no context in memory or persisted did not return null'); + }, function(error){ + t.fail('Client tests: getUserContext with no context in memory or persisted did not returned error. ' + error); + }); + + client.setUserContext(new User('someUser'), true) + .then(function(response){ + if (response && response.getName() === 'someUser') { + t.pass('Client tests: successfully setUserContext with skipPersistence.'); + debugger;//check out this return + return response; + } + else t.fail('Client tests: failed name check after setUserContext with skipPersistence.'); + }, function(error){ + t.fail('Client tests: Unexpected error, failed setUserContext with skipPersistence. ' + error); + }) + .then(function(response){ + client.getUserContext('someUser') + .then(function(response){ + if (response && response.getName() === 'someUser') + t.pass('Client tests: getUserContext not persisted/skipPersistence was successful.'); + else t.fail('Client tests: getUserContext not persisted/skipPersistence was not successful.'); + }, function(error){ + t.fail('Client tests: Unexpected error, getUserContext not persisted/skipPersistence. ' + error); + }); + }); + + client.setUserContext(new User('someUser')) + .then(function(result){ + t.fail('Client tests: setUserContext without skipPersistence and no stateStore should not return result.'); + }, + function(error){ + if (error.message === 'Cannot save user to state store when stateStore is null.') + t.pass('Client tests: Should throw "Cannot save user to state store when stateStore is null"'); + else t.fail('Client tests: Unexpected error message thrown, should throw "Cannot save user to state store when stateStore is null." ' + error); + }); + var chain = client.newChain('someChain'); t.equals(chain.getName(), 'someChain', 'Checking chain names match'); t.throws( @@ -150,6 +224,13 @@ test('\n\n ** lib/Client.js **\n\n', function (t) { null, 'Client tests: getChain()'); + t.throws( + function () { + client.getChain('someOtherChain'); + }, + /^Error: Chain not found for name someOtherChain./, + 'Client tests: Should throw Error: Chain not found for name someOtherChain.'); + t.throws( function() { client.setStateStore({}); @@ -187,7 +268,7 @@ test('\n\n ** lib/Client.js **\n\n', function (t) { t.fail('Client getStateStore test: Failed to set value, reason: ' + reason); t.end(); } - ); + ); }); }); @@ -624,7 +705,19 @@ test('\n\n ** Chain - method tests **\n\n', function (t) { t.equals(error.toString(),'Error: Initial transaction id is not defined','Chain tests: transaction id is required when initializing'); } } - ); t.end(); + ); + var client3 = new Client(); + var test_chain3 = new Chain('someTestChain3', client3); + test_chain3.addOrderer(new Orderer('grpc://somehost.com:1234')); + test_chain3.initializeChain().then(function(response){ + t.fail('Chain tests: no user defined should, throw error, response '+response); + },function(error){ + if (error && error.message && error.message === 'no user defined') + t.pass('Chain tests: no user defined, should throw error'); + else t.fail('Chain tests: no user defined, should have thrown error "no user defined"'); + }); + + t.end(); }); // User tests /////////