From e563cfe6b11ea2373f4df0487c1c4e1e0000ca6e Mon Sep 17 00:00:00 2001 From: nabarun Date: Tue, 19 Apr 2022 15:54:41 +0530 Subject: [PATCH] Add tests for record and authority expiry --- README.md | 20 ++++++ package.json | 3 +- src/auction.test.ts | 2 +- src/index.ts | 1 - src/nameservice-expiry.test.ts | 116 +++++++++++++++++++++++++++++++++ src/naming.test.ts | 4 +- src/util.test.ts | 2 +- 7 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 src/nameservice-expiry.test.ts diff --git a/README.md b/README.md index 7e2c8ec..c176f41 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,26 @@ Follow these steps to run the tests: yarn test:auctions ``` +- Run the tests for record and authority expiry + + - In chiba-clonk repo run: + + ```bash + TEST_NAMESERVICE_EXPIRY=true ./init.sh + ``` + + - Export the private key and change it in `.env` file again using: + + ```bash + chibaclonkd keys export mykey --unarmored-hex --unsafe + ``` + + - Run tests: + + ```bash + yarn test:expiry + ``` + ## Development [README](./DEVELOPMENT.md) diff --git a/package.json b/package.json index 52d8b50..52ec1e1 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ }, "scripts": { "test": "jest --runInBand --verbose", - "test:auctions": "AUCTIONS_ENABLED=1 jest --runInBand --verbose", + "test:auctions": "TEST_AUCTIONS_ENABLED=1 jest --runInBand --verbose src/auction.test.ts", + "test:expiry": "TEST_NAMESERVICE_EXPIRY=1 jest --runInBand --verbose src/nameservice-expiry.test.ts", "build": "tsc" } } diff --git a/src/auction.test.ts b/src/auction.test.ts index 32b6ca4..00e9088 100644 --- a/src/auction.test.ts +++ b/src/auction.test.ts @@ -110,7 +110,7 @@ const auctionTests = (numBidders = 3) => { const withNumBidders = (numBidders: number) => () => auctionTests(numBidders); -if (!process.env.AUCTIONS_ENABLED) { +if (!process.env.TEST_AUCTIONS_ENABLED) { // Required as jest complains if file has no tests. test('skipping auction tests', () => {}); } else { diff --git a/src/index.ts b/src/index.ts index 2687842..63da9cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,7 +95,6 @@ export class Registry { _client: RegistryClient static processWriteError(error: string) { - console.log("error", error) // error string a stacktrace containing the message. // https://gist.github.com/nikugogoi/de55d390574ded3466abad8bffd81952#file-txresponse-js-L7 const errorMessage = NAMESERVICE_ERRORS.find(message => error.includes(message)) diff --git a/src/nameservice-expiry.test.ts b/src/nameservice-expiry.test.ts new file mode 100644 index 0000000..4acf730 --- /dev/null +++ b/src/nameservice-expiry.test.ts @@ -0,0 +1,116 @@ +import path from 'path'; + +import { Registry } from './index'; +import { ensureUpdatedConfig, getConfig } from './testing/helper'; + +const WATCHER_YML_PATH = path.join(__dirname, './testing/data/watcher.yml'); + +jest.setTimeout(120 * 1000); + +const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); + +const nameserviceExpiryTests = () => { + let registry: Registry; + + let bondId: string; + let watcher: any; + + let authorityName: string; + let authorityExpiryTime: Date; + let recordExpiryTime: Date; + + beforeAll(async () => { + registry = new Registry(restEndpoint, gqlEndpoint, chainId); + + // Create bond. + bondId = await registry.getNextBondId(privateKey); + await registry.createBond({ denom: 'aphoton', amount: '3000000' }, privateKey, fee); + }); + + test('Set record and check bond balance', async () => { + // Create watcher. + watcher = await ensureUpdatedConfig(WATCHER_YML_PATH); + await registry.setRecord( + { + privateKey, + bondId, + record: watcher.record + }, + privateKey, + fee + ) + + const [record] = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }, true); + recordExpiryTime = new Date(record.expiryTime); + + const [bond] = await registry.getBondsByIds([bondId]); + expect(bond).toBeDefined(); + expect(bond.balance).toHaveLength(1); + expect(bond.balance[0].quantity).toBe('2000000'); + }) + + test('Reserve authority and set bond', async () => { + authorityName = `dxos-${Date.now()}`; + await registry.reserveAuthority({ name: authorityName }, privateKey, fee); + await registry.setAuthorityBond({ name: authorityName, bondId }, privateKey, fee); + const [authority] = await registry.lookupAuthorities([authorityName]); + expect(authority.status).toBe('active'); + authorityExpiryTime = new Date(authority.expiryTime); + }); + + test('Wait for expiry duration', (done) => { + setTimeout(done, 60 * 1000); + }); + + test('Check record expiry time', async() => { + const [record] = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }, true); + const updatedExpiryTime = new Date(record.expiryTime); + expect(updatedExpiryTime.getTime()).toBeGreaterThan(recordExpiryTime.getTime()); + recordExpiryTime = updatedExpiryTime; + }) + + test('Check authority expiry time', async() => { + const [authority] = await registry.lookupAuthorities([authorityName]); + const updatedExpiryTime = new Date(authority.expiryTime); + expect(updatedExpiryTime.getTime()).toBeGreaterThan(authorityExpiryTime.getTime()); + authorityExpiryTime = updatedExpiryTime; + }) + + test('Check bond balance', async () => { + const [bond] = await registry.getBondsByIds([bondId]); + expect(bond).toBeDefined(); + expect(bond.balance).toHaveLength(0); + }) + + test('Wait for expiry duration', (done) => { + setTimeout(done, 60 * 1000); + }); + + test('Check record deleted without balance', async() => { + const records = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }, true); + expect(records).toHaveLength(0); + }) + + test('Check authority expired without balance', async() => { + const [authority] = await registry.lookupAuthorities([authorityName]); + expect(authority.status).toBe('expired'); + }) +} + +if (!process.env.TEST_NAMESERVICE_EXPIRY) { + // Required as jest complains if file has no tests. + test('skipping nameservice expiry tests', () => {}); +} else { + /** + Running these tests requires timers to be set. In chiba-clonk repo run: + + TEST_NAMESERVICE_EXPIRY=true ./init.sh + + + Run tests: + + yarn test:expiry + */ + + describe('Nameservice Expiry', nameserviceExpiryTests) +} diff --git a/src/naming.test.ts b/src/naming.test.ts index 49239c3..80616d4 100644 --- a/src/naming.test.ts +++ b/src/naming.test.ts @@ -31,7 +31,7 @@ const namingTests = () => { bondId = await registry.getNextBondId(privateKey); await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, privateKey, fee); - // Create bot. + // Create watcher. watcher = await ensureUpdatedConfig(WATCHER_YML_PATH); await registry.setRecord( { @@ -267,7 +267,7 @@ const namingTests = () => { }); }; -if (process.env.AUCTIONS_ENABLED) { +if (process.env.TEST_AUCTIONS_ENABLED) { // Required as jest complains if file has no tests. test('skipping naming tests', () => {}); } else { diff --git a/src/util.test.ts b/src/util.test.ts index e686773..d586b0e 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -24,7 +24,7 @@ const utilTests = () => { bondId = await registry.getNextBondId(privateKey); await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, privateKey, fee); - // Create bot. + // Create watcher. watcher = await getBaseConfig(WATCHER_YML_PATH); await registry.setRecord( {