From 394f015a5e4290316e2c0cd701ff589890ab3db6 Mon Sep 17 00:00:00 2001 From: nabarun Date: Fri, 8 Apr 2022 18:02:47 +0530 Subject: [PATCH] Add tests for nameservice deleteName --- src/index.ts | 1 - src/naming.test.ts | 36 +++++++++++++++++++++++++++++++----- src/registry-client.ts | 4 ++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 981d508..16477b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ - import isUrl from 'is-url'; import { sha256 } from 'js-sha256'; import { generatePostBodyBroadcast, BroadcastMode } from '@tharsis/provider'; diff --git a/src/naming.test.ts b/src/naming.test.ts index 8006786..780fa0d 100644 --- a/src/naming.test.ts +++ b/src/naming.test.ts @@ -24,6 +24,7 @@ const namingTests = () => { let authorityName: string; let otherAuthorityName: string; let otherPrivateKey: string; + let otherAccountAddress: string; let wrn: string; @@ -155,8 +156,7 @@ const namingTests = () => { expect(records).toHaveLength(1); const [{ attributes }] = records; - // TODO: Set name with new record. - // expect(attributes).toEqual(watcher.record); + expect(attributes).toEqual(watcher.record); }); test('Lookup name with history', async () => { @@ -207,14 +207,15 @@ const namingTests = () => { const mnenonic = Account.generateMnemonic(); const otherAccount = await Account.generateFromMnemonic(mnenonic); await otherAccount.init() - assert(otherAccount.formattedCosmosAddress) // TODO: Get correct account address from private key. - await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount.formattedCosmosAddress }, accountAddress, privateKey, fee); + assert(otherAccount.formattedCosmosAddress); + otherAccountAddress = otherAccount.formattedCosmosAddress + await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccountAddress }, accountAddress, privateKey, fee); // Other account reserves an authority. otherAuthorityName = `other-${Date.now()}`; otherPrivateKey = otherAccount.privateKey.toString('hex'); - await registry.reserveAuthority({ name: otherAuthorityName, owner: otherAccount.formattedCosmosAddress }, otherAccount.formattedCosmosAddress, otherPrivateKey, fee); + await registry.reserveAuthority({ name: otherAuthorityName, owner: otherAccountAddress }, otherAccountAddress, otherPrivateKey, fee); // Try setting name under other authority. await expect(registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, accountAddress, privateKey, fee)).rejects.toThrow('Access denied.'); @@ -260,6 +261,31 @@ const namingTests = () => { expect(records).toBeDefined(); // expect(records).toHaveLength(1); }); + + test('Delete already deleted name', async () => { + await registry.deleteName({ wrn }, accountAddress, privateKey, fee); + + const records = await registry.lookupNames([wrn], true); + expect(records).toBeDefined(); + expect(records).toBeDefined(); + expect(records).toHaveLength(1); + + const [{ latest }] = records; + expect(latest).toBeDefined(); + expect(latest.id).toBeDefined(); + expect(latest.id).toBe(''); + expect(latest.height).toBeDefined(); + }); + + xtest('Delete name for non-owned authority.', async () => { + const otherBondId = await registry.getNextBondId(otherPrivateKey); + await registry.createBond({ denom: 'aphoton', amount: '10000' }, otherAccountAddress, otherPrivateKey, fee); + await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherAccountAddress, otherPrivateKey, fee); + await registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, otherAccountAddress, otherPrivateKey, fee); + + // Try deleting name under other authority. + await expect(registry.deleteName({ wrn: `wrn://${otherAuthorityName}/app/test` }, accountAddress, privateKey, fee)).rejects.toThrow('Access denied.'); + }); }; if (process.env.AUCTIONS_ENABLED) { diff --git a/src/registry-client.ts b/src/registry-client.ts index d9d0d44..5fa4e23 100644 --- a/src/registry-client.ts +++ b/src/registry-client.ts @@ -251,8 +251,8 @@ export class RegistryClient { names }; - const result = (await this._graph(query)(variables))['resolveNames']; - result.records = RegistryClient.prepareAttributes('attributes')(result); + let result = (await this._graph(query)(variables))['resolveNames']; + result = RegistryClient.prepareAttributes('attributes')(result); return result; }