Skip to content

Commit

Permalink
test(besu): refactor jest test negative test cases
Browse files Browse the repository at this point in the history
Primary Changes
----------------
1. Refactored all negative test case exception assertions for cactus-plugin-ledger-connector-besu.
Removed try-catch blocks, replaced with declarations through jest and jest-extended's own API.
2. Noted two tests within openapi-validation.test.ts
(GetPastLogsEndpoint and GetBesuRecordEndpointV1 with empty parameters) where the status
code returned should be 400 but is 200. This could be investigated in a seperate issue.

Fixes hyperledger-cacti#3467

Signed-off-by: ashnashahgrover <ashnashahgrover777@gmail.com>
  • Loading branch information
ashnashahgrover committed Aug 12, 2024
1 parent 053224f commit f4a4bda
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,34 +265,27 @@ describe("PluginLedgerConnectorBesu", () => {
});
expect(setNameOut).toBeTruthy();

try {
await connector.invokeContract({
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
signingCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
});
throw new Error("This operation should not have succeeded, but it did.");
} catch (ex: unknown) {
// 'Returned error: Nonce too low'
expect(ex).toHaveProperty(
"message",
expect.stringContaining("Nonce too low"),
);
expect(ex).toHaveProperty(
"stack",
expect.stringContaining("Nonce too low"),
);
}
await expect(connector.invokeContract({
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
signingCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
})).rejects.toThrowError(
expect.objectContaining({
message: expect.stringContaining("Nonce too low"),
stack: expect.stringContaining("Nonce too low"),
})
);

const req: InvokeContractV1Request = {
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,25 @@ describe(testCase, () => {
});
expect(setNameOut).toBeTruthy();

try {
const setNameOutInvalid = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
signingCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
});
expect(setNameOutInvalid.transactionReceipt).toBeFalsy();
} catch (error) {
expect(error.message).toMatch("Nonce too low");
}
await expect(connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
signingCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
})).rejects.toThrowError(
expect.objectContaining({
message: expect.stringContaining("Nonce too low"),
})
);


const { callOutput: getNameOut } = await connector.invokeContract({
contractName,
Expand Down Expand Up @@ -286,8 +286,8 @@ describe(testCase, () => {
});
expect(setNameOut).toBeTruthy();

try {
const setNameOutInvalid = await connector.invokeContract({
await expect(
connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
Expand All @@ -296,11 +296,8 @@ describe(testCase, () => {
gas: 1000000,
signingCredential,
nonce: 4,
});
expect(setNameOutInvalid.transactionReceipt).toBeFalsy();
} catch (error) {
expect(error.message).toMatch("Nonce too low");
}
})
).rejects.toThrow("Nonce too low");

const { callOutput: getNameOut } = await connector.invokeContract({
contractName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,30 +250,26 @@ test(testCase, async (t: Test) => {
});
t2.ok(setNameOut, "setName() invocation #1 output is truthy OK");

try {
const setNameOutInvalid = await connector.invokeContract({
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
signingCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
});
t2.ifError(setNameOutInvalid);
} catch (error) {
t2.notStrictEqual(
error,
"Nonce too low",
"setName() invocation with invalid nonce",
);
}
await expect(connector.invokeContract({
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
contractAddress,
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
signingCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
})).rejects.toThrowError(
expect.objectContaining({
message: expect.stringContaining("Nonce too low"),
})
);

const req: InvokeContractV1Request = {
contractName: HelloWorldContractJson.contractName,
contractAbi: HelloWorldContractJson.abi,
Expand Down Expand Up @@ -342,4 +338,4 @@ test("AFTER " + testCase, async (t: Test) => {
const pruning = pruneDockerAllIfGithubAction({ logLevel });
await t.doesNotReject(pruning, "Pruning didn't throw OK");
t.end();
});
});
Loading

0 comments on commit f4a4bda

Please sign in to comment.