From 0d87f1bb53066dc3fab007a8cf9896b27c81ffbd Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Fri, 19 Jul 2024 11:53:28 -0700 Subject: [PATCH] test(plugin-keychain-google-sm): migrate remaining tape tests to Jest 1. Also turned off tape test execution completely for the package in ci.yaml This would've been a no-op anyway just wasting resources since we have no tests left that need tape at all. Signed-off-by: Peter Somogyvari --- .github/workflows/ci.yaml | 3 +- .taprc | 3 - jest.config.js | 3 - .../openapi/openapi-validation.test.ts | 325 +++++++++--------- .../plugin-factory-keychain.test.ts | 18 +- .../plugin-keychain-google-sm.test.ts | 188 +++++----- 6 files changed, 264 insertions(+), 276 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bb09b75514..8fbb46a768 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1086,8 +1086,7 @@ jobs: JEST_TEST_RUNNER_DISABLED: false JEST_TEST_COVERAGE_PATH: ./code-coverage-ts/cpk-google-sm JEST_TEST_CODE_COVERAGE_ENABLED: true - TAPE_TEST_PATTERN: '--files={./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts,./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts,./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-keychain-google-sm.test.ts}' - TAPE_TEST_RUNNER_DISABLED: false + TAPE_TEST_RUNNER_DISABLED: true needs: build-dev runs-on: ubuntu-22.04 steps: diff --git a/.taprc b/.taprc index 99284f4332..3c1f271bc3 100644 --- a/.taprc +++ b/.taprc @@ -7,7 +7,6 @@ ts: true files: - ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/add-orgs.test.ts - ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/obtain-profiles.test.ts - - ./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts - ./packages/cactus-plugin-keychain-aws-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts - ./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 @@ -17,8 +16,6 @@ files: - ./packages/cactus-test-cmd-api-server/src/test/typescript/integration/plugin-import-with-npm-install-version-selection.test.ts - ./packages/cactus-plugin-ledger-connector-corda/src/test/typescript/integration/openapi/openapi-validation.test.ts - ./packages/cactus-plugin-ledger-connector-corda/src/test/typescript/integration/flow-database-access-v4.8.test.ts - - ./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts - - ./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-keychain-google-sm.test.ts - ./packages/cactus-test-plugin-consortium-manual/src/test/typescript/integration/plugin-consortium-manual/openapi/openapi-validation.test.ts - ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-record-locator.test.ts - ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/private-deploy-contract-from-json-web3-eea.test.ts diff --git a/jest.config.js b/jest.config.js index 5e76bb7d59..cb07630cb3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -19,7 +19,6 @@ module.exports = { `./packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/manual/geth-alchemy-integration-manual-check.test.ts`, `./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/add-orgs.test.ts`, `./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/obtain-profiles.test.ts`, - `./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts`, `./packages/cactus-plugin-keychain-aws-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts`, `./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`, @@ -29,8 +28,6 @@ module.exports = { `./packages/cactus-test-cmd-api-server/src/test/typescript/integration/plugin-import-with-npm-install-version-selection.test.ts`, `./packages/cactus-plugin-ledger-connector-corda/src/test/typescript/integration/openapi/openapi-validation.test.ts`, `./packages/cactus-plugin-ledger-connector-corda/src/test/typescript/integration/flow-database-access-v4.8.test.ts`, - `./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts`, - `./packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-keychain-google-sm.test.ts`, `./packages/cactus-test-plugin-consortium-manual/src/test/typescript/integration/plugin-consortium-manual/openapi/openapi-validation.test.ts`, `./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-record-locator.test.ts`, `./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/private-deploy-contract-from-json-web3-eea.test.ts`, diff --git a/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts b/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts index 0088f95045..ec20125c85 100644 --- a/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts +++ b/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts @@ -1,5 +1,5 @@ -import test, { Test } from "tape-promise/tape"; -import { v4 as uuidv4 } from "uuid"; +import "jest-extended"; +import { randomUUID } from "node:crypto"; import http from "http"; import type { AddressInfo } from "net"; import express from "express"; @@ -9,31 +9,44 @@ import { LogLevelDesc, IListenOptions, Servers, + hasKey, } from "@hyperledger/cactus-common"; - +import { installOpenapiValidationMiddleware } from "@hyperledger/cactus-core"; +import { DeleteKeychainEntryRequestV1 } from "@hyperledger/cactus-core-api"; import { - Configuration, - DefaultApi as KeychainGoogleSmApi, - DeleteKeychainEntryRequestV1, GetKeychainEntryRequestV1, HasKeychainEntryRequestV1, - IPluginKeychainGoogleSmOptions, - PluginKeychainGoogleSm, SetKeychainEntryRequestV1, -} from "../../../../main/typescript/public-api"; +} from "@hyperledger/cactus-core-api"; + +import { DefaultApi as KeychainGoogleSmApi } from "../../../../main/typescript/generated/openapi/typescript-axios/index"; +import { Configuration } from "../../../../main/typescript/generated/openapi/typescript-axios/index"; import { SecretManagerServiceClientMock } from "../../mock/plugin-keychain-google-sm-mock"; -import { installOpenapiValidationMiddleware } from "@hyperledger/cactus-core"; import OAS from "../../../../main/json/openapi.json"; +import { + IPluginKeychainGoogleSmOptions, + PluginKeychainGoogleSm, +} from "../../../../main/typescript/plugin-keychain-google-sm"; const logLevel: LogLevelDesc = "TRACE"; -const testCase = "Test cactus-plugin-keychain-azure-kv openapi validation"; -test(testCase, async (t: Test) => { +describe("PluginKeychainGoogleSm", () => { + const key = `${randomUUID()}?${randomUUID()}`; + const value = randomUUID(); + + const fSet = "setKeychainEntryV1"; + const fGet = "getKeychainEntryV1"; + const fHas = "hasKeychainEntryV1"; + const fDelete = "deleteKeychainEntryV1"; + const cOk = "without bad request error"; + const cWithoutParams = "not sending all required parameters"; + const cInvalidParams = "sending invalid parameters"; + const options: IPluginKeychainGoogleSmOptions = { - instanceId: uuidv4(), - keychainId: uuidv4(), + instanceId: randomUUID(), + keychainId: randomUUID(), logLevel: logLevel, backend: new SecretManagerServiceClientMock({ logLevel: logLevel, @@ -49,141 +62,135 @@ test(testCase, async (t: Test) => { 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}`; - const configuration = new Configuration({ basePath: apiHost }); - const apiClient = new KeychainGoogleSmApi(configuration); - - await installOpenapiValidationMiddleware({ - logLevel, - app: expressApp, - apiSpec: OAS, - }); - await plugin.getOrCreateWebServices(); - await plugin.registerWebServices(expressApp); + let apiClient: KeychainGoogleSmApi; - const key = `${uuidv4()}?${uuidv4()}`; - const value = uuidv4(); + beforeAll(async () => { + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; + const { address, port } = addressInfo; + const apiHost = `http://${address}:${port}`; + const configuration = new Configuration({ basePath: apiHost }); + apiClient = new KeychainGoogleSmApi(configuration); - const fSet = "setKeychainEntryV1"; - const fGet = "getKeychainEntryV1"; - const fHas = "hasKeychainEntryV1"; - const fDelete = "deleteKeychainEntryV1"; - const cOk = "without bad request error"; - const cWithoutParams = "not sending all required parameters"; - const cInvalidParams = "sending invalid parameters"; + await installOpenapiValidationMiddleware({ + logLevel, + app: expressApp, + apiSpec: OAS, + }); + + await plugin.getOrCreateWebServices(); + await plugin.registerWebServices(expressApp); + }); + + afterAll(async () => await Servers.shutdown(server)); - test(`${testCase} - ${fSet} - ${cOk}`, async (t2: Test) => { + it(` ${fSet} - ${cOk}`, async () => { const res = await apiClient.setKeychainEntryV1({ key, value, }); - t2.equal(res.status, 200, `Endpoint ${fSet}: response.status === 200 OK`); - t2.end(); + expect(res.status).toEqual(200); }); - test(`${testCase} - ${fGet} - ${cOk}`, async (t2: Test) => { + it(` ${fGet} - ${cOk}`, async () => { const res = await apiClient.getKeychainEntryV1({ key }); - t2.equal(res.status, 200, `Endpoint ${fGet}: response.status === 200 OK`); - t2.end(); + expect(res.status).toEqual(200); }); - test(`${testCase} - ${fHas} - ${cOk}`, async (t2: Test) => { + it(` ${fHas} - ${cOk}`, async () => { const res = await apiClient.hasKeychainEntryV1({ key }); - t2.equal(res.status, 200, `Endpoint ${fHas}: response.status === 200 OK`); - t2.end(); + expect(res.status).toEqual(200); }); - test(`${testCase} - ${fDelete} - ${cOk}`, async (t2: Test) => { + it(` ${fDelete} - ${cOk}`, async () => { const res = await apiClient.deleteKeychainEntryV1({ key }); - t2.equal( - res.status, - 200, - `Endpoint ${fDelete}: response.status === 200 OK`, - ); - t2.end(); + expect(res.status).toEqual(200); }); - test(`${testCase} - ${fSet} - ${cWithoutParams}`, async (t2: Test) => { + it(` ${fSet} - ${cWithoutParams}`, async () => { try { await apiClient.setKeychainEntryV1({ value, - } as any as SetKeychainEntryRequestV1); + } as unknown as SetKeychainEntryRequestV1); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fSet} without required key: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok(fields.includes("key"), "Rejected because key is required"); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("key")).toBeTrue(); } - t2.end(); }); - test(`${testCase} - ${fGet} - ${cWithoutParams}`, async (t2: Test) => { + it(` ${fGet} - ${cWithoutParams}`, async () => { try { await apiClient.getKeychainEntryV1( - {} as any as GetKeychainEntryRequestV1, + {} as unknown as GetKeychainEntryRequestV1, ); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fGet} without required key: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok(fields.includes("key"), "Rejected because key is required"); + expect(e.response.status).toEqual(400); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("key")).toBeTrue(); } - t2.end(); }); - test(`${testCase} - ${fHas} - ${cWithoutParams}`, async (t2: Test) => { + it(` ${fHas} - ${cWithoutParams}`, async () => { try { await apiClient.hasKeychainEntryV1( - {} as any as HasKeychainEntryRequestV1, + {} as unknown as HasKeychainEntryRequestV1, ); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fHas} without required key: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok(fields.includes("key"), "Rejected because key is required"); + expect(e.response.status).toEqual(400); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("key")).toBeTrue(); } - t2.end(); }); - test(`${testCase} - ${fDelete} - ${cWithoutParams}`, async (t2: Test) => { + it(` ${fDelete} - ${cWithoutParams}`, async () => { try { await apiClient.deleteKeychainEntryV1( - {} as any as DeleteKeychainEntryRequestV1, + {} as unknown as DeleteKeychainEntryRequestV1, ); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fDelete} without required key: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok(fields.includes("key"), "Rejected because key is required"); + expect(e.response.status).toEqual(400); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("key")).toBeTrue(); } - t2.end(); }); - test(`${testCase} - ${fSet} - ${cInvalidParams}`, async (t2: Test) => { + it(` ${fSet} - ${cInvalidParams}`, async () => { try { await apiClient.setKeychainEntryV1({ key, @@ -191,90 +198,84 @@ test(testCase, async (t: Test) => { fake: 4, } as any as SetKeychainEntryRequestV1); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fSet} with fake=4: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok( - fields.includes("fake"), - "Rejected because fake is not a valid parameter", - ); + expect(e.response.status).toEqual(400); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("fake")).toBeTrue(); } - t2.end(); }); - test(`${testCase} - ${fGet} - ${cInvalidParams}`, async (t2: Test) => { + it(` ${fGet} - ${cInvalidParams}`, async () => { try { await apiClient.getKeychainEntryV1({ key, fake: 4, - } as any as GetKeychainEntryRequestV1); + } as unknown as GetKeychainEntryRequestV1); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fGet} with fake=4: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok( - fields.includes("fake"), - "Rejected because fake is not a valid parameter", - ); + expect(e.response.status).toEqual(400); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("fake")).toBeTrue(); } - t2.end(); }); - test(`${testCase} - ${fHas} - ${cInvalidParams}`, async (t2: Test) => { + it(` ${fHas} - ${cInvalidParams}`, async () => { try { await apiClient.hasKeychainEntryV1({ key, fake: 4, - } as any as HasKeychainEntryRequestV1); + } as unknown as HasKeychainEntryRequestV1); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fHas} with fake=4: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok( - fields.includes("fake"), - "Rejected because fake is not a valid parameter", - ); + expect(e.response.status).toEqual(400); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("fake")).toBeTrue(); } - t2.end(); }); - test(`${testCase} - ${fDelete} - ${cInvalidParams}`, async (t2: Test) => { + it(` ${fDelete} - ${cInvalidParams}`, async () => { try { await apiClient.deleteKeychainEntryV1({ key, fake: 4, - } as any as DeleteKeychainEntryRequestV1); + } as unknown as DeleteKeychainEntryRequestV1); } catch (e) { - t2.equal( - e.response.status, - 400, - `Endpoint ${fDelete} with fake=4: response.status === 400 OK`, - ); - const fields = e.response.data.map((param: any) => - param.path.replace("/body/", ""), - ); - t2.ok( - fields.includes("fake"), - "Rejected because fake is not a valid parameter", - ); + expect(e.response.status).toEqual(400); + const fields = e.response.data.map((param: unknown) => { + if (!hasKey(param, "path")) { + throw new TypeError("Expected param.path to exist."); + } + if (typeof param.path !== "string") { + throw new TypeError("Expected param.path to be string"); + } + return param.path.replace("/body/", ""); + }); + + expect(fields.includes("fake")).toBeTrue(); } - t2.end(); }); - - t.end(); }); diff --git a/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts b/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts index a5f2f2a256..35e12289d8 100644 --- a/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts +++ b/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-factory-keychain.test.ts @@ -1,14 +1,15 @@ +import "jest-extended"; + import { PluginRegistry } from "@hyperledger/cactus-core"; import { IPluginFactoryOptions, PluginImportType, } from "@hyperledger/cactus-core-api"; -import test, { Test } from "tape-promise/tape"; import { PluginFactoryKeychain } from "../../../main/typescript/plugin-factory-keychain"; import { PluginKeychainGoogleSm } from "../../../main/typescript/plugin-keychain-google-sm"; -import { v4 as uuidv4 } from "uuid"; +import { randomUUID } from "crypto"; -test("get,set,has,delete alters state as expected", async (t: Test) => { +test("get,set,has,delete alters state as expected", async () => { const iPluginFactoryOptions: IPluginFactoryOptions = { pluginImportType: PluginImportType.Local, }; @@ -16,8 +17,8 @@ test("get,set,has,delete alters state as expected", async (t: Test) => { const pluginRegistry = new PluginRegistry(); const iPluginKeychainGoogleSmOptions = { pluginRegistry, - instanceId: uuidv4(), - keychainId: uuidv4(), + instanceId: randomUUID(), + keychainId: randomUUID(), googleProfile: "true", googleRegion: "true", googleEndpoint: "true", @@ -33,10 +34,5 @@ test("get,set,has,delete alters state as expected", async (t: Test) => { iPluginKeychainGoogleSmOptions, ); - t.true( - pluginKeychainGoogleSm instanceof PluginKeychainGoogleSm, - "pluginImportType.Local results in pluginKeychainGoogleSm", - ); - - t.end(); + expect(pluginKeychainGoogleSm instanceof PluginKeychainGoogleSm).toBeTrue(); }); diff --git a/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-keychain-google-sm.test.ts b/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-keychain-google-sm.test.ts index 49a5f7b38a..c46e927922 100644 --- a/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-keychain-google-sm.test.ts +++ b/packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/plugin-keychain-google-sm.test.ts @@ -1,14 +1,12 @@ -import test, { Test } from "tape-promise/tape"; +import { AddressInfo } from "node:net"; +import { randomUUID } from "node:crypto"; +import "jest-extended"; import express from "express"; import bodyParser from "body-parser"; import http from "http"; -import { AddressInfo } from "net"; import { StatusCodes } from "http-status-codes"; import { IListenOptions, Servers } from "@hyperledger/cactus-common"; - -import { v4 as uuidv4 } from "uuid"; - import { LogLevelDesc } from "@hyperledger/cactus-common"; import { @@ -25,10 +23,10 @@ import { SecretManagerServiceClientMock } from "../mock/plugin-keychain-google-s const logLevel: LogLevelDesc = "TRACE"; -test("get,set,has,delete alters state as expected", async (t: Test) => { +describe("IPluginKeychainGoogleSm", () => { const options: IPluginKeychainGoogleSmOptions = { - instanceId: uuidv4(), - keychainId: uuidv4(), + instanceId: randomUUID(), + keychainId: randomUUID(), logLevel: logLevel, backend: new SecretManagerServiceClientMock({ logLevel: logLevel, @@ -44,93 +42,93 @@ test("get,set,has,delete alters state as expected", async (t: Test) => { 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}`; - const config = new Configuration({ basePath: apiHost }); - const apiClient = new KeychainGoogleSmApi(config); - - await plugin.registerWebServices(expressApp); - - t.equal(plugin.getKeychainId(), options.keychainId, "Keychain ID set OK"); - t.equal(plugin.getInstanceId(), options.instanceId, "Instance ID set OK"); - - /** - * To implement this the key should consist of parent and secretId seperated by ? - * For example, key = "projects/my-project?my-secret" - */ - const key = `${uuidv4()}?${uuidv4()}`; - const value = uuidv4(); - console.log(key); - console.log(value); - - const res1 = await apiClient.hasKeychainEntryV1({ key }); - t.true(res1.status >= 200, "res1.status >= 200 OK"); - t.true(res1.status < 300, "res1.status < 300"); - - t.ok(res1.data, "res1.data truthy OK"); - t.false(res1.data.isPresent, "res1.data.isPresent === false OK"); - t.ok(res1.data.checkedAt, "res1.data.checkedAt truthy OK"); - t.equal(res1.data.key, key, "res1.data.key === key OK"); - - const res2 = await apiClient.setKeychainEntryV1({ - key: key, - value: value, + + let apiClient: KeychainGoogleSmApi; + + afterAll(async () => await Servers.shutdown(server)); + + beforeAll(async () => { + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; + const { address, port } = addressInfo; + const apiHost = `http://${address}:${port}`; + const config = new Configuration({ basePath: apiHost }); + apiClient = new KeychainGoogleSmApi(config); + await plugin.registerWebServices(expressApp); }); - t.true(res2.status >= 200, "res2.status >= 200 OK"); - t.true(res2.status < 300, "res2.status < 300 OK"); - t.notOk(res2.data, "res2.data truthy OK"); - - const res3 = await apiClient.hasKeychainEntryV1({ key }); - t.true(res3.status >= 200, "res3.status >= 200 OK"); - t.true(res3.status < 300, "res3.status < 300 OK"); - t.ok(res3.data, "res3.data truthy OK"); - t.true(res3.data.isPresent, "res3.data.isPresent === true OK"); - t.ok(res3.data.checkedAt, "res3.data.checkedAt truthy OK"); - t.equal(res3.data.key, key, "res3.data.key === key OK"); - - const res4 = await apiClient.getKeychainEntryV1({ - key: key, + + it("get,set,has,delete alters state as expected", async () => { + expect(plugin.getKeychainId()).toEqual(options.keychainId); + expect(plugin.getInstanceId()).toEqual(options.instanceId); + + /** + * To implement this the key should consist of parent and secretId seperated by ? + * For example, key = "projects/my-project?my-secret" + */ + const key = `${randomUUID()}?${randomUUID()}`; + const value = randomUUID(); + console.log(key); + console.log(value); + + const res1 = await apiClient.hasKeychainEntryV1({ key }); + expect(res1.status >= 200).toBeTrue(); + expect(res1.status < 300).toBeTrue(); + + expect(res1.data).toBeTruthy(); + expect(res1.data.isPresent).toBeFalse(); + expect(res1.data.checkedAt).toBeTruthy(); + expect(res1.data.key).toEqual(key); + + const res2 = await apiClient.setKeychainEntryV1({ + key: key, + value: value, + }); + expect(res2.status >= 200).toBeTrue(); + expect(res2.status < 300).toBeTrue(); + expect(res2.data).toBeFalsy(); + + const res3 = await apiClient.hasKeychainEntryV1({ key }); + expect(res3.status >= 200).toBeTrue(); + expect(res3.status < 300).toBeTrue(); + expect(res3.data).toBeTruthy(); + expect(res3.data.isPresent).toBeTrue(); + expect(res3.data.checkedAt).toBeTruthy(); + expect(res3.data.key).toEqual(key); + + const res4 = await apiClient.getKeychainEntryV1({ + key: key, + }); + expect(res4.status >= 200).toBeTrue(); + expect(res4.status < 300).toBeTrue(); + expect(res4.data).toBeTruthy(); + //t.equal(res4.data.value, value, "res4.data.value === value OK"); + //add try catch code and then add what the error message should be -- because of the exception wrapping + //t.equals currently does not pass the test case + + const res5 = await apiClient.deleteKeychainEntryV1({ key }); + expect(res5.status >= 200).toBeTrue(); + expect(res5.status < 300).toBeTrue(); + expect(res5.data).toBeFalsy(); + + const res6 = await apiClient.hasKeychainEntryV1({ key }); + expect(res6.status >= 200).toBeTrue(); + expect(res6.status < 300).toBeTrue(); + expect(res6.data).toBeTruthy(); + expect(res6.data.isPresent).toBeFalse(); + expect(res6.data.checkedAt).toBeTruthy(); + expect(res6.data.key).toEqual(key); + try { + await apiClient.getKeychainEntryV1({ key }); + } catch (ex) { + expect(ex).toBeTruthy(); + expect(ex.response).toBeTruthy(); + expect(ex.response.data).toBeTruthy(); + expect(ex.response.data.error).toBeTruthy(); + expect( + ex.response.data.error.includes(`${key} secret not found`), + ).toBeTrue(); + + expect(ex.response.status).toEqual(StatusCodes.NOT_FOUND); + expect(ex.response.data.success).toBeFalsy(); + } }); - t.true(res4.status >= 200, "res4.status >= 200 OK"); - t.true(res4.status < 300, "res4.status < 300 OK"); - t.ok(res4.data, "res4.data truthy OK"); - //t.equal(res4.data.value, value, "res4.data.value === value OK"); - //add try catch code and then add what the error message should be -- because of the exception wrapping - //t.equals currently does not pass the test case - - const res5 = await apiClient.deleteKeychainEntryV1({ key }); - t.true(res5.status >= 200, "res5.status >= 200 OK"); - t.true(res5.status < 300, "res5.status < 300 OK"); - t.notOk(res5.data, "res5.data falsy OK"); - - const res6 = await apiClient.hasKeychainEntryV1({ key }); - t.true(res6.status >= 200, "res6.status >= 200 OK"); - t.true(res6.status < 300, "res6.status < 300 OK"); - t.ok(res6.data, "res6.data truthy OK"); - t.false(res6.data.isPresent, "res6.data.isPresent === false OK"); - t.ok(res6.data.checkedAt, "res6.data.checkedAt truthy OK"); - t.equal(res6.data.key, key, "res6.data.key === key OK"); - try { - await apiClient.getKeychainEntryV1({ key }); - } catch (out) { - t.ok(out, "error thrown for not found endpoint truthy OK"); - t.ok(out.response, "deploy contract response truthy OK"); - t.ok(out.response.data, "out.response.data truthy OK"); - t.ok(out.response.data.error, "out.response.data.error truthy OK"); - t.true( - out.response.data.error.includes(`${key} secret not found`), - "HTTP 404 response for non-existent key contains legible error message OK", - ); - - t.equal( - out.response.status, - StatusCodes.NOT_FOUND, - "deploy contract response status === 404 OK", - ); - t.notok(out.response.data.success, "out.response.data.success falsy OK"); - } - - t.end(); });