diff --git a/api/balances.js b/api/balances.js index cf041711..4c500480 100644 --- a/api/balances.js +++ b/api/balances.js @@ -4,6 +4,7 @@ var ripple = require('ripple-lib'); var remote = require('./../lib/remote.js'); var respond = require('../lib/response-handler.js'); var errors = require('./../lib/errors.js'); +var validator = require('./../lib/schema-validator.js'); module.exports = { get: getBalances @@ -42,6 +43,8 @@ function getBalances(request, response, next) { var balances = []; var nextMarker; var responseLimit; + var responseLedger; + var responseValidated; function validateOptions(callback) { if (!ripple.UInt160.is_valid(options.account)) { @@ -57,7 +60,12 @@ function getBalances(request, response, next) { }; function getXRPBalance(callback) { - var accountInfoRequest = remote.requestAccountInfo({account: options.account}); + var ledger = validator.isValid(request.query.ledger, 'UINT32') ? Number(request.query.ledger) : 'validated'; + + var accountInfoRequest = remote.requestAccountInfo({ + account: options.account, + ledger: ledger + }); accountInfoRequest.once('error', callback); accountInfoRequest.once('success', function(info) { @@ -67,6 +75,9 @@ function getBalances(request, response, next) { counterparty: '' }); + responseLedger = info.ledger_index; + responseValidated = info.validated; + callback(); }); @@ -76,8 +87,8 @@ function getBalances(request, response, next) { function getLineBalances(callback) { var accountLinesRequest; var marker = request.query.marker; - var limit = /^[0-9]*$/.test(request.query.limit) ? Number(request.query.limit) : void(0); - var ledger = /^[0-9]*$/.test(request.query.ledger) ? Number(request.query.ledger) : void(0); + var limit = validator.isValid(request.query.limit, 'UINT32') ? Number(request.query.limit) : void(0); + var ledger = validator.isValid(request.query.ledger, 'UINT32') ? Number(request.query.ledger) : 'validated'; try { accountLinesRequest = remote.requestAccountLines({ @@ -114,6 +125,9 @@ function getBalances(request, response, next) { responseLimit = result.limit; } + responseLedger = result.ledger_index; + responseValidated = result.validated; + callback(); }); @@ -136,7 +150,13 @@ function getBalances(request, response, next) { if (error) { next(error); } else { - respond.success(response, { marker: nextMarker, limit: responseLimit, balances: balances }); + respond.success(response, { + marker: nextMarker, + limit: responseLimit, + ledger: responseLedger, + validated: responseValidated, + balances: balances + }); } }); }; diff --git a/api/orders.js b/api/orders.js index dcd2c36f..1780e5e1 100644 --- a/api/orders.js +++ b/api/orders.js @@ -7,6 +7,7 @@ var SubmitTransactionHooks = require('./../lib/submit_transaction_hooks.js'); var respond = require('./../lib/response-handler.js'); var utils = require('./../lib/utils'); var errors = require('./../lib/errors.js'); +var validator = require('./../lib/schema-validator.js'); const InvalidRequestError = errors.InvalidRequestError; @@ -34,6 +35,16 @@ function getOrders(request, response, next) { orders.marker = result.marker; } + if (result.limit) { + orders.limit = result.limit; + } + + if (result.ledger_index) { + orders.ledger = result.ledger_index; + } + + orders.validated = result.validated; + orders.orders = result.offers; respond.success(response, orders); @@ -56,8 +67,8 @@ function getOrders(request, response, next) { var promise = new Promise(function(resolve, reject) { var accountOrdersRequest; var marker = request.query.marker; - var limit = /^[0-9]*$/.test(request.query.limit) ? Number(request.query.limit) : void(0); - var ledger = /^[0-9]*$/.test(request.query.ledger) ? Number(request.query.ledger) : void(0); + var limit = validator.isValid(request.query.limit, 'UINT32') ? Number(request.query.limit) : void(0); + var ledger = validator.isValid(request.query.ledger, 'UINT32') ? Number(request.query.ledger) : 'validated'; accountOrdersRequest = remote.requestAccountOffers({ account: options.account, @@ -248,4 +259,4 @@ module.exports = { getOrders: getOrders, placeOrder: placeOrder, cancelOrder: cancelOrder -}; \ No newline at end of file +}; diff --git a/api/trustlines.js b/api/trustlines.js index 3a8b540f..7642ce6d 100644 --- a/api/trustlines.js +++ b/api/trustlines.js @@ -81,8 +81,8 @@ function getTrustLines(request, response, next) { function getAccountLines(callback) { var accountLinesRequest; var marker = request.query.marker; - var limit = /^[0-9]*$/.test(request.query.limit) ? Number(request.query.limit) : void(0); - var ledger = /^[0-9]*$/.test(request.query.ledger) ? Number(request.query.ledger) : void(0); + var limit = validator.isValid(request.query.limit, 'UINT32') ? Number(request.query.limit) : void(0); + var ledger = validator.isValid(request.query.ledger, 'UINT32') ? Number(request.query.ledger) : 'validated'; try { accountLinesRequest = remote.requestAccountLines({ @@ -127,6 +127,12 @@ function getTrustLines(request, response, next) { trustlines.limit = result.limit; } + if (result.ledger_index) { + trustlines.ledger = result.ledger_index; + } + + trustlines.validated = result.validated + trustlines.trustlines = lines; callback(null, trustlines); diff --git a/test/_payments-test.js b/test/_payments-test.js index 7a5756c8..3108a7f2 100644 --- a/test/_payments-test.js +++ b/test/_payments-test.js @@ -226,7 +226,8 @@ suite('payments', function() { delete data.id; assert.deepEqual(data, { command: 'account_info', - account: 'rJRLoJSErtNRFnbCyHEUYnRUKNwkVYDM7U' + account: 'rJRLoJSErtNRFnbCyHEUYnRUKNwkVYDM7U', + ledger_index: 'validated' }); orderlist.mark('account_info'); }; @@ -235,7 +236,8 @@ suite('payments', function() { delete data.id; assert.deepEqual(data, { command: 'account_lines', - account: 'rJRLoJSErtNRFnbCyHEUYnRUKNwkVYDM7U' + account: 'rJRLoJSErtNRFnbCyHEUYnRUKNwkVYDM7U', + ledger_index: 'validated' }); orderlist.mark('account_lines'); }; @@ -522,7 +524,8 @@ suite('payments', function() { delete data.id; assert.deepEqual(data, { command: 'account_info', - account: 'rwmityd4Ss34DBUsRy7Pacv6UA5n7yjfe5' + account: 'rwmityd4Ss34DBUsRy7Pacv6UA5n7yjfe5', + ledger_index: 'validated' }); orderlist.mark('account_info'); }; @@ -531,7 +534,8 @@ suite('payments', function() { delete data.id; assert.deepEqual(data, { command: 'account_lines', - account: 'rwmityd4Ss34DBUsRy7Pacv6UA5n7yjfe5' + account: 'rwmityd4Ss34DBUsRy7Pacv6UA5n7yjfe5', + ledger_index: 'validated' }); orderlist.mark('account_lines'); }; diff --git a/test/balances-test.js b/test/balances-test.js index 2589ec2f..a766ef0f 100644 --- a/test/balances-test.js +++ b/test/balances-test.js @@ -8,6 +8,7 @@ var requestPath = fixtures.requestPath; const MARKER = '29F992CC252056BF690107D1E8F2D9FBAFF29FF107B62B1D1F4E4E11ADF2CC73'; const NEXT_MARKER = '0C812C919D343EAE789B29E8027C62C5792C22172D37EA2B2C0121D2381F80E1'; +const LEDGER = 9592219; const LIMIT = 5; suite('get balances', function() { @@ -29,6 +30,7 @@ suite('get balances', function() { self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); + assert.strictEqual(message.ledger_index, 'validated'); conn.send(fixtures.accountLinesResponse(message)); }); @@ -50,7 +52,7 @@ suite('get balances', function() { self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger, void(0)); + assert.strictEqual(message.ledger_index, 'validated'); conn.send(fixtures.accountLinesResponse(message)); }); @@ -72,22 +74,53 @@ suite('get balances', function() { self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); + assert.strictEqual(message.ledger_index, LEDGER); conn.send(fixtures.accountLinesResponse(message, { - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER })); }); self.app - .get(requestPath(addresses.VALID, '?ledger=9592219')) + .get(requestPath(addresses.VALID, '?ledger=' + LEDGER)) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountBalancesResponse({ - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER }))) .end(done); }); + test('/accounts/:account/balances -- with non-validated ledger', function(done) { + self.wss.once('request_account_info', function(message, conn) { + assert.strictEqual(message.command, 'account_info'); + assert.strictEqual(message.account, addresses.VALID); + conn.send(fixtures.accountInfoResponse(message)); + }); + + self.wss.once('request_account_lines', function(message, conn) { + assert.strictEqual(message.command, 'account_lines'); + assert.strictEqual(message.account, addresses.VALID); + assert.strictEqual(message.ledger_index, LEDGER); + conn.send(fixtures.accountLinesResponse(message, { + marker: NEXT_MARKER, + ledger: LEDGER, + validated: false + })); + }); + + self.app + .get(requestPath(addresses.VALID, '?ledger=' + LEDGER)) + .expect(testutils.checkStatus(200)) + .expect(testutils.checkHeaders) + .expect(testutils.checkBody(fixtures.RESTAccountBalancesResponse({ + marker: NEXT_MARKER, + ledger: LEDGER, + validated: false + }))) + .end(done); + }); test('/accounts/:account/balances -- with invalid marker', function(done) { self.wss.once('request_account_info', function(message, conn) { assert.strictEqual(message.command, 'account_info'); @@ -158,16 +191,18 @@ suite('get balances', function() { assert.strictEqual(message.ledger_index, 9592219); assert.strictEqual(message.marker, MARKER); conn.send(fixtures.accountLinesResponse(message, { - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER })); }); self.app - .get(requestPath(addresses.VALID, '?marker=' + MARKER + '&ledger=9592219')) + .get(requestPath(addresses.VALID, '?marker=' + MARKER + '&ledger=' + LEDGER)) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountBalancesResponse({ - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER }))) .end(done); }); @@ -182,20 +217,22 @@ suite('get balances', function() { self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); - assert.strictEqual(message.limit, 5); + assert.strictEqual(message.ledger_index, LEDGER); + assert.strictEqual(message.limit, LIMIT); conn.send(fixtures.accountLinesResponse(message, { - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER })); }); self.app - .get(requestPath(addresses.VALID, '?ledger=9592219&limit=' + LIMIT)) + .get(requestPath(addresses.VALID, '?ledger=' + LEDGER + '&limit=' + LIMIT)) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountBalancesResponse({ marker: NEXT_MARKER, - limit: LIMIT + limit: LIMIT, + ledger: LEDGER }))) .end(done); }); @@ -230,21 +267,23 @@ suite('get balances', function() { self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); assert.strictEqual(message.limit, LIMIT); + assert.strictEqual(message.ledger_index, LEDGER); assert.strictEqual(message.marker, MARKER); conn.send(fixtures.accountLinesResponse(message, { - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER })); }); self.app - .get(requestPath(addresses.VALID, '?marker=' + MARKER + '&limit=' + LIMIT + '&ledger=9592219')) + .get(requestPath(addresses.VALID, '?marker=' + MARKER + '&limit=' + LIMIT + '&ledger=' + LEDGER)) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountBalancesResponse({ marker: NEXT_MARKER, - limit: LIMIT + limit: LIMIT, + ledger: LEDGER }))) .end(done); }); @@ -296,14 +335,19 @@ suite('get balances', function() { self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountLinesResponse(message)); + assert.strictEqual(message.ledger_index, 'validated'); + conn.send(fixtures.accountLinesResponse(message, { + ledger: LEDGER + })); }); self.app .get(requestPath(addresses.VALID, '?currency=USD')) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) - .expect(testutils.checkBody(fixtures.RESTAccountBalancesUSDResponse)) + .expect(testutils.checkBody(fixtures.RESTAccountBalancesUSDResponse({ + ledger: LEDGER + }))) .end(done); }); @@ -311,7 +355,10 @@ suite('get balances', function() { self.wss.once('request_account_info', function(message, conn) { assert.strictEqual(message.command, 'account_info'); assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); + assert.strictEqual(message.ledger_index, 'validated'); + conn.send(fixtures.accountInfoResponse(message,{ + ledger: LEDGER + })); }); self.wss.once('request_account_lines', function(message, conn) { @@ -322,7 +369,9 @@ suite('get balances', function() { .get(requestPath(addresses.VALID, '?currency=XRP')) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) - .expect(testutils.checkBody(fixtures.RESTAccountBalancesXRPResponse)) + .expect(testutils.checkBody(fixtures.RESTAccountBalancesXRPResponse({ + ledger: LEDGER + }))) .end(done); }); @@ -352,14 +401,21 @@ suite('get balances', function() { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); assert.strictEqual(message.peer, addresses.COUNTERPARTY); - conn.send(fixtures.accountLinesCounterpartyResponse(message)); + assert.strictEqual(message.ledger_index, 'validated'); + conn.send(fixtures.accountLinesCounterpartyResponse(message,{ + marker: NEXT_MARKER, + ledger: LEDGER + })); }); self.app .get(requestPath(addresses.VALID, '?counterparty=' + addresses.COUNTERPARTY)) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) - .expect(testutils.checkBody(fixtures.RESTAccountBalancesCounterpartyResponse)) + .expect(testutils.checkBody(fixtures.RESTAccountBalancesCounterpartyResponse({ + marker: NEXT_MARKER, + ledger: LEDGER + }))) .end(done); }); @@ -400,7 +456,7 @@ suite('get balances', function() { .end(done); }); - test('/accounts/:account/balances?counterparty¤cy', function(done) { + test('/accounts/:account/balances?counterparty¤cy=EUR', function(done) { self.wss.once('request_account_info', function(message, conn) { assert(false, 'Should not request account info'); }); @@ -409,14 +465,20 @@ suite('get balances', function() { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); assert.strictEqual(message.peer, addresses.COUNTERPARTY); - conn.send(fixtures.accountLinesCounterpartyResponse(message)); + conn.send(fixtures.accountLinesCounterpartyResponse(message,{ + marker: NEXT_MARKER, + ledger: LEDGER + })); }); self.app .get(requestPath(addresses.VALID, '?counterparty=' + addresses.COUNTERPARTY + '¤cy=EUR')) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) - .expect(testutils.checkBody(fixtures.RESTAccountBalancesCounterpartyCurrencyResponse)) + .expect(testutils.checkBody(fixtures.RESTAccountBalancesCounterpartyCurrencyResponse({ + marker: NEXT_MARKER, + ledger: LEDGER + }))) .end(done); }); }); diff --git a/test/fixtures/balances.js b/test/fixtures/balances.js index f1156c54..634e21c4 100644 --- a/test/fixtures/balances.js +++ b/test/fixtures/balances.js @@ -1,10 +1,17 @@ +var _ = require('lodash'); var addresses = require('./addresses'); module.exports.requestPath = function(address, params) { return '/v1/accounts/' + address + '/balances' + ( params || '' ); }; -module.exports.accountInfoResponse = function(request) { +module.exports.accountInfoResponse = function(request, options) { + options = options || {}; + + _.defaults(options, { + validated: true + }); + return JSON.stringify({ id: request.id, status: 'success', @@ -26,7 +33,8 @@ module.exports.accountInfoResponse = function(request) { index: '396400950EA27EB5710C0D5BE1D2B4689139F168AC5D07C13B8140EC3F82AE71', urlgravatar: 'http://www.gravatar.com/avatar/23463b99b62a72f26ed677cc556c44e8' }, - ledger_current_index: 6614628 + ledger_index: options.ledger, + validated: options.validated } }); }; @@ -53,6 +61,10 @@ module.exports.accountNotFoundResponse = function(request) { module.exports.accountLinesResponse = function(request, options) { var options = options || {}; + _.defaults(options, { + validated: true + }); + return JSON.stringify({ id: request.id, status: 'success', @@ -61,6 +73,8 @@ module.exports.accountLinesResponse = function(request, options) { account: addresses.VALID, marker: options.marker, limit: request.limit, + ledger_index: options.ledger, + validated: options.validated, lines: [ { account: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z', @@ -299,13 +313,23 @@ module.exports.accountLinesResponse = function(request, options) { }); }; -module.exports.accountLinesCounterpartyResponse = function(request) { +module.exports.accountLinesCounterpartyResponse = function(request, options) { + var options = options || {}; + + _.defaults(options, { + validated: true + }); + return JSON.stringify({ id: request.id, status: 'success', type: 'response', result: { account: addresses.VALID, + marker: options.marker, + limit: request.limit, + ledger_index: options.ledger, + validated: options.validated, lines: [ { account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', @@ -374,10 +398,16 @@ module.exports.accountLinesNoCounterpartyResponse = function(request) { module.exports.RESTAccountBalancesResponse = function(options) { options = options || {}; + _.defaults(options, { + validated: true + }); + return JSON.stringify({ success: true, marker: options.marker, limit: options.limit, + ledger: options.ledger, + validated: options.validated, balances: [ { value: '922.913243', currency: 'XRP', counterparty: '' }, { value: '0', currency: 'ASP', counterparty: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z' }, @@ -408,45 +438,93 @@ module.exports.RESTAccountBalancesResponse = function(options) { }); }; -module.exports.RESTAccountBalancesUSDResponse = JSON.stringify({ - success: true, - balances: [ - { value: '2.497605752725159', currency: 'USD', counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q' }, - { value: '0', currency: 'USD', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, - { value: '1', currency: 'USD', counterparty: 'rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun' }, - { value: '0', currency: 'USD', counterparty: 'r9vbV3EHvXWjSkeQ6CAcYVPGeq7TuiXY2X' }, - { value: '35', currency: 'USD', counterparty: 'rfF3PNkwkq1DygW2wum2HK3RGfgkJjdPVD' }, - { value: '0', currency: 'USD', counterparty: 'rE6R3DWF9fBD7CyiQciePF9SqK58Ubp8o2' }, - { value: '0', currency: 'USD', counterparty: 'rEhDDUUNxpXgEHVJtC2cjXAgyx5VCFxdMF' } - ] -}); +module.exports.RESTAccountBalancesUSDResponse = function(options) { + options = options || {}; -module.exports.RESTAccountBalancesXRPResponse = JSON.stringify({ - success: true, - balances: [ - { value: '922.913243', currency: 'XRP', counterparty: '' } - ] -}); + _.defaults(options, { + validated: true + }); -module.exports.RESTAccountBalancesCounterpartyResponse = JSON.stringify({ - success: true, - balances: [ - { value: '0.3488146605801446', currency: 'CHF', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, - { value: '2.114103174931847', currency: 'BTC', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, - { value: '0', currency: 'USD', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, - { value: '7.292695098901099', currency: 'JPY', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, - { value: '12.41688780720394', currency: 'EUR', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' } - ] -}); + return JSON.stringify({ + + success: true, + marker: options.marker, + limit: options.limit, + ledger: options.ledger, + validated: options.validated, + balances: [ + { value: '2.497605752725159', currency: 'USD', counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q' }, + { value: '0', currency: 'USD', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, + { value: '1', currency: 'USD', counterparty: 'rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun' }, + { value: '0', currency: 'USD', counterparty: 'r9vbV3EHvXWjSkeQ6CAcYVPGeq7TuiXY2X' }, + { value: '35', currency: 'USD', counterparty: 'rfF3PNkwkq1DygW2wum2HK3RGfgkJjdPVD' }, + { value: '0', currency: 'USD', counterparty: 'rE6R3DWF9fBD7CyiQciePF9SqK58Ubp8o2' }, + { value: '0', currency: 'USD', counterparty: 'rEhDDUUNxpXgEHVJtC2cjXAgyx5VCFxdMF' } + ] + }); +}; + +module.exports.RESTAccountBalancesXRPResponse = function(options) { + options = options || {}; + + _.defaults(options, { + validated: true + }); + + return JSON.stringify({ + success: true, + limit: options.limit, + ledger: options.ledger, + validated: options.validated, + balances: [ + { value: '922.913243', currency: 'XRP', counterparty: '' } + ] + }); +}; + +module.exports.RESTAccountBalancesCounterpartyResponse = function(options) { + options = options || {}; + + _.defaults(options, { + validated: true + }); + + return JSON.stringify({ + success: true, + marker: options.marker, + limit: options.limit, + ledger: options.ledger, + validated: options.validated, + balances: [ + { value: '0.3488146605801446', currency: 'CHF', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, + { value: '2.114103174931847', currency: 'BTC', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, + { value: '0', currency: 'USD', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, + { value: '7.292695098901099', currency: 'JPY', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' }, + { value: '12.41688780720394', currency: 'EUR', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' } + ] + }); +}; module.exports.RESTAccountBalancesNoCounterpartyResponse = JSON.stringify({ success: true, balances: [ ] }); -module.exports.RESTAccountBalancesCounterpartyCurrencyResponse = JSON.stringify({ - success: true, - balances: [ - { value: '12.41688780720394', currency: 'EUR', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' } - ] -}); +module.exports.RESTAccountBalancesCounterpartyCurrencyResponse = function(options) { + options = options || {}; + + _.defaults(options, { + validated: true + }); + + return JSON.stringify({ + success: true, + marker: options.marker, + limit: options.limit, + ledger: options.ledger, + validated: options.validated, + balances: [ + { value: '12.41688780720394', currency: 'EUR', counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' } + ] + }); +}; diff --git a/test/fixtures/orders.js b/test/fixtures/orders.js index b0d10c0d..71d09b02 100644 --- a/test/fixtures/orders.js +++ b/test/fixtures/orders.js @@ -46,8 +46,10 @@ module.exports.order = function(options) { module.exports.accountOrdersResponse = function(request, options) { options = options || {}; + _.defaults(options, { - account: addresses.VALID + account: addresses.VALID, + validated: true }); return JSON.stringify({ @@ -55,7 +57,8 @@ module.exports.accountOrdersResponse = function(request, options) { "result": { "account": options.account, "marker": options.marker, - "ledger_index": 9941609, + "limit": options.limit, + "ledger_index": options.ledger, "offers": [ { "flags": 0, @@ -288,7 +291,7 @@ module.exports.accountOrdersResponse = function(request, options) { } } ], - "validated": false + "validated": options.validated }, "status": "success", "type": "response" @@ -693,9 +696,16 @@ module.exports.unfundedOrderFinalizedResponse = function(options) { module.exports.RESTAccountOrdersResponse = function(options) { options = options || {}; + _.defaults(options, { + validated: true + }); + return JSON.stringify({ success: true, marker: options.marker, + limit: options.limit, + ledger: options.ledger, + validated: options.validated, orders: [ { "flags": 0, diff --git a/test/fixtures/trustlines.js b/test/fixtures/trustlines.js index cdcb181e..3ed66404 100644 --- a/test/fixtures/trustlines.js +++ b/test/fixtures/trustlines.js @@ -7,6 +7,10 @@ module.exports.requestPath = function(address, params) { module.exports.accountLinesResponse = function(request, options) { options = options || {}; + _.defaults(options, { + validated: true + }); + return JSON.stringify({ id: request.id, status: 'success', @@ -15,6 +19,8 @@ module.exports.accountLinesResponse = function(request, options) { account: 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE', marker: options.marker, limit: request.limit, + ledger_index: options.ledger, + validated: options.validated, lines: [ { account: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z', @@ -258,10 +264,17 @@ module.exports.accountLinesResponse = function(request, options) { module.exports.RESTAccountTrustlinesResponse = function(options) { options = options || {}; + _.defaults(options, { + validated: true + }); + + return JSON.stringify({ success: true, marker: options.marker, limit: options.limit, + ledger: options.ledger, + validated: options.validated, trustlines: [ { account: 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE', counterparty: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z', diff --git a/test/orders-test.js b/test/orders-test.js index 9c02744b..1bf95ecd 100644 --- a/test/orders-test.js +++ b/test/orders-test.js @@ -11,8 +11,11 @@ const HEX_CURRENCY = '0158415500000000C1F76FF6ECB0BAC600000000'; const ISSUER = 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' const VALUE = '0.00000001' +const LIMIT = 100; + const MARKER = '29F992CC252056BF690107D1E8F2D9FBAFF29FF107B62B1D1F4E4E11ADF2CC73'; const NEXT_MARKER = '0C812C919D343EAE789B29E8027C62C5792C22172D37EA2B2C0121D2381F80E1'; +const LEDGER = 9592219; suite('get orders', function() { var self = this; @@ -21,40 +24,37 @@ suite('get orders', function() { teardown(testutils.teardown.bind(self)); test('/accounts/:account/orders', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert.strictEqual(message.command, 'account_offers'); assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountOrdersResponse(message)); + assert.strictEqual(message.ledger_index, 'validated'); + conn.send(fixtures.accountOrdersResponse(message,{ + ledger: LEDGER, + marker: NEXT_MARKER, + limit: LIMIT + })); }); self.app .get('/v1/accounts/' + addresses.VALID + '/orders') .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) - .expect(testutils.checkBody(fixtures.RESTAccountOrdersResponse())) + .expect(testutils.checkBody(fixtures.RESTAccountOrdersResponse({ + ledger: LEDGER, + marker: NEXT_MARKER, + limit: LIMIT + }))) .end(done); }); test('/accounts/:account/orders -- with invalid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert.strictEqual(message.command, 'account_offers'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger, void(0)); + assert.strictEqual(message.ledger_index, 'validated'); conn.send(fixtures.accountOrdersResponse(message)); }); - + self.app .get('/v1/accounts/' + addresses.VALID + '/orders?ledger=foo') .expect(testutils.checkStatus(200)) @@ -64,38 +64,28 @@ suite('get orders', function() { }); test('/accounts/:account/orders -- with ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert.strictEqual(message.command, 'account_offers'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); + assert.strictEqual(message.ledger_index, LEDGER); conn.send(fixtures.accountOrdersResponse(message, { - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER })); }); self.app - .get('/v1/accounts/' + addresses.VALID + '/orders?ledger=9592219') + .get('/v1/accounts/' + addresses.VALID + '/orders?ledger=' + LEDGER) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountOrdersResponse({ - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER }))) .end(done); }); test('/accounts/:account/orders -- with invalid marker', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert(false); }); @@ -109,12 +99,6 @@ suite('get orders', function() { }); test('/accounts/:account/orders -- with valid marker and invalid limit', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert(false); }); @@ -128,18 +112,12 @@ suite('get orders', function() { }); test('/accounts/:account/orders -- with valid marker and valid limit', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert(false); }); self.app - .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&limit=100') + .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&limit=' + LIMIT) .expect(testutils.checkStatus(500)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(errors.RESTLedgerMissingWithMarker)) @@ -147,72 +125,58 @@ suite('get orders', function() { }); test('/accounts/:account/orders -- with valid marker and valid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert.strictEqual(message.command, 'account_offers'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); + assert.strictEqual(message.ledger_index, LEDGER); assert.strictEqual(message.marker, MARKER); conn.send(fixtures.accountOrdersResponse(message, { - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER })); }); self.app - .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&ledger=9592219') + .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&ledger=' + LEDGER) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountOrdersResponse({ - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER }))) .end(done); }); test('/accounts/:account/orders -- valid ledger and valid limit', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert.strictEqual(message.command, 'account_offers'); assert.strictEqual(message.account, addresses.VALID); assert.strictEqual(message.ledger_index, 9592219); - assert.strictEqual(message.limit, 5); + assert.strictEqual(message.limit, LIMIT); conn.send(fixtures.accountOrdersResponse(message, { - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER })); }); self.app - .get('/v1/accounts/' + addresses.VALID + '/orders?ledger=9592219&limit=5') + .get('/v1/accounts/' + addresses.VALID + '/orders?ledger=' + LEDGER + '&limit=' + LIMIT) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountOrdersResponse({ - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER }))) .end(done); }); test('/accounts/:account/orders -- with valid marker, valid limit, and invalid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert(false); }); self.app - .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&limit=100&ledger=foo') + .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&limit=' + LIMIT + '&ledger=foo') .expect(testutils.checkStatus(500)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(errors.RESTLedgerMissingWithMarker)) @@ -220,18 +184,11 @@ suite('get orders', function() { }); test('/accounts/:account/orders -- with valid marker, valid limit, and valid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_offers', function(message, conn) { assert.strictEqual(message.command, 'account_offers'); assert.strictEqual(message.account, addresses.VALID); assert.strictEqual(message.ledger_index, 9592219); - assert.strictEqual(message.limit, 1); + assert.strictEqual(message.limit, LIMIT); assert.strictEqual(message.marker, MARKER); conn.send(fixtures.accountOrdersResponse(message, { marker: NEXT_MARKER @@ -239,7 +196,7 @@ suite('get orders', function() { }); self.app - .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&limit=1&ledger=9592219') + .get('/v1/accounts/' + addresses.VALID + '/orders?marker=' + MARKER + '&limit=' + LIMIT + '&ledger=' + LEDGER) .expect(testutils.checkStatus(200)) .expect(testutils.checkHeaders) .expect(testutils.checkBody(fixtures.RESTAccountOrdersResponse({ @@ -249,10 +206,6 @@ suite('get orders', function() { }); test('/accounts/:account/orders -- invalid account', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert(false, 'Should not request account info'); - }); - self.wss.once('request_account_offers', function(message, conn) { assert(false, 'Should not request account lines'); }); diff --git a/test/testutils.js b/test/testutils.js index ed8a1f51..5312ad5a 100644 --- a/test/testutils.js +++ b/test/testutils.js @@ -104,5 +104,6 @@ module.exports = { checkHeaders: checkHeaders, checkBody: checkBody, generateHash: generateHash, - loadArguments: loadArguments + loadArguments: loadArguments, + generateHash: generateHash }; diff --git a/test/trustlines-test.js b/test/trustlines-test.js index 812ef9cd..9f3c4f49 100644 --- a/test/trustlines-test.js +++ b/test/trustlines-test.js @@ -8,9 +8,11 @@ var addresses = require('./fixtures').addresses; // Transaction LastLedgerSequence offset from current ledger sequence const LEDGER_OFFSET = 8; +const LIMIT = 5; + const MARKER = '29F992CC252056BF690107D1E8F2D9FBAFF29FF107B62B1D1F4E4E11ADF2CC73'; const NEXT_MARKER = '0C812C919D343EAE789B29E8027C62C5792C22172D37EA2B2C0121D2381F80E1'; -const LIMIT = 5; +const LEDGER = 9592219; suite('get trustlines', function() { var self = this; @@ -55,6 +57,7 @@ suite('get trustlines', function() { self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); + assert.strictEqual(message.ledger_index, 'validated'); conn.send(fixtures.accountLinesResponse(message)); }); @@ -67,16 +70,10 @@ suite('get trustlines', function() { }); test('/accounts/:account/trustlines -- with invalid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger, void(0)); + assert.strictEqual(message.ledger_index, 'validated'); conn.send(fixtures.accountLinesResponse(message)); }); @@ -89,38 +86,42 @@ suite('get trustlines', function() { }); test('/accounts/:account/trustlines -- with ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); - conn.send(fixtures.accountLinesResponse(message, { - marker: NEXT_MARKER - })); + assert.strictEqual(message.ledger_index, LEDGER); + conn.send(fixtures.accountLinesResponse(message)); }); testGetRequest({ account: addresses.VALID, - queryString: '?ledger=9592219', - expectedBody: fixtures.RESTAccountTrustlinesResponse({ - marker: NEXT_MARKER - }), + queryString: '?ledger=' + LEDGER, + expectedBody: fixtures.RESTAccountTrustlinesResponse(), expectedStatus: 200 }, done); }); - test('/accounts/:account/trustlines -- with invalid marker', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); + test('/accounts/:account/trustlines -- with non-validated ledger', function(done) { + self.wss.once('request_account_lines', function(message, conn) { + assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); + assert.strictEqual(message.ledger_index, LEDGER); + conn.send(fixtures.accountLinesResponse(message, { + validated: false + })); }); + self.app + .get(fixtures.requestPath(addresses.VALID, '?ledger=' + LEDGER)) + .expect(testutils.checkStatus(200)) + .expect(testutils.checkHeaders) + .expect(testutils.checkBody(fixtures.RESTAccountTrustlinesResponse({ + validated: false + }))) + .end(done); + }); + + test('/accounts/:account/trustlines -- with invalid marker', function(done) { self.wss.once('request_account_lines', function(message, conn) { assert(false); }); @@ -134,12 +135,6 @@ suite('get trustlines', function() { }); test('/accounts/:account/trustlines -- with valid marker and invalid limit', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert(false); }); @@ -153,12 +148,6 @@ suite('get trustlines', function() { }); test('/accounts/:account/trustlines -- with valid marker and valid limit', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert(false); }); @@ -172,67 +161,53 @@ suite('get trustlines', function() { }); test('/accounts/:account/trustlines -- with valid marker and valid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); + assert.strictEqual(message.ledger_index, LEDGER); assert.strictEqual(message.marker, MARKER); conn.send(fixtures.accountLinesResponse(message, { + ledger: LEDGER, marker: NEXT_MARKER })); }); testGetRequest({ account: addresses.VALID, - queryString: '?marker=' + MARKER + '&ledger=9592219', + queryString: '?marker=' + MARKER + '&ledger=' + LEDGER, expectedBody: fixtures.RESTAccountTrustlinesResponse({ - marker: NEXT_MARKER + marker: NEXT_MARKER, + ledger: LEDGER }), expectedStatus: 200 }, done); }); test('/accounts/:account/trustlines -- valid ledger and valid limit', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); + assert.strictEqual(message.ledger_index, LEDGER); assert.strictEqual(message.limit, LIMIT); - conn.send(fixtures.accountLinesResponse(message, { - marker: NEXT_MARKER + conn.send(fixtures.accountLinesResponse(message,{ + marker: NEXT_MARKER, + ledger: LEDGER })); }); testGetRequest({ account: addresses.VALID, - queryString: '?ledger=9592219&limit=5', + queryString: '?ledger=' + LEDGER + '&limit=' + LIMIT, expectedBody: fixtures.RESTAccountTrustlinesResponse({ marker: NEXT_MARKER, - limit: LIMIT + limit: LIMIT, + ledger: LEDGER }), expectedStatus: 200 }, done); }); test('/accounts/:account/trustlines -- with valid marker, valid limit, and invalid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert(false); }); @@ -246,30 +221,25 @@ suite('get trustlines', function() { }); test('/accounts/:account/trustlines -- with valid marker, valid limit, and valid ledger', function(done) { - self.wss.once('request_account_info', function(message, conn) { - assert.strictEqual(message.command, 'account_info'); - assert.strictEqual(message.account, addresses.VALID); - - conn.send(fixtures.accountInfoResponse(message)); - }); - self.wss.once('request_account_lines', function(message, conn) { assert.strictEqual(message.command, 'account_lines'); assert.strictEqual(message.account, addresses.VALID); - assert.strictEqual(message.ledger_index, 9592219); + assert.strictEqual(message.ledger_index, LEDGER); assert.strictEqual(message.limit, LIMIT); assert.strictEqual(message.marker, MARKER); - conn.send(fixtures.accountLinesResponse(message, { - marker: NEXT_MARKER + conn.send(fixtures.accountLinesResponse(message,{ + marker: NEXT_MARKER, + ledger: LEDGER })); }); testGetRequest({ account: addresses.VALID, - queryString: '?marker=' + MARKER + '&limit=' + LIMIT + '&ledger=9592219', + queryString: '?marker=' + MARKER + '&limit=' + LIMIT + '&ledger=' + LEDGER, expectedBody: fixtures.RESTAccountTrustlinesResponse({ marker: NEXT_MARKER, - limit: LIMIT + limit: LIMIT, + ledger: LEDGER }), expectedStatus: 200 }, done);