Merge pull request #452 from darkdarkdragon/develop-RLJS-370-9-1

Increase tests coverage even more
This commit is contained in:
Chris Clark
2015-07-27 16:35:59 -07:00
16 changed files with 477 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ const utils = require('./utils');
const parseTransaction = require('./parse/transaction');
const validate = utils.common.validate;
const errors = utils.common.errors;
const RippleError = require('../../core/rippleerror').RippleError;
function attachTransactionDate(remote, tx, callback) {
if (tx.date) {
@@ -43,7 +44,13 @@ function getTransaction(identifier, options, callback) {
const maxLedgerVersion = Math.min(options.maxLedgerVersion,
remote.getLedgerSequence());
function callbackWrapper(error, tx) {
function callbackWrapper(error_, tx) {
let error = error_;
if (error instanceof RippleError && error.remote &&
error.remote.error === 'txnNotFound') {
error = new errors.NotFoundError('Transaction not found');
}
if (error instanceof errors.NotFoundError
&& !utils.hasCompleteLedgerRange(remote,
options.minLedgerVersion, maxLedgerVersion)) {

View File

@@ -1,4 +1,5 @@
/* @flow */
/* eslint-disable max-params */
'use strict';
const _ = require('lodash');
const utils = require('./utils');
@@ -28,7 +29,7 @@ function transactionFilter(address, filters, tx) {
return false;
}
if (filters.counterparty && tx.address !== filters.counterparty
&& tx.Destination !== filters.counterparty) {
&& tx.specification.destination.address !== filters.counterparty) {
return false;
}
return true;

View File

@@ -71,10 +71,12 @@ function signum(num) {
*/
function compareTransactions(first, second) {
if (first.ledgerVersion === second.ledgerVersion) {
return signum(Number(first.indexInLedger) - Number(second.indexInLedger));
if (first.outcome.ledgerVersion === second.outcome.ledgerVersion) {
return signum(Number(first.outcome.indexInLedger) -
Number(second.outcome.indexInLedger));
}
return Number(first.ledgerVersion) < Number(second.ledgerVersion) ? -1 : 1;
return Number(first.outcome.ledgerVersion) <
Number(second.outcome.ledgerVersion) ? -1 : 1;
}
function hasCompleteLedgerRange(remote, minLedgerVersion, maxLedgerVersion) {

View File

@@ -2,6 +2,7 @@
'use strict';
const _ = require('lodash');
const common = require('../common');
// If a ledger is not received in this time, consider the connection offline
@@ -28,7 +29,8 @@ function isConnected(): boolean {
function getServerInfo(callback: (err: any, data: any) => void): void {
this.remote.requestServerInfo((error, response) => {
if (error) {
callback(new common.errors.RippledNetworkError(error.message));
const message = _.get(error, ['remote', 'error_message'], error.message);
callback(new common.errors.RippledNetworkError(message));
} else {
callback(null, response.info);
}

View File

@@ -11,6 +11,10 @@ const hashes = require('./fixtures/hashes');
const MockPRNG = require('./mock-prng');
const sjcl = require('../src').sjcl;
const address = addresses.ACCOUNT;
const validate = require('../src/api/common/validate');
const RippleError = require('../src/core/rippleerror').RippleError;
const utils = require('../src/api/ledger/utils');
const ledgerClosed = require('./fixtures/api/rippled/ledger-close-newer');
const schemaValidate = require('../src/api/common/schema-validator');
const orderbook = {
@@ -202,6 +206,64 @@ describe('RippleAPI', function() {
'getTransaction', done));
});
it('getTransaction - not found in range', function(done) {
const hash =
'809335DD3B0B333865096217AA2F55A4DF168E0198080B3A090D12D88880FF0E';
const options = {
minLedgerVersion: 32570,
maxLedgerVersion: 32571
};
this.api.getTransaction(hash, options, (error) => {
assert(error instanceof this.api.errors.NotFoundError);
done();
});
});
it('getTransaction - not found by hash', function(done) {
this.api.getTransaction(hashes.NOTFOUND_TRANSACTION_HASH, {}, (error) => {
assert(error instanceof this.api.errors.NotFoundError);
done();
});
});
it('getTransaction - missing ledger history', function(done) {
// make gaps in history
this.api.remote.getServer().emit('message', ledgerClosed);
this.api.getTransaction(hashes.NOTFOUND_TRANSACTION_HASH, {}, (error) => {
assert(error instanceof this.api.errors.MissingLedgerHistoryError);
done();
});
});
it('getTransaction - ledger_index not found', function(done) {
const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11';
this.api.getTransaction(hash, {}, (error) => {
assert(error instanceof this.api.errors.NotFoundError);
assert(error.message.indexOf('ledger_index') !== -1);
done();
});
});
it('getTransaction - transaction ledger not found', function(done) {
const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA12';
this.api.getTransaction(hash, {}, (error) => {
assert(error instanceof this.api.errors.NotFoundError);
assert(error.message.indexOf('ledger not found') !== -1);
done();
});
});
it('getTransaction - ledger missing close time', function(done) {
const hash =
'0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A04';
this.api.getTransaction(hash, {}, (error) => {
assert(error instanceof this.api.errors.ApiError);
done();
});
});
it('getTransactions', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2};
this.api.getTransactions(address, options,
@@ -209,6 +271,84 @@ describe('RippleAPI', function() {
'getTransactions', done));
});
it('getTransactions - earliest first', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
earliestFirst: true
};
const expected = _.cloneDeep(responses.getTransactions)
.sort(utils.compareTransactions);
this.api.getTransactions(address, options,
_.partial(checkResult, expected, 'getTransactions', done));
});
it('getTransactions - earliest first with start option', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
start: hashes.VALID_TRANSACTION_HASH,
earliestFirst: true
};
this.api.getTransactions(address, options, (error, data) => {
assert.strictEqual(data.length, 0);
done(error);
});
});
it('getTransactions - gap', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
maxLedgerVersion: 348858000
};
this.api.getTransactions(address, options, (error) => {
assert(error instanceof this.api.errors.MissingLedgerHistoryError);
done();
});
});
it('getTransactions - tx not found', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
start: hashes.NOTFOUND_TRANSACTION_HASH,
counterparty: address
};
this.api.getTransactions(address, options, (error) => {
assert(error instanceof this.api.errors.NotFoundError);
done();
});
});
it('getTransactions - filters', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 10,
excludeFailures: true,
counterparty: addresses.ISSUER
};
this.api.getTransactions(address, options, (error, data) => {
assert.strictEqual(data.length, 10);
assert(_.every(data, t => t.type === 'payment' || t.type === 'order'));
assert(_.every(data, t => t.outcome.result === 'tesSUCCESS'));
done();
});
});
it('getTransactions - filters for incoming', function(done) {
const options = {types: ['payment', 'order'], initiated: false, limit: 10,
excludeFailures: true,
counterparty: addresses.ISSUER
};
this.api.getTransactions(address, options, (error, data) => {
assert.strictEqual(data.length, 10);
assert(_.every(data, t => t.type === 'payment' || t.type === 'order'));
assert(_.every(data, t => t.outcome.result === 'tesSUCCESS'));
done();
});
});
// this is the case where core.RippleError just falls
// through the api to the user
it('getTransactions - error', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 13};
this.api.getTransactions(address, options, (error) => {
assert(error instanceof RippleError);
done();
});
});
// TODO: this doesn't test much, just that it doesn't crash
it('getTransactions with start option', function(done) {
const options = {
@@ -298,6 +438,15 @@ describe('RippleAPI', function() {
_.partial(checkResult, responses.getServerInfo, null, done));
});
it('getServerInfo - error', function(done) {
this.mockRippled.returnErrorOnServerInfo = true;
this.api.getServerInfo((error) => {
assert(error instanceof this.api.errors.NetworkError);
assert(error.message.indexOf('too much load') !== -1);
done();
});
});
it('getFee', function() {
assert.strictEqual(this.api.getFee(), '0.000012');
});
@@ -360,4 +509,59 @@ describe('RippleAPI', function() {
it('getLedgerVersion', function() {
assert.strictEqual(this.api.getLedgerVersion(), 8819951);
});
it('ledger utils - compareTransactions', function() {
let first = {outcome: {ledgerVersion: 1, indexInLedger: 100}};
let second = {outcome: {ledgerVersion: 1, indexInLedger: 200}};
assert.strictEqual(utils.compareTransactions(first, second), -1);
first = {outcome: {ledgerVersion: 1, indexInLedger: 100}};
second = {outcome: {ledgerVersion: 1, indexInLedger: 100}};
assert.strictEqual(utils.compareTransactions(first, second), 0);
first = {outcome: {ledgerVersion: 1, indexInLedger: 200}};
second = {outcome: {ledgerVersion: 1, indexInLedger: 100}};
assert.strictEqual(utils.compareTransactions(first, second), 1);
});
it('ledger utils - renameCounterpartyToIssuer', function() {
assert.strictEqual(utils.renameCounterpartyToIssuer(undefined), undefined);
const amountArg = {issuer: '1'};
assert.deepEqual(utils.renameCounterpartyToIssuer(amountArg), amountArg);
});
it('ledger utils - getRecursive', function(done) {
function getter(marker, limit, callback) {
if (marker === undefined) {
callback(null, {marker: 'A', results: [1]});
} else {
callback(new Error(), null);
}
}
utils.getRecursive(getter, 10, (error) => {
assert(error instanceof Error);
done();
});
});
it('validator', function() {
const noSecret = {address: address};
assert.throws(_.partial(validate.addressAndSecret, noSecret),
this.api.errors.ValidationError);
assert.throws(_.partial(validate.addressAndSecret, noSecret),
/Parameter missing/);
const badSecret = {address: address, secret: 'bad'};
assert.throws(_.partial(validate.addressAndSecret, badSecret),
this.api.errors.ValidationError);
assert.throws(_.partial(validate.addressAndSecret, badSecret),
/not match/);
const goodWallet = {address: 'rpZMK8hwyrBvLorFNWHRCGt88nCJWbixur',
secret: 'shzjfakiK79YQdMjy4h8cGGfQSV6u'
};
assert.doesNotThrow(_.partial(validate.addressAndSecret, goodWallet));
});
});

View File

@@ -86,7 +86,7 @@
}
]
},
"ledgerVersion": 348860,
"ledgerVersion": 348859,
"indexInLedger": 0
}
},
@@ -177,7 +177,7 @@
}
]
},
"ledgerVersion": 348860,
"ledgerVersion": 348858,
"indexInLedger": 0
}
}

View File

@@ -4,6 +4,8 @@ const _ = require('lodash');
const hashes = require('../../hashes');
const addresses = require('../../addresses');
const SerializedObject = require('../../../../src/core').SerializedObject;
const AccountSet = require('./tx/account-set.json');
const NotFound = require('./tx/not-found.json');
module.exports = function(request, options={}) {
_.defaults(options, {
@@ -17,7 +19,7 @@ module.exports = function(request, options={}) {
validated: true
});
const tx = {
let tx = {
Account: addresses.ACCOUNT,
Amount: {
currency: 'USD',
@@ -52,7 +54,7 @@ module.exports = function(request, options={}) {
TxnSignature: '304502204EE3E9D1B01D8959B08450FCA9E22025AF503DEF310E34A93863A85CAB3C0BC5022100B61F5B567F77026E8DEED89EED0B7CAF0E6C96C228A2A65216F0DC2D04D52083'
};
const meta = {
let meta = {
AffectedNodes: [
{
ModifiedNode: {
@@ -196,15 +198,37 @@ module.exports = function(request, options={}) {
TransactionResult: 'tesSUCCESS'
};
let marker = Number(request.marker) || 0;
marker += 1;
if (marker === 5) {
meta.TransactionResult = 'tecINSUFFICIENT_RESERVE';
} else if (marker === 6) {
tx = _.cloneDeep(AccountSet.result);
meta = tx.meta;
delete tx.meta;
} else if (marker === 7) {
tx.Account = addresses.OTHER_ACCOUNT;
} else if (marker === 8) {
tx.Destination = addresses.THIRD_ACCOUNT;
} else if (marker > 25) {
marker = undefined;
} else if (marker > 15) {
tx.Account = addresses.ISSUER;
tx.Destination = addresses.ACCOUNT;
}
if (request.limit === 13) {
const res = _.assign({}, NotFound, {id: request.id});
return JSON.stringify(res);
}
return JSON.stringify({
id: request.id,
status: 'success',
type: 'response',
result: {
marker: request.marker === undefined ? 'ABC' : undefined,
marker: marker === undefined ? undefined : String(marker),
transactions: [
{
ledger_index: 348860,
ledger_index: 348860 - Number(marker),
tx_blob: SerializedObject.from_json(tx).to_hex(),
meta: SerializedObject.from_json(meta).to_hex(),
validated: options.validated

View File

@@ -3,6 +3,8 @@
module.exports = {
submit: require('./submit'),
ledger: require('./ledger'),
ledgerNotFound: require('./ledger-not-found'),
ledgerWithoutCloseTime: require('./ledger-without-close-time'),
subscribe: require('./subscribe'),
unsubscribe: require('./unsubscribe'),
account_info: {
@@ -13,6 +15,7 @@ module.exports = {
account_tx: require('./account-tx'),
book_offers: require('./book-offers'),
server_info: require('./server-info'),
server_info_error: require('./server-info-error'),
ripple_path_find: {
generate: require('./ripple-path-find'),
sendUSD: require('./ripple-path-find-send-usd'),
@@ -24,6 +27,9 @@ module.exports = {
OfferCreate: require('./tx/offer-create.json'),
OfferCancel: require('./tx/offer-cancel.json'),
TrustSet: require('./tx/trust-set.json'),
NotFound: require('./tx/not-found.json')
NotFound: require('./tx/not-found.json'),
NoLedgerIndex: require('./tx/no-ledger-index.json'),
NoLedgerFound: require('./tx/no-ledger-found.json'),
LedgerWithoutTime: require('./tx/ledger-without-time.json')
}
};

View File

@@ -0,0 +1,12 @@
{
"fee_base": 10,
"fee_ref": 10,
"ledger_hash": "9141FA171F2C0CE63E609466AF728FF66C12F7ACD4B4B50B0947A7F3409D593A",
"ledger_index": 14804627,
"ledger_time": 490945840,
"reserve_base": 20000000,
"reserve_inc": 5000000,
"txn_count": 19,
"type": "ledgerClosed",
"validated_ledgers": "13983423-14804627"
}

View File

@@ -0,0 +1,13 @@
{
"id": 0,
"status": "error",
"type": "response",
"error": "lgrNotFound",
"error_code": 20,
"error_message": "ledgerNotFound",
"request": {
"command": "ledger",
"id": 3,
"ledger_index": 34
}
}

View File

@@ -0,0 +1,26 @@
{
"id": 0,
"status": "success",
"type": "response",
"result": {
"ledger": {
"accepted": true,
"account_hash": "EC028EC32896D537ECCA18D18BEBE6AE99709FEFF9EF72DBD3A7819E918D8B96",
"close_time_human": "2014-Sep-24 21:21:50",
"close_time_resolution": 10,
"closed": true,
"hash": "0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A04",
"ledger_hash": "0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A01",
"ledger_index": "9038215",
"parent_hash": "4BB9CBE44C39DC67A1BE849C7467FE1A6D1F73949EA163C38A0121A15E04FFDE",
"seqNum": "9038214",
"totalCoins": "99999973964317514",
"total_coins": "99999973964317514",
"transaction_hash": "ECB730839EB55B1B114D5D1AD2CD9A932C35BA9AB6D3A8C2F08935EAC2BAC239",
"transactions": [
"1FC4D12C30CE206A6E23F46FAC62BD393BE9A79A1C452C6F3A04A13BC7A5E5A3",
"E25C38FDB8DD4A2429649588638EE05D055EE6D839CABAF8ABFB4BD17CFE1F3E"
]
}
}
}

View File

@@ -0,0 +1,12 @@
{
"id": 0,
"status": "error",
"type": "response",
"error": "slowDown",
"error_code": 15,
"error_message": "You are placing too much load on the server.",
"request": {
"command": "server_info",
"id": 0
}
}

View File

@@ -0,0 +1,45 @@
{
"id": 0,
"status": "success",
"type": "response",
"result": {
"Account": "rLVKsA4F9iJBbA6rX2x4wCmkj6drgtqpQe",
"Fee": "10",
"Flags": 2147483648,
"Sequence": 1,
"SetFlag": 2,
"SigningPubKey": "03EA3ADCA632F125EC2CC4F7F6A82DE0DCE2B65290CAC1F22242C5163F0DA9652D",
"TransactionType": "AccountSet",
"TxnSignature": "3045022100DE8B666B1A31EA65011B0F32130AB91A5747E32FA49B3054CEE8E8362DBAB98A022040CF0CF254677A8E5CD04C59CA2ED7F6F15F7E184641BAE169C561650967B226",
"hash": "4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA12",
"inLedger": 8206418,
"ledger_index": 9038215,
"meta": {
"AffectedNodes": [
{
"ModifiedNode": {
"FinalFields": {
"Account": "rLVKsA4F9iJBbA6rX2x4wCmkj6drgtqpQe",
"Balance": "29999990",
"Flags": 786432,
"OwnerCount": 0,
"Sequence": 2
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "3F5072C4875F32ED770DAF3610A716600ED7C7BB0348FADC7A98E011BB2CD36F",
"PreviousFields": {
"Balance": "30000000",
"Flags": 4194304,
"Sequence": 1
},
"PreviousTxnID": "3FB0350A3742BBCC0D8AA3C5247D1AEC01177D0A24D9C34762BAA2FEA8AD88B3",
"PreviousTxnLgrSeq": 8206397
}
}
],
"TransactionIndex": 5,
"TransactionResult": "tesSUCCESS"
},
"validated": true
}
}

View File

@@ -0,0 +1,45 @@
{
"id": 0,
"status": "success",
"type": "response",
"result": {
"Account": "rLVKsA4F9iJBbA6rX2x4wCmkj6drgtqpQe",
"Fee": "10",
"Flags": 2147483648,
"Sequence": 1,
"SetFlag": 2,
"SigningPubKey": "03EA3ADCA632F125EC2CC4F7F6A82DE0DCE2B65290CAC1F22242C5163F0DA9652D",
"TransactionType": "AccountSet",
"TxnSignature": "3045022100DE8B666B1A31EA65011B0F32130AB91A5747E32FA49B3054CEE8E8362DBAB98A022040CF0CF254677A8E5CD04C59CA2ED7F6F15F7E184641BAE169C561650967B226",
"hash": "4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA12",
"inLedger": 8206418,
"ledger_index": 34,
"meta": {
"AffectedNodes": [
{
"ModifiedNode": {
"FinalFields": {
"Account": "rLVKsA4F9iJBbA6rX2x4wCmkj6drgtqpQe",
"Balance": "29999990",
"Flags": 786432,
"OwnerCount": 0,
"Sequence": 2
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "3F5072C4875F32ED770DAF3610A716600ED7C7BB0348FADC7A98E011BB2CD36F",
"PreviousFields": {
"Balance": "30000000",
"Flags": 4194304,
"Sequence": 1
},
"PreviousTxnID": "3FB0350A3742BBCC0D8AA3C5247D1AEC01177D0A24D9C34762BAA2FEA8AD88B3",
"PreviousTxnLgrSeq": 8206397
}
}
],
"TransactionIndex": 5,
"TransactionResult": "tesSUCCESS"
},
"validated": true
}
}

View File

@@ -0,0 +1,44 @@
{
"id": 0,
"status": "success",
"type": "response",
"result": {
"Account": "rLVKsA4F9iJBbA6rX2x4wCmkj6drgtqpQe",
"Fee": "10",
"Flags": 2147483648,
"Sequence": 1,
"SetFlag": 2,
"SigningPubKey": "03EA3ADCA632F125EC2CC4F7F6A82DE0DCE2B65290CAC1F22242C5163F0DA9652D",
"TransactionType": "AccountSet",
"TxnSignature": "3045022100DE8B666B1A31EA65011B0F32130AB91A5747E32FA49B3054CEE8E8362DBAB98A022040CF0CF254677A8E5CD04C59CA2ED7F6F15F7E184641BAE169C561650967B226",
"hash": "4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11",
"inLedger": 8206418,
"meta": {
"AffectedNodes": [
{
"ModifiedNode": {
"FinalFields": {
"Account": "rLVKsA4F9iJBbA6rX2x4wCmkj6drgtqpQe",
"Balance": "29999990",
"Flags": 786432,
"OwnerCount": 0,
"Sequence": 2
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "3F5072C4875F32ED770DAF3610A716600ED7C7BB0348FADC7A98E011BB2CD36F",
"PreviousFields": {
"Balance": "30000000",
"Flags": 4194304,
"Sequence": 1
},
"PreviousTxnID": "3FB0350A3742BBCC0D8AA3C5247D1AEC01177D0A24D9C34762BAA2FEA8AD88B3",
"PreviousTxnLgrSeq": 8206397
}
}
],
"TransactionIndex": 5,
"TransactionResult": "tesSUCCESS"
},
"validated": true
}
}

View File

@@ -71,7 +71,11 @@ module.exports = function(port) {
mock.on('request_server_info', function(request, conn) {
assert.strictEqual(request.command, 'server_info');
conn.send(createResponse(request, fixtures.server_info));
if (mock.returnErrorOnServerInfo) {
conn.send(createResponse(request, fixtures.server_info_error));
} else {
conn.send(createResponse(request, fixtures.server_info));
}
});
mock.on('request_subscribe', function(request, conn) {
@@ -111,7 +115,13 @@ module.exports = function(port) {
mock.on('request_ledger', function(request, conn) {
assert.strictEqual(request.command, 'ledger');
conn.send(createResponse(request, fixtures.ledger));
if (request.ledger_index === 34) {
conn.send(createResponse(request, fixtures.ledgerNotFound));
} else if (request.ledger_index === 9038215) {
conn.send(createResponse(request, fixtures.ledgerWithoutCloseTime));
} else {
conn.send(createResponse(request, fixtures.ledger));
}
});
mock.on('request_tx', function(request, conn) {
@@ -130,6 +140,15 @@ module.exports = function(port) {
} else if (request.transaction ===
'635A0769BD94710A1F6A76CDE65A3BC661B20B798807D1BBBDADCEA26420538D') {
conn.send(createResponse(request, fixtures.tx.TrustSet));
} else if (request.transaction ===
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11') {
conn.send(createResponse(request, fixtures.tx.NoLedgerIndex));
} else if (request.transaction ===
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA12') {
conn.send(createResponse(request, fixtures.tx.NoLedgerFound));
} else if (request.transaction ===
'0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A04') {
conn.send(createResponse(request, fixtures.tx.LedgerWithoutTime));
} else if (request.transaction === hashes.NOTFOUND_TRANSACTION_HASH) {
conn.send(createResponse(request, fixtures.tx.NotFound));
} else {