Merge pull request #42 from chester1000/tests

Tests
This commit is contained in:
Josh Harvey 2014-09-03 19:31:56 -04:00
commit 4511695e13
17 changed files with 400 additions and 458 deletions

7
.travis.yml Normal file
View file

@ -0,0 +1,7 @@
language: node_js
node_js:
# - '0.11'
- '0.10'
env:
- TRAVIS=true

View file

@ -35,6 +35,7 @@
}, },
"devDependencies": { "devDependencies": {
"chai": "^1.9.1", "chai": "^1.9.1",
"chai-http": "^0.5.0",
"git-rev": "^0.2.1", "git-rev": "^0.2.1",
"lodash": "^2.4.1", "lodash": "^2.4.1",
"mocha": "^1.21.4", "mocha": "^1.21.4",

View file

@ -1,14 +0,0 @@
// var fs = require('fs');
// var path = require('path');
// var https = require('https');
// var fixtures = path.join(__dirname, '..', 'fixtures');
// module.exports = function(handler, callback) {
// var server = https.createServer({
// key: fs.readFileSync(path.join(fixtures, 'privatekey.pem')),
// cert: fs.readFileSync(path.join(fixtures, 'certificate.pem'))
// }, handler);
// server.listen(0, function() {
// callback(null, server);
// });
// };

View file

@ -1,139 +0,0 @@
/* global describe, it, before, afterEach */
'use strict';
var _ = require('lodash');
var should = require('chai').should();
var mockery = require('mockery');
var config = require('./mocks/config');
var CONFIG = _.cloneDeep(config);
function requireFreshConfig() {
return _.cloneDeep(CONFIG);
}
var walletMock = require('./mocks/wallet');
var tickerMock = require('./mocks/ticker');
var traderMock = require('./mocks/trader');
var verifierMock = require('./mocks/verifier');
mockery.registerMock('lamassu-mockWallet', walletMock);
mockery.registerMock('lamassu-mockTicker', tickerMock);
mockery.registerMock('lamassu-mockTrader', traderMock);
mockery.registerMock('lamassu-mockVerifier', verifierMock);
describe('Plugins', function() {
var plugins = null;
before(function() {
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});
plugins = require('../lib/plugins');
});
afterEach(function() {
config = requireFreshConfig();
});
it('should properly load', function() {
should.exist(plugins);
});
it('should throw when db is not provided', function() {
plugins.init.should.throw(/db.*required/);
});
it('should throw when invalid balance margin', function() {
config.exchanges.settings.lowBalanceMargin = 0.99;
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/lowBalanceMargin/);
});
it('should throw when module is not installed', function() {
config.exchanges.plugins.current.ticker = 'inexistent-plugin';
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/module.*not installed/);
});
it('should throw when used plugin has no SUPPORTED_MODULES', function() {
var tmp = tickerMock.SUPPORTED_MODULES;
delete tickerMock.SUPPORTED_MODULES;
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/required.*SUPPORTED_MODULES/);
tickerMock.SUPPORTED_MODULES = tmp;
});
it('should throw when used plugin has required method missing', function() {
var tmp = tickerMock.ticker;
delete tickerMock.ticker;
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/fails.*implement.*method/);
tickerMock.ticker = tmp;
});
describe('should configure all enabled plugins', function() {
var confList = {};
before(function() {
function configTest(name) {
return function config(localConfig) {
should.exist(config);
/* jshint expr: true */
localConfig.should.be.an.Object;
/* jshint expr: true */
confList[name] = config;
};
}
walletMock.config = configTest('wallet');
tickerMock.config = configTest('ticker');
traderMock.config = configTest('trader');
verifierMock.config = configTest('verifier');
plugins.configure(config);
});
['wallet', 'ticker', 'trader', 'verifier'].forEach(function(name) {
it('should configure ' + name, function() {
confList.should.have.property(name);
should.exist(confList[name]);
/* jshint expr: true */
confList[name].should.be.an.Object;
/* jshint expr: true */
});
});
});
this.timeout(9000);
describe('Ticker', function() {
it('should have .ticker() called at least once', function() {
tickerMock.tickerCalls.should.be.at.least(1);
});
});
});

14
test/mocks/config.js Normal file
View file

@ -0,0 +1,14 @@
'use strict';
var LamassuConfig = module.exports = function (conString, pairingTokenTTL) {
};
LamassuConfig.prototype.load = function load() {
};
LamassuConfig.prototype.on = function on() {
};
LamassuConfig.prototype.isAuthorized = function isAuthorized(fingerprint, cb) {
cb(null, true);
};

44
test/mocks/db.js Normal file
View file

