Merge pull request #1166 from maraoz/add/mocha-to-karma

Add browser mocha tests to karma
This commit is contained in:
Matias Alejo Garcia 2014-08-26 17:00:45 -03:00
commit 100f501db1
11 changed files with 847 additions and 1235 deletions

View File

@ -167,6 +167,7 @@ Network.prototype.iterateNonce = function() {
var noncep2uint = this.networkNonce.slice(4, 8).readUInt32BE(0);
var noncep2 = this.networkNonce.slice(4, 8);
noncep2.writeUInt32BE(noncep2uint + 1, 0);
this.networkNonce = Buffer.concat([noncep1, noncep2], 8);
return this.networkNonce;
};

View File

@ -1,149 +0,0 @@
'use strict';
var fs = require('fs');
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var passwords = [];
function Storage(opts) {
opts = opts || {};
this.data = {};
passwords[0] = opts.password;
}
Storage.prototype._encrypt = function(string) {
var encrypted = CryptoJS.AES.encrypt(string, passwords[0]);
var encryptedBase64 = encrypted.toString();
return encryptedBase64;
};
Storage.prototype._encryptObj = function(obj) {
var string = JSON.stringify(obj);
return this._encrypt(string);
};
Storage.prototype._decrypt = function(base64) {
var decrypted = CryptoJS.AES.decrypt(base64, passwords[0]);
var decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
return decryptedStr;
};
Storage.prototype._decryptObj = function(base64) {
var decryptedStr = this._decrypt(base64);
return JSON.parse(decryptedStr);
};
Storage.prototype.load = function(walletId, callback) {
var self = this;
fs.readFile(walletId, function(err, base64) {
if (typeof base64 !== 'string')
base64 = base64.toString();
var data = self._decryptObj(base64);
if (err) return callback(err);
try {
this.data[walletId] = JSON.parse(data);
} catch (err) {
if (callback)
return callback(err);
}
if (callback)
return callback(null);
});
};
Storage.prototype.save = function(walletId, callback) {
var obj = this.data[walletId];
var encryptedBase64 = this._encryptObj(obj);
//TODO: update to use a queue to ensure that saves are made sequentially
fs.writeFile(walletId, encryptedBase64, function(err) {
if (callback)
return callback(err);
});
};
Storage.prototype._read = function(k) {
var split = k.split('::');
var walletId = split[0];
var key = split[1];
return this.data[walletId][key];
};
Storage.prototype._write = function(k, v, callback) {
var split = k.split('::');
var walletId = split[0];
var key = split[1];
if (!this.data[walletId])
this.data[walletId] = {};
this.data[walletId][key] = v;
this.save(walletId, callback);
};
// get value by key
Storage.prototype.getGlobal = function(k) {
return this._read(k);
};
// set value for key
Storage.prototype.setGlobal = function(k, v, callback) {
this._write(k, v, callback);
};
// remove value for key
Storage.prototype.removeGlobal = function(k, callback) {
var split = k.split('::');
var walletId = split[0];
var key = split[1];
delete this.data[walletId][key];
this.save(walletId, callback);
};
Storage.prototype._key = function(walletId, k) {
return walletId + '::' + k;
};
// get value by key
Storage.prototype.get = function(walletId, k) {
return this.getGlobal(this._key(walletId, k));
};
// set value for key
Storage.prototype.set = function(walletId, k, v, callback) {
this.setGlobal(this._key(walletId, k), v, callback);
};
// remove value for key
Storage.prototype.remove = function(walletId, k, callback) {
this.removeGlobal(this._key(walletId, k), callback);
};
Storage.prototype.getWalletIds = function() {
return [];
};
Storage.prototype.setFromObj = function(walletId, obj, callback) {
this.data[walletId] = obj;
this.save(walletId, callback);
};
Storage.prototype.setFromEncryptedObj = function(walletId, base64, callback) {
var obj = this._decryptObj(base64);
this.setFromObj(walletId, obj, callback);
};
Storage.prototype.getEncryptedObj = function(walletId) {
var encryptedBase64 = this._encryptObj(this.data[walletId]);
return encryptedBase64;
};
// remove all values
Storage.prototype.clearAll = function(callback) {
this.data = {};
this.save(callback);
};
module.exports = Storage;

View File

@ -59,7 +59,10 @@ module.exports = function(config) {
'test/mocha.conf.js',
//test files
'test/unit/**/*.js'
'test/test.*.js',
'test/unit/**/*.js',
],
@ -99,7 +102,7 @@ module.exports = function(config) {
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
browsers: ['Chrome', 'Firefox'],
// Continuous Integration mode

View File

@ -24,7 +24,7 @@
"setup-shell": "node shell/scripts/download-atom-shell.js",
"start": "node server.js",
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --reporter spec test",
"test": "node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && ./node_modules/karma/bin/karma start --browsers Firefox --single-run",
"test": "sh test/run.sh",
"dist": "node shell/scripts/dist.js",
"sign": "gpg -u 1112CFA1 --output browser-extensions/firefox/copay.xpi.sig --detach-sig browser-extensions/firefox/copay.xpi; gpg -u 1112CFA1 --output browser-extensions/chrome/copay-chrome-extension.zip.sig --detach-sig browser-extensions/chrome/copay-chrome-extension.zip",
"verify": "gpg --verify browser-extensions/firefox/copay.xpi.sig browser-extensions/firefox/copay.xpi; gpg --verify browser-extensions/chrome/copay-chrome-extension.zip.sig browser-extensions/chrome/copay-chrome-extension.zip"

View File

@ -1,39 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Mocha</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../lib/crypto-js/rollups/aes.js"></script>
<script>mocha.setup('bdd')</script>
<script src="../lib/bitcore.js"></script>
<script src="../js/copayBundle.js"></script>
<script src="test.blockchain.Insight.js"></script>
<script src="test.network.Async.js"></script>
<script src="test.PayPro.js"></script>
<script src="test.PrivateKey.js"></script>
<script src="test.PublicKeyRing.js"></script>
<script src="test.LocalEncrypted.js"></script>
<script src="test.TxProposals.js"></script>
<script src="test.Wallet.js"></script>
<script src="test.WalletFactory.js"></script>
<script src="test.performance.js"></script>
<script src="test.HDParams.js"></script>
<script src="test.HDPath.js"></script>
<script src="test.Passphrase.js"></script>
<script src="test.TxProposal.js"></script>
<!--
Do not try to run test.storage.File.js in browser.
It is only applicable to the node environment.
-->
<script>
mocha.run();
</script>
</body>
</html>

5
test/run.sh Normal file
View File

@ -0,0 +1,5 @@
#! /bin/bash
node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && \
cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && \
./node_modules/karma/bin/karma start --browsers Firefox --single-run

File diff suppressed because it is too large Load Diff

View File

@ -18,21 +18,21 @@ try {
}
var PrivateKey = copay.PrivateKey || require('../js/models/core/PrivateKey');
var config = {
var pkConfig = {
networkName: 'livenet',
};
describe('PrivateKey model', function() {
it('should create an instance', function() {
var w = new PrivateKey(config);
var w = new PrivateKey(pkConfig);
should.exist(w);
should.exist(w.bip);
should.exist(w.bip.derive);
});
it('should derive priv keys', function() {
var pk = new PrivateKey(config);
var pk = new PrivateKey(pkConfig);
for (var j = false; !j; j=true) {
for (var i = 0; i < 3; i++) {
var wk = pk.get(i, j);
@ -51,7 +51,7 @@ describe('PrivateKey model', function() {
}
});
it('should derive priv keys array', function() {
var w = new PrivateKey(config);
var w = new PrivateKey(pkConfig);
var wks = w.getAll(2, 3);
wks.length.should.equal(5);
for (var j = 0; j < wks.length; j++) {
@ -71,7 +71,7 @@ describe('PrivateKey model', function() {
});
it('fromObj toObj roundtrip', function() {
var w1 = new PrivateKey(config);
var w1 = new PrivateKey(pkConfig);
var o = JSON.parse(JSON.stringify(w1.toObj()))
var w2 = PrivateKey.fromObj(o);
@ -86,7 +86,7 @@ describe('PrivateKey model', function() {
describe('#getId', function() {
it('should calculate the copayerId', function() {
var w1 = new PrivateKey(config);
var w1 = new PrivateKey(pkConfig);
should.exist(w1.getId());
w1.getId().length.should.equal(33 * 2);
});
@ -94,7 +94,7 @@ describe('PrivateKey model', function() {
describe('#getIdPriv', function() {
it('should calculate .id', function() {
var w1 = new PrivateKey(config);
var w1 = new PrivateKey(pkConfig);
should.exist(w1.getIdPriv());
w1.getIdPriv().length.should.equal(32 * 2);
});
@ -102,7 +102,7 @@ describe('PrivateKey model', function() {
describe('#cacheId', function() {
it('should set .id and .idpriv', function() {
var w1 = new PrivateKey(config);
var w1 = new PrivateKey(pkConfig);
w1.cacheId();
var pub = w1.id;
var priv = w1.idpriv;
@ -111,7 +111,7 @@ describe('PrivateKey model', function() {
});
it('should set the id equal to the public key of the idpriv private key', function() {
var w1 = new PrivateKey(config);
var w1 = new PrivateKey(pkConfig);
w1.cacheId();
var pub = w1.id;
var priv = w1.idpriv;

View File

@ -20,7 +20,7 @@ var TransactionBuilder = bitcore.TransactionBuilder;
var Transaction = bitcore.Transaction;
var Address = bitcore.Address;
var config = {
var walletConfig = {
requiredCopayers: 3,
totalCopayers: 5,
spendUnconfirmed: true,
@ -30,7 +30,7 @@ var config = {
var getNewEpk = function() {
return new PrivateKey({
networkName: config.networkName,
networkName: walletConfig.networkName,
})
.deriveBIP45Branch()
.extendedPublicKeyString();
@ -46,7 +46,7 @@ describe('Wallet model', function() {
it('should fail to create an instance', function() {
(function() {
new Wallet(config)
new Wallet(walletConfig)
}).should.
throw();
});
@ -58,11 +58,11 @@ describe('Wallet model', function() {
var createW = function(N, conf) {
var c = JSON.parse(JSON.stringify(conf || config));
var c = JSON.parse(JSON.stringify(conf || walletConfig));
if (!N) N = c.totalCopayers;
var mainPrivateKey = new copay.PrivateKey({
networkName: config.networkName
networkName: walletConfig.networkName
});
var mainCopayerEPK = mainPrivateKey.deriveBIP45Branch().extendedPublicKeyString();
c.privateKey = mainPrivateKey;
@ -78,9 +78,9 @@ describe('Wallet model', function() {
networkName: c.networkName,
});
var storage = new Storage(config.storage);
var network = new Network(config.network);
var blockchain = new Blockchain(config.blockchain);
var storage = new Storage(walletConfig.storage);
var network = new Network(walletConfig.network);
var blockchain = new Blockchain(walletConfig.blockchain);
c.storage = storage;
c.network = network;
c.blockchain = blockchain;
@ -100,8 +100,8 @@ describe('Wallet model', function() {
}
};
c.networkName = config.networkName;
c.verbose = config.verbose;
c.networkName = walletConfig.networkName;
c.verbose = walletConfig.verbose;
c.version = '0.0.1';
return new Wallet(c);
@ -322,9 +322,9 @@ describe('Wallet model', function() {
o.opts.reconnectDelay = 100;
var w2 = Wallet.fromObj(o,
new Storage(config.storage),
new Network(config.network),
new Blockchain(config.blockchain));
new Storage(walletConfig.storage),
new Network(walletConfig.network),
new Blockchain(walletConfig.blockchain));
should.exist(w2);
w2.publicKeyRing.requiredCopayers.should.equal(w.publicKeyRing.requiredCopayers);
should.exist(w2.publicKeyRing.getCopayerId);
@ -580,7 +580,7 @@ describe('Wallet model', function() {
});
it('#getUnspent should honor spendUnconfirmed = false', function(done) {
var conf = JSON.parse(JSON.stringify(config));
var conf = JSON.parse(JSON.stringify(walletConfig));
conf.spendUnconfirmed = false;
var w = createW2(null, null, conf);
w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
@ -592,7 +592,7 @@ describe('Wallet model', function() {
});
it('#getUnspent and spendUnconfirmed should count transactions with 1 confirmations', function(done) {
var conf = JSON.parse(JSON.stringify(config));
var conf = JSON.parse(JSON.stringify(walletConfig));
conf.spendUnconfirmed = false;
var w = cachedCreateW2(null, null, conf);
w.blockchain.getUnspent = w.blockchain.getUnspent2;
@ -684,7 +684,7 @@ describe('Wallet model', function() {
it('should create & sign transaction from received funds', function(done) {
var k2 = new PrivateKey({
networkName: config.networkName
networkName: walletConfig.networkName
});
var w = createW2([k2]);
@ -1093,7 +1093,7 @@ describe('Wallet model', function() {
it('should throw if network is different', function() {
var backup = copayConfig.forceNetwork;
copayConfig.forceNetwork = true;
config.networkName = 'livenet';
walletConfig.networkName = 'livenet';
createW2.should.throw(Error);
copayConfig.forceNetwork = backup;
});

View File

@ -1,204 +0,0 @@
'use strict';
var chai = chai || require('chai');
var should = chai.should();
var Storage = require('../js/models/storage/File');
var sinon = require('sinon');
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var mock = require('mock-fs');
describe('Storage/File', function() {
it('should exist', function() {
should.exist(Storage);
});
var mockFS = function() {
var obj = {
"test": "test"
};
var encryptedStr = CryptoJS.AES.encrypt(JSON.stringify(obj), 'password').toString();
mock({
'myfilename': encryptedStr
});
};
describe('#load', function(done) {
it('should call fs.readFile', function(done) {
mockFS();
var storage = new Storage({
password: 'password'
});
storage.load('myfilename', function(err) {
mock.restore();
done();
});
});
});
describe('#save', function(done) {
it('should call fs.writeFile', function(done) {
mockFS();
var storage = new Storage({
password: 'password'
});
storage.save('myfilename', function(err) {
mock.restore();
done();
});
});
});
describe('#_read', function() {
it('should return the value of a key', function() {
var storage = new Storage();
storage.data = {
'walletId': {
'test': 'data'
}
};
storage._read('walletId::test').should.equal('data');
});
});
describe('#_write', function() {
it('should save the value of a key and then run save', function(done) {
var storage = new Storage();
storage.save = function(walletId, callback) {
storage.data[walletId]['key'].should.equal('value');
callback();
};
storage._write('walletId::key', 'value', function() {
done();
});
});
});
describe('#getGlobal', function() {
it('should call storage._read', function() {
var storage = new Storage();
storage.data = {
'walletId': {
'test': 'test'
}
};
storage._read = sinon.spy();
storage.getGlobal('walletId::test');
storage._read.calledOnce.should.equal(true);
});
});
describe('#setGlobal', function() {
it('should store a global key', function(done) {
var storage = new Storage();
storage.save = function(walletId, callback) {
storage.data[walletId]['key'].should.equal('value');
callback();
};
storage.setGlobal('walletId::key', 'value', function() {
done();
});
});
});
describe('#removeGlobal', function() {
it('should remove a global key', function(done) {
var storage = new Storage();
storage.data = {
'walletId': {
'key': 'value'
}
};
storage.save = function(walletId, callback) {
should.not.exist(storage.data[walletId]['key']);
callback();
};
storage.removeGlobal('walletId::key', function() {
done();
});
});
});
describe('#_key', function() {
it('should merge the wallet id and item key', function() {
var storage = new Storage();
storage._key('wallet', 'key').should.equal('wallet::key');
});
});
describe('#get', function() {
it('should call getGlobal with the correct key', function() {
var storage = new Storage();
storage.getGlobal = sinon.spy();
storage.get('wallet', 'key');
storage.getGlobal.calledOnce.should.equal(true);
storage.getGlobal.calledWith('wallet::key').should.equal(true);
});
});
describe('#set', function() {
it('should call setGlobal with the correct key', function() {
var storage = new Storage();
storage.setGlobal = sinon.spy();
storage.set('wallet', 'key');
storage.setGlobal.calledOnce.should.equal(true);
storage.setGlobal.calledWith('wallet::key').should.equal(true);
});
});
describe('#remove', function() {
it('should call removeGlobal with the correct key', function() {
var storage = new Storage();
storage.removeGlobal = sinon.spy();
storage.remove('wallet', 'key');
storage.removeGlobal.calledOnce.should.equal(true);
storage.removeGlobal.calledWith('wallet::key').should.equal(true);
});
});
describe('#setFromObj', function() {
it('should set this object for a wallet', function(done) {
var obj = {
test: 'testval'
};
var storage = new Storage();
storage.save = function(walletId, callback) {
callback();
};
storage.setFromObj('walletId', obj, function() {
storage.data.walletId.test.should.equal('testval');
done();
});
});
});
describe('#getEncryptedObj', function() {
it('should give an encrypted object', function() {
var obj = {
test: 'testval'
};
var data = JSON.stringify(obj);
var encrypted = CryptoJS.AES.encrypt(data, 'password');
var base64 = encrypted.toString();
var storage = new Storage({
password: 'password'
});
storage.data['walletId'] = obj;
var enc = storage.getEncryptedObj('walletId');
//enc.length.should.equal(96);
enc.length.should.be.greaterThan(10);
enc.slice(0, 10).should.equal(base64.slice(0, 10));
//enc.slice(0,6).should.equal("53616c");
});
});
describe('#clearAll', function() {
it('should set data to {}', function() {
});
});
});

View File

@ -59,9 +59,6 @@ var createBundle = function(opts) {
b.require('./js/models/core/HDPath', {
expose: '../js/models/core/HDPath'
});
b.require('./js/models/storage/File', {
expose: '../js/models/storage/File'
});
b.require('./config', {
expose: '../config'
});