From ab2dd159df40cb4964186ad4d4b6c08d43afecbf Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 28 Jul 2020 17:57:57 +0200 Subject: [PATCH] Extract and improve getAllContractState tests --- packages/cosmwasm/src/lcdapi/wasm.spec.ts | 49 +++++++++++++---------- packages/cosmwasm/src/testutils.spec.ts | 3 +- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/packages/cosmwasm/src/lcdapi/wasm.spec.ts b/packages/cosmwasm/src/lcdapi/wasm.spec.ts index d61b4f90..98c4cbcb 100644 --- a/packages/cosmwasm/src/lcdapi/wasm.spec.ts +++ b/packages/cosmwasm/src/lcdapi/wasm.spec.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { Sha256 } from "@cosmjs/crypto"; -import { Bech32, fromAscii, fromHex, toAscii, toBase64, toHex } from "@cosmjs/encoding"; +import { Bech32, fromAscii, fromHex, fromUtf8, toAscii, toBase64, toHex } from "@cosmjs/encoding"; import { assertIsPostTxSuccess, AuthExtension, @@ -29,6 +29,7 @@ import { } from "../msgs"; import { alice, + base64Matcher, bech32AddressMatcher, ContractUploadInstructions, deployedErc20, @@ -132,6 +133,7 @@ async function executeContract( describe("WasmExtension", () => { const hackatom = getHackatom(); + const hackatomConfigKey = toAscii("config"); let hackatomCodeId: number | undefined; let hackatomContractAddress: string | undefined; @@ -278,6 +280,28 @@ describe("WasmExtension", () => { }); }); + describe("getAllContractState", () => { + it("can get all state", async () => { + pendingWithoutWasmd(); + assert(hackatomContractAddress); + const client = makeWasmClient(wasmd.endpoint); + const state = await client.wasm.getAllContractState(hackatomContractAddress); + expect(state.length).toEqual(1); + const data = state[0]; + expect(data.key).toEqual(hackatomConfigKey); + const value = JSON.parse(fromUtf8(data.val)); + expect(value.verifier).toMatch(base64Matcher); + expect(value.beneficiary).toMatch(base64Matcher); + }); + + it("is empty for non-existent address", async () => { + const client = makeWasmClient(wasmd.endpoint); + const nonExistentAddress = makeRandomAddress(); + const state = await client.wasm.getAllContractState(nonExistentAddress); + expect(state).toEqual([]); + }); + }); + describe("txsQuery", () => { it("can query by tags (module + code_id)", async () => { pendingWithoutWasmd(); @@ -452,32 +476,13 @@ describe("WasmExtension", () => { describe("contract state", () => { const client = makeWasmClient(wasmd.endpoint); const noContract = makeRandomAddress(); - const expectedKey = toAscii("config"); - - it("can get all state", async () => { - pendingWithoutWasmd(); - assert(hackatomContractAddress); - - // get contract state - const state = await client.wasm.getAllContractState(hackatomContractAddress); - expect(state.length).toEqual(1); - const data = state[0]; - expect(data.key).toEqual(expectedKey); - const value = JSON.parse(fromAscii(data.val)); - expect(value.verifier).toBeDefined(); - expect(value.beneficiary).toBeDefined(); - - // bad address is empty array - const noContractState = await client.wasm.getAllContractState(noContract); - expect(noContractState).toEqual([]); - }); it("can query by key", async () => { pendingWithoutWasmd(); assert(hackatomContractAddress); // query by one key - const raw = await client.wasm.queryContractRaw(hackatomContractAddress, expectedKey); + const raw = await client.wasm.queryContractRaw(hackatomContractAddress, hackatomConfigKey); assert(raw, "must get result"); const model = JSON.parse(fromAscii(raw)); expect(model.verifier).toBeDefined(); @@ -488,7 +493,7 @@ describe("WasmExtension", () => { expect(missing).toBeNull(); // bad address is null - const noContractModel = await client.wasm.queryContractRaw(noContract, expectedKey); + const noContractModel = await client.wasm.queryContractRaw(noContract, hackatomConfigKey); expect(noContractModel).toBeNull(); }); diff --git a/packages/cosmwasm/src/testutils.spec.ts b/packages/cosmwasm/src/testutils.spec.ts index ab014313..8c953ee6 100644 --- a/packages/cosmwasm/src/testutils.spec.ts +++ b/packages/cosmwasm/src/testutils.spec.ts @@ -25,7 +25,8 @@ export function makeRandomAddress(): string { } export const tendermintIdMatcher = /^[0-9A-F]{64}$/; - +/** @see https://rgxdb.com/r/1NUN74O6 */ +export const base64Matcher = /^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$/; // https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32 export const bech32AddressMatcher = /^[\x21-\x7e]{1,83}1[02-9ac-hj-np-z]{38}$/;