format to standard

This commit is contained in:
Josh Harvey 2016-04-23 02:15:09 +03:00
parent 8caeeb6e54
commit 76e53dfc74

View file

@ -1,267 +1,240 @@
/* global describe, it, before, afterEach */
'use strict';
'use strict'
var _ = require('lodash');
var should = require('chai').should();
var mockery = require('mockery');
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 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');
var infoMock = require('./mocks/info');
var walletMock = require('./mocks/wallet')
var tickerMock = require('./mocks/ticker')
var traderMock = require('./mocks/trader')
var verifierMock = require('./mocks/verifier')
var infoMock = require('./mocks/info')
mockery.registerMock('lamassu-mockWallet', walletMock);
mockery.registerMock('lamassu-mockTicker', tickerMock);
mockery.registerMock('lamassu-mockTrader', traderMock);
mockery.registerMock('lamassu-mockVerifier', verifierMock);
mockery.registerMock('lamassu-mockInfo', infoMock);
mockery.registerMock('lamassu-mockWallet', walletMock)
mockery.registerMock('lamassu-mockTicker', tickerMock)
mockery.registerMock('lamassu-mockTrader', traderMock)
mockery.registerMock('lamassu-mockVerifier', verifierMock)
mockery.registerMock('lamassu-mockInfo', infoMock)
describe('Plugins', function() {
var plugins = null;
describe('Plugins', function () {
var plugins = null
before(function() {
before(function () {
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});
})
plugins = require('../lib/plugins');
});
plugins = require('../lib/plugins')
})
afterEach(function() {
config = requireFreshConfig();
});
afterEach(function () {
config = requireFreshConfig()
})
it('should properly load', function() {
should.exist(plugins);
});
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 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;
it('should throw when invalid balance margin', function () {
config.exchanges.settings.lowBalanceMargin = 0.99
function configurer() {
plugins.configure(config);
function configurer () {
plugins.configure(config)
}
configurer.should.throw(/lowBalanceMargin/);
});
configurer.should.throw(/lowBalanceMargin/)
})
it('should throw when module is not installed', function() {
config.exchanges.plugins.current.ticker = 'inexistent-plugin';
it('should throw when module is not installed', function () {
config.exchanges.plugins.current.ticker = 'inexistent-plugin'
function configurer() {
plugins.configure(config);
function configurer () {
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() {
var tmp = tickerMock.SUPPORTED_MODULES;
delete tickerMock.SUPPORTED_MODULES;
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);
function configurer () {
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() {
var tmp = tickerMock.ticker;
delete tickerMock.ticker;
it('should throw when used plugin has required method missing', function () {
var tmp = tickerMock.ticker
delete tickerMock.ticker
function configurer() {
plugins.configure(config);
function configurer () {
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() {
var confList = {};
describe('should configure all enabled plugins', function () {
var confList = {}
before(function() {
function configTest(name) {
return function config(localConfig) {
should.exist(config);
before(function () {
function configTest (name) {
return function config (localConfig) {
should.exist(config)
/* jshint expr: true */
localConfig.should.be.an.Object;
localConfig.should.be.an.Object
/* jshint expr: false */
confList[name] = config;
};
confList[name] = config
}
}
walletMock.config = configTest('wallet');
tickerMock.config = configTest('ticker');
traderMock.config = configTest('trader');
verifierMock.config = configTest('verifier');
infoMock.config = configTest('info');
walletMock.config = configTest('wallet')
tickerMock.config = configTest('ticker')
traderMock.config = configTest('trader')
verifierMock.config = configTest('verifier')
infoMock.config = configTest('info')
plugins.configure(config);
});
plugins.configure(config)
})
['wallet', 'ticker', 'trader', 'verifier', 'info'].forEach(function(name) {
it('should configure ' + name, function() {
confList.should.have.property(name);
should.exist(confList[name]);
;['wallet', 'ticker', 'trader', 'verifier', 'info'].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.getConfig();
should.exist(config);
/* jshint expr: true */
config.should.be.an.Object;
confList[name].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);
it('should return config', function () {
var config = plugins.getConfig()
should.exist(config)
/* jshint expr: true */
rate.should.be.an.Object;
config.should.be.an.Object
/* jshint expr: false */
rate.should.have.property('currency');
rate.should.have.property('rates');
})
})
var rates = rate.rates;
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;
rate.should.be.an.Object
/* jshint expr: false */
rates.should.have.property('ask');
rates.should.have.property('bid');
});
});
rate.should.have.property('currency')
rate.should.have.property('rates')
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);
var rates = rate.rates
/* jshint expr: true */
balance.should.be.an.Object;
rate.should.be.an.Object
/* jshint expr: false */
balance.should.have.property('BTC');
balance.BTC.should.equal(1e8);
});
rates.should.have.property('ask')
rates.should.have.property('bid')
})
})
it('should return fiat balance', function() {
var fiatBalance = plugins.fiatBalance();
should.exist(fiatBalance);
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 */
fiatBalance.should.be.a.Number;
balance.should.be.an.Object
/* jshint expr: false */
fiatBalance.should.be.below(999);
});
balance.should.have.property('BTC')
balance.BTC.should.equal(1e8)
})
describe('Send Bitcoins', function() {
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)
})
before(function() {
plugins.trade({currency: 'USD', satoshis: 1e7}, db.FINGERPRINT_NEW);
});
describe('Send Bitcoins', function () {
before(function () {
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 = {
txId: 1,
toAddress: walletMock.ADDR,
satoshis: 1e7
};
}
plugins.sendBitcoins(db.FINGERPRINT_NEW, txDetails, function(err,
response) {
should.not.exist(err);
should.exist(response);
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);
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();
});
}
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;
// db.wasErrorReported().should.be.true
// /* jshint expr: false */
// done();
// });
// });
// 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();
// });
// });
});
// /* jshint expr: true */
// db.wasErrorReported().should.be.false // should not report error again
// /* jshint expr: false */
// done()
// })
// })
})
})
});
describe('Trader', function() {});
});
describe('Trader', function () {})
})