@ -0,0 +1,44 @@
'use strict';
var FINGERPRINT_NEW = 'XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX';
var FINGERPRINT_FUNDS = 'YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY:YY';
var txs = {};
module.exports = {
FINGERPRINT_NEW: FINGERPRINT_NEW,
FINGERPRINT_FUNDS: FINGERPRINT_FUNDS,
summonTransaction: function(fingerprint, tx, cb) {
var cachedTx = txs[tx.txId];
if (cachedTx) cb(null, cachedTx);
else {
txs[tx.txId] = tx;
cb(null, null);
}
},
reportTransactionError: function(tx, err, status) {
txs[tx.txId].err = err;
txs[tx.txId].status = status;
calls.fail = true;
},
completeTransaction: function(tx, txHash) {
calls.status = true;
}
};
var calls = {};
module.exports.wasErrorReported = function() {
return calls.fail;
};
module.exports.wasStatusReported = function() {
return calls.status;
};
module.exports.resetCalls = function() {
calls = {
status: false,
fail: false
};
};
module.exports.resetCalls();

5
test/mocks/plugins.js Normal file
View file

@ -0,0 +1,5 @@
'use strict';
exports.init = function init () {
};

4
test/mocks/psql.js Normal file
View file

@ -0,0 +1,4 @@
'use strict';
var PostgresqlInterface = module.exports = function (conString) {
};

View file

@ -4,11 +4,9 @@ module.exports = {
SUPPORTED_MODULES: [ 'ticker' ], SUPPORTED_MODULES: [ 'ticker' ],
NAME: 'Mock Ticker', NAME: 'Mock Ticker',
tickerCalls: 0, config: function config() {},
ticker: function ticker(currency, callback) {
config: function() {}, tickerCalls++;
ticker: function(currency, callback) {
this.tickerCalls++;
var out = {}; var out = {};
out[currency] = { out[currency] = {
@ -21,3 +19,10 @@ module.exports = {
callback(null, out); callback(null, out);
} }
}; };
// mock stuff
var tickerCalls = 0;
module.exports.getTickerCalls = function() {
return tickerCalls;
};

View file

@ -4,8 +4,8 @@ module.exports = {
SUPPORTED_MODULES: ['trader'], SUPPORTED_MODULES: ['trader'],
NAME: 'Mock Trader', NAME: 'Mock Trader',
config: function() {}, config: function config() {},
balance: function() {}, balance: function balance() {},
purchase: function() {}, purchase: function purchase() {},
sell: function() {} sell: function sell() {}
}; };

View file

@ -4,7 +4,7 @@ module.exports = {
SUPPORTED_MODULES: ['idVerifier'], SUPPORTED_MODULES: ['idVerifier'],
NAME: 'Mock Verifier', NAME: 'Mock Verifier',
config: function() {}, config: function config() {},
verifyUser: function() {}, verifyUser: function verifyUser() {},
verifyTransaction: function() {} verifyTransaction: function verifyTransaction() {}
}; };

View file

@ -4,7 +4,40 @@ module.exports = {
SUPPORTED_MODULES: ['wallet'], SUPPORTED_MODULES: ['wallet'],
NAME: 'Mock Wallet', NAME: 'Mock Wallet',
config: function() {}, config: function config() {},
balance: function() {}, balance: function balance(callback) {
sendBitcoins: function() {} calls.balance++;
callback(null, {
BTC: 1e8
});
},
sendBitcoins: function(addr, satoshis, fee, cb) {
calls.send = true;
if (satoshis <= 1e8) cb(null, TX_HASH);
else {
var e = new Error('Insufficient funds');
e.name = 'InsufficientFunds';
cb(e);
}
}
}; };
// mock stuff
var calls = {
balance: 0,
send: false
};
var ADDR = module.exports.ADDR = '12sENwECeRSmTeDwyLNqwh47JistZqFmW8';
var TX_HASH = module.exports.TX_HASH = '1c12443203a48f42cdf7b1acee5b4b1c1fedc144cb909a3bf5edbffafb0cd204';
module.exports.getBalanceCalls = function() {
return calls.balance;
};
module.exports.wasSendCalled = function() {
return calls.send;
};

272
test/plugins.js Normal file
View file

