mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Add unit test for submit
This commit is contained in:
@@ -49,7 +49,7 @@ function computeSignature(txJSON, keypair) {
|
||||
|
||||
/*:: type TxJSON = {Account: string; SigningPubKey: string,
|
||||
TxnSignature: string};
|
||||
type Signed = {tx_blob: string; hash: string}; */
|
||||
type Signed = {signedTransaction: string; id: string}; */
|
||||
function sign(txJSON: TxJSON, secret: string): Signed {
|
||||
validate.txJSON(txJSON);
|
||||
validate.addressAndSecret({address: txJSON.Account, secret: secret});
|
||||
@@ -61,8 +61,8 @@ function sign(txJSON: TxJSON, secret: string): Signed {
|
||||
txJSON.TxnSignature = computeSignature(txJSON, keypair);
|
||||
const serialized = serialize(txJSON);
|
||||
return {
|
||||
tx_blob: serialized.to_hex(),
|
||||
hash: hashSerialization(serialized, HASH_TX_ID)
|
||||
signedTransaction: serialized.to_hex(),
|
||||
id: hashSerialization(serialized, HASH_TX_ID)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ function submit(tx_blob: string, callback: Callback): void {
|
||||
validate.blob(tx_blob);
|
||||
const request = new ripple.Request(this.remote, 'submit');
|
||||
request.message.tx_blob = tx_blob;
|
||||
request.request(callback);
|
||||
request.request(null, callback);
|
||||
}
|
||||
|
||||
module.exports = submit;
|
||||
|
||||
@@ -19,6 +19,7 @@ const signInput = require('./fixtures/sign-input');
|
||||
const signOutput = require('./fixtures/sign-output');
|
||||
const MockPRNG = require('./mock-prng');
|
||||
const sjcl = require('../src').sjcl;
|
||||
const submitResponse = require('./fixtures/submit-response');
|
||||
|
||||
function checkResult(expected, done, error, response) {
|
||||
if (error) {
|
||||
@@ -74,6 +75,11 @@ describe('RippleAPI', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('submit', function(done) {
|
||||
this.api.submit(signOutput.signedTransaction,
|
||||
_.partial(checkResult, submitResponse, done));
|
||||
});
|
||||
|
||||
it('getBalances', function(done) {
|
||||
this.api.getBalances(address, {},
|
||||
_.partial(checkResult, balancesResponse, done));
|
||||
|
||||
16
test/fixtures/mock.js
vendored
16
test/fixtures/mock.js
vendored
@@ -697,3 +697,19 @@ module.exports.serverInfoResponse = function(request) {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.submitResponse = function(request) {
|
||||
return JSON.stringify({
|
||||
id: request.id,
|
||||
status: 'success',
|
||||
type: 'response',
|
||||
result: {
|
||||
success: true,
|
||||
engine_result: 'tesSUCCESS',
|
||||
engine_result_code: 0,
|
||||
engine_result_message: 'The transaction was applied. Only final in a validated ledger.',
|
||||
tx_blob: request.tx_blob,
|
||||
tx_json: {} // stubbed out for simplicity, not needed for testing
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
4
test/fixtures/sign-output.json
vendored
4
test/fixtures/sign-output.json
vendored
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"tx_blob": "12000322000000002400000017201B0086955468400000000000000C732102F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D87446304402207660BDEF67105CE1EBA9AD35DC7156BAB43FF1D47633199EE257D70B6B9AAFBF02207F5517BC8AEF2ADC1325897ECDBA8C673838048BCA62F4E98B252F19BE88796D770A726970706C652E636F6D81144FBFF73DA4ECF9B701940F27341FA8020C313443",
|
||||
"hash": "DB44C111583A95AF973A0B0A40348D90512FCBCDDCA3315A286D2BF4FAC100F1"
|
||||
"signedTransaction": "12000322000000002400000017201B0086955468400000000000000C732102F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D87446304402207660BDEF67105CE1EBA9AD35DC7156BAB43FF1D47633199EE257D70B6B9AAFBF02207F5517BC8AEF2ADC1325897ECDBA8C673838048BCA62F4E98B252F19BE88796D770A726970706C652E636F6D81144FBFF73DA4ECF9B701940F27341FA8020C313443",
|
||||
"id": "DB44C111583A95AF973A0B0A40348D90512FCBCDDCA3315A286D2BF4FAC100F1"
|
||||
}
|
||||
|
||||
8
test/fixtures/submit-response.json
vendored
Normal file
8
test/fixtures/submit-response.json
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"success": true,
|
||||
"engine_result": "tesSUCCESS",
|
||||
"engine_result_code": 0,
|
||||
"engine_result_message": "The transaction was applied. Only final in a validated ledger.",
|
||||
"tx_blob": "12000322000000002400000017201B0086955468400000000000000C732102F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D87446304402207660BDEF67105CE1EBA9AD35DC7156BAB43FF1D47633199EE257D70B6B9AAFBF02207F5517BC8AEF2ADC1325897ECDBA8C673838048BCA62F4E98B252F19BE88796D770A726970706C652E636F6D81144FBFF73DA4ECF9B701940F27341FA8020C313443",
|
||||
"tx_json": {}
|
||||
}
|
||||
@@ -32,9 +32,9 @@ module.exports = function(port) {
|
||||
};
|
||||
|
||||
mock.once('connection', function(conn) {
|
||||
conn.on('message', function(messageJSON) {
|
||||
const message = JSON.parse(messageJSON);
|
||||
mock.emit('request_' + message.command, message, conn);
|
||||
conn.on('message', function(requestJSON) {
|
||||
const request = JSON.parse(requestJSON);
|
||||
mock.emit('request_' + request.command, request, conn);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -55,55 +55,60 @@ module.exports = function(port) {
|
||||
mock.expectedRequests[this.event] -= 1;
|
||||
});
|
||||
|
||||
mock.on('request_server_info', function(message, conn) {
|
||||
assert.strictEqual(message.command, 'server_info');
|
||||
conn.send(fixtures.serverInfoResponse(message));
|
||||
mock.on('request_server_info', function(request, conn) {
|
||||
assert.strictEqual(request.command, 'server_info');
|
||||
conn.send(fixtures.serverInfoResponse(request));
|
||||
});
|
||||
|
||||
mock.on('request_subscribe', function(message, conn) {
|
||||
assert.strictEqual(message.command, 'subscribe');
|
||||
if (message.accounts) {
|
||||
assert.strictEqual(message.accounts[0], addresses.ACCOUNT);
|
||||
mock.on('request_subscribe', function(request, conn) {
|
||||
assert.strictEqual(request.command, 'subscribe');
|
||||
if (request.accounts) {
|
||||
assert.strictEqual(request.accounts[0], addresses.ACCOUNT);
|
||||
} else {
|
||||
assert.deepEqual(message.streams, ['ledger', 'server']);
|
||||
assert.deepEqual(request.streams, ['ledger', 'server']);
|
||||
}
|
||||
conn.send(fixtures.subscribeResponse(message));
|
||||
conn.send(fixtures.subscribeResponse(request));
|
||||
});
|
||||
|
||||
mock.on('request_account_info', function(message, conn) {
|
||||
assert.strictEqual(message.command, 'account_info');
|
||||
if (message.account === addresses.ACCOUNT) {
|
||||
conn.send(fixtures.accountInfoResponse(message));
|
||||
} else if (message.account === addresses.NOTFOUND) {
|
||||
conn.send(fixtures.accountNotFoundResponse(message));
|
||||
mock.on('request_account_info', function(request, conn) {
|
||||
assert.strictEqual(request.command, 'account_info');
|
||||
if (request.account === addresses.ACCOUNT) {
|
||||
conn.send(fixtures.accountInfoResponse(request));
|
||||
} else if (request.account === addresses.NOTFOUND) {
|
||||
conn.send(fixtures.accountNotFoundResponse(request));
|
||||
} else {
|
||||
assert(false, 'Unrecognized account address: ' + message.account);
|
||||
assert(false, 'Unrecognized account address: ' + request.account);
|
||||
}
|
||||
});
|
||||
|
||||
mock.on('request_ledger', function(message, conn) {
|
||||
assert.strictEqual(message.command, 'ledger');
|
||||
conn.send(fixtures.ledgerResponse(message));
|
||||
mock.on('request_ledger', function(request, conn) {
|
||||
assert.strictEqual(request.command, 'ledger');
|
||||
conn.send(fixtures.ledgerResponse(request));
|
||||
});
|
||||
|
||||
mock.on('request_tx', function(message, conn) {
|
||||
assert.strictEqual(message.command, 'tx');
|
||||
if (message.transaction === hashes.VALID_TRANSACTION_HASH) {
|
||||
conn.send(fixtures.transactionResponse(message));
|
||||
} else if (message.transaction === hashes.NOTFOUND_TRANSACTION_HASH) {
|
||||
conn.send(fixtures.transactionNotFoundResponse(message));
|
||||
mock.on('request_tx', function(request, conn) {
|
||||
assert.strictEqual(request.command, 'tx');
|
||||
if (request.transaction === hashes.VALID_TRANSACTION_HASH) {
|
||||
conn.send(fixtures.transactionResponse(request));
|
||||
} else if (request.transaction === hashes.NOTFOUND_TRANSACTION_HASH) {
|
||||
conn.send(fixtures.transactionNotFoundResponse(request));
|
||||
} else {
|
||||
assert(false, 'Unrecognized transaction hash: ' + message.transaction);
|
||||
assert(false, 'Unrecognized transaction hash: ' + request.transaction);
|
||||
}
|
||||
});
|
||||
|
||||
mock.on('request_account_lines', function(message, conn) {
|
||||
if (message.account === addresses.ACCOUNT) {
|
||||
conn.send(fixtures.accountLinesResponse(message));
|
||||
} else if (message.account === addresses.OTHER_ACCOUNT) {
|
||||
conn.send(fixtures.accountLinesCounterpartyResponse(message));
|
||||
mock.on('request_submit', function(request, conn) {
|
||||
assert.strictEqual(request.command, 'submit');
|
||||
conn.send(fixtures.submitResponse(request));
|
||||
});
|
||||
|
||||
mock.on('request_account_lines', function(request, conn) {
|
||||
if (request.account === addresses.ACCOUNT) {
|
||||
conn.send(fixtures.accountLinesResponse(request));
|
||||
} else if (request.account === addresses.OTHER_ACCOUNT) {
|
||||
conn.send(fixtures.accountLinesCounterpartyResponse(request));
|
||||
} else {
|
||||
assert(false, 'Unrecognized account address: ' + message.account);
|
||||
assert(false, 'Unrecognized account address: ' + request.account);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user