Set FullyCanoncialSig flag and fix integration tests

This commit is contained in:
Chris Clark
2015-10-19 15:32:05 -07:00
parent 01ecd197ce
commit 3fbde86548
33 changed files with 293 additions and 278 deletions

View File

@@ -3,143 +3,129 @@
'use strict';
const _ = require('lodash');
const assert = require('assert');
const async = require('async');
const errors = require('../../src/api/common/errors');
const composeAsync = require('../../src/api/common/utils').composeAsync;
const common = require('../../src/api/common');
const validate = common.validate;
const validate = require('../../src/api/common').validate;
const wallet = require('./wallet');
const settingsSpecification = require('../fixtures/settings-specification');
const trustlineSpecification = require('../fixtures/trustline-specification');
const payments = require('../fixtures/payments');
const requests = require('../fixtures/api/requests');
const RippleAPI = require('../../src').RippleAPI;
const TIMEOUT = 20000; // how long before each test case times out
const INTERVAL = 2000; // how long to wait between checks for validated ledger
const TIMEOUT = 30000; // how long before each test case times out
const INTERVAL = 1000; // how long to wait between checks for validated ledger
function verifyTransaction(testcase, hash, type, options, txData, done) {
testcase.api.getTransaction(hash, options, (err, data) => {
if (err instanceof errors.NotFoundError
&& testcase.api.getLedgerVersion() <= options.maxLedgerVersion) {
console.log('NOT VALIDATED YET...');
setTimeout(_.partial(verifyTransaction, testcase, hash, type,
options, txData, done), INTERVAL);
return;
} else if (err) {
done(err);
return;
}
function verifyTransaction(testcase, hash, type, options, txData) {
console.log('VERIFY...');
return testcase.api.getTransaction(hash, options).then(data => {
assert(data && data.outcome);
assert.strictEqual(data.type, type);
assert.strictEqual(data.address, wallet.getAddress());
assert.strictEqual(data.outcome.result, 'tesSUCCESS');
testcase.transactions.push(hash);
done(null, txData);
return {txJSON: JSON.stringify(txData), id: hash, tx: data};
}).catch(error => {
if (error instanceof errors.PendingLedgerVersionError) {
console.log('NOT VALIDATED YET...');
return new Promise((resolve, reject) => {
setTimeout(() => verifyTransaction(testcase, hash, type,
options, txData).then(resolve, reject), INTERVAL);
});
}
assert(false, 'Transaction not successful: ' + error.message);
});
}
function testTransaction(testcase, type, lastClosedLedgerVersion,
txData, done) {
const signedData = testcase.api.sign(txData, wallet.getSecret());
function testTransaction(testcase, type, lastClosedLedgerVersion, prepared) {
const txJSON = prepared.txJSON;
assert(txJSON, 'missing txJSON');
const txData = JSON.parse(txJSON);
assert.strictEqual(txData.Account, wallet.getAddress());
const signedData = testcase.api.sign(txJSON, wallet.getSecret());
console.log('PREPARED...');
testcase.api.submit(signedData.signedTransaction, (error, data) => {
return testcase.api.submit(signedData.signedTransaction).then(data => {
console.log('SUBMITTED...');
if (error) {
done(error);
return;
}
assert.strictEqual(data.engine_result, 'tesSUCCESS');
assert.strictEqual(data.engineResult, 'tesSUCCESS');
const options = {
minLedgerVersion: lastClosedLedgerVersion,
maxLedgerVersion: txData.LastLedgerSequence
};
setTimeout(_.partial(verifyTransaction, testcase, signedData.id, type,
options, txData, done), INTERVAL);
});
}
function verifyResult(transactionType, transaction, done) {
assert(transaction);
assert.strictEqual(transaction.Account, wallet.getAddress());
assert.strictEqual(transaction.TransactionType, transactionType);
done(null, transaction);
}
function setup(done) {
this.api = new RippleAPI({servers: ['wss://s1.ripple.com:443']});
this.api.connect(() => {
this.api.remote.getServer().once('ledger_closed', () => {
// this will initialiaze startLedgerVersion with
// on first run and will not overwrite on next invocations
if (!this.startLedgerVersion) {
this.startLedgerVersion = this.api.getLedgerVersion();
}
done();
return new Promise((resolve, reject) => {
setTimeout(() => verifyTransaction(testcase, signedData.id, type,
options, txData).then(resolve, reject), INTERVAL);
});
});
}
function teardown(done) {
this.api.remote.disconnect(done);
function setup() {
this.api = new RippleAPI({servers: ['wss://s1.ripple.com']});
console.log('CONNECTING...');
return this.api.connect().then(() => {
console.log('CONNECTED...');
});
}
describe.skip('integration tests', function() {
const instructions = {maxLedgerVersionOffset: 100};
function teardown() {
return this.api.disconnect();
}
function suiteSetup() {
this.transactions = [];
return setup.bind(this)().then(() => {
return this.api.getLedgerVersion().then(ledgerVersion => {
this.startLedgerVersion = ledgerVersion;
});
}).then(teardown.bind(this));
}
describe('integration tests', function() {
const address = wallet.getAddress();
const instructions = {maxLedgerVersionOffset: 10};
this.timeout(TIMEOUT);
before(function() {
this.transactions = [];
});
before(suiteSetup);
beforeEach(setup);
afterEach(teardown);
it('settings', function(done) {
const lastClosedLedgerVersion = this.api.getLedgerVersion();
async.waterfall([
this.api.prepareSettings.bind(this.api, wallet.getAddress(),
settingsSpecification, instructions),
_.partial(verifyResult, 'AccountSet'),
_.partial(testTransaction, this, 'settings', lastClosedLedgerVersion)
], () => done());
});
it('trustline', function(done) {
const lastClosedLedgerVersion = this.api.getLedgerVersion();
async.waterfall([
this.api.prepareTrustline.bind(this.api, wallet.getAddress(),
trustlineSpecification, instructions),
_.partial(verifyResult, 'TrustSet'),
_.partial(testTransaction, this, 'trustline', lastClosedLedgerVersion)
], () => done());
});
it('payment', function(done) {
const paymentSpecification = payments.payment({
value: '0.000001',
sourceAccount: wallet.getAddress(),
destinationAccount: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc'
it('settings', function() {
return this.api.getLedgerVersion().then(ledgerVersion => {
return this.api.prepareSettings(address,
requests.prepareSettings, instructions).then(prepared =>
testTransaction(this, 'settings', ledgerVersion, prepared));
});
const lastClosedLedgerVersion = this.api.getLedgerVersion();
async.waterfall([
this.api.preparePayment.bind(this.api, wallet.getAddress(),
paymentSpecification, instructions),
_.partial(verifyResult, 'Payment'),
_.partial(testTransaction, this, 'payment', lastClosedLedgerVersion)
], () => done());
});
it('order', function(done) {
it('trustline', function() {
return this.api.getLedgerVersion().then(ledgerVersion => {
return this.api.prepareTrustline(address,
requests.prepareTrustline.simple, instructions).then(prepared =>
testTransaction(this, 'trustline', ledgerVersion, prepared));
});
});
it('payment', function() {
const amount = {currency: 'XRP', value: '0.000001'};
const paymentSpecification = {
source: {
address: address,
maxAmount: amount
},
destination: {
address: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
amount: amount
}
};
return this.api.getLedgerVersion().then(ledgerVersion => {
return this.api.preparePayment(address,
paymentSpecification, instructions).then(prepared =>
testTransaction(this, 'payment', ledgerVersion, prepared));
});
});
it('order', function() {
const orderSpecification = {
direction: 'buy',
quantity: {
@@ -152,34 +138,28 @@ describe.skip('integration tests', function() {
value: '0.0002'
}
};
const self = this;
const lastClosedLedgerVersion = this.api.getLedgerVersion();
async.waterfall([
this.api.prepareOrder.bind(this.api, wallet.getAddress(),
orderSpecification, instructions),
_.partial(verifyResult, 'OfferCreate'),
_.partial(testTransaction, this, 'order', lastClosedLedgerVersion),
(txData, callback) => {
this.api.getOrders(wallet.getAddress(), {}, composeAsync(orders => {
assert(orders && orders.length > 0);
const createdOrder = _.first(_.filter(orders, (order) => {
return order.properties.sequence === txData.Sequence;
}));
assert(createdOrder);
assert.strictEqual(createdOrder.properties.maker,
wallet.getAddress());
assert.deepEqual(createdOrder.specification, orderSpecification);
return txData;
}, callback));
},
(txData, callback) => {
self.api.prepareOrderCancellation(wallet.getAddress(), txData.Sequence,
instructions, callback);
},
_.partial(verifyResult, 'OfferCancel'),
_.partial(testTransaction, this, 'orderCancellation',
lastClosedLedgerVersion)
], () => done());
return this.api.getLedgerVersion().then(ledgerVersion => {
return this.api.prepareOrder(address,
orderSpecification, instructions).then(prepared =>
testTransaction(this, 'order', ledgerVersion, prepared)
).then(result => {
const txData = JSON.parse(result.txJSON);
return this.api.getOrders(address).then(orders => {
assert(orders && orders.length > 0);
const createdOrder = _.first(_.filter(orders, order => {
return order.properties.sequence === txData.Sequence;
}));
assert(createdOrder);
assert.strictEqual(createdOrder.properties.maker, address);
assert.deepEqual(createdOrder.specification, orderSpecification);
return txData;
});
}).then(txData => this.api.prepareOrderCancellation(
address, txData.Sequence, instructions).then(prepared =>
testTransaction(this, 'orderCancellation', ledgerVersion,
prepared))
);
});
});
@@ -188,81 +168,74 @@ describe.skip('integration tests', function() {
});
it('getServerInfo', function(done) {
this.api.getServerInfo(composeAsync(data => {
assert(data && data.info && data.info.pubkey_node);
}, done));
it('getServerInfo', function() {
return this.api.getServerInfo().then(data => {
assert(data && data.pubkeyNode);
});
});
it('getFee', function() {
const fee = this.api.getFee();
assert.strictEqual(typeof fee, 'string');
assert(!isNaN(+fee));
assert(!isNaN(Number(fee)));
assert(parseFloat(fee) === Number(fee));
});
it('getLedgerVersion', function() {
const ledgerVersion = this.api.getLedgerVersion();
assert.strictEqual(typeof ledgerVersion, 'number');
assert(ledgerVersion >= this.startLedgerVersion);
return this.api.getLedgerVersion().then(ledgerVersion => {
assert.strictEqual(typeof ledgerVersion, 'number');
assert(ledgerVersion >= this.startLedgerVersion);
});
});
it('getTransactions', function(done) {
it('getTransactions', function() {
const options = {
outgoing: true,
initiated: true,
minLedgerVersion: this.startLedgerVersion
};
this.api.getTransactions(wallet.getAddress(), options,
composeAsync(transactionsData => {
assert(transactionsData);
assert.strictEqual(transactionsData.length, this.transactions.length);
}, done));
return this.api.getTransactions(address, options).then(transactionsData => {
assert(transactionsData);
assert.strictEqual(transactionsData.length, this.transactions.length);
});
});
it('getTrustlines', function(done) {
const options = {
currency: trustlineSpecification.currency,
counterparty: trustlineSpecification.counterparty
};
this.api.getTrustlines(wallet.getAddress(), options, composeAsync(data => {
it('getTrustlines', function() {
const fixture = requests.prepareTrustline.simple;
const options = _.pick(fixture, ['currency', 'counterparty']);
return this.api.getTrustlines(address, options).then(data => {
assert(data && data.length > 0 && data[0] && data[0].specification);
const specification = data[0].specification;
assert.strictEqual(specification.limit, trustlineSpecification.limit);
assert.strictEqual(specification.currency,
trustlineSpecification.currency);
assert.strictEqual(specification.counterparty,
trustlineSpecification.counterparty);
}, done));
assert.strictEqual(Number(specification.limit), Number(fixture.limit));
assert.strictEqual(specification.currency, fixture.currency);
assert.strictEqual(specification.counterparty, fixture.counterparty);
});
});
it('getBalances', function(done) {
const options = {
currency: trustlineSpecification.currency,
counterparty: trustlineSpecification.counterparty
};
this.api.getBalances(wallet.getAddress(), options, composeAsync(data => {
assert(data && data.length > 1 && data[0] && data[1]);
assert.strictEqual(data[0].currency, 'XRP');
assert.strictEqual(data[1].currency, trustlineSpecification.currency);
assert.strictEqual(data[1].counterparty,
trustlineSpecification.counterparty);
}, done));
it('getBalances', function() {
const fixture = requests.prepareTrustline.simple;
const options = _.pick(fixture, ['currency', 'counterparty']);
return this.api.getBalances(address, options).then(data => {
assert(data && data.length > 0 && data[0]);
assert.strictEqual(data[0].currency, fixture.currency);
assert.strictEqual(data[0].counterparty, fixture.counterparty);
});
});
it('getSettings', function(done) {
this.api.getSettings(wallet.getAddress(), {}, composeAsync(data => {
assert(data && data.sequence);
assert.strictEqual(data.domain, settingsSpecification.domain);
}, done));
it('getSettings', function() {
return this.api.getSettings(address).then(data => {
assert(data);
assert.strictEqual(data.domain, requests.prepareSettings.domain);
});
});
it('getOrderbook', function(done) {
it('getOrderbook', function() {
const orderbook = {
base: {
currency: 'XRP'
@@ -272,73 +245,53 @@ describe.skip('integration tests', function() {
counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
}
};
this.api.getOrderbook(wallet.getAddress(), orderbook, {},
composeAsync(book => {
assert(book && book.bids && book.bids.length > 0);
assert(book.asks && book.asks.length > 0);
const bid = book.bids[0];
assert(bid && bid.specification && bid.specification.quantity);
assert(bid.specification.totalPrice);
assert.strictEqual(bid.specification.direction, 'buy');
assert.strictEqual(bid.specification.quantity.currency, 'XRP');
assert.strictEqual(bid.specification.totalPrice.currency, 'USD');
const ask = book.asks[0];
assert(ask && ask.specification && ask.specification.quantity);
assert(ask.specification.totalPrice);
assert.strictEqual(ask.specification.direction, 'sell');
assert.strictEqual(ask.specification.quantity.currency, 'XRP');
assert.strictEqual(ask.specification.totalPrice.currency, 'USD');
}, done));
return this.api.getOrderbook(address, orderbook).then(book => {
assert(book && book.bids && book.bids.length > 0);
assert(book.asks && book.asks.length > 0);
const bid = book.bids[0];
assert(bid && bid.specification && bid.specification.quantity);
assert(bid.specification.totalPrice);
assert.strictEqual(bid.specification.direction, 'buy');
assert.strictEqual(bid.specification.quantity.currency, 'XRP');
assert.strictEqual(bid.specification.totalPrice.currency, 'USD');
const ask = book.asks[0];
assert(ask && ask.specification && ask.specification.quantity);
assert(ask.specification.totalPrice);
assert.strictEqual(ask.specification.direction, 'sell');
assert.strictEqual(ask.specification.quantity.currency, 'XRP');
assert.strictEqual(ask.specification.totalPrice.currency, 'USD');
});
});
it('getPaths', function(done) {
it('getPaths', function() {
const pathfind = {
source: {
address: wallet.getAddress()
address: address
},
destination: {
address: wallet.getAddress(),
address: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
amount: {
value: '0.000001',
currency: trustlineSpecification.currency,
counterparty: trustlineSpecification.counterparty
currency: 'USD',
counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'
}
}
};
this.api.getPaths(pathfind, composeAsync(data => {
return this.api.getPaths(pathfind).then(data => {
assert(data && data.length > 0);
const path = data[0];
assert(path && path.source);
assert.strictEqual(path.source.address, wallet.getAddress());
assert.strictEqual(path.source.address, address);
assert(path.paths && path.paths.length > 0);
}, done));
});
});
it('generateWallet', function() {
const newWallet = this.api.generateWallet();
const newWallet = this.api.generateAddress();
assert(newWallet && newWallet.address && newWallet.secret);
validate.addressAndSecret(newWallet);
});
/*
// the 'order' test case already tests order cancellation
// this is just for cancelling orders if something goes wrong during testing
it('cancel order', function(done) {
const sequence = 280;
const lastClosedLedgerVersion = this.api.getLedgerVersion();
this.api.prepareOrderCancellation(wallet.getAddress(), sequence,
instructions, (cerr, cancellationTxData) => {
if (cerr) {
done(cerr);
return;
}
verifyResult('OfferCancel', cancellationTxData);
testTransaction(this, cancellationTxData, 'orderCancellation',
lastClosedLedgerVersion, done);
});
});
*/
});