@ -0,0 +1,272 @@
/* global describe, it, before, afterEach */
'use strict';
var _ = require('lodash');
var should = require('chai').should();
var mockery = require('mockery');
var config = require('./mocks/config.json');
var CONFIG = _.cloneDeep(config);
function requireFreshConfig() {
return _.cloneDeep(CONFIG);
}
var walletMock = require('./mocks/wallet');
var tickerMock = require('./mocks/ticker');
var traderMock = require('./mocks/trader');
var verifierMock = require('./mocks/verifier');
mockery.registerMock('lamassu-mockWallet', walletMock);
mockery.registerMock('lamassu-mockTicker', tickerMock);
mockery.registerMock('lamassu-mockTrader', traderMock);
mockery.registerMock('lamassu-mockVerifier', verifierMock);
describe('Plugins', function() {
var plugins = null;
before(function() {
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});
plugins = require('../lib/plugins');
});
afterEach(function() {
config = requireFreshConfig();
});
it('should properly load', function() {
should.exist(plugins);
});
it('should throw when db is not provided', function() {
plugins.init.should.throw(/db.*required/);
});
it('should throw when invalid balance margin', function() {
config.exchanges.settings.lowBalanceMargin = 0.99;
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/lowBalanceMargin/);
});
it('should throw when module is not installed', function() {
config.exchanges.plugins.current.ticker = 'inexistent-plugin';
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/module.*not installed/);
});
it('should throw when used plugin has no SUPPORTED_MODULES', function() {
var tmp = tickerMock.SUPPORTED_MODULES;
delete tickerMock.SUPPORTED_MODULES;
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/required.*SUPPORTED_MODULES/);
tickerMock.SUPPORTED_MODULES = tmp;
});
it('should throw when used plugin has required method missing', function() {
var tmp = tickerMock.ticker;
delete tickerMock.ticker;
function configurer() {
plugins.configure(config);
}
configurer.should.throw(/fails.*implement.*method/);
tickerMock.ticker = tmp;
});
describe('should configure all enabled plugins', function() {
var confList = {};
before(function() {
function configTest(name) {
return function config(localConfig) {
should.exist(config);
/* jshint expr: true */
localConfig.should.be.an.Object;
/* jshint expr: false */
confList[name] = config;
};
}
walletMock.config = configTest('wallet');
tickerMock.config = configTest('ticker');
traderMock.config = configTest('trader');
verifierMock.config = configTest('verifier');
plugins.configure(config);
});
['wallet', 'ticker', 'trader', 'verifier'].forEach(function(name) {
it('should configure ' + name, function() {
confList.should.have.property(name);
should.exist(confList[name]);
/* jshint expr: true */
confList[name].should.be.an.Object;
/* jshint expr: false */
});
});
it('should return config', function() {
var config = plugins.getCachedConfig();
should.exist(config);
/* jshint expr: true */
config.should.be.an.Object;
/* jshint expr: false */
});
});
describe('Ticker', function() {
it('should have called .ticker() at least once', function() {
tickerMock.getTickerCalls().should.be.at.least(1);
});
it('should return last ticker price', function() {
var rate = plugins.getDeviceRate();
should.exist(rate);
/* jshint expr: true */
rate.should.be.an.Object;
/* jshint expr: false */
rate.should.have.property('currency');
rate.should.have.property('rates');
var rates = rate.rates;
/* jshint expr: true */
rate.should.be.an.Object;
/* jshint expr: false */
rates.should.have.property('ask');
rates.should.have.property('bid');
});
});
describe('Wallet', function() {
var db = require('./mocks/db');
before(function() {
plugins.init(db);
});
it('should have called .balance() at least once', function() {
walletMock.getBalanceCalls().should.be.at.least(1);
});
it('should return BTC balance', function() {
var balance = plugins.getBalance();
should.exist(balance);
/* jshint expr: true */
balance.should.be.an.Object;
/* jshint expr: false */
balance.should.have.property('BTC');
balance.BTC.should.equal(1e8);
});
it('should return fiat balance', function() {
var fiatBalance = plugins.fiatBalance();
should.exist(fiatBalance);
/* jshint expr: true */
fiatBalance.should.be.a.Number;
/* jshint expr: false */
fiatBalance.should.be.below(999);
});
describe('Send Bitcoins', function() {
before(function() {
plugins.trade({currency: 'USD', satoshis: 1e7}, db.FINGERPRINT_NEW);
});
it('should send bitcoins successfully', function(done) {
var txDetails = {
txId: 1,
toAddress: walletMock.ADDR,
satoshis: 1e7
};
plugins.sendBitcoins(db.FINGERPRINT_NEW, txDetails, function(err, txHash) {
should.not.exist(err);
should.exist(txHash);
/* jshint expr: true */
txHash.should.be.a.String;
txHash.should.equal(walletMock.TX_HASH);
walletMock.wasSendCalled().should.be.true;
db.wasStatusReported().should.be.true;
db.wasErrorReported().should.be.false;
/* jshint expr: false */
done();
});
});
function notEnoughFundsTx(done) {
db.resetCalls();
var txDetails = {
txId: 2,
toAddress: walletMock.ADDR,
satoshis: 1e9
};
plugins.sendBitcoins(db.FINGERPRINT_FUNDS, txDetails, function(err, txHash) {
should.exist(err);
err.should.be.instanceof(Error);
err.name.should.equal('InsufficientFunds');
/* jshint expr: true */
walletMock.wasSendCalled().should.be.true;
db.wasStatusReported().should.be.false;
/* jshint expr: false */
done();
});
}
// this fail comes from external plugin
it('should fail when not enough funds', function(done) {
notEnoughFundsTx(function() {
/* jshint expr: true */
db.wasErrorReported().should.be.true;
/* jshint expr: false */
done();
});
});
// this once comes from plugins.js
it('should fail again', function(done) {
notEnoughFundsTx(function() {
/* jshint expr: true */
db.wasErrorReported().should.be.false; // should not report error again
/* jshint expr: false */
done();
});
});
});
});
describe('Trader', function() {});
});

