format to standard
This commit is contained in:
parent
8caeeb6e54
commit
76e53dfc74
1 changed files with 163 additions and 190 deletions
281
test/plugins.js
281
test/plugins.js
|
|
@ -1,267 +1,240 @@
|
||||||
/* global describe, it, before, afterEach */
|
/* global describe, it, before, afterEach */
|
||||||
|
|
||||||
'use strict';
|
'use strict'
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash')
|
||||||
var should = require('chai').should();
|
var should = require('chai').should()
|
||||||
var mockery = require('mockery');
|
var mockery = require('mockery')
|
||||||
|
|
||||||
var config = require('./mocks/config.json');
|
var config = require('./mocks/config.json')
|
||||||
var CONFIG = _.cloneDeep(config);
|
var CONFIG = _.cloneDeep(config)
|
||||||
function requireFreshConfig () {
|
function requireFreshConfig () {
|
||||||
return _.cloneDeep(CONFIG);
|
return _.cloneDeep(CONFIG)
|
||||||
}
|
}
|
||||||
|
|
||||||
var walletMock = require('./mocks/wallet');
|
var walletMock = require('./mocks/wallet')
|
||||||
var tickerMock = require('./mocks/ticker');
|
var tickerMock = require('./mocks/ticker')
|
||||||
var traderMock = require('./mocks/trader');
|
var traderMock = require('./mocks/trader')
|
||||||
var verifierMock = require('./mocks/verifier');
|
var verifierMock = require('./mocks/verifier')
|
||||||
var infoMock = require('./mocks/info');
|
var infoMock = require('./mocks/info')
|
||||||
|
|
||||||
mockery.registerMock('lamassu-mockWallet', walletMock);
|
mockery.registerMock('lamassu-mockWallet', walletMock)
|
||||||
mockery.registerMock('lamassu-mockTicker', tickerMock);
|
mockery.registerMock('lamassu-mockTicker', tickerMock)
|
||||||
mockery.registerMock('lamassu-mockTrader', traderMock);
|
mockery.registerMock('lamassu-mockTrader', traderMock)
|
||||||
mockery.registerMock('lamassu-mockVerifier', verifierMock);
|
mockery.registerMock('lamassu-mockVerifier', verifierMock)
|
||||||
mockery.registerMock('lamassu-mockInfo', infoMock);
|
mockery.registerMock('lamassu-mockInfo', infoMock)
|
||||||
|
|
||||||
describe('Plugins', function () {
|
describe('Plugins', function () {
|
||||||
var plugins = null;
|
var plugins = null
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
mockery.enable({
|
mockery.enable({
|
||||||
useCleanCache: true,
|
useCleanCache: true,
|
||||||
warnOnReplace: false,
|
warnOnReplace: false,
|
||||||
warnOnUnregistered: false
|
warnOnUnregistered: false
|
||||||
});
|
})
|
||||||
|
|
||||||
plugins = require('../lib/plugins');
|
plugins = require('../lib/plugins')
|
||||||
});
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
config = requireFreshConfig();
|
config = requireFreshConfig()
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should properly load', function () {
|
it('should properly load', function () {
|
||||||
should.exist(plugins);
|
should.exist(plugins)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should throw when db is not provided', function () {
|
it('should throw when db is not provided', function () {
|
||||||
plugins.init.should.throw(/db.*required/);
|
plugins.init.should.throw(/db.*required/)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should throw when invalid balance margin', function () {
|
it('should throw when invalid balance margin', function () {
|
||||||
config.exchanges.settings.lowBalanceMargin = 0.99;
|
config.exchanges.settings.lowBalanceMargin = 0.99
|
||||||
|
|
||||||
function configurer () {
|
function configurer () {
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
}
|
}
|
||||||
configurer.should.throw(/lowBalanceMargin/);
|
configurer.should.throw(/lowBalanceMargin/)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should throw when module is not installed', function () {
|
it('should throw when module is not installed', function () {
|
||||||
config.exchanges.plugins.current.ticker = 'inexistent-plugin';
|
config.exchanges.plugins.current.ticker = 'inexistent-plugin'
|
||||||
|
|
||||||
function configurer () {
|
function configurer () {
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
}
|
}
|
||||||
configurer.should.throw(/module.*not installed/);
|
configurer.should.throw(/module.*not installed/)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should throw when used plugin has no SUPPORTED_MODULES', function () {
|
it('should throw when used plugin has no SUPPORTED_MODULES', function () {
|
||||||
var tmp = tickerMock.SUPPORTED_MODULES;
|
var tmp = tickerMock.SUPPORTED_MODULES
|
||||||
delete tickerMock.SUPPORTED_MODULES;
|
delete tickerMock.SUPPORTED_MODULES
|
||||||
|
|
||||||
function configurer () {
|
function configurer () {
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
}
|
}
|
||||||
configurer.should.throw(/required.*SUPPORTED_MODULES/);
|
configurer.should.throw(/required.*SUPPORTED_MODULES/)
|
||||||
|
|
||||||
tickerMock.SUPPORTED_MODULES = tmp;
|
tickerMock.SUPPORTED_MODULES = tmp
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should throw when used plugin has required method missing', function () {
|
it('should throw when used plugin has required method missing', function () {
|
||||||
var tmp = tickerMock.ticker;
|
var tmp = tickerMock.ticker
|
||||||
delete tickerMock.ticker;
|
delete tickerMock.ticker
|
||||||
|
|
||||||
function configurer () {
|
function configurer () {
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
}
|
}
|
||||||
configurer.should.throw(/fails.*implement.*method/);
|
configurer.should.throw(/fails.*implement.*method/)
|
||||||
|
|
||||||
tickerMock.ticker = tmp;
|
tickerMock.ticker = tmp
|
||||||
});
|
})
|
||||||
|
|
||||||
describe('should configure all enabled plugins', function () {
|
describe('should configure all enabled plugins', function () {
|
||||||
var confList = {};
|
var confList = {}
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
function configTest (name) {
|
function configTest (name) {
|
||||||
return function config (localConfig) {
|
return function config (localConfig) {
|
||||||
should.exist(config);
|
should.exist(config)
|
||||||
/* jshint expr: true */
|
/* jshint expr: true */
|
||||||
localConfig.should.be.an.Object;
|
localConfig.should.be.an.Object
|
||||||
/* jshint expr: false */
|
/* jshint expr: false */
|
||||||
confList[name] = config;
|
confList[name] = config
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
walletMock.config = configTest('wallet');
|
walletMock.config = configTest('wallet')
|
||||||
tickerMock.config = configTest('ticker');
|
tickerMock.config = configTest('ticker')
|
||||||
traderMock.config = configTest('trader');
|
traderMock.config = configTest('trader')
|
||||||
verifierMock.config = configTest('verifier');
|
verifierMock.config = configTest('verifier')
|
||||||
infoMock.config = configTest('info');
|
infoMock.config = configTest('info')
|
||||||
|
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
});
|
})
|
||||||
|
|
||||||
['wallet', 'ticker', 'trader', 'verifier', 'info'].forEach(function(name) {
|
;['wallet', 'ticker', 'trader', 'verifier', 'info'].forEach(function (name) {
|
||||||
it('should configure ' + name, function () {
|
it('should configure ' + name, function () {
|
||||||
confList.should.have.property(name);
|
confList.should.have.property(name)
|
||||||
should.exist(confList[name]);
|
should.exist(confList[name])
|
||||||
/* jshint expr: true */
|
/* jshint expr: true */
|
||||||
confList[name].should.be.an.Object;
|
confList[name].should.be.an.Object
|
||||||
/* jshint expr: false */
|
/* jshint expr: false */
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should return config', function () {
|
it('should return config', function () {
|
||||||
var config = plugins.getConfig();
|
var config = plugins.getConfig()
|
||||||
should.exist(config);
|
should.exist(config)
|
||||||
/* jshint expr: true */
|
/* jshint expr: true */
|
||||||
config.should.be.an.Object;
|
config.should.be.an.Object
|
||||||
/* jshint expr: false */
|
/* jshint expr: false */
|
||||||
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Ticker', function () {
|
describe('Ticker', function () {
|
||||||
it('should have called .ticker() at least once', function () {
|
it('should have called .ticker() at least once', function () {
|
||||||
tickerMock.getTickerCalls().should.be.at.least(1);
|
tickerMock.getTickerCalls().should.be.at.least(1)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should return last ticker price', function () {
|
it('should return last ticker price', function () {
|
||||||
var rate = plugins.getDeviceRate();
|
var rate = plugins.getDeviceRate()
|
||||||
should.exist(rate);
|
should.exist(rate)
|
||||||
/* jshint expr: true */
|
/* jshint expr: true */
|
||||||
rate.should.be.an.Object;
|
rate.should.be.an.Object
|
||||||
/* jshint expr: false */
|
/* jshint expr: false */
|
||||||
rate.should.have.property('currency');
|
rate.should.have.property('currency')
|
||||||
rate.should.have.property('rates');
|
rate.should.have.property('rates')
|
||||||
|
|
||||||
var rates = rate.rates;
|
var rates = rate.rates
|
||||||
/* jshint expr: true */
|
/* jshint expr: true */
|
||||||
rate.should.be.an.Object;
|
rate.should.be.an.Object
|
||||||
/* jshint expr: false */
|
/* jshint expr: false */
|
||||||
rates.should.have.property('ask');
|
rates.should.have.property('ask')
|
||||||
rates.should.have.property('bid');
|
rates.should.have.property('bid')
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
describe('Wallet', function () {
|
describe('Wallet', function () {
|
||||||
|
var db = require('./mocks/db')
|
||||||
var db = require('./mocks/db');
|
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
plugins.init(db);
|
plugins.init(db)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should have called .balance() at least once', function () {
|
it('should have called .balance() at least once', function () {
|
||||||
walletMock.getBalanceCalls().should.be.at.least(1);
|
walletMock.getBalanceCalls().should.be.at.least(1)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should return BTC balance', function () {
|
it('should return BTC balance', function () {
|
||||||
var balance = plugins.getBalance();
|
var balance = plugins.getBalance()
|
||||||
should.exist(balance);
|
should.exist(balance)
|
||||||
/* jshint expr: true */
|
/* jshint expr: true */
|
||||||
balance.should.be.an.Object;
|
balance.should.be.an.Object
|
||||||
/* jshint expr: false */
|
/* jshint expr: false */
|
||||||
balance.should.have.property('BTC');
|
balance.should.have.property('BTC')
|
||||||
balance.BTC.should.equal(1e8);
|
balance.BTC.should.equal(1e8)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should return fiat balance', function () {
|
it('should return fiat balance', function () {
|
||||||
var fiatBalance = plugins.fiatBalance();
|
var fiatBalance = plugins.fiatBalance()
|
||||||
should.exist(fiatBalance);
|
should.exist(fiatBalance)
|
||||||
/* jshint expr: true */
|
/* jshint expr: true */
|
||||||
fiatBalance.should.be.a.Number;
|
fiatBalance.should.be.a.Number
|
||||||
/* jshint expr: false */
|
/* jshint expr: false */
|
||||||
fiatBalance.should.be.below(999);
|
fiatBalance.should.be.below(999)
|
||||||
});
|
})
|
||||||
|
|
||||||
describe('Send Bitcoins', function () {
|
describe('Send Bitcoins', function () {
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
plugins.trade({currency: 'USD', satoshis: 1e7}, db.FINGERPRINT_NEW);
|
plugins.trade({currency: 'USD', satoshis: 1e7}, db.FINGERPRINT_NEW)
|
||||||
});
|
})
|
||||||
|
|
||||||
it('should send bitcoins successfully', function (done) {
|
it('should send bitcoins successfully', function (done) {
|
||||||
var txDetails = {
|
var txDetails = {
|
||||||
txId: 1,
|
txId: 1,
|
||||||
toAddress: walletMock.ADDR,
|
toAddress: walletMock.ADDR,
|
||||||
satoshis: 1e7
|
satoshis: 1e7
|
||||||
};
|
|
||||||
|
|
||||||
plugins.sendBitcoins(db.FINGERPRINT_NEW, txDetails, function(err,
|
|
||||||
response) {
|
|
||||||
should.not.exist(err);
|
|
||||||
should.exist(response);
|
|
||||||
/* jshint expr: true */
|
|
||||||
response.should.be.an.Object;
|
|
||||||
response.should.have.property('statusCode');
|
|
||||||
response.statusCode.should.equal(201);
|
|
||||||
/* 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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
plugins.sendCoins(db.FINGERPRINT_NEW, txDetails, function (err,
|
||||||
|
response) {
|
||||||
|
should.not.exist(err)
|
||||||
|
should.exist(response)
|
||||||
|
/* jshint expr: true */
|
||||||
|
response.should.be.an.Object
|
||||||
|
response.should.have.property('statusCode')
|
||||||
|
response.statusCode.should.equal(201)
|
||||||
|
/* jshint expr: false */
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// // this fail comes from external plugin
|
// // this fail comes from external plugin
|
||||||
// it('should fail when not enough funds', function(done) {
|
// it('should fail when not enough funds', function(done) {
|
||||||
// notEnoughFundsTx(function() {
|
// notEnoughFundsTx(function() {
|
||||||
|
|
||||||
// /* jshint expr: true */
|
// /* jshint expr: true */
|
||||||
// db.wasErrorReported().should.be.true;
|
// db.wasErrorReported().should.be.true
|
||||||
// /* jshint expr: false */
|
// /* jshint expr: false */
|
||||||
// done();
|
// done()
|
||||||
// });
|
// })
|
||||||
// });
|
// })
|
||||||
|
|
||||||
// // this once comes from plugins.js
|
// // this once comes from plugins.js
|
||||||
// it('should fail again', function(done) {
|
// it('should fail again', function(done) {
|
||||||
// notEnoughFundsTx(function() {
|
// notEnoughFundsTx(function() {
|
||||||
|
|
||||||
// /* jshint expr: true */
|
// /* jshint expr: true */
|
||||||
// db.wasErrorReported().should.be.false; // should not report error again
|
// db.wasErrorReported().should.be.false // should not report error again
|
||||||
// /* jshint expr: false */
|
// /* jshint expr: false */
|
||||||
// done();
|
// done()
|
||||||
// });
|
// })
|
||||||
// });
|
// })
|
||||||
});
|
})
|
||||||
|
})
|
||||||
|
|
||||||
});
|
describe('Trader', function () {})
|
||||||
|
})
|
||||||
describe('Trader', function() {});
|
|
||||||
});
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue