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 path = require('path');
var https = require('https');
var fixtures = path.join(__dirname, '..', 'fixtures');
// 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);
});
};
// 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);
// });
// };

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 Trader = require('../../lib/trader.js');
var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// 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 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();
});
});
// 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 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
}
}
});
// 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);
});
});
// assert.ok(trader.tickerExchange);
// assert.ok(trader.transferExchange);
// });
// });

View file

@ -1,107 +1,107 @@
/*global describe, it */
'use strict';
// /*global describe, it */
// 'use strict';
var assert = require('chai').assert;
var Trader = require('../../lib/trader.js');
// var assert = require('chai').assert;
// 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 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 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
};
// 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
}
});
// 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));
});
// // 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
}
});
// 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);
});
// // 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
}
});
// 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);
});
// // 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 +1,77 @@
'use strict';
// '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 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 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 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();
// var OUR_TXID = uuid();
describe('trader/send', function () {
var trader = new Trader(psqlInterface);
trader.config = {
exchanges: {
settings: {
transactionFee: TRANSACTION_FEE
}
}
};
// describe('trader/send', function () {
// var trader = new Trader(psqlInterface);
// trader.config = {
// exchanges: {
// settings: {
// transactionFee: TRANSACTION_FEE
// }
// }
// };
trader.pollRate = function () {};
// 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 () {}
};
// 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();
});
});
// 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 () {}
};
// 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();
});
});
});
// 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 +1,52 @@
/*global describe, it */
'use strict';
// /*global describe, it */
// 'use strict';
var assert = require('chai').assert;
var Trader = require('../../lib/trader.js');
var PostgresqlInterface = require('../../lib/postgresql_interface.js');
// 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 db = 'psql://lamassu:lamassu@localhost/lamassu-test';
// var psqlInterface = new PostgresqlInterface(db);
var CURRENCY = 'USD';
// var CURRENCY = 'USD';
describe('trader/send', function () {
var trader = new Trader(psqlInterface);
trader.config = {
exchanges: {
settings: { currency: CURRENCY }
}
};
// 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);
}
};
// 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();
});
});
// 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}});
}
};
// 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();
});
});
});
// trader.pollRate(function (err) {
// assert.notOk(err);
// var rate = trader.rate(CURRENCY);
// assert.equal(rate.rate, 100);
// assert.ok(rate.timestamp);
// done();
// });
// });
// });