From 331d6b422b2c03f89d737d2e32c3fc79a563fd69 Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Sat, 13 Apr 2024 22:50:41 -0700 Subject: [PATCH] test(connector-besu): migrate v21-get-transaction-endpoint to Jest The full file path of the test case: ```sh packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/ integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts ``` Signed-off-by: Peter Somogyvari --- .github/workflows/ci.yaml | 2 +- .taprc | 1 - jest.config.js | 1 - .../v21-get-transaction-endpoint.test.ts | 251 ++++++++++-------- 4 files changed, 134 insertions(+), 121 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 780604dd5a..c3cb1d8976 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1931,7 +1931,7 @@ jobs: JEST_TEST_PATTERN: packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/(unit|integration|benchmark)/.*/*.test.ts JEST_TEST_RUNNER_DISABLED: false TAPE_TEST_PATTERN: >- - --files={./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-balance-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-endpoint.test.ts} + --files={./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-balance-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-block-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts,./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-endpoint.test.ts} TAPE_TEST_RUNNER_DISABLED: false runs-on: ubuntu-22.04 steps: diff --git a/.taprc b/.taprc index a8cd162f3d..2be47e68ed 100644 --- a/.taprc +++ b/.taprc @@ -69,7 +69,6 @@ files: - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts - - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-endpoint.test.ts - ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/identity-client.test.ts - ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v1-4-x/run-transaction-endpoint-v1.test.ts diff --git a/jest.config.js b/jest.config.js index 7425b9a2a1..e905585339 100644 --- a/jest.config.js +++ b/jest.config.js @@ -78,7 +78,6 @@ module.exports = { `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-past-logs-endpoint.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-transaction-endpoint.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-block-endpoint.test.ts`, - `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-sign-transaction-endpoint.test.ts`, `./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/identity-client.test.ts`, `./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v1-4-x/run-transaction-endpoint-v1.test.ts`, diff --git a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts index 291b2ce391..b26597af95 100644 --- a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-transaction-endpoint.test.ts @@ -1,4 +1,4 @@ -import test, { Test } from "tape-promise/tape"; +import "jest-extended"; import { v4 as uuidv4 } from "uuid"; import { createServer } from "http"; @@ -16,10 +16,12 @@ import { Secp256k1Keys, KeyFormat, LogLevelDesc, + LoggerProvider, } from "@hyperledger/cactus-common"; import { BesuTestLedger, + IKeyPair, pruneDockerAllIfGithubAction, } from "@hyperledger/cactus-test-tooling"; @@ -36,146 +38,159 @@ import { PluginRegistry } from "@hyperledger/cactus-core"; import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; const testCase = "API client can call get-transaction via network"; -const logLevel: LogLevelDesc = "TRACE"; +const logLevel: LogLevelDesc = "INFO"; -test("BEFORE " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning didn't throw OK"); - t.end(); -}); - -test(testCase, async (t: Test) => { - const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1"); - const keychainId = uuidv4(); - const keychainRef = uuidv4(); - - const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer(); - const keyHex = privateKey.toString("hex"); - const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM); - - const keychain = new PluginKeychainMemory({ - backend: new Map([[keychainRef, pem]]), - keychainId, - logLevel, - instanceId: uuidv4(), +describe("PluginLedgerBesu", () => { + const log = LoggerProvider.getOrCreate({ + label: "v21-get-transaction-endpoint.test.ts", + level: logLevel, }); - const httpServer1 = createServer(); - await new Promise((resolve, reject) => { - httpServer1.once("error", reject); - httpServer1.once("listening", resolve); - httpServer1.listen(0, "127.0.0.1"); - }); - const addressInfo1 = httpServer1.address() as AddressInfo; - t.comment(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`); - const node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`; - t.comment(`Cactus Node 1 Host: ${node1Host}`); - const containerImageVersion = "2021-08-24--feat-1244"; const containerImageName = "ghcr.io/hyperledger/cactus-besu-21-1-6-all-in-one"; const besuOptions = { containerImageName, containerImageVersion }; const besuTestLedger = new BesuTestLedger(besuOptions); - await besuTestLedger.start(); - - const tearDown = async () => { - await besuTestLedger.stop(); - await besuTestLedger.destroy(); - }; - - test.onFinish(tearDown); - - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); - // 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin - const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); + let apiServer: ApiServer; + let web3JsQuorum: IWeb3Quorum; + let orionKeyPair: IKeyPair; + let besuPrivateKey: string; + let web3: Web3; + let node1Host: string; - // 3. Instantiate the web service consortium plugin - const options: IPluginLedgerConnectorBesuOptions = { - instanceId: uuidv4(), - rpcApiHttpHost, - rpcApiWsHost, - pluginRegistry, - logLevel, - }; - const pluginValidatorBesu = new PluginLedgerConnectorBesu(options); - - // 4. Create the API Server object that we embed in this test - const configService = new ConfigService(); - const apiServerOptions = await configService.newExampleConfig(); - apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE; - apiServerOptions.configFile = ""; - apiServerOptions.apiCorsDomainCsv = "*"; - apiServerOptions.apiPort = addressInfo1.port; - apiServerOptions.cockpitPort = 0; - apiServerOptions.apiTlsEnabled = false; - const config = await configService.newExampleConfigConvict(apiServerOptions); - - pluginRegistry.add(pluginValidatorBesu); - - const apiServer = new ApiServer({ - httpServerApi: httpServer1, - config: config.getProperties(), - pluginRegistry, + beforeAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).toResolve(); }); - // 5. make sure the API server is shut down when the testing if finished. - test.onFinish(() => apiServer.shutdown()); + beforeAll(async () => { + await besuTestLedger.start(); + }); - // 6. Start the API server which is now listening on port A and it's healthcheck works through the main SDK - await apiServer.start(); + beforeAll(async () => { + const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1"); + const keychainId = uuidv4(); + const keychainRef = uuidv4(); + + const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer(); + const keyHex = privateKey.toString("hex"); + const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM); + + const keychain = new PluginKeychainMemory({ + backend: new Map([[keychainRef, pem]]), + keychainId, + logLevel, + instanceId: uuidv4(), + }); + + const httpServer1 = createServer(); + await new Promise((resolve, reject) => { + httpServer1.once("error", reject); + httpServer1.once("listening", resolve); + httpServer1.listen(0, "127.0.0.1"); + }); + const addressInfo1 = httpServer1.address() as AddressInfo; + log.debug(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`); + node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`; + log.debug(`Cactus Node 1 Host: ${node1Host}`); + + const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); + const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + + // 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin + const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); + + // 3. Instantiate the web service consortium plugin + const options: IPluginLedgerConnectorBesuOptions = { + instanceId: uuidv4(), + rpcApiHttpHost, + rpcApiWsHost, + pluginRegistry, + logLevel, + }; + const pluginValidatorBesu = new PluginLedgerConnectorBesu(options); + + // 4. Create the API Server object that we embed in this test + const configService = new ConfigService(); + const apiServerOptions = await configService.newExampleConfig(); + apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE; + apiServerOptions.configFile = ""; + apiServerOptions.apiCorsDomainCsv = "*"; + apiServerOptions.apiPort = addressInfo1.port; + apiServerOptions.cockpitPort = 0; + apiServerOptions.apiTlsEnabled = false; + const config = + await configService.newExampleConfigConvict(apiServerOptions); + + pluginRegistry.add(pluginValidatorBesu); + + apiServer = new ApiServer({ + httpServerApi: httpServer1, + config: config.getProperties(), + pluginRegistry, + }); + + // 6. Start the API server which is now listening on port A and it's healthcheck works through the main SDK + await apiServer.start(); + + const web3Provider = new Web3.providers.HttpProvider(rpcApiHttpHost); + web3 = new Web3(web3Provider); + web3JsQuorum = Web3JsQuorum(web3); + + orionKeyPair = await besuTestLedger.getOrionKeyPair(); + const besuKeyPair = await besuTestLedger.getBesuKeyPair(); + + besuPrivateKey = besuKeyPair.privateKey.toLowerCase().startsWith("0x") + ? besuKeyPair.privateKey.substring(2) + : besuKeyPair.privateKey; // besu node's private key + }); - // 7. Instantiate the main SDK dynamically with whatever port the API server ended up bound to (port 0) - t.comment(`AddressInfo: ${JSON.stringify(addressInfo1)}`); + afterAll(async () => { + await apiServer.shutdown(); + }); - const web3Provider = new Web3.providers.HttpProvider(rpcApiHttpHost); - const web3 = new Web3(web3Provider); - const web3JsQuorum: IWeb3Quorum = Web3JsQuorum(web3); + const tearDown = async () => { + await besuTestLedger.stop(); + await besuTestLedger.destroy(); + }; - const orionKeyPair = await besuTestLedger.getOrionKeyPair(); - const besuKeyPair = await besuTestLedger.getBesuKeyPair(); + afterAll(tearDown); - const besuPrivateKey = besuKeyPair.privateKey.toLowerCase().startsWith("0x") - ? besuKeyPair.privateKey.substring(2) - : besuKeyPair.privateKey; // besu node's private key + afterAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).toResolve(); + }); - const contractOptions = { - data: `0x123`, - // privateFrom : Orion public key of the sender. - privateFrom: orionKeyPair.publicKey, - // privateFor : Orion public keys of recipients or privacyGroupId: Privacy group to receive the transaction - privateFor: [orionKeyPair.publicKey], - // privateKey: Ethereum private key with which to sign the transaction. - privateKey: besuPrivateKey, - }; + test(testCase, async () => { + const contractOptions = { + data: `0x123`, + // privateFrom : Orion public key of the sender. + privateFrom: orionKeyPair.publicKey, + // privateFor : Orion public keys of recipients or privacyGroupId: Privacy group to receive the transaction + privateFor: [orionKeyPair.publicKey], + // privateKey: Ethereum private key with which to sign the transaction. + privateKey: besuPrivateKey, + }; - const transactionHash = - await web3JsQuorum.priv.generateAndSendRawTransaction(contractOptions); + const transactionHash = + await web3JsQuorum.priv.generateAndSendRawTransaction(contractOptions); - await web3.eth.getTransaction(transactionHash); + await web3.eth.getTransaction(transactionHash); - const request: GetTransactionV1Request = { - transactionHash: transactionHash, - }; + const request: GetTransactionV1Request = { + transactionHash: transactionHash, + }; - const configuration = new BesuApiClientOptions({ basePath: node1Host }); - const api = new BesuApiClient(configuration); + const configuration = new BesuApiClientOptions({ basePath: node1Host }); + const api = new BesuApiClient(configuration); - // Test for 200 valid response test case - const res = await api.getTransactionV1(request); + // Test for 200 valid response test case + const res = await api.getTransactionV1(request); - // const { } = res; - t.ok(res, "API response object is truthy"); - t.ok(res.data.transaction, "Transaction is truthy ok"); - t.true( - typeof res.data.transaction === "object", - "Response.transaction is OK", - ); -}); + expect(res).toBeTruthy(); -test("AFTER " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning didn't throw OK"); - t.end(); + expect(res.data.transaction).toBeTruthy(); + expect(res.data.transaction).toBeObject(); + }); });