Skip to content

Commit

Permalink
test: refactor jest test negative test cases - phase 1
Browse files Browse the repository at this point in the history
Primary Changes
---------------
1. Refactored all remaining negative test case exception assertions under cacti/packages
Removed try-catch blocks, replaced with declarations through jest-extended's own API.

Fixes #3483

Signed-off-by: ashnashahgrover <ashnashahgrover777@gmail.com>
  • Loading branch information
ashnashahgrover committed Aug 18, 2024
1 parent 141ee24 commit 947d28a
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,14 @@ test("Test optional hash function", async () => {
});

test("Test missing required constructor field", async () => {
try {
const pkey: unknown = undefined;
const jsObjectSignerOptions: IJsObjectSignerOptions = {
privateKey: pkey as Uint8Array,
};
new JsObjectSigner(jsObjectSignerOptions);
} catch (e: unknown) {
expect(e).toBeInstanceOf(Error);
expect(e).toContainEntry([
"message",

"JsObjectSigner#ctor options.privateKey falsy.",
]);
}
const pkey: unknown = undefined;
const jsObjectSignerOptions: IJsObjectSignerOptions = {
privateKey: pkey as Uint8Array,
};

await expect(() => new JsObjectSigner(jsObjectSignerOptions)).toThrowError(
expect.objectContaining({
message: "JsObjectSigner#ctor options.privateKey falsy.",
}),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -309,44 +309,48 @@ describe("Generate and send signed transaction tests", () => {
const domainName = addRandomSuffix("errorCheckDomain");
expect(domainName).toBeTruthy();

// Generate transaction with wrong instruction
try {
await env.apiClient.generateTransactionV1({
request: {
instruction: {
name: "foo" as IrohaInstruction,
params: [domainName],
},
const transactionGeneration = env.apiClient.generateTransactionV1({
request: {
instruction: {
name: "foo" as IrohaInstruction,
params: [domainName],
},
baseConfig: env.defaultBaseConfig,
});
expect(false).toBe(true); // should always throw by now
} catch (err: any) {
expect(err.response.status).toBe(500);
expect(err.response.data.message).toEqual("Internal Server Error");
expect(err.response.data.error).toBeTruthy();
}
},
baseConfig: env.defaultBaseConfig,
});

await expect(transactionGeneration).rejects.toMatchObject({
response: expect.objectContaining({
status: 500,
data: expect.objectContaining({
message: "Internal Server Error",
error: expect.anything(),
}),
}),
});
});

/**
* Test generateTransactionV1 query error handling
*/
test("generateTransactionV1 returns error for invalid query parameter number", async () => {
// Query domain without it's ID
try {
await env.apiClient.generateTransactionV1({
request: {
query: IrohaQuery.FindDomainById,
params: [],
},
baseConfig: env.defaultBaseConfig,
});
expect(false).toBe(true); // should always throw by now
} catch (err: any) {
expect(err.response.status).toBe(500);
expect(err.response.data.message).toEqual("Internal Server Error");
expect(err.response.data.error).toBeTruthy();
}
const transactionGeneration = env.apiClient.generateTransactionV1({
request: {
query: IrohaQuery.FindDomainById,
params: [],
},
baseConfig: env.defaultBaseConfig,
});

await expect(transactionGeneration).rejects.toMatchObject({
response: expect.objectContaining({
status: 500,
data: expect.objectContaining({
message: "Internal Server Error",
error: expect.anything(),
}),
}),
});
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ describe(testCase, () => {
});
expect(setNameOut).toBeTruthy();

//This is saying the function should NOT fail with a message containing "None too low"
//But the returned error message does contain that string.
try {
await connector.invokeContract({
contractName,
Expand All @@ -255,8 +257,29 @@ describe(testCase, () => {
});
fail("It should not reach here");
} catch (error) {
console.log(error.message);
//error.message is Returned error: Nonce too low, below is not failing because it checks for an exact match
//And also it checks the error, not error.message
expect(error).not.toBe("Nonce too low");
}

// const contractInvocation = connector.invokeContract({
// contractName,
// keychainId: keychainPlugin.getKeychainId(),
// invocationType: EthContractInvocationType.Send,
// methodName: "setName",
// params: [newName],
// gas: 1000000,
// web3SigningCredential: {
// ethAccount: testEthAccount.address,
// secret: testEthAccount.privateKey,
// type: Web3SigningCredentialType.PrivateKeyHex,
// },
// nonce: 1,
// });

// await expect(contractInvocation).rejects.not.toThrow("Returned error: Nonce too low");

const { callOutput: getNameOut } = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand Down Expand Up @@ -354,9 +377,23 @@ describe(testCase, () => {
});
fail("It should not reach here");
} catch (error) {
//the actual error message here also contains "Nonce too low" in the body
expect(error).not.toBe("Nonce too low");
}

// const contractInvocation = connector.invokeContract({
// contractName,
// keychainId: keychainPlugin.getKeychainId(),
// invocationType: EthContractInvocationType.Send,
// methodName: "setName",
// params: [newName],
// gas: 1000000,
// web3SigningCredential,
// nonce: 4,
// });

// await expect(contractInvocation).rejects.not.toThrow("Nonce too low");

const { callOutput: getNameOut } = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,27 @@ describe(testCase, () => {
});
expect(setNameOut).toBeTruthy();

try {
await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
});
fail("invalid nonce should have thrown");
} catch (error: any) {
expect(error.message).toContain("Nonce too low");
}
const contractInvocation = connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential: {
ethAccount: testEthAccount.address,
secret: testEthAccount.privateKey,
type: Web3SigningCredentialType.PrivateKeyHex,
},
nonce: 1,
});

await expect(contractInvocation).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining("Nonce too low"),
}),
);

