Skip to content

Commit

Permalink
test(cbdc-example): removed dependency on chai and update tests
Browse files Browse the repository at this point in the history
* Chai was removed as dependency of the cbdc-backend package
* A test scenario was removed in the 'bridge-back' feature due to
having duplicated logic with another one in the same file
* Some tests were failing due to timeouts, so we added an explicit
and generous timeout limit for each step

closes #3034

Signed-off-by: André Augusto <[email protected]>
  • Loading branch information
AndreAugusto11 committed Feb 28, 2024
1 parent f57ab70 commit 4d5339a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 64 deletions.
2 changes: 0 additions & 2 deletions examples/cactus-example-cbdc-bridging-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@
"web3-utils": "1.5.2"
},
"devDependencies": {
"@types/chai": "^4.3.1",
"@types/crypto-js": "4.1.1",
"@types/cucumber": "^4.0.4",
"@types/express": "4.17.19",
"@types/express-jwt": "6.0.2",
"@types/fs-extra": "9.0.13",
"@types/node": "^10.17.60",
"@types/uuid": "8.3.4",
"chai": "^4.1.2",
"cucumber": "^5.0.3",
"hardhat": "2.17.2",
"http-status-codes": "2.1.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class ClientHelper extends ClientGatewayHelper {
.isValidBridgeBackCBDC(
sessionData.sourceLedgerAssetID,
sessionData.assetProfile.keyInformationLink[0].toString(), // Amount
sessionData.assetProfile.keyInformationLink[2].toString(), // Amount
sessionData.assetProfile.keyInformationLink[2].toString(), // ethAddress
)
.catch((err) => {
throw new Error(`${err}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,18 @@ Feature: Client successfully bridges back CBDC
Given "bob" locks and deletes an asset reference with id "c5dfbd04-a71b-4848-92d1-78cd1fafaaf1" in the source chain
Given "alice" with 500 CBDC available in the sidechain smart contract
When "alice" escrows 500 CBDC and creates an asset reference with id "d25fbcbb-0895-4905-b8d5-502d5e83b122" in the sidechain
Then "alice" fails to initiate bridge back of 10000 CBDC referenced by id "d25fbcbb-0895-4905-b8d5-502d5e83b122" to "alice" address in the source chain

@bridgeBack
@fabric
@besu
Scenario: Client fails to initiate bridge back of CBDC to another user address in the source chain but transfer
Given "alice" with 500 CBDC available in the source chain
Given "alice" escrows 500 CBDC and creates an asset reference with id "c5dfbd04-a71b-4848-92d1-78cd1fafaaf1" in the source chain
Given "bob" locks and deletes an asset reference with id "c5dfbd04-a71b-4848-92d1-78cd1fafaaf1" in the source chain
Given "alice" with 500 CBDC available in the sidechain smart contract
When "alice" escrows 500 CBDC and creates an asset reference with id "d25fbcbb-0895-4905-b8d5-502d5e83b122" in the sidechain
Then "alice" fails to initiate bridge back of 500 CBDC referenced by id "d25fbcbb-0895-4905-b8d5-502d5e83b122" to "charlie" address in the source chain
Then "alice" has 0 CBDC available in the source chain
Then "alice" has 0 CBDC available in the sidechain
Then "bob" has 500 CBDC available in the source chain
Then "bob" has 500 CBDC available in the sidechain
Then "charlie" has 0 CBDC available in the source chain
Then "charlie" has 0 CBDC available in the sidechain
Then "alice" fails to initiate bridge back of 10000 CBDC referenced by id "d25fbcbb-0895-4905-b8d5-502d5e83b122"

@bridgeBack
@fabric
@besu
Scenario: Impersonator fails to initiate bridge back of CBDC escrowed by another user address but transfer
Given "alice" with 500 CBDC available in the source chain
Given "alice" escrows 500 CBDC and creates an asset reference with id "c5dfbd04-a71b-4848-92d1-78cd1fafaaf1" in the source chain
Given "bob" locks and deletes an asset reference with id "c5dfbd04-a71b-4848-92d1-78cd1fafaaf1" in the source chain
Given "alice" with 500 CBDC available in the sidechain smart contract
When "alice" escrows 500 CBDC and creates an asset reference with id "d25fbcbb-0895-4905-b8d5-502d5e83b122" in the sidechain
Then "charlie" fails to initiate bridge back of 500 CBDC referenced by id "d25fbcbb-0895-4905-b8d5-502d5e83b122" to "alice" address in the source chain
Then "charlie" fails to initiate bridge back of 500 CBDC referenced by id "d25fbcbb-0895-4905-b8d5-502d5e83b122"
Then "alice" has 0 CBDC available in the source chain
Then "alice" has 0 CBDC available in the sidechain
Then "bob" has 500 CBDC available in the source chain
Then "bob" has 0 CBDC available in the source chain
Then "bob" has 500 CBDC available in the sidechain
Then "charlie" has 0 CBDC available in the source chain
Then "charlie" has 0 CBDC available in the sidechain
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Given, When, Then, Before, After } from "cucumber";
import { expect } from "chai";
import axios from "axios";
import CryptoMaterial from "../../../../crypto-material/crypto-material.json";
import {
Expand All @@ -10,7 +9,7 @@ import {
} from "../besu-helper";
import AssetReferenceContractJson from "../../../../solidity/asset-reference-contract/AssetReferenceContract.json";
import CBDCcontractJson from "../../../../solidity/cbdc-erc-20/CBDCcontract.json";
import { getEthAddress, getPrvKey } from "./common";
import { getEthAddress, getPrvKey, assertEqual, assertStringContains } from "./common";

const BESU_CONTRACT_CBDC_ERC20_NAME = CBDCcontractJson.contractName;
const BESU_CONTRACT_ASSET_REF_NAME = AssetReferenceContractJson.contractName;
Expand All @@ -25,6 +24,7 @@ After({ timeout: 20 * 1000, tags: "@besu" }, async function () {

Given(
"{string} with {int} CBDC available in the sidechain smart contract",
{ timeout: 10 * 1000 },
async function (user: string, amount: number) {
await axios.post(
"http://127.0.0.1:4100/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/invoke-contract",
Expand All @@ -47,6 +47,7 @@ Given(

When(
"{string} escrows {int} CBDC and creates an asset reference with id {string} in the sidechain",
{ timeout: 10 * 1000 },
async function (user: string, amount: number, assetRefID: string) {
await axios.post(
"http://127.0.0.1:4100/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/invoke-contract",
Expand All @@ -69,6 +70,7 @@ When(

When(
"bob locks the asset reference with id {string} in the sidechain",
{ timeout: 10 * 1000 },
async function (assetRefID: string) {
await lockBesuAssetReference(
getEthAddress("bob"),
Expand All @@ -80,6 +82,7 @@ When(

When(
"bob deletes the asset reference with id {string} in the sidechain",
{ timeout: 10 * 1000 },
async function (assetRefID: string) {
await axios.post(
"http://127.0.0.1:4100/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/invoke-contract",
Expand All @@ -103,21 +106,21 @@ When(
Then(
"the asset reference smart contract has an asset reference with id {string}",
async function (assetRefID: string) {
expect(await isBesuAssetReference(assetRefID)).to.be.true;
assertEqual(await isBesuAssetReference(assetRefID), true);
},
);

Then(
"the asset reference smart contract has no asset reference with id {string}",
async function (assetRefID: string) {
expect(await isBesuAssetReference(assetRefID)).to.be.false;
assertEqual(await isBesuAssetReference(assetRefID), false);
},
);

Then(
"{string} has {int} CBDC available in the sidechain",
async function (user: string, amount: number) {
expect(await getBesuBalance(getEthAddress(user))).to.equal(amount);
assertEqual(await getBesuBalance(getEthAddress(user)), amount);
},
);

Expand All @@ -141,7 +144,7 @@ Then(
},
);

expect(response.data.callOutput).to.equal(true);
assertEqual(response.data.callOutput, true);
},
);

Expand All @@ -153,7 +156,7 @@ Then(
getPrvKey(user),
assetRefID,
).catch((err) => {
expect(err.response.data.error).to.contain(
assertStringContains(err.response.data.error,
`Transaction has been reverted by the EVM`,
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Then } from "cucumber";
import { expect } from "chai";
import axios from "axios";
import CryptoMaterial from "../../../../crypto-material/crypto-material.json";
import { getFabricId, getEthAddress } from "./common";
import { getFabricId, getEthAddress, assertEqual } from "./common";

const MAX_RETRIES = 5;
const MAX_TIMEOUT = 5000;
Expand Down Expand Up @@ -64,21 +63,20 @@ Then(
},
);

expect(response.status).to.equal(200);
assertEqual(response.status, 200);
},
);

Then(
"{string} fails to initiate bridge back of {int} CBDC referenced by id {string} to {string} address in the source chain",
"{string} fails to initiate bridge back of {int} CBDC referenced by id {string}",
{ timeout: 60 * 1000 },
async function (
user: string,
amount: number,
assetRefID: string,
finalUser: string,
) {
const address = getEthAddress(user);
const fabricID = getFabricId(finalUser);
const fabricID = getFabricId(user);

const assetProfile = {
expirationDate: new Date(2060, 11, 24).toString(),
Expand Down Expand Up @@ -126,7 +124,7 @@ Then(
},
)
.catch((err) => {
expect(err.response.status).to.equal(500);
assertEqual(err.response.status, 500);
});
},
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { When, Then } from "cucumber";
import { expect } from "chai";
import axios from "axios";
import CryptoMaterial from "../../../../crypto-material/crypto-material.json";
import { getUserFromPseudonim, getFabricId, getEthAddress } from "./common";
import { getUserFromPseudonim, getFabricId, getEthAddress, assertEqual, assertStringContains } from "./common";

const MAX_RETRIES = 5;
const MAX_TIMEOUT = 5000;
Expand All @@ -28,7 +27,7 @@ Then(
},
);

expect(parseInt(response.data.functionOutput)).to.equal(amount);
assertEqual(parseInt(response.data.functionOutput), amount);
},
);

Expand Down Expand Up @@ -88,8 +87,8 @@ When(
recipientLedgerAssetID: "FABRIC_ASSET_ID",
},
);

expect(response.status).to.equal(200);
assertEqual(response.status, 200);
},
);

Expand Down Expand Up @@ -152,7 +151,7 @@ Then(
},
)
.catch((err) => {
expect(err.response.data.error).to.contain(failureReason);
assertStringContains(err.response.data.error, failureReason);
});
},
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assert } from "chai";
import CryptoMaterial from "../../../../crypto-material/crypto-material.json";

export function getUserFromPseudonim(user: string): string {
Expand All @@ -10,7 +9,7 @@ export function getUserFromPseudonim(user: string): string {
case "bob":
return "bridge";
default:
assert.fail(0, 1, "Invalid user provided");
throw Error("Invalid user provided");
}
}

Expand All @@ -23,7 +22,7 @@ export function getFabricId(user: string): string {
case "bridge":
return CryptoMaterial.accounts["bridge"].fabricID;
default:
assert.fail(0, 1, "Invalid user provided");
throw Error("Invalid user provided");
}
}

Expand All @@ -36,7 +35,7 @@ export function getEthAddress(user: string): string {
case "bridge":
return CryptoMaterial.accounts["bridge"].ethAddress;
default:
assert.fail(0, 1, "Invalid user provided");
throw Error("Invalid user provided");
}
}

Expand All @@ -49,6 +48,32 @@ export function getPrvKey(user: string): string {
case "bridge":
return CryptoMaterial.accounts["bridge"].privateKey;
default:
assert.fail(0, 1, "Invalid user provided");
throw Error("Invalid user provided");
}
}

export function assertEqual(
value_1: unknown,
value_2: unknown,
) {
if (value_1 !== value_2) {
throw Error(`Expected ${value_1} to be equal to ${value_2}`);
}
}

export function assertStringContains(
mainString: string,
subString: string,
): void {
if (!mainString.includes(subString)) {
throw new Error(`String "${mainString}" does not contain "${subString}"`);
}
}

export function assertNonNullish<TValue>(
value: TValue,
): asserts value is NonNullable<TValue> {
if (value === null || value === undefined) {
throw Error(`${value} was expected to be non-null`);
}
}
Loading

0 comments on commit 4d5339a

Please sign in to comment.