mocha test passing on console

This commit is contained in:
Matias Alejo Garcia 2014-08-15 09:57:47 -04:00
commit eb9acb958f
7 changed files with 185 additions and 191 deletions

View file

@ -1670,7 +1670,7 @@ Wallet.prototype.indexDiscovery = function(start, change, cosigner, gap, cb) {
Wallet.prototype.disconnect = function() { Wallet.prototype.disconnect = function() {
this.log('## DISCONNECTING'); this.log('## DISCONNECTING');
this.unlock(); this.lock.release();
this.network.disconnect(); this.network.disconnect();
}; };

View file

@ -170,7 +170,7 @@ WalletFactory.prototype._checkNetwork = function(inNetworkName) {
WalletFactory.prototype.open = function(walletId, passphrase) { WalletFactory.prototype.open = function(walletId, passphrase) {
this.storage._setPassphrase(passphrase); this.storage._setPassphrase(passphrase);
var w = this.read(walletId, opts); var w = this.read(walletId);
if (w) if (w)
w.store(); w.store();

View file

@ -141,7 +141,7 @@ Storage.prototype.getWalletIds = function() {
if (split.length == 2) { if (split.length == 2) {
var walletId = split[0]; var walletId = split[0];
if (walletId === 'nameFor' || walletId === 'lock') if (!walletId || walletId === 'nameFor')
continue; continue;
if (typeof uniq[walletId] === 'undefined') { if (typeof uniq[walletId] === 'undefined') {
@ -190,19 +190,6 @@ Storage.prototype.getLastOpened = function() {
return this.getGlobal('lastOpened'); return this.getGlobal('lastOpened');
} }
// Lock related
Storage.prototype.setLock = function(walletId) {
this.setGlobal(this._key(walletId, 'Lock'), this.sessionId());
}
Storage.prototype.getLock = function(walletId) {
return this.getGlobal(this._key(walletId, 'Lock'));
}
Storage.prototype.removeLock = function(walletId) {
this.removeGlobal(this._key(walletId, 'Lock'));
}
//obj contains keys to be set //obj contains keys to be set
Storage.prototype.setFromObj = function(walletId, obj) { Storage.prototype.setFromObj = function(walletId, obj) {
for (var k in obj) { for (var k in obj) {

View file

@ -69,7 +69,11 @@ FakeStorage.prototype.getWalletIds = function() {
var split = ii.split('::'); var split = ii.split('::');
if (split.length == 2) { if (split.length == 2) {
var walletId = split[0]; var walletId = split[0];
if (walletId !== 'nameFor' && typeof uniq[walletId] === 'undefined') {
if (!walletId || walletId === 'nameFor' || walletId ==='lock')
continue;
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId); walletIds.push(walletId);
uniq[walletId] = 1; uniq[walletId] = 1;
} }

View file

@ -1,14 +1,17 @@
'use strict'; 'use strict';
var copay = copay || require('../copay');
var chai = chai || require('chai'); var chai = chai || require('chai');
var should = chai.should(); var should = chai.should();
var LocalEncrypted = copay.StorageLocalEncrypted;
var fakeWallet = 'fake-wallet-id'; var fakeWallet = 'fake-wallet-id';
var timeStamp = Date.now(); var timeStamp = Date.now();
var localMock = require('./mocks/FakeLocalStorage'); var localMock = require('./mocks/FakeLocalStorage');
var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined';
if (is_browser) {
var copay = require('copay'); //browser
} else {
var copay = require('../copay'); //node
}
var LocalEncrypted = copay.StorageLocalEncrypted;
describe('Storage/LocalEncrypted model', function() { describe('Storage/LocalEncrypted model', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
@ -22,24 +25,26 @@ describe('Storage/LocalEncrypted model', function() {
}); });
should.exist(s2); should.exist(s2);
}); });
it('should fail when encrypting without a password', function() { });
it('should fail when encrypting without a password', function() {
var s2 = new LocalEncrypted({ var s2 = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
}); });
(function() { (function() {
s2.set(fakeWallet, timeStamp, 1); s2.set(fakeWallet, timeStamp, 1);
}).should.throw(); }).should.throw();
}); });
it('should be able to encrypt and decrypt', function() { it('should be able to encrypt and decrypt', function() {
s._write(fakeWallet + timeStamp, 'value'); s._write(fakeWallet + timeStamp, 'value');
s._read(fakeWallet + timeStamp).should.equal('value'); s._read(fakeWallet + timeStamp).should.equal('value');
localMock.removeItem(fakeWallet + timeStamp); localMock.removeItem(fakeWallet + timeStamp);
}); });
it('should be able to set a value', function() { it('should be able to set a value', function() {
s.set(fakeWallet, timeStamp, 1); s.set(fakeWallet, timeStamp, 1);
localMock.removeItem(fakeWallet + '::' + timeStamp); localMock.removeItem(fakeWallet + '::' + timeStamp);
}); });
var getSetData = [ var getSetData = [
1, 1000, -15, -1000, 1, 1000, -15, -1000,
0.1, -0.5, -0.5e-10, Math.PI, 0.1, -0.5, -0.5e-10, Math.PI,
'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b',
@ -55,17 +60,17 @@ describe('Storage/LocalEncrypted model', function() {
c: [1, 2, 'hi'] c: [1, 2, 'hi']
}, },
null null
]; ];
getSetData.forEach(function(obj) { getSetData.forEach(function(obj) {
it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { it('should be able to set a value and get it for ' + JSON.stringify(obj), function() {
s.set(fakeWallet, timeStamp, obj); s.set(fakeWallet, timeStamp, obj);
var obj2 = s.get(fakeWallet, timeStamp); var obj2 = s.get(fakeWallet, timeStamp);
JSON.stringify(obj2).should.equal(JSON.stringify(obj)); JSON.stringify(obj2).should.equal(JSON.stringify(obj));
localMock.removeItem(fakeWallet + '::' + timeStamp); localMock.removeItem(fakeWallet + '::' + timeStamp);
}); });
}); });
describe('#export', function() { describe('#export', function() {
it('should export the encrypted wallet', function() { it('should export the encrypted wallet', function() {
var storage = new LocalEncrypted({ var storage = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -80,9 +85,21 @@ describe('Storage/LocalEncrypted model', function() {
localMock.removeItem(fakeWallet + '::' + timeStamp); localMock.removeItem(fakeWallet + '::' + timeStamp);
//encrypted.slice(0,6).should.equal("53616c"); //encrypted.slice(0,6).should.equal("53616c");
}); });
});
describe('#_decryptObj', function() {
it('should decrypt and Obj', function() {
var storage = new LocalEncrypted({
password: 'password',
localStorage: localMock,
}); });
storage._decryptObj('{"a":"2"}').should.deep.equal({
a: "2"
});
});
});
describe('#remove', function() {
describe('#remove', function() {
it('should remove an item', function() { it('should remove an item', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -94,10 +111,10 @@ describe('Storage/LocalEncrypted model', function() {
should.not.exist(s.get('1', 'hola')); should.not.exist(s.get('1', 'hola'));
}); });
}); });
describe('#getWalletIds', function() { describe('#getWalletIds', function() {
it('should get wallet ids', function() { it('should get wallet ids', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -107,9 +124,9 @@ describe('Storage/LocalEncrypted model', function() {
s.set('2', "hola", 'juan'); s.set('2', "hola", 'juan');
s.getWalletIds().should.deep.equal(['1', '2']); s.getWalletIds().should.deep.equal(['1', '2']);
}); });
}); });
describe('#getName #setName', function() { describe('#getName #setName', function() {
it('should get/set names', function() { it('should get/set names', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -118,9 +135,9 @@ describe('Storage/LocalEncrypted model', function() {
s.setName(1, 'hola'); s.setName(1, 'hola');
s.getName(1).should.equal('hola'); s.getName(1).should.equal('hola');
}); });
}); });
describe('#getLastOpened #setLastOpened', function() { describe('#getLastOpened #setLastOpened', function() {
it('should get/set names', function() { it('should get/set names', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -129,22 +146,24 @@ describe('Storage/LocalEncrypted model', function() {
s.setLastOpened('hey'); s.setLastOpened('hey');
s.getLastOpened().should.equal('hey'); s.getLastOpened().should.equal('hey');
}); });
}); });
describe('#WalletLock', function() { if (is_browser) {
it('should get/set/remove opened', function() { describe('#getSessionId', function() {
it('should get SessionId', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
password: 'password' password: 'password'
}); });
s.setLock('walletId'); var sid = s.getSessionId();
s.getLock('walletId').should.equal(true); should.exist(sid);
s.removeLock('walletId'); var sid2 = s.getSessionId();
should.not.exist(s.getLock('walletId')); sid2.should.equal(sid);
}); });
}); });
}
describe('#getWallets', function() { describe('#getWallets', function() {
it('should retreive wallets from storage', function() { it('should retreive wallets from storage', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -162,8 +181,8 @@ describe('Storage/LocalEncrypted model', function() {
name: undefined name: undefined
}); });
}); });
}); });
describe('#deleteWallet', function() { describe('#deleteWallet', function() {
it('should delete a wallet', function() { it('should delete a wallet', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -180,9 +199,9 @@ describe('Storage/LocalEncrypted model', function() {
name: undefined name: undefined
}); });
}); });
}); });
describe('#setFromObj', function() { describe('#setFromObj', function() {
it('set localstorage from an object', function() { it('set localstorage from an object', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -198,10 +217,10 @@ describe('Storage/LocalEncrypted model', function() {
s.get('id1', 'key').should.equal('val'); s.get('id1', 'key').should.equal('val');
}); });
}); });
describe('#globals', function() { describe('#globals', function() {
it('should set, get and remove keys', function() { it('should set, get and remove keys', function() {
var s = new LocalEncrypted({ var s = new LocalEncrypted({
localStorage: localMock, localStorage: localMock,
@ -216,5 +235,4 @@ describe('Storage/LocalEncrypted model', function() {
s.removeGlobal('a'); s.removeGlobal('a');
should.not.exist(s.getGlobal('a')); should.not.exist(s.getGlobal('a'));
}); });
});
}); });

View file

@ -1024,22 +1024,6 @@ describe('Wallet model', function() {
w.network.start.getCall(0).args[0].privkey.length.should.equal(64); w.network.start.getCall(0).args[0].privkey.length.should.equal(64);
}); });
it('should not start if locked', function() {
var w = cachedCreateW2();
w.netStart();
w.emit = sinon.spy();
w.netStart();
w.emit.getCall(0).args[0].should.equal('locked');
});
it('should accept ignoreLocked', function() {
var w = cachedCreateW2();
w.netStart();
w.network.start = sinon.spy();
w.ignoreLock=1;
w.netStart();
w.network.start.getCall(0).args[0].privkey.length.should.equal(64);
});
}); });
describe('#forceNetwork in config', function() { describe('#forceNetwork in config', function() {

View file

@ -298,6 +298,7 @@ describe('WalletFactory model', function() {
var w = wf.create({ var w = wf.create({
name: 'test wallet' name: 'test wallet'
}); });
ws = wf.getWallets(); ws = wf.getWallets();
ws.length.should.equal(1); ws.length.should.equal(1);
ws[0].name.should.equal('test wallet'); ws[0].name.should.equal('test wallet');