const { callOutput: getNameOut } = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand Down Expand Up @@ -294,21 +296,23 @@ describe(testCase, () => {
});
expect(setNameOut).toBeTruthy();

try {
await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential,
nonce: 4,
});
fail("invalid nonce should have thrown");
} catch (error: any) {
expect(error.message).toContain("Nonce too low");
}
const contractInvocation = connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
invocationType: EthContractInvocationType.Send,
methodName: "setName",
params: [newName],
gas: 1000000,
web3SigningCredential,
nonce: 4,
});

await expect(contractInvocation).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining("Nonce too low"),
}),
);

const { callOutput: getNameOut } = await connector.invokeContract({
contractName,
keychainId: keychainPlugin.getKeychainId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,32 @@ describe("Ethereum persistence PostgreSQL PostgresDatabaseClient tests", () => {
method_name: "",
};

try {
await dbClient.insertBlockData({
// try {
// await dbClient.insertBlockData({
// block,
// transactions: [
// {
// ...transaction,
// token_transfers: [token_transfer],
// },
// ],
// });
// expect(true).toBe(false); // Block insertion should fail
// } catch (error: unknown) {
// log.info("insertBlockData was rejected as expected");
// }

await expect(
dbClient.insertBlockData({
block,
transactions: [
{
...transaction,
token_transfers: [token_transfer],
},
],
});
expect(true).toBe(false); // Block insertion should fail
} catch (error: unknown) {
log.info("insertBlockData was rejected as expected");
}
}),
).rejects.toThrow(); // or any specific error or condition you expect

// Assert no data was added
const blocksResponse = await getDbBlocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ describe(testCase, () => {
web3SigningCredential,
gas: estimatedGas,
};
try {
const res = await api.initializeV1(request);
expect(res.status).toEqual(400);
} catch (error: any) {
expect(error.response.status).toEqual(400);
}

await expect(api.initializeV1(request)).rejects.toMatchObject({
response: expect.objectContaining({
status: 400,
}),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -312,26 +312,26 @@ describe(testCase, () => {
});
expect(callOutput).toEqual("10");

try {
const request: NewContractRequest = {
contractAddress: hashTimeLockAddress,
inputAmount: 0,
outputAmount: 0,
expiration,
hashLock,
tokenAddress,
receiver,
outputNetwork: "BTC",
outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8",
connectorId,
keychainId,
web3SigningCredential,
gas: estimatedGas,
};
const res = await api.newContractV1(request);
expect(res.status).toEqual(400);
} catch (error: any) {
expect(error.response.status).toEqual(400);
}
const request: NewContractRequest = {
contractAddress: hashTimeLockAddress,
inputAmount: 0,
outputAmount: 0,
expiration,
hashLock,
tokenAddress,
receiver,
outputNetwork: "BTC",
outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8",
connectorId,
keychainId,
web3SigningCredential,
gas: estimatedGas,
};

await expect(api.newContractV1(request)).rejects.toMatchObject({
response: expect.objectContaining({
status: 400,
}),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,18 @@ describe(testCase, () => {
expect(responseTxId.callOutput).toBeTruthy();
const id = responseTxId.callOutput as string;

try {
const refundRequest: RefundRequest = {
id,
web3SigningCredential,
connectorId,
keychainId,
};
const resRefund = await api.refundV1(refundRequest);
expect(resRefund.status).toEqual(400);
} catch (error: any) {
expect(error.response.status).toEqual(400);
}
const refundRequest: RefundRequest = {
id,
web3SigningCredential,
connectorId,
keychainId,
};

await expect(api.refundV1(refundRequest)).rejects.toMatchObject({
response: expect.objectContaining({
status: 400,
}),
});

const responseFinalBalance = await connector.invokeContract({
contractName: TestTokenJSON.contractName,
Expand Down
Loading

0 comments on commit 947d28a

Please sign in to comment.