test(plugins): plugin load + configure tests added

This commit is contained in:
Damian Mee 2014-08-21 04:10:13 +02:00
parent f97fda585f
commit edffd2f595
11 changed files with 509 additions and 267 deletions

View file

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

127
test/index.js Normal file
View file

@ -0,0 +1,127 @@
'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 currents = config.exchanges.plugins.current;
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 = .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(config) {
should.exist(config);
config.should.be.an.Object;
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]);
confList[name].should.be.an.Object;
});
});
});
});

75
test/mocks/config.json Normal file
View file

@ -0,0 +1,75 @@
{
"exchanges": {
"settings": {
"compliance": {
"maximum": {
"limit": null
}
},
"commission": 1,
"fastPoll": 5000,
"fastPollLimit": 10,
"tickerInterval": 5000,
"balanceInterval": 5000,
"tradeInterval": 5000,
"retryInterval": 5000,
"retries": 3,
"lowBalanceMargin": 1.05,
"transactionFee": 10000,
"tickerDelta": 0,
"minimumTradeFiat": 0,
"currency": "PLN",
"networkTimeout": 20000
},
"plugins": {
"current": {
"ticker": "mockTicker",
"trade": "mockTrader",
"wallet": "mockWallet",
"transfer": "mockWallet",
"idVerifier": "mockVerifier"
},
"settings": {
"bitpay": { },
"bitstamp": {
"currency": "USD",
"key": "",
"secret": "",
"clientId": ""
},
"blockchain": {
"retryInterval": 10000,
"retryTimeout": 60000,
"guid": "",
"password": "",
"fromAddress": ""
}
}
}
},
"brain": {
"qrTimeout": 60000,
"goodbyeTimeout": 2000,
"billTimeout": 60000,
"completedTimeout": 60000,
"networkTimeout": 20000,
"triggerRetry": 5000,
"idleTime": 600000,
"checkIdleTime": 60000,
"maxProcessSize": 104857600,
"freeMemRatio": 0.15,
"unit": {
"ssn": "xx-1234-45",
"owner": "Lamassu, Inc. \/ Trofa \/ Portugal"
},
"locale": {
"currency": "PLN",
"localeInfo": {
"primaryLocale": "pl-PL",
"primaryLocales": [
"pl-PL"
]
}
}
}
}

9
test/mocks/ticker.js Normal file
View file

@ -0,0 +1,9 @@
'use strict';
module.exports = {
SUPPORTED_MODULES: [ 'ticker' ],
NAME: 'Mock Ticker',
config: function() {},
ticker: function() {}
};

11
test/mocks/trader.js Normal file
View file

@ -0,0 +1,11 @@
'use strict';
module.exports = {
SUPPORTED_MODULES: ['trader'],
NAME: 'Mock Trader',
config: function() {},
balance: function() {},
purchase: function() {},
sell: function() {}
};

10
test/mocks/verifier.js Normal file
View file

@ -0,0 +1,10 @@
'use strict';
module.exports = {
SUPPORTED_MODULES: ['idVerifier'],
NAME: 'Mock Verifier',
config: function() {},
verifyUser: function() {},
verifyTransaction: function() {}
};

10
test/mocks/wallet.js Normal file
View file

@ -0,0 +1,10 @@
'use strict';
module.exports = {
SUPPORTED_MODULES: ['wallet'],
NAME: 'Mock Wallet',
config: function() {},
balance: function() {},
sendBitcoins: function() {}
};

View file

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

View file

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

View file

@ -1,77 +1,77 @@
'use strict'; // 'use strict';
var assert = require('chai').assert; // var assert = require('chai').assert;
var hock = require('hock'); // var hock = require('hock');
var uuid = require('node-uuid').v4; // var uuid = require('node-uuid').v4;
var Trader = require('../../lib/trader.js'); // var Trader = require('../../lib/trader.js');
var PostgresqlInterface = require('../../lib/postgresql_interface.js'); // var PostgresqlInterface = require('../../lib/postgresql_interface.js');
var db = 'psql://lamassu:lamassu@localhost/lamassu-test'; // var db = 'psql://lamassu:lamassu@localhost/lamassu-test';
var psqlInterface = new PostgresqlInterface(db); // var psqlInterface = new PostgresqlInterface(db);
var TRANSACTION_FEE = 1; // 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 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 TXID = '216dabdb692670bae940deb71e59486038a575f637903d3c9af601ddd48057fc';
var ADDRESS = '1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64'; // var ADDRESS = '1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64';
var SATOSHIS = 1337; // var SATOSHIS = 1337;
var CURRENCY = 'USD'; // var CURRENCY = 'USD';
var OUR_TXID = uuid(); // var OUR_TXID = uuid();
describe('trader/send', function () { // describe('trader/send', function () {
var trader = new Trader(psqlInterface); // var trader = new Trader(psqlInterface);
trader.config = { // trader.config = {
exchanges: { // exchanges: {
settings: { // settings: {
transactionFee: TRANSACTION_FEE // transactionFee: TRANSACTION_FEE
} // }
} // }
}; // };
trader.pollRate = function () {}; // trader.pollRate = function () {};
it('should call `sendBitcoins` on the transfer exchange', function (done) { // it('should call `sendBitcoins` on the transfer exchange', function (done) {
trader.transferExchange = { // trader.transferExchange = {
sendBitcoins: function (address, satoshis, transactionFee, callback) { // sendBitcoins: function (address, satoshis, transactionFee, callback) {
assert.equal(ADDRESS, address); // assert.equal(ADDRESS, address);
assert.equal(SATOSHIS, satoshis); // assert.equal(SATOSHIS, satoshis);
assert.equal(transactionFee, TRANSACTION_FEE); // assert.equal(transactionFee, TRANSACTION_FEE);
callback(null, TXID); // callback(null, TXID);
}, // },
balance: function () {} // balance: function () {}
}; // };
trader.sendBitcoins(FINGERPRINT, { // trader.sendBitcoins(FINGERPRINT, {
fiat: 100, // fiat: 100,
txId: OUR_TXID, // txId: OUR_TXID,
currencyCode: CURRENCY, // currencyCode: CURRENCY,
toAddress: ADDRESS, // toAddress: ADDRESS,
satoshis: SATOSHIS // satoshis: SATOSHIS
}, function (err, txId) { // }, function (err, txId) {
assert.notOk(err); // assert.notOk(err);
assert.equal(txId, TXID); // assert.equal(txId, TXID);
done(); // done();
}); // });
}); // });
it('should not call `sendBitcoins` on the transfer exchange with same send', function (done) { // it('should not call `sendBitcoins` on the transfer exchange with same send', function (done) {
trader.transferExchange = { // trader.transferExchange = {
sendBitcoins: function () { // sendBitcoins: function () {
throw new Error('This should not have been called'); // throw new Error('This should not have been called');
}, // },
balance: function () {} // balance: function () {}
}; // };
trader.sendBitcoins(FINGERPRINT, { // trader.sendBitcoins(FINGERPRINT, {
fiat: 100, // fiat: 100,
txId: OUR_TXID, // txId: OUR_TXID,
currencyCode: CURRENCY, // currencyCode: CURRENCY,
toAddress: ADDRESS, // toAddress: ADDRESS,
satoshis: SATOSHIS // satoshis: SATOSHIS
}, function (err, txId) { // }, function (err, txId) {
assert.notOk(err); // assert.notOk(err);
assert.equal(txId, TXID); // assert.equal(txId, TXID);
done(); // done();
}); // });
}); // });
}); // });

View file

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