Skip to content

Commit

Permalink
add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbosworth committed Oct 9, 2019
1 parent c0cd322 commit 44b0d6b
Show file tree
Hide file tree
Showing 31 changed files with 954 additions and 121 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ for `unlocker` methods.
- [createWallet](#createWallet) - Make a new wallet
- [decodePaymentRequest](#decodePaymentRequest) - Decode a Lightning invoice
- [deleteForwardingReputations](#deleteForwardingReputations) - Wipe node reps
- [deletePayments](#deletePayments) - Delete entire history of past payments
- [disconnectWatchtower](#disconnectWatchtower) - Disconnect a watchtower
- [getAutopilot](#getAutopilot) - Get autopilot status or node scores
- [getBackup](#getBackup) - Get a backup of a channel
Expand Down Expand Up @@ -675,11 +676,29 @@ Requires LND built with routerrpc build tag

@returns via cbk or Promise

Example:

```node
const {deleteForwardingReputations} = require('ln-service');
await deleteForwardingReputations({});
```

### deletePayments

Delete all records of payments

{
lnd: <Authenticated LND gRPC API Object>
}

@returns via cbk or Promise

Example:
```node
const {deletePayments} = require('ln-service');
await deletePayments({lnd});
```

### disconnectWatchtower

Disconnect a watchtower
Expand Down
4 changes: 4 additions & 0 deletions chain/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const isHash = require('./is_hash');
const isTransaction = require('./is_transaction');
const subscribeToBlocks = require('./subscribe_to_blocks');
const subscribeToChainAddress = require('./subscribe_to_chain_address');
const subscribeToChainSpend = require('./subscribe_to_chain_spend');

module.exports = {
isHash,
isTransaction,
subscribeToBlocks,
subscribeToChainAddress,
subscribeToChainSpend,
Expand Down
18 changes: 18 additions & 0 deletions chain/is_hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const isHex = require('is-hex');

const hashHexLength = 64;

/** Determine if a string looks like a regular 256 bit hex encoded hash
{
[hash]: <Hash String>
}
@returns
{
is_hash: <Looks Like Regular Hash Bool>
}
*/
module.exports = ({hash}) => {
return {is_hash: !!hash && isHex(hash) && hash.length === hashHexLength};
};
25 changes: 25 additions & 0 deletions chain/is_transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const isHex = require('is-hex');
const {Transaction} = require('bitcoinjs-lib');

/** Determine if a hex string is a regular Transaction
{
[transaction]: <Transaction Hex String>
}
@returns
<Looks Like Regular Transaction Bool>
*/
module.exports = ({transaction}) => {
if (!transaction || !isHex(transaction)) {
return false;
}

try {
Transaction.fromHex(transaction);
} catch (err) {
return false;
}

return true;
};
3 changes: 2 additions & 1 deletion grpc/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const authenticatedLndGrpc = require('./authenticated_lnd_grpc');
const isLnd = require('./is_lnd');
const unauthenticatedLndGrpc = require('./unauthenticated_lnd_grpc');

module.exports = {authenticatedLndGrpc, unauthenticatedLndGrpc};
module.exports = {authenticatedLndGrpc, isLnd, unauthenticatedLndGrpc};
14 changes: 14 additions & 0 deletions grpc/is_lnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** Determine if object is an expected LND Object
{
[lnd]: <LND Object>
[method]: <Method Name String>
[type]: <Method Type String>
}
@returns
<Is Expected LND Object Bool>
*/
module.exports = ({lnd, method, type}) => {
return !!lnd && !!lnd[type] && !!lnd[type][method];
};
2 changes: 2 additions & 0 deletions lightning/delete_payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {returnResult} = require('asyncjs-util');
{
lnd: <Authenticated LND gRPC API Object>
}
@returns via cbk or Promise
*/
module.exports = ({lnd}, cbk) => {
return new Promise((resolve, reject) => {
Expand Down
5 changes: 3 additions & 2 deletions lightning/get_backup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const asyncAuto = require('async/auto');
const isHex = require('is-hex');
const {returnResult} = require('asyncjs-util');

const {isHash} = require('./../chain');

/** Get the static channel backup for a channel
{
Expand All @@ -24,7 +25,7 @@ module.exports = (args, cbk) => {
return cbk([400, 'ExpectedGrpcApiConnectionToGetChannelBackup']);
}

if (!args.transaction_id || !isHex(args.transaction_id)) {
if (!isHash({hash: args.transaction_id}).is_hash) {
return cbk([400, 'ExpectedTxIdOfChannelToGetChannelBackup']);
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
"url": "https://github.com/alexbosworth/ln-service.git"
},
"scripts": {
"all-integration-tests": "tap -j 1 -t 120 test/autopilot/*.js test/autopilotrpc-integration/*.js test/backups/*.js test/chain/*.js test/bolt02/*.js test/bolt11/*.js test/chainrpc-integration/*.js test/graph/*.js test/grpc/*.js test/grpc-integration/*.js test/integration/*.js test/invoices/*.js test/invoicesrpc-integration/*.js test/router/*.js test/routerrpc-integration/*.js test/routing/*.js test/signerrpc-integration/*.js test/tower_serverrpc-integration/*.js test/walletrpc-integration/*.js",
"all-integration-tests": "tap -j 1 -t 120 test/autopilot/*.js test/autopilotrpc-integration/*.js test/backups/*.js test/chain/*.js test/bolt02/*.js test/bolt11/*.js test/chainrpc-integration/*.js test/graph/*.js test/grpc/*.js test/grpc-integration/*.js test/integration/*.js test/invoices/*.js test/invoicesrpc-integration/*.js test/router/*.js test/routerrpc-integration/*.js test/routing/*.js test/signerrpc-integration/*.js test/tower_serverrpc-integration/*.js test/towers/*.js test/unlocker/*.js test/wallet/*.js test/walletrpc-integration/*.js",
"autopilot-integration-tests": "tap --no-coverage -t 90 test/autopilotrpc-integration/*.js",
"chain-integration-tests": "tap --no-coverage -t 90 test/chainrpc-integration/*.js",
"integration-tests": "tap --no-coverage -t 90 test/grpc-integration/*.js test/integration/*.js",
"invoices-integration-tests": "tap --no-coverage -t 90 test/invoicesrpc-integration/*.js",
"router-integration-tests": "tap --no-coverage -j 2 -t 90 test/routerrpc-integration/*.js",
"signer-integration-tests": "tap --no-coverage test/signerrpc-integration/*.js",
"start": "node server.js",
"test": "tap test/autopilot/*.js test/backups/*.js test/bolt02/*.js test/bolt11/*.js test/chain/*.js test/graph/*.js test/grpc/*.js test/invoices/*.js test/router/*.js test/routing/*.js",
"test": "tap test/autopilot/*.js test/backups/*.js test/bolt02/*.js test/bolt11/*.js test/chain/*.js test/graph/*.js test/grpc/*.js test/invoices/*.js test/router/*.js test/routing/*.js test/towers/*.js test/unlocker/*.js test/wallet/*.js",
"tower_client-integration-tests": "tap --no-coverage test/tower_clientrpc-integration/*.js",
"tower_server-integration-tests": "tap --no-coverage test/tower_serverrpc-integration/*.js",
"wallet-integration-tests": "tap --no-coverage test/walletrpc-integration/*.js"
Expand Down
99 changes: 99 additions & 0 deletions test/backups/test_get_backup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const {test} = require('tap');

const {getBackup} = require('./../../');

const txId = Buffer.alloc(32).toString('hex');

const tests = [
{
args: {},
description: 'LND object is required',
error: [400, 'ExpectedGrpcApiConnectionToGetChannelBackup'],
},
{
args: {lnd: {}},
description: 'LND object with default methods is required',
error: [400, 'ExpectedGrpcApiConnectionToGetChannelBackup'],
},
{
args: {lnd: {default: {}}},
description: 'A funding transaction id is required',
error: [400, 'ExpectedTxIdOfChannelToGetChannelBackup'],
},
{
args: {lnd: {default: {}}, transaction_id: txId},
description: 'A funding transaction vout is required',
error: [400, 'ExpectedTxOutputIndexToGetChannelBackup'],
},
{
args: {
lnd: {default: {exportChannelBackup: ({}, cbk) => cbk('err')}},
transaction_id: txId,
transaction_vout: 0,
},
description: 'An unexpected error is returned',
error: [503, 'UnexpectedErrExportingBackupForChannel', {err: 'err'}],
},
{
args: {
lnd: {default: {exportChannelBackup: ({}, cbk) => cbk()}},
transaction_id: txId,
transaction_vout: 0,
},
description: 'A result is returned',
error: [503, 'ExpectedResultOfGetChannelBackupRequest'],
},
{
args: {
lnd: {default: {exportChannelBackup: ({}, cbk) => cbk(null, {})}},
transaction_id: txId,
transaction_vout: 0,
},
description: 'A chan backup result is returned',
error: [503, 'UnexpectedResponseForChannelBackupRequest'],
},
{
args: {
lnd: {
default: {
exportChannelBackup: ({}, cbk) => cbk(null, {
chan_backup: Buffer.alloc(0),
}),
},
},
transaction_id: txId,
transaction_vout: 0,
},
description: 'A non empty chan backup result is returned',
error: [503, 'UnexpectedResponseForChannelBackupRequest'],
},
{
args: {
lnd: {
default: {
exportChannelBackup: ({}, cbk) => cbk(null, {
chan_backup: Buffer.alloc(1),
}),
},
},
transaction_id: txId,
transaction_vout: 0,
},
description: 'A non empty chan backup result is returned',
expected: {backup: '00'},
},
];

tests.forEach(({args, description, error, expected}) => {
return test(description, async ({deepEqual, end, equal, rejects}) => {
if (!!error) {
rejects(() => getBackup(args), error, 'Got expected error');
} else {
const {backup} = await getBackup(args);

equal(backup, expected.backup, 'Got expected backup');
}

return end();
});
});
107 changes: 107 additions & 0 deletions test/chain/test_create_chain_address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const {test} = require('tap');

const {createChainAddress} = require('./../../');

const tests = [
{
args: {},
description: 'An address format is required',
error: [400, 'ExpectedKnownAddressFormat'],
},
{
args: {format: 'foo'},
description: 'A known address format is required',
error: [400, 'ExpectedKnownAddressFormat'],
},
{
args: {format: 'p2wpkh'},
description: 'LND is required',
error: [400, 'ExpectedLndForAddressCreation'],
},
{
args: {format: 'p2wpkh', lnd: {}},
description: 'LND with default is required',
error: [400, 'ExpectedLndForAddressCreation'],
},
{
args: {format: 'p2wpkh', lnd: {default: {}}},
description: 'LND with default is required',
error: [400, 'ExpectedLndForAddressCreation'],
},
{
args: {
format: 'p2wpkh',
lnd: {
default: {
newAddress: ({}, cbk) => cbk({
message: '14 UNAVAILABLE: Connect Failed',
}),
},
},
},
description: 'Connection failure error is returned',
error: [503, 'FailedToConnectToDaemonToCreateChainAddress'],
},
{
args: {
format: 'p2wpkh',
lnd: {default: {newAddress: ({}, cbk) => cbk('err')}},
},
description: 'Unanticipated errors are returned',
error: [503, 'UnexpectedErrorCreatingAddress', {err: 'err'}],
},
{
args: {format: 'p2wpkh', lnd: {default: {newAddress: ({}, cbk) => cbk()}}},
description: 'A result is required',
error: [503, 'ExpectedResponseForAddressCreation'],
},
{
args: {
format: 'p2wpkh',
lnd: {default: {newAddress: ({}, cbk) => cbk(null, {})}},
},
description: 'An address is required',
error: [503, 'ExpectedAddressInCreateAddressResponse'],
},
{
args: {
format: 'p2wpkh',
lnd: {default: {newAddress: ({}, cbk) => cbk(null, {address: 'addr'})}},
},
description: 'An address is required',
expected: {address: 'addr'},
},
{
args: {
format: 'p2wpkh',
is_unused: true,
lnd: {
default: {
newAddress: (args, cbk) => {
if (args.type !== 2) {
return cbk([500, 'FailedToSetUnusedFlagForAddress', args.type]);
}

return cbk(null, {address: 'addr'});
},
},
},
},
description: 'An unused address gets an unused address',
expected: {address: 'addr'},
},
];

tests.forEach(({args, description, error, expected}) => {
return test(description, async ({deepEqual, end, equal, rejects}) => {
if (!!error) {
rejects(() => createChainAddress(args), error, 'Got expected error');
} else {
const {address} = await createChainAddress(args);

equal(address, expected.address, 'Got expected new address');
}

return end();
});
});
Loading

0 comments on commit 44b0d6b

Please sign in to comment.