View file

@ -1,54 +0,0 @@
// 'use strict';
// var assert = require('chai').assert;
// var Trader = require('../../lib/trader.js');
// var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var psqlInterface = new PostgresqlInterface(db);
// describe('trader/api', function () {
// it('should throw when trying to create a trader with no DB', function () {
// assert.throws(function () {
// new Trader();
// });
// });
// it('should throw when trying to configure a trader with `lowBalanceMargin` < 1', function () {
// var trader = new Trader(psqlInterface);
// assert.throws(function () {
// trader.configure({
// exchanges: {
// settings: {
// lowBalanceMargin: 0.8
// }
// }
// });
// });
// });
// it('should find and instantiate ticker and trade exchanges', function () {
// var trader = new Trader(psqlInterface);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// ticker: 'bitpay',
// transfer: 'blockchain'
// },
// settings: {
// bitpay: {},
// blockchain: {}
// }
// },
// settings: {
// currency: 'USD',
// lowBalanceMargin: 2
// }
// }
// });
// assert.ok(trader.tickerExchange);
// assert.ok(trader.transferExchange);
// });
// });

View file

@ -1,107 +0,0 @@
// /*global describe, it */
// 'use strict';
// var assert = require('chai').assert;
// var Trader = require('../../lib/trader.js');
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var RATE = 101;
// var CURRENCY = 'USD';
// var SATOSHI_FACTOR = 1e8;
// var LOW_BALANCE_MARGIN = 1.2;
// var COMMISSION = 1.1;
// var FINGERPRINT = '00:7A:5A:B3:02:F1:44:46:E2:EA:24:D3:A8:29:DE:22:BA:1B:F9:50';
// var settings = {
// currency: CURRENCY,
// lowBalanceMargin: LOW_BALANCE_MARGIN,
// commission: COMMISSION
// };
// describe('trader/fiatBalance', function() {
// it('should calculate balance correctly with transfer exchange only', function() {
// var trader = new Trader(db);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// transfer: 'blockchain',
// ticker: 'bitpay'
// },
// settings: { blockchain: {}, bitpay: {} }
// },
// settings: settings
// }
// });
// // We have 3 bitcoins, want to trade 1 bitcoin for 100 fiat
// trader.balance = {
// transferBalance: 3 * SATOSHI_FACTOR,
// tradeBalance: null
// };
// trader.rates[CURRENCY] = { rate: RATE };
// trader.rateInfo = {rates: {USD: {rate: RATE}}};
// var fiatBalance = trader.fiatBalance(FINGERPRINT);
// assert.equal(fiatBalance, (3 * RATE * COMMISSION / LOW_BALANCE_MARGIN));
// });
// it('should calculate balance correctly with transfer and trade exchange', function() {
// var trader = new Trader(db);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// transfer: 'blockchain',
// ticker: 'bitpay',
// trade: 'bitstamp'
// },
// settings: { blockchain: {}, bitpay: {}, bitstamp: {} }
// },
// settings: settings
// }
// });
// // We have 3 bitcoins in transfer, worth 3 * RATE * COMMISSION = 333.3
// // We have 150 USD in trade
// trader.balance = {
// transferBalance: 3 * SATOSHI_FACTOR,
// tradeBalance: 150
// };
// trader.rates[CURRENCY] = { rate: RATE };
// trader.rateInfo = {rates: {USD: {rate: RATE}}};
// var fiatBalance = trader.fiatBalance(FINGERPRINT);
// assert.equal(fiatBalance, 150 / LOW_BALANCE_MARGIN);
// });
// it('should calculate balance correctly with transfer and ' +
// 'trade exchange with different currencies', function() {
// var trader = new Trader(db);
// trader.configure({
// exchanges: {
// plugins: {
// current: {
// transfer: 'blockchain',
// ticker: 'bitpay',
// trade: 'bitstamp'
// },
// settings: { blockchain: {}, bitpay: {}, bitstamp: {} }
// },
// settings: settings
// }
// });
// // We have 6 bitcoins in transfer, worth 6 * RATE * COMMISSION = 666.6
// // We have 150 USD in trade, 1 USD = 4 ILS => 600 ILS in trade
// trader.balance = {
// transferBalance: 6 * SATOSHI_FACTOR,
// tradeBalance: 600
// };
// trader.rates = {USD: {rate: RATE}, ILS: {rate: RATE * 4} };
// trader.rateInfo = {rates: {USD: {rate: RATE}}};
// var fiatBalance = trader.fiatBalance(FINGERPRINT);
// assert.equal(fiatBalance, 600 / LOW_BALANCE_MARGIN);
// });
// });

