Skip to content

Commit

Permalink
run http integration tests against standalone server
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdarkdragon committed Dec 15, 2015
1 parent 63fd50e commit 39fd3b1
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 56 deletions.
74 changes: 57 additions & 17 deletions test/integration/http-integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const assert = require('assert-diff');
const _ = require('lodash');
const jayson = require('jayson');

const RippleAPI = require('../../src').RippleAPI;
const createHTTPServer = require('../../src/index').createHTTPServer;
const {pay, payTo, ledgerAccept} = require('./utils');

const apiFixtures = require('../fixtures');
const apiRequests = apiFixtures.requests;
Expand All @@ -14,8 +16,9 @@ const fixtures = require('./fixtures');

const TIMEOUT = 20000; // how long before each test case times out

const serverUri = 'ws://127.0.0.1:6006';
const apiOptions = {
server: 'wss://s1.ripple.com'
server: serverUri
};

const httpPort = 3000;
Expand All @@ -38,11 +41,16 @@ function random() {
return _.fill(Array(16), 0);
}

const masterAccount = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';

describe('http server integration tests', function() {
this.timeout(TIMEOUT);

let server = null;
let client = null;
let paymentId = null;
let newWallet = null;

function createTestInternal(testName, methodName, params, testFunc, id) {
it(testName, function() {
Expand All @@ -60,6 +68,21 @@ describe('http server integration tests', function() {
makeNamedParams(params), testFunc, id);
}

before(() => {
this.api = new RippleAPI({server: serverUri});
console.log('CONNECTING...');
return this.api.connect().then(() => {
console.log('CONNECTED...');
})
.then(() => ledgerAccept(this.api))
.then(() => newWallet = this.api.generateAddress())
.then(() => ledgerAccept(this.api))
.then(() => payTo(this.api, newWallet.address))
.then(paymentId_ => {
paymentId = paymentId_;
});
});

beforeEach(function() {
server = createHTTPServer(apiOptions, httpPort);
return server.start().then(() => {
Expand All @@ -85,26 +108,43 @@ describe('http server integration tests', function() {
result => assert(_.isNumber(result.result.validatedLedger.ledgerVersion))
);

createTest(
'getTransaction',
[{id: '4EB6B76237DEEE99F1EA16FAACED2D1E69C5F9CB54F727A4ECA51A08AD3AF466'}],
result => assert.deepEqual(result, fixtures.getTransaction),
'2'
);
it('getTransaction', function() {
const params = [{id: paymentId}];
return new Promise((resolve, reject) => {
client.request('getTransaction', makePositionalParams(params),
(err, result) => {
if (err) {
reject(err);
}
assert.strictEqual(result.result.id, paymentId);
const outcome = result.result.outcome;
assert.strictEqual(outcome.result, 'tesSUCCESS');
assert.strictEqual(outcome.balanceChanges
.rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh[0].value, '-4003218.000012');
resolve(result);
});
});
});

createTest(
'getTransactions',
[{address: 'rpP2JgiMyTF5jR5hLG3xHCPi1knBb1v9cM'}, {
it('getTransactions', function() {
const params = [{address: newWallet.address}, {
options: {
binary: true,
limit: 1,
start:
'FBAAC31D6BAEEFA9E501266FD62DA7A7982662BC19BC42F49BB41405C2F820DB'
limit: 1
}
}],
result => assert.deepEqual(result, fixtures.getTransactions),
'3'
);
}];
return new Promise((resolve, reject) => {
client.request('getTransactions', makeNamedParams(params),
(err, result) => {
if (err) {
reject(err);
}
assert.strictEqual(result.result.length, 1);
assert.strictEqual(result.result[0].id, paymentId);
resolve(result);
});
});
});

createTest(
'prepareSettings',
Expand Down
40 changes: 1 addition & 39 deletions test/integration/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const requests = require('../fixtures/requests');
const RippleAPI = require('../../src').RippleAPI;
const {isValidAddress} = require('ripple-address-codec');
const {isValidSecret} = require('../../src/common');
const {pay, payTo, ledgerAccept} = require('./utils');


const TIMEOUT = 10000; // how long before each test case times out
Expand Down Expand Up @@ -38,11 +39,6 @@ function verifyTransaction(testcase, hash, type, options, txData) {
});
}

function ledgerAccept(api) {
const request = {command: 'ledger_accept'};
return api.connection.request(request);
}

function testTransaction(testcase, type, lastClosedLedgerVersion, prepared) {
const txJSON = prepared.txJSON;
assert(txJSON, 'missing txJSON');
Expand Down Expand Up @@ -73,43 +69,9 @@ function setup() {
});
}

function pay(api, from, to, amount, secret, currency = 'XRP', counterparty) {
const paymentSpecification = {
source: {
address: from,
maxAmount: {
value: amount,
currency: currency
}
},
destination: {
address: to,
amount: {
value: amount,
currency: currency
}
}
};

if (counterparty !== undefined) {
paymentSpecification.source.maxAmount.counterparty = counterparty;
paymentSpecification.destination.amount.counterparty = counterparty;
}

return api.preparePayment(from, paymentSpecification, {})
.then(data => api.sign(data.txJSON, secret))
.then(signed => api.submit(signed.signedTransaction))
.then(() => ledgerAccept(api));
}

const masterAccount = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';

function payTo(api, to, amount = '4003218', currency = 'XRP', counterparty) {
return pay(api, masterAccount, to, amount, masterSecret, currency,
counterparty);
}

function makeTrustLine(testcase, address, secret) {
const api = testcase.api;
const specification = {
Expand Down
56 changes: 56 additions & 0 deletions test/integration/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const masterAccount = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';

function ledgerAccept(api) {
const request = {command: 'ledger_accept'};
return api.connection.request(request);
}

function pay(api, from, to, amount, secret, currency = 'XRP', counterparty) {
const paymentSpecification = {
source: {
address: from,
maxAmount: {
value: amount,
currency: currency
}
},
destination: {
address: to,
amount: {
value: amount,
currency: currency
}
}
};

if (counterparty !== undefined) {
paymentSpecification.source.maxAmount.counterparty = counterparty;
paymentSpecification.destination.amount.counterparty = counterparty;
}

let id = null;
return api.preparePayment(from, paymentSpecification, {})
.then(data => api.sign(data.txJSON, secret))
.then(signed => {
id = signed.id;
api.submit(signed.signedTransaction);
})
.then(() => ledgerAccept(api))
.then(() => id);
}


function payTo(api, to, amount = '4003218', currency = 'XRP', counterparty) {
return pay(api, masterAccount, to, amount, masterSecret, currency,
counterparty);
}


module.exports = {
pay,
payTo,
ledgerAccept
};

0 comments on commit 39fd3b1

Please sign in to comment.