diff --git a/index.js b/index.js index ef4bd3d..def6ea5 100644 --- a/index.js +++ b/index.js @@ -12,17 +12,17 @@ const config = require('./config'); const bot = require('./bot'); var express = require('express'), -cors = require('cors'), -helmet = require('helmet'), -app = express(), -bodyParser = require('body-parser'), -jsonParser = bodyParser.json(); + cors = require('cors'), + helmet = require('helmet'), + app = express(), + bodyParser = require('body-parser'), + jsonParser = bodyParser.json(); app.use(cors()); app.use(helmet()); // Receive a POST request at the url specified by an env. var. -app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) { +app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) { if (!req.body || !req.body.action) { bot.error('', 'Wrong format'); return res.sendStatus(400); @@ -35,7 +35,7 @@ app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) { }, config.delayInMiliSeconds); }); -const processRequest = function(req, res) { +const processRequest = function (req, res) { const eth = bot.eth; const from = config.sourceAddress; const to = bot.getAddress(req); @@ -45,25 +45,25 @@ const processRequest = function(req, res) { const gasPricePromise = bot.getGasPrice(); Promise.all([amountPromise, gasPricePromise]) - .then(function(amount, gasPrice){ - let transaction = sendTransaction(eth, from, to, amount, gasPrice); - - transaction - .then(function() { - return res.sendStatus(200); + .then(function (amount, gasPrice) { + let transaction = sendTransaction(eth, from, to, amount, gasPrice); + + transaction + .then(function () { + return res.sendStatus(200); + }) + .catch(function (error) { + bot.error(req.body, error); + }); + }) - .catch(function(error) { + .catch(function (error) { bot.error(req.body, error); }); - - }) - .catch(function(error) { - bot.error(req.body, error); - }); } -const sendTransaction = function(eth, from, to, amount, gasPrice){ - if (!config.debug){ +const sendTransaction = function (eth, from, to, amount, gasPrice) { + if (!config.debug) { eth.getTransactionCount(from, (err, nonce) => { eth.sendTransaction({ @@ -91,6 +91,6 @@ const sendTransaction = function(eth, from, to, amount, gasPrice){ } const port = process.env.PORT || 8181 -app.listen(port, function(){ +app.listen(port, function () { bot.log('Autobounty listening on port', port); }); diff --git a/test/test_bot.js b/test/test_bot.js index e356e37..54bbcaa 100644 --- a/test/test_bot.js +++ b/test/test_bot.js @@ -9,58 +9,58 @@ let sob_comment = 'Current balance: 0.000000 ETH\nTokens: SNT: 2500.00 ANT: 25.0 // Fake requests let requests = [ - {body: {action: 'created', comment: {body: 'Creating my first comment', user: {login: 'jomsdev'}}}}, - {body: {action: 'edited', comment: {body: 'Editing my comment', user: {login: 'jomsdev'}}}}, - {body: {action: 'edited', comment: {body: sob_comment, user: {login: 'status-open-bounty'}}}}, - {body: {action: 'created', comment: {body: sob_comment, user: {login: 'status-open-bounty'}}}} + { body: { action: 'created', comment: { body: 'Creating my first comment', user: { login: 'jomsdev' } } } }, + { body: { action: 'edited', comment: { body: 'Editing my comment', user: { login: 'jomsdev' } } } }, + { body: { action: 'edited', comment: { body: sob_comment, user: { login: 'status-open-bounty' } } } }, + { body: { action: 'created', comment: { body: sob_comment, user: { login: 'status-open-bounty' } } } } ]; -describe('Bot behavior', function() { - describe('#needsFunding()', function() { - it('should return false because the comment is not from status-open-bounty', function() { - assert.isFalse(bot.needsFunding(requests[0])); +describe('Bot behavior', function () { + describe('#needsFunding()', function () { + it('should return false because the comment is not from status-open-bounty', function () { + assert.isFalse(bot.needsFunding(requests[0])); + }); + it('should return false because a user is editing a comment', function () { + assert.isFalse(bot.needsFunding(requests[1])); + }); + it('should return false because status-open-bounty edited a comment', function () { + assert.isFalse(bot.needsFunding(requests[2])); + }); + it('should return true, it is all right and we should fund', function () { + assert.isTrue(bot.needsFunding(requests[3])); + }); }); - it('should return false because a user is editing a comment', function() { - assert.isFalse(bot.needsFunding(requests[1])); - }); - it('should return false because status-open-bounty edited a comment', function() { - assert.isFalse(bot.needsFunding(requests[2])); - }); - it('should return true, it is all right and we should fund', function() { - assert.isTrue(bot.needsFunding(requests[3])); - }); - }); - describe('#getAddress', function() { - it('should return the address from a status-open-bounty bot comment', function() { - assert.equal(bot.getAddress(requests[3]),'0x3645fe42b1a744ad98cc032c22472388806f86f9'); + describe('#getAddress', function () { + it('should return the address from a status-open-bounty bot comment', function () { + assert.equal(bot.getAddress(requests[3]), '0x3645fe42b1a744ad98cc032c22472388806f86f9'); + }); }); - }); - // TODO: test getAmount which involves call to github and bounty tags - describe('#getAmount', function() { - it('should fail and log that there is no bounty label for this issue', function() { - // Code + // TODO: test getAmount which involves call to github and bounty tags + describe('#getAmount', function () { + it('should fail and log that there is no bounty label for this issue', function () { + // Code + }); + it('should fail and log that there are more than one bounty labels for this issue', function () { + // Code + }); + it('should return the amount for the issue given the price per hour and the bounty label for this issue', function () { + // Code + }); }); - it('should fail and log that there are more than one bounty labels for this issue', function() { - // Code - }); - it('should return the amount for the issue given the price per hour and the bounty label for this issue', function() { - // Code - }); - }); // TODO: test getLabel which involves call to github and bounty tags - describe('#getGasPrice', function() { - it('should go to the gasStation and comeback with a reasonable number', function() { + describe('#getGasPrice', function () { + it('should go to the gasStation and comeback with a reasonable number', function () { bot.getGasPrice() - .then(function(gasPrice) { - assert.isNumber(gasPrice); - }) - .catch(function() { - assert.fail(); - }) + .then(function (gasPrice) { + assert.isNumber(gasPrice); + }) + .catch(function () { + assert.fail(); + }) }); - }); + }); });