move mocks to tests only

This commit is contained in:
Manuel Araoz 2015-03-06 17:03:38 -03:00
parent e53beed522
commit c86b7c4277
5 changed files with 24 additions and 17 deletions

View File

@ -5,10 +5,6 @@ var _ = bitcore.deps._;
var $ = bitcore.util.preconditions; var $ = bitcore.util.preconditions;
var Block = bitcore.Block; var Block = bitcore.Block;
// mocks
var mockBlocks = require('../test/data/blocks');
var Blocks = {}; var Blocks = {};
var node; var node;
@ -25,8 +21,7 @@ Blocks.setNode = function(aNode) {
* Finds a block by its hash * Finds a block by its hash
*/ */
Blocks.blockHashParam = function(req, res, next, blockHash) { Blocks.blockHashParam = function(req, res, next, blockHash) {
// TODO: fetch block from service var block = node.getBlock(blockHash);
var block = mockBlocks[blockHash];
if (_.isUndefined(block)) { if (_.isUndefined(block)) {
res.status(404).send('Block with id ' + blockHash + ' not found'); res.status(404).send('Block with id ' + blockHash + ' not found');
@ -40,9 +35,8 @@ Blocks.blockHashParam = function(req, res, next, blockHash) {
* Finds a block by its height * Finds a block by its height
*/ */
Blocks.heightParam = function(req, res, next, height) { Blocks.heightParam = function(req, res, next, height) {
// TODO: fetch block from service
height = parseInt(height); height = parseInt(height);
var block = mockBlocks[Object.keys(mockBlocks)[height]]; var block = node.getBlock(height);
if (_.isUndefined(block)) { if (_.isUndefined(block)) {
res.status(404).send('Block with height ' + height + ' not found'); res.status(404).send('Block with height ' + height + ' not found');
@ -58,7 +52,7 @@ Blocks.heightParam = function(req, res, next, height) {
*/ */
Blocks.getLatest = function(req, res) { Blocks.getLatest = function(req, res) {
req.block = mockBlocks[Object.keys(mockBlocks).splice(-1)[0]]; req.block = node.getLatestBlock();
Blocks.get(req, res); Blocks.get(req, res);
}; };

View File

@ -5,8 +5,6 @@ var _ = bitcore.deps._;
var $ = bitcore.util.preconditions; var $ = bitcore.util.preconditions;
var Transaction = bitcore.Transaction; var Transaction = bitcore.Transaction;
var mockTransactions = require('../test/data/transactions');
var Transactions = {}; var Transactions = {};
var node; var node;
@ -23,8 +21,7 @@ Transactions.setNode = function(aNode) {
* Finds a transaction by its hash * Finds a transaction by its hash
*/ */
Transactions.txHashParam = function(req, res, next, txHash) { Transactions.txHashParam = function(req, res, next, txHash) {
// TODO: fetch tx from service var tx = node.getTransaction(txHash);
var tx = mockTransactions[txHash];
if (_.isUndefined(tx)) { if (_.isUndefined(tx)) {
res.status(404).send('Transaction with id ' + txHash + ' not found'); res.status(404).send('Transaction with id ' + txHash + ' not found');
@ -43,6 +40,12 @@ Transactions.get = function(req, res) {
$.checkState(req.tx instanceof Transaction); $.checkState(req.tx instanceof Transaction);
res.send(req.tx.toObject()); res.send(req.tx.toObject());
}; };
Transactions.send = function(req, res) {
var tx = new Transaction(req.body);
node.broadcast(tx);
};
Transactions.getTxError = function(req, res) { Transactions.getTxError = function(req, res) {
res.status(422); res.status(422);
res.send('/v1/transactions/ parameter must be a 64 digit hex'); res.send('/v1/transactions/ parameter must be a 64 digit hex');

View File

@ -36,10 +36,7 @@ function initRouter(node) {
// Transaction routes // Transaction routes
router.get('/transactions', mockResponse); router.get('/transactions', mockResponse);
router.get('/transactions/:txHash([A-Fa-f0-9]{64})', Transactions.get); router.get('/transactions/:txHash([A-Fa-f0-9]{64})', Transactions.get);
router.get('/transactions/:txHash([A-Fa-f0-9]{64})/addresses', mockResponse); router.post('/transactions/send', Transactions.send);
router.get('/transactions/:txHash([A-Fa-f0-9]{64})/outputs/addresses', mockResponse);
router.get('/transactions/:txHash([A-Fa-f0-9]{64})/inputs/addresses', mockResponse);
router.post('/transactions/send', mockResponse);
// Input routes // Input routes
router.get('/transactions/:txHash/inputs', mockResponse); router.get('/transactions/:txHash/inputs', mockResponse);

View File

@ -17,6 +17,16 @@ describe('BitcoreHTTP v1 blocks routes', function() {
var nodeMock, app, agent; var nodeMock, app, agent;
beforeEach(function() { beforeEach(function() {
nodeMock = new EventEmitter(); nodeMock = new EventEmitter();
nodeMock.getBlock = function(blockHash) {
if (typeof blockHash === 'number') {
var height = blockHash;
return mockBlocks[Object.keys(mockBlocks)[height]];
}
return mockBlocks[blockHash];
};
nodeMock.getLatestBlock = function() {
return mockBlocks[Object.keys(mockBlocks).splice(-1)[0]];
};
app = new BitcoreHTTP(nodeMock).app; app = new BitcoreHTTP(nodeMock).app;
agent = request(app); agent = request(app);
}); });

View File

@ -15,6 +15,9 @@ describe('BitcoreHTTP v1 transactions routes', function() {
var nodeMock, app, agent; var nodeMock, app, agent;
beforeEach(function() { beforeEach(function() {
nodeMock = new EventEmitter(); nodeMock = new EventEmitter();
nodeMock.getTransaction = function(txHash) {
return mockTransactions[txHash];
};
app = new BitcoreHTTP(nodeMock).app; app = new BitcoreHTTP(nodeMock).app;
agent = request(app); agent = request(app);
}); });