From d6333db8cb96f983644562657d1b5465f49b4b07 Mon Sep 17 00:00:00 2001 From: Mike Carson Date: Fri, 7 Aug 2020 11:11:19 -0400 Subject: [PATCH] test: update wallet http test to use hs-client --- test/wallet-http-test.js | 184 +++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 93 deletions(-) diff --git a/test/wallet-http-test.js b/test/wallet-http-test.js index 46e3f6fcb..cf196ba0b 100644 --- a/test/wallet-http-test.js +++ b/test/wallet-http-test.js @@ -58,8 +58,6 @@ const { transferLockup } = network.names; -// TODO: convert to using hs-client methods -// when the new version is published describe('Wallet HTTP', function() { this.timeout(20000); @@ -224,7 +222,7 @@ describe('Wallet HTTP', function() { }); it('should have no name state indexed initially', async () => { - const names = await wallet.client.get(`/wallet/${wallet.id}/name`); + const names = await wallet.getNames(); assert.strictEqual(names.length, 0); }); @@ -254,7 +252,7 @@ describe('Wallet HTTP', function() { }); it('should create an open and broadcast the tx', async () => { - const json = await wallet.client.post(`/wallet/${wallet.id}/open`, { + const json = await wallet.createOpen({ name: name }); @@ -274,7 +272,7 @@ describe('Wallet HTTP', function() { }); it('should create an open and not broadcast the transaction', async () => { - const json = await wallet.client.post(`/wallet/${wallet.id}/open`, { + const json = await wallet.createOpen({ name: name, broadcast: false }); @@ -310,7 +308,7 @@ describe('Wallet HTTP', function() { }); it('should create an open and not sign the transaction', async () => { - const json = await wallet.client.post(`/wallet/${wallet.id}/open`, { + const json = await wallet.createOpen({ name: name, broadcast: false, sign: false @@ -346,7 +344,7 @@ describe('Wallet HTTP', function() { }); it('should throw error with incompatible broadcast and sign options', async () => { - const fn = async () => await (wallet.client.post(`/wallet/${wallet.id}/open`, { + const fn = async () => await (wallet.createOpen({ name: name, broadcast: true, sign: false @@ -360,7 +358,7 @@ describe('Wallet HTTP', function() { assert.equal(info.balance.tx, 0); assert.equal(info.balance.coin, 0); - const fn = async () => (await wallet.client.post(`/wallet/${wallet.id}/open`, { + const fn = async () => (await wallet.createOpen({ name: name, account: accountTwo })); @@ -381,7 +379,7 @@ describe('Wallet HTTP', function() { }); it('should create open for specific account', async () => { - const json = await wallet.client.post(`/wallet/${wallet.id}/open`, { + const json = await wallet.createOpen({ name: name, account: accountTwo }); @@ -396,7 +394,7 @@ describe('Wallet HTTP', function() { }); it('should open an auction', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); @@ -405,7 +403,7 @@ describe('Wallet HTTP', function() { await mineBlocks(treeInterval + 1, cbAddress); - const json = await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const json = await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -433,7 +431,7 @@ describe('Wallet HTTP', function() { it('should be able to get nonce', async () => { const bid = 100; - const response = await wallet.client.get(`/wallet/${wallet.id}/nonce/${name}`, { + const response = await wallet.getNonce(name, { address: cbAddress, bid: bid }); @@ -458,7 +456,7 @@ describe('Wallet HTTP', function() { it('should be able to get nonce for bid=0', async () => { const bid = 0; - const response = await wallet.client.get(`/wallet/${wallet.id}/nonce/${name}`, { + const response = await wallet.getNonce(name, { address: cbAddress, bid: bid }); @@ -481,18 +479,18 @@ describe('Wallet HTTP', function() { }); it('should get name info', async () => { - const names = await wallet.client.get(`/wallet/${wallet.id}/name`); + const names = await wallet.getNames(); assert(names.length > 0); const [ns] = names; - const nameInfo = await wallet.client.get(`/wallet/${wallet.id}/name/${ns.name}`); + const nameInfo = await wallet.getName(ns.name); assert.deepEqual(ns, nameInfo); }); it('should fail to open a bid without a bid value', async () => { - const fn = async () => (await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const fn = async () => (await wallet.createBid({ name: name })); @@ -500,7 +498,7 @@ describe('Wallet HTTP', function() { }); it('should fail to open a bid without a lockup value', async () => { - const fn = async () => (await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const fn = async () => (await wallet.createBid({ name: name, bid: 1000 })); @@ -509,13 +507,13 @@ describe('Wallet HTTP', function() { }); it('should send bid with 0 value and non-dust lockup', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 0, lockup: 1000 @@ -523,13 +521,13 @@ describe('Wallet HTTP', function() { }); it('should fail to send bid with 0 value and 0 lockup', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - const fn = async () => await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const fn = async () => await wallet.createBid({ name: name, bid: 0, lockup: 0 @@ -539,25 +537,25 @@ describe('Wallet HTTP', function() { }); it('should get all bids (single player)', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - const tx1 = await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const tx1 = await wallet.createBid({ name: name, bid: 1000, lockup: 2000 }); - const tx2 = await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const tx2 = await wallet.createBid({ name: name, bid: 2000, lockup: 3000 }); - const tx3 = await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const tx3 = await wallet.createBid({ name: name, bid: 4000, lockup: 5000 @@ -566,7 +564,7 @@ describe('Wallet HTTP', function() { await mineBlocks(1, cbAddress); // this method gets all bids for all names - const bids = await wallet.client.get(`/wallet/${wallet.id}/bid`); + const bids = await wallet.getBids(); // this depends on this it block creating // the first bids of this test suite @@ -596,19 +594,19 @@ describe('Wallet HTTP', function() { }); it('should get all bids (two players)', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - const tx1 = await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const tx1 = await wallet.createBid({ name: name, bid: 1000, lockup: 2000 }); - const tx2 = await wallet2.client.post(`/wallet/${wallet2.id}/bid`, { + const tx2 = await wallet2.createBid({ name: name, bid: 2000, lockup: 3000 @@ -619,7 +617,7 @@ describe('Wallet HTTP', function() { { await sleep(100); // fetch all bids for the name - const bids = await wallet.client.get(`/wallet/${wallet.id}/bid/${name}`); + const bids = await wallet.getBidsByName(name); assert.equal(bids.length, 2); // there is no value property on bids @@ -637,7 +635,7 @@ describe('Wallet HTTP', function() { { // fetch only own bids for the name - const bids = await wallet.client.get(`/wallet/${wallet.id}/bid/${name}`, {own: true}); + const bids = await wallet.getBidsByName(name, {own: true}); assert.equal(bids.length, 1); const [bid] = bids; assert.equal(bid.prevout.hash, tx1.hash); @@ -645,13 +643,13 @@ describe('Wallet HTTP', function() { }); it('should create a reveal', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -663,7 +661,7 @@ describe('Wallet HTTP', function() { assert.equal(info.name, name); assert.equal(info.state, 'REVEAL'); - const json = await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + const json = await wallet.createReveal({ name: name }); @@ -672,25 +670,25 @@ describe('Wallet HTTP', function() { }); it('should get all reveals (single player)', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); const name2 = await nclient.execute('grindname', [5]); - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name2 }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 }); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name2, bid: 2000, lockup: 3000 @@ -698,24 +696,24 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name2 }); await mineBlocks(revealPeriod + 1, cbAddress); { - const reveals = await wallet.client.get(`/wallet/${wallet.id}/reveal`); + const reveals = await wallet.getReveals(); assert.equal(reveals.length, 2); } { // a single reveal per name - const reveals = await wallet.client.get(`/wallet/${wallet.id}/reveal/${name}`); + const reveals = await wallet.getRevealsByName(name); assert.equal(reveals.length, 1); } }); @@ -731,19 +729,19 @@ describe('Wallet HTTP', function() { it('should get own reveals (two players)', async () => { state.name = name; - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - const b1 = await wallet.client.post(`/wallet/${wallet.id}/bid`, { + const b1 = await wallet.createBid({ name: name, bid: 1000, lockup: 2000 }); - const b2 = await wallet2.client.post(`/wallet/${wallet2.id}/bid`, { + const b2 = await wallet2.createBid({ name: name, bid: 2000, lockup: 3000 @@ -754,11 +752,11 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - const r1 = await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + const r1 = await wallet.createReveal({ name: name }); - const r2 = await wallet2.client.post(`/wallet/${wallet2.id}/reveal`, { + const r2 = await wallet2.createReveal({ name: name }); @@ -768,7 +766,7 @@ describe('Wallet HTTP', function() { await mineBlocks(revealPeriod + 1, cbAddress); { - const reveals = await wallet.client.get(`/wallet/${wallet.id}/reveal/${name}`, {own: true}); + const reveals = await wallet.getRevealsByName(name, {own: true}); assert.equal(reveals.length, 1); const [reveal] = reveals; assert.equal(reveal.own, true); @@ -776,7 +774,7 @@ describe('Wallet HTTP', function() { } { - const reveals = await wallet.client.get(`/wallet/${wallet.id}/reveal/${name}`); + const reveals = await wallet.getRevealsByName(name); assert.equal(reveals.length, 2); assert.ok(reveals.find(reveal => @@ -790,9 +788,9 @@ describe('Wallet HTTP', function() { }); it('should get auction info', async () => { - const ns = await wallet.client.get(`/wallet/${wallet.id}/name/${state.name}`); + const ns = await wallet.getName(state.name); - const auction = await wallet.client.get(`/wallet/${wallet.id}/auction/${ns.name}`); + const auction = await wallet.getAuctionByName(ns.name); // auction info returns a list of bids // and a list of reveals for the name @@ -815,20 +813,20 @@ describe('Wallet HTTP', function() { }); it('should create a redeem', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); // wallet2 wins the auction, wallet can submit redeem - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 }); - await wallet2.client.post(`/wallet/${wallet2.id}/bid`, { + await wallet2.createBid({ name: name, bid: 2000, lockup: 3000 @@ -836,24 +834,24 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); - await wallet2.client.post(`/wallet/${wallet2.id}/reveal`, { + await wallet2.createReveal({ name: name }); await mineBlocks(revealPeriod + 1, cbAddress); // wallet2 is the winner, therefore cannot redeem - const fn = async () => (await wallet2.client.post(`/wallet/${wallet2.id}/redeem`, { + const fn = async () => (await wallet2.createRedeem({ name: name })); await assert.rejects(fn, {message: 'No reveals to redeem.'}); - const json = await wallet.client.post(`wallet/${wallet.id}/redeem`, { + const json = await wallet.createRedeem({ name: name }); @@ -862,13 +860,13 @@ describe('Wallet HTTP', function() { }); it('should create an update', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -876,14 +874,14 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); await mineBlocks(revealPeriod + 1, cbAddress); { - const json = await wallet.client.post(`/wallet/${wallet.id}/update`, { + const json = await wallet.createUpdate({ name: name, data: { records: [ @@ -904,7 +902,7 @@ describe('Wallet HTTP', function() { await mineBlocks(1, cbAddress); { - const json = await wallet.client.post(`/wallet/${wallet.id}/update`, { + const json = await wallet.createUpdate({ name: name, data: { records: [ @@ -923,7 +921,7 @@ describe('Wallet HTTP', function() { }); it('should get name resource', async () => { - const names = await wallet.client.get(`/wallet/${wallet.id}/name`); + const names = await wallet.getNames(); // filter out names that have data // this test depends on the previous test const [ns] = names.filter(n => n.data.length > 0); @@ -931,7 +929,7 @@ describe('Wallet HTTP', function() { const state = Resource.decode(Buffer.from(ns.data, 'hex')); - const resource = await wallet.client.get(`wallet/${wallet.id}/resource/${ns.name}`); + const resource = await wallet.getResource(ns.name); assert(resource); const res = Resource.fromJSON(resource); @@ -941,18 +939,18 @@ describe('Wallet HTTP', function() { it('should fail to get name resource for non existent name', async () => { const name = await nclient.execute('grindname', [10]); - const resource = await wallet.client.get(`/wallet/${wallet.id}/resource/${name}`); + const resource = await wallet.getResource(name); assert.equal(resource, null); }); it('should create a renewal', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -960,13 +958,13 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); await mineBlocks(revealPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/update`, { + await wallet.createUpdate({ name: name, data: { records: [ @@ -982,7 +980,7 @@ describe('Wallet HTTP', function() { // can be submitted, a treeInterval into the future await mineBlocks(treeInterval + 1, cbAddress); - const json = await wallet.client.post(`/wallet/${wallet.id}/renewal`, { + const json = await wallet.createRenewal({ name }); @@ -991,13 +989,13 @@ describe('Wallet HTTP', function() { }); it('should create a transfer', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -1005,13 +1003,13 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); await mineBlocks(revealPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/update`, { + await wallet.createUpdate({ name: name, data: { records: [ @@ -1027,7 +1025,7 @@ describe('Wallet HTTP', function() { const {receiveAddress} = await wallet.getAccount(accountTwo); - const json = await wallet.client.post(`/wallet/${wallet.id}/transfer`, { + const json = await wallet.createTransfer({ name, address: receiveAddress }); @@ -1037,13 +1035,13 @@ describe('Wallet HTTP', function() { }); it('should create a finalize', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -1051,13 +1049,13 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); await mineBlocks(revealPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/update`, { + await wallet.createUpdate({ name: name, data: { records: [ @@ -1073,14 +1071,14 @@ describe('Wallet HTTP', function() { const {receiveAddress} = await wallet.getAccount(accountTwo); - await wallet.client.post(`/wallet/${wallet.id}/transfer`, { + await wallet.createTransfer({ name, address: receiveAddress }); await mineBlocks(transferLockup + 1, cbAddress); - const json = await wallet.client.post(`/wallet/${wallet.id}/finalize`, { + const json = await wallet.createFinalize({ name }); @@ -1096,13 +1094,13 @@ describe('Wallet HTTP', function() { }); it('should create a cancel', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -1110,13 +1108,13 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); await mineBlocks(revealPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/update`, { + await wallet.createUpdate({ name: name, data: { records: [ @@ -1132,14 +1130,14 @@ describe('Wallet HTTP', function() { const {receiveAddress} = await wallet.getAccount(accountTwo); - await wallet.client.post(`/wallet/${wallet.id}/transfer`, { + await wallet.createTransfer({ name, address: receiveAddress }); await mineBlocks(transferLockup + 1, cbAddress); - const json = await wallet.client.post(`/wallet/${wallet.id}/cancel`, {name}); + const json = await wallet.createCancel({name}); const cancel = json.outputs.filter(({covenant}) => covenant.type === types.UPDATE); assert.equal(cancel.length, 1); @@ -1157,13 +1155,13 @@ describe('Wallet HTTP', function() { }); it('should create a revoke', async () => { - await wallet.client.post(`/wallet/${wallet.id}/open`, { + await wallet.createOpen({ name: name }); await mineBlocks(treeInterval + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/bid`, { + await wallet.createBid({ name: name, bid: 1000, lockup: 2000 @@ -1171,13 +1169,13 @@ describe('Wallet HTTP', function() { await mineBlocks(biddingPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/reveal`, { + await wallet.createReveal({ name: name }); await mineBlocks(revealPeriod + 1, cbAddress); - await wallet.client.post(`/wallet/${wallet.id}/update`, { + await wallet.createUpdate({ name: name, data: { records: [ @@ -1191,7 +1189,7 @@ describe('Wallet HTTP', function() { await mineBlocks(treeInterval + 1, cbAddress); - const json = await wallet.client.post(`/wallet/${wallet.id}/revoke`, {name}); + const json = await wallet.createRevoke({name}); const final = json.outputs.filter(({covenant}) => covenant.type === types.REVOKE); assert.equal(final.length, 1);