refactor WalletFactory test to use sinon instead of mocks WIP

This commit is contained in:
Matias Alejo Garcia 2014-09-13 10:25:13 -03:00
commit c3574ae57f
9 changed files with 223 additions and 114 deletions

View file

@ -21,8 +21,6 @@ if (localConfig) {
var log = function() {
if (config.verbose) console.log(arguments);
}
var copayApp = window.copayApp = angular.module('copayApp', [
var modules = [
'ngRoute',

View file

@ -12,7 +12,7 @@ function Storage(opts) {
this.__uniqueid = ++id;
if (opts.password)
this._setPassphrase(opts.password);
this.setPassphrase(opts.password);
try {
this.storage = opts.storage || localStorage;
@ -33,7 +33,7 @@ Storage.prototype._getPassphrase = function() {
return pps[this.__uniqueid];
}
Storage.prototype._setPassphrase = function(password) {
Storage.prototype.setPassphrase = function(password) {
pps[this.__uniqueid] = password;
}
@ -231,6 +231,7 @@ Storage.prototype.getWallets = function(cb) {
Storage.prototype.deleteWallet = function(walletId, cb) {
preconditions.checkArgument(walletId);
preconditions.checkArgument(cb);
var err;
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
@ -245,11 +246,15 @@ Storage.prototype.deleteWallet = function(walletId, cb) {
});
var l = toDelete.length,
i = 0;
j = 0;
if (!l)
return cb(new Error('WNOTFOUND: Wallet not found'));
for (var i in toDelete) {
this.removeGlobal(i, function() {
if (++i == l)
return cb();
if (++j == l)
return cb(err);
});
}
};

View file

@ -1,5 +1,6 @@
'use strict';
var preconditions = require('preconditions').singleton();
var log = require('../../log');
function PluginManager(config) {
this.registered = {};
@ -11,7 +12,7 @@ function PluginManager(config) {
if (!config.plugins[pluginName])
continue;
console.log('Loading plugin: ' + pluginName);
log.info('Loading plugin: ' + pluginName);
var pluginClass = require('../plugins/' + pluginName);
var pluginObj = new pluginClass(config[pluginName]);
pluginObj.init();

View file

@ -64,7 +64,7 @@ function Wallet(opts) {
'publicKeyRing', 'txProposals', 'privateKey', 'version',
'reconnectDelay'
].forEach(function(k) {
preconditions.checkArgument(!_.isUndefined(opts[k]), 'missing required option for Wallet: ' + k);
preconditions.checkArgument(!_.isUndefined(opts[k]), 'MISSOPT: missing required option for Wallet: ' + k);
self[k] = opts[k];
});
preconditions.checkArgument(!copayConfig.forceNetwork || this.getNetworkName() === copayConfig.networkName,

View file

@ -120,7 +120,7 @@ WalletFactory.prototype.fromObj = function(obj, skipFields) {
* @return {Wallet}
*/
WalletFactory.prototype.fromEncryptedObj = function(base64, password, skipFields) {
this.storage._setPassphrase(password);
this.storage.setPassphrase(password);
var walletObj = this.storage.import(base64);
if (!walletObj) return false;
var w = this.fromObj(walletObj, skipFields);
@ -148,18 +148,34 @@ WalletFactory.prototype.import = function(base64, password, skipFields) {
* @desc Retrieve a wallet from storage
* @param {string} walletId - the wallet id
* @param {string[]} skipFields - parameters to ignore when importing
* @return {Wallet}
* @param {function} callback - {err, Wallet}
*/
WalletFactory.prototype.read = function(walletId, skipFields, cb) {
var self = this, err;
var self = this,
err;
var obj = {};
obj.id = walletId;
this.storage.getMany(walletId, Wallet.PERSISTED_PROPERTIES, function(ret) {
for (var ii in ret) {
obj[ii] = ret[ii];
}
return cb(err, self.fromObj(obj, skipFields));
if (!_.any(_.values(obj)))
return cb(new Error('Wallet not found'));
var w, err;
obj.id = walletId;
try {
w = self.fromObj(obj, skipFields);
} catch (e) {
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('Could not read: ' + walletId);
} else {
err = e;
}
w = null;
}
return cb(err, w);
});
};
@ -221,7 +237,7 @@ WalletFactory.prototype.create = function(opts, cb) {
});
log.debug('\t### TxProposals Initialized');
this.storage._setPassphrase(opts.passphrase);
this.storage.setPassphrase(opts.passphrase);
opts.storage = this.storage;
opts.network = this.networks[opts.networkName];
@ -272,8 +288,8 @@ WalletFactory.prototype._checkVersion = function(inVersion) {
WalletFactory.prototype.open = function(walletId, passphrase, cb) {
preconditions.checkArgument(cb);
var self = this;
self.storage._setPassphrase(passphrase);
self.read(err, walletId, null, function(w) {
self.storage.setPassphrase(passphrase);
self.read(walletId, null, function(err, w) {
if (err) return cb(err);
w.store(function(err) {
@ -285,11 +301,11 @@ WalletFactory.prototype.open = function(walletId, passphrase, cb) {
};
WalletFactory.prototype.getWallets = function(cb) {
var ret = this.storage.getWallets(function(ret) {
this.storage.getWallets(function(ret) {
ret.forEach(function(i) {
i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id;
});
return cb(ret);
return cb(null, ret);
});
};
@ -303,9 +319,12 @@ WalletFactory.prototype.getWallets = function(cb) {
*/
WalletFactory.prototype.delete = function(walletId, cb) {
var s = this.storage;
s.deleteWallet(walletId);
s.setLastOpened(undefined);
return cb();
s.deleteWallet(walletId, function(err) {
if (err) return cb(err);
s.setLastOpened(null, function(err) {
return cb(err);
});
});
};
/**