From 4c480745f58e8b8e718fb9f3b574ce1d2f510c80 Mon Sep 17 00:00:00 2001 From: awadhana Date: Thu, 18 Nov 2021 16:27:26 -0500 Subject: [PATCH] test(connector-quorum): jestify v2.3.0-deploy-contract-from-json.test Migrated test from Tap to Jest. File Path: packages/cactus-plugin-ledger-connector-quorum/ src/test/typescript/integration/plugin-ledger-connector-quorum/ deploy-contract/v2.3.0-deploy-contract-from-json.test.ts This is a PARTIAL resolution to issue #238 Signed-off-by: awadhana Signed-off-by: Peter Somogyvari --- .taprc | 1 - jest.config.js | 1 - .../v2.3.0-deploy-contract-from-json.test.ts | 343 ++++++++---------- 3 files changed, 156 insertions(+), 189 deletions(-) diff --git a/.taprc b/.taprc index a8ee26cabfd..9a9305ef52d 100644 --- a/.taprc +++ b/.taprc @@ -27,7 +27,6 @@ files: - ./packages/cactus-plugin-keychain-aws-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts - ./packages/cactus-plugin-keychain-azure-kv/src/test/typescript/integration/plugin-keychain-azure-kv.test.ts - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-invoke-contract-json-object.test.ts - - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json.test.ts - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v21.4.1-deploy-contract-from-json-json-object.test.ts - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json-json-object.test.ts - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/openapi/openapi-validation.test.ts diff --git a/jest.config.js b/jest.config.js index c954fb29a37..24b0074455c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -32,7 +32,6 @@ module.exports = { `./packages/cactus-plugin-keychain-aws-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts`, `./packages/cactus-plugin-keychain-azure-kv/src/test/typescript/integration/plugin-keychain-azure-kv.test.ts`, `./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-invoke-contract-json-object.test.ts`, - `./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json.test.ts`, `./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v21.4.1-deploy-contract-from-json-json-object.test.ts`, `./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json-json-object.test.ts`, `./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/openapi/openapi-validation.test.ts`, diff --git a/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json.test.ts b/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json.test.ts index 09122401d06..a9c9b20bb57 100644 --- a/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json.test.ts +++ b/packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json.test.ts @@ -1,7 +1,7 @@ -import test, { Test } from "tape-promise/tape"; import Web3 from "web3"; +import { Account } from "web3-core"; import { v4 as uuidV4 } from "uuid"; - +import "jest-extended"; import { LogLevelDesc, IListenOptions, @@ -29,8 +29,6 @@ import { pruneDockerAllIfGithubAction, } from "@hyperledger/cactus-test-tooling"; import { PluginRegistry } from "@hyperledger/cactus-core"; - -const testCase = "Quorum Ledger Connector Plugin"; import express from "express"; import bodyParser from "body-parser"; import http from "http"; @@ -38,116 +36,136 @@ import { AddressInfo } from "net"; import { Configuration, Constants } from "@hyperledger/cactus-core-api"; import { Server as SocketIoServer } from "socket.io"; -const logLevel: LogLevelDesc = "INFO"; -const contractName = "HelloWorld"; +const testCase = "Quorum Ledger Connector Plugin"; -test("BEFORE " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning didn't throw OK"); - t.end(); -}); +describe(testCase, () => { + const logLevel: LogLevelDesc = "INFO"; + const contractName = "HelloWorld"; + const keychainEntryKey = uuidV4(); + let firstHighNetWorthAccount: string, + testEthAccount: Account, + web3: Web3, + addressInfo, + address: string, + port: number, + contractAddress: string, + apiHost, + apiConfig, + ledger: QuorumTestLedger, + apiClient: QuorumApi, + connector: PluginLedgerConnectorQuorum, + rpcApiHttpHost: string, + keychainPlugin: PluginKeychainMemory; + const expressApp = express(); + expressApp.use(bodyParser.json({ limit: "250mb" })); + const server = http.createServer(expressApp); + + const wsApi = new SocketIoServer(server, { + path: Constants.SocketIoConnectionPathV1, + }); + + beforeAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).resolves.toBeTruthy(); + }); + + beforeAll(async () => { + const containerImageVersion = "2021-01-08-7a055c3"; // Quorum v2.3.0, Tessera v0.10.0 + const containerImageName = "hyperledger/cactus-quorum-all-in-one"; + const ledgerOptions = { containerImageName, containerImageVersion }; + ledger = new QuorumTestLedger(ledgerOptions); + await ledger.start(); -test(testCase, async (t: Test) => { - const containerImageVersion = "2021-01-08-7a055c3"; // Quorum v2.3.0, Tessera v0.10.0 + const quorumGenesisOptions: IQuorumGenesisOptions = await ledger.getGenesisJsObject(); - const ledgerOptions = { containerImageVersion }; - const ledger = new QuorumTestLedger(ledgerOptions); - test.onFinish(async () => { + expect(quorumGenesisOptions).toBeTruthy(); + expect(quorumGenesisOptions.alloc).toBeTruthy(); + + const highNetWorthAccounts: string[] = Object.keys( + quorumGenesisOptions.alloc, + ).filter((address: string) => { + const anAccount: IAccount = quorumGenesisOptions.alloc[address]; + const theBalance = parseInt(anAccount.balance, 10); + return theBalance > 10e7; + }); + [firstHighNetWorthAccount] = highNetWorthAccounts; + }); + + afterAll(async () => { await ledger.stop(); await ledger.destroy(); - await pruneDockerAllIfGithubAction({ logLevel }); }); - await ledger.start(); - - const rpcApiHttpHost = await ledger.getRpcApiHttpHost(); - const quorumGenesisOptions: IQuorumGenesisOptions = await ledger.getGenesisJsObject(); - t.ok(quorumGenesisOptions); - t.ok(quorumGenesisOptions.alloc); - - const highNetWorthAccounts: string[] = Object.keys( - quorumGenesisOptions.alloc, - ).filter((address: string) => { - const anAccount: IAccount = quorumGenesisOptions.alloc[address]; - const theBalance = parseInt(anAccount.balance, 10); - return theBalance > 10e7; - }); - const [firstHighNetWorthAccount] = highNetWorthAccounts; - const web3 = new Web3(rpcApiHttpHost); - const testEthAccount = web3.eth.accounts.create(uuidV4()); + afterAll(async () => await Servers.shutdown(server)); - const keychainEntryKey = uuidV4(); - const keychainEntryValue = testEthAccount.privateKey; - const keychainPlugin = new PluginKeychainMemory({ - instanceId: uuidV4(), - keychainId: uuidV4(), - // pre-provision keychain with mock backend holding the private key of the - // test account that we'll reference while sending requests with the - // signing credential pointing to this keychain entry. - backend: new Map([[keychainEntryKey, keychainEntryValue]]), - logLevel, + afterAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).resolves.toBeTruthy(); }); - keychainPlugin.set( - HelloWorldContractJson.contractName, - JSON.stringify(HelloWorldContractJson), - ); - // Instantiate connector with the keychain plugin that already has the - // private key we want to use for one of our tests - const connector: PluginLedgerConnectorQuorum = new PluginLedgerConnectorQuorum( - { + beforeAll(async () => { + await ledger.start(); + + const listenOptions: IListenOptions = { + hostname: "0.0.0.0", + port: 0, + server, + }; + addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; + ({ address, port } = addressInfo); + apiHost = `http://${address}:${port}`; + apiConfig = new Configuration({ basePath: apiHost }); + apiClient = new QuorumApi(apiConfig); + rpcApiHttpHost = await ledger.getRpcApiHttpHost(); + web3 = new Web3(rpcApiHttpHost); + testEthAccount = web3.eth.accounts.create(uuidV4()); + + const keychainEntryValue = testEthAccount.privateKey; + keychainPlugin = new PluginKeychainMemory({ + instanceId: uuidV4(), + keychainId: uuidV4(), + // pre-provision keychain with mock backend holding the private key of the + // test account that we'll reference while sending requests with the + // signing credential pointing to this keychain entry. + backend: new Map([[keychainEntryKey, keychainEntryValue]]), + logLevel, + }); + keychainPlugin.set( + HelloWorldContractJson.contractName, + JSON.stringify(HelloWorldContractJson), + ); + connector = new PluginLedgerConnectorQuorum({ instanceId: uuidV4(), rpcApiHttpHost, logLevel, pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), - }, - ); - - const expressApp = express(); - expressApp.use(bodyParser.json({ limit: "250mb" })); - const server = http.createServer(expressApp); - const listenOptions: IListenOptions = { - hostname: "localhost", - port: 0, - server, - }; - const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; - test.onFinish(async () => await Servers.shutdown(server)); - const { address, port } = addressInfo; - const apiHost = `http://${address}:${port}`; - t.comment( - `Metrics URL: ${apiHost}/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-quorum/get-prometheus-exporter-metrics`, - ); - - const apiConfig = new Configuration({ basePath: apiHost }); - const apiClient = new QuorumApi(apiConfig); - - const wsApi = new SocketIoServer(server, { - path: Constants.SocketIoConnectionPathV1, + }); }); - await connector.getOrCreateWebServices(); - await connector.registerWebServices(expressApp, wsApi); - - await connector.transact({ - web3SigningCredential: { - ethAccount: firstHighNetWorthAccount, - secret: "", - type: Web3SigningCredentialType.GethKeychainPassword, - }, - transactionConfig: { - from: firstHighNetWorthAccount, - to: testEthAccount.address, - value: 10e9, - }, - }); + test(testCase, async () => { + // Instantiate connector with the keychain plugin that already has the + // private key we want to use for one of our tests + await connector.getOrCreateWebServices(); + await connector.registerWebServices(expressApp, wsApi); - const balance = await web3.eth.getBalance(testEthAccount.address); - t.ok(balance, "Retrieved balance of test account OK"); - t.equals(parseInt(balance, 10), 10e9, "Balance of test account is OK"); + await connector.transact({ + web3SigningCredential: { + ethAccount: firstHighNetWorthAccount, + secret: "", + type: Web3SigningCredentialType.GethKeychainPassword, + }, + transactionConfig: { + from: firstHighNetWorthAccount, + to: testEthAccount.address, + value: 10e9, + }, + }); - let contractAddress: string; + const balance = await web3.eth.getBalance(testEthAccount.address); + expect(balance).toBeTruthy(); + expect(parseInt(balance, 10)).toBe(10e9); + }); - test("deploys contract via .json file", async (t2: Test) => { + test("deploys contract via .json file", async () => { const deployOut = await connector.deployContract({ contractName: HelloWorldContractJson.contractName, keychainId: keychainPlugin.getKeychainId(), @@ -158,22 +176,12 @@ test(testCase, async (t: Test) => { }, gas: 1000000, }); - t2.ok(deployOut, "deployContract() output is truthy OK"); - t2.ok( - deployOut.transactionReceipt, - "deployContract() output.transactionReceipt is truthy OK", - ); - t2.ok( - deployOut.transactionReceipt.contractAddress, - "deployContract() output.transactionReceipt.contractAddress is truthy OK", - ); + expect(deployOut).toBeTruthy(); + expect(deployOut.transactionReceipt).toBeTruthy(); + expect(deployOut.transactionReceipt.contractAddress).toBeTruthy(); contractAddress = deployOut.transactionReceipt.contractAddress as string; - t2.ok( - typeof contractAddress === "string", - "contractAddress typeof string OK", - ); - + expect(typeof contractAddress).toBe("string"); const { callOutput: helloMsg } = await connector.getContractInfoKeychain({ contractName, invocationType: EthContractInvocationType.Call, @@ -187,14 +195,11 @@ test(testCase, async (t: Test) => { }, gas: 1000000, }); - t2.ok(helloMsg, "sayHello() output is truthy"); - t2.true( - typeof helloMsg === "string", - "sayHello() output is type of string", - ); + expect(helloMsg).toBeTruthy(); + expect(typeof helloMsg).toBe("string"); }); - test("invoke Web3SigningCredentialType.GETHKEYCHAINPASSWORD", async (t2: Test) => { + test("invoke Web3SigningCredentialType.GETHKEYCHAINPASSWORD", async () => { const newName = `DrCactus${uuidV4()}`; const setNameOut = await connector.getContractInfoKeychain({ contractName, @@ -209,10 +214,10 @@ test(testCase, async (t: Test) => { }, nonce: 2, }); - t2.ok(setNameOut, "setName() invocation #1 output is truthy OK"); + expect(setNameOut).toBeTruthy(); try { - const setNameOutInvalid = await connector.getContractInfoKeychain({ + await connector.getContractInfoKeychain({ contractName, invocationType: EthContractInvocationType.Send, methodName: "setName", @@ -226,13 +231,9 @@ test(testCase, async (t: Test) => { }, nonce: 2, }); - t2.ifError(setNameOutInvalid.transactionReceipt); + fail("Expected getContractInfoKeychain call to fail but it succeeded."); } catch (error) { - t2.notStrictEqual( - error, - "Nonce too low", - "setName() invocation with invalid nonce", - ); + expect(error).not.toEqual("Nonce too low"); } const getNameOut = await connector.getContractInfoKeychain({ @@ -247,7 +248,7 @@ test(testCase, async (t: Test) => { type: Web3SigningCredentialType.GethKeychainPassword, }, }); - t2.ok(getNameOut.success, `getName() SEND invocation produced receipt OK`); + expect(getNameOut.success).toBeTruthy(); const { callOutput: getNameOut2 } = await connector.getContractInfoKeychain( { @@ -263,16 +264,10 @@ test(testCase, async (t: Test) => { }, }, ); - t2.equal( - getNameOut2, - newName, - "setName() invocation #2 output is truthy OK", - ); - - t2.end(); + expect(getNameOut2).toBe(newName); }); - test("invoke Web3SigningCredentialType.NONE", async (t2: Test) => { + test("invoke Web3SigningCredentialType.NONE", async () => { const testEthAccount2 = web3.eth.accounts.create(uuidV4()); const { rawTransaction } = await web3.eth.accounts.signTransaction( @@ -295,12 +290,11 @@ test(testCase, async (t: Test) => { }); const balance2 = await web3.eth.getBalance(testEthAccount2.address); - t2.ok(balance2, "Retrieved balance of test account 2 OK"); - t2.equals(parseInt(balance2, 10), 10e6, "Balance of test account2 is OK"); - t2.end(); + expect(balance2).toBeTruthy(); + expect(parseInt(balance2, 10)).toBe(10e6); }); - test("invoke Web3SigningCredentialType.PrivateKeyHex", async (t2: Test) => { + test("invoke Web3SigningCredentialType.PrivateKeyHex", async () => { const newName = `DrCactus${uuidV4()}`; const setNameOut = await connector.getContractInfoKeychain({ contractName, @@ -315,10 +309,10 @@ test(testCase, async (t: Test) => { }, nonce: 1, }); - t2.ok(setNameOut, "setName() invocation #1 output is truthy OK"); + expect(setNameOut).toBeTruthy(); try { - const setNameOutInvalid = await connector.getContractInfoKeychain({ + await connector.getContractInfoKeychain({ contractName, invocationType: EthContractInvocationType.Send, methodName: "setName", @@ -332,14 +326,11 @@ test(testCase, async (t: Test) => { }, nonce: 1, }); - t2.ifError(setNameOutInvalid.transactionReceipt); + fail("Expected getContractInfoKeychain call to fail but it succeeded."); } catch (error) { - t2.notStrictEqual( - error, - "Nonce too low", - "setName() invocation with invalid nonce", - ); + expect(error).not.toEqual("Nonce too low"); } + const { callOutput: getNameOut } = await connector.getContractInfoKeychain({ contractName, invocationType: EthContractInvocationType.Call, @@ -353,7 +344,7 @@ test(testCase, async (t: Test) => { type: Web3SigningCredentialType.PrivateKeyHex, }, }); - t2.equal(getNameOut, newName, `getName() output reflects the update OK`); + expect(getNameOut).toBe(newName); const getNameOut2 = await connector.getContractInfoKeychain({ contractName, @@ -368,12 +359,10 @@ test(testCase, async (t: Test) => { type: Web3SigningCredentialType.PrivateKeyHex, }, }); - t2.ok(getNameOut2, "getName() invocation #2 output is truthy OK"); - - t2.end(); + expect(getNameOut2).toBeTruthy(); }); - test("invoke Web3SigningCredentialType.CactusKeychainRef", async (t2: Test) => { + test("invoke Web3SigningCredentialType.CactusKeychainRef", async () => { const newName = `DrCactus${uuidV4()}`; const web3SigningCredential: Web3SigningCredentialCactusKeychainRef = { @@ -393,10 +382,10 @@ test(testCase, async (t: Test) => { web3SigningCredential, nonce: 3, }); - t2.ok(setNameOut, "setName() invocation #1 output is truthy OK"); + expect(setNameOut).toBeTruthy(); try { - const setNameOutInvalid = await connector.getContractInfoKeychain({ + await connector.getContractInfoKeychain({ contractName, invocationType: EthContractInvocationType.Send, methodName: "setName", @@ -410,14 +399,11 @@ test(testCase, async (t: Test) => { }, nonce: 3, }); - t2.ifError(setNameOutInvalid.transactionReceipt); + fail("Expected getContractInfoKeychain call to fail but it succeeded."); } catch (error) { - t2.notStrictEqual( - error, - "Nonce too low", - "setName() invocation with invalid nonce", - ); + expect(error).not.toEqual("Nonce too low"); } + const { callOutput: getNameOut } = await connector.getContractInfoKeychain({ contractName, invocationType: EthContractInvocationType.Call, @@ -427,7 +413,7 @@ test(testCase, async (t: Test) => { gas: 1000000, web3SigningCredential, }); - t2.equal(getNameOut, newName, `getName() output reflects the update OK`); + expect(getNameOut).toContain(newName); const getNameOut2 = await connector.getContractInfoKeychain({ contractName, @@ -438,12 +424,10 @@ test(testCase, async (t: Test) => { gas: 1000000, web3SigningCredential, }); - t2.ok(getNameOut2, "getName() invocation #2 output is truthy OK"); - - t2.end(); + expect(getNameOut2).toBeTruthy(); }); - test("get prometheus exporter metrics", async (t2: Test) => { + test("get prometheus exporter metrics", async () => { const res = await apiClient.getPrometheusMetricsV1(); const promMetricsOutput = "# HELP " + @@ -456,17 +440,13 @@ test(testCase, async (t: Test) => { '{type="' + K_CACTUS_QUORUM_TOTAL_TX_COUNT + '"} 5'; - t2.ok(res); - t2.ok(res.data); - t2.equal(res.status, 200); - t2.true( - res.data.includes(promMetricsOutput), - "Total Transaction Count of 5 recorded as expected. RESULT OK.", - ); - t2.end(); + expect(res); + expect(res.data); + expect(res.status).toEqual(200); + expect(res.data).toContain(promMetricsOutput); }); - test("deploys contract via .json file with constructorArgs", async (t2: Test) => { + test("deploys contract via .json file with constructorArgs", async () => { const deployOut = await connector.deployContract({ contractName: HelloWorldContractJson.contractName, contractJSON: HelloWorldContractJson, @@ -479,22 +459,11 @@ test(testCase, async (t: Test) => { gas: 1000000, constructorArgs: ["Test Arg"], }); - t2.ok(deployOut, "deployContract() output is truthy OK"); - t2.ok( - deployOut.transactionReceipt, - "deployContract() output.transactionReceipt is truthy OK", - ); - t2.ok( - deployOut.transactionReceipt.contractAddress, - "deployContract() output.transactionReceipt.contractAddress is truthy OK", - ); + expect(deployOut).toBeTruthy(); + expect(deployOut.transactionReceipt).toBeTruthy(); + expect(deployOut.transactionReceipt.contractAddress).toBeTruthy(); contractAddress = deployOut.transactionReceipt.contractAddress as string; - t2.ok( - typeof contractAddress === "string", - "contractAddress typeof string OK", - ); + expect(contractAddress).toBeString(); }); - - t.end(); });