View file

@ -1,77 +0,0 @@
// 'use strict';
// var assert = require('chai').assert;
// var hock = require('hock');
// var uuid = require('node-uuid').v4;
// var Trader = require('../../lib/trader.js');
// var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var psqlInterface = new PostgresqlInterface(db);
// var TRANSACTION_FEE = 1;
// var FINGERPRINT = 'CB:3D:78:49:03:39:BA:47:0A:33:29:3E:31:25:F7:C6:4F:74:71:D7';
// var TXID = '216dabdb692670bae940deb71e59486038a575f637903d3c9af601ddd48057fc';
// var ADDRESS = '1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64';
// var SATOSHIS = 1337;
// var CURRENCY = 'USD';
// var OUR_TXID = uuid();
// describe('trader/send', function () {
// var trader = new Trader(psqlInterface);
// trader.config = {
// exchanges: {
// settings: {
// transactionFee: TRANSACTION_FEE
// }
// }
// };
// trader.pollRate = function () {};
// it('should call `sendBitcoins` on the transfer exchange', function (done) {
// trader.transferExchange = {
// sendBitcoins: function (address, satoshis, transactionFee, callback) {
// assert.equal(ADDRESS, address);
// assert.equal(SATOSHIS, satoshis);
// assert.equal(transactionFee, TRANSACTION_FEE);
// callback(null, TXID);
// },
// balance: function () {}
// };
// trader.sendBitcoins(FINGERPRINT, {
// fiat: 100,
// txId: OUR_TXID,
// currencyCode: CURRENCY,
// toAddress: ADDRESS,
// satoshis: SATOSHIS
// }, function (err, txId) {
// assert.notOk(err);
// assert.equal(txId, TXID);
// done();
// });
// });
// it('should not call `sendBitcoins` on the transfer exchange with same send', function (done) {
// trader.transferExchange = {
// sendBitcoins: function () {
// throw new Error('This should not have been called');
// },
// balance: function () {}
// };
// trader.sendBitcoins(FINGERPRINT, {
// fiat: 100,
// txId: OUR_TXID,
// currencyCode: CURRENCY,
// toAddress: ADDRESS,
// satoshis: SATOSHIS
// }, function (err, txId) {
// assert.notOk(err);
// assert.equal(txId, TXID);
// done();
// });
// });
// });

View file

@ -1,52 +0,0 @@
// /*global describe, it */
// 'use strict';
// var assert = require('chai').assert;
// var Trader = require('../../lib/trader.js');
// var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var psqlInterface = new PostgresqlInterface(db);
// var CURRENCY = 'USD';
// describe('trader/send', function () {
// var trader = new Trader(psqlInterface);
// trader.config = {
// exchanges: {
// settings: { currency: CURRENCY }
// }
// };
// it('should call `balance` on the transfer exchange', function (done) {
// trader.transferExchange = {
// balance: function (callback) {
// callback(null, 100);
// }
// };
// trader.pollBalance(function (err) {
// assert.notOk(err);
// assert.equal(trader.balance.transferBalance, 100);
// assert.ok(trader.balance.timestamp);
// done();
// });
// });
// it('should call `ticker` on the ticker exchange', function (done) {
// trader.tickerExchange = {
// ticker: function (currencies, callback) {
// assert.equal(currencies[0], CURRENCY);
// callback(null, {USD: {rate: 100}});
// }
// };
// trader.pollRate(function (err) {
// assert.notOk(err);
// var rate = trader.rate(CURRENCY);
// assert.equal(rate.rate, 100);
// assert.ok(rate.timestamp);
// done();
// });
// });
// });