Skip to content

Commit

Permalink
test: repalce endpoints from old testsolution
Browse files Browse the repository at this point in the history
  • Loading branch information
pcheremu committed Oct 19, 2023
1 parent ee9545a commit 86e6244
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/integration-tests/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { execSync } from "child_process";
import { ethers } from "ethers";
import { promises as fs } from "fs";
import * as path from "path";
import { Provider } from "zksync-web3";

import { localConfig } from "./config";
import { Logger } from "./entities";

export class Helper {
Expand Down Expand Up @@ -35,4 +38,20 @@ export class Helper {
console.log(`There is no the expected file: ${fileName}`);
}
}

async getBalanceETH(walletAddress: string, layer: string) {
let network: any;
let provider: any;
if (layer == "L1") {
network = localConfig.L1Network;
provider = ethers.getDefaultProvider(network);
} else if (layer == "L2") {
network = localConfig.L2Network;
provider = new Provider(network);
} else {
console.log(`Wrong layer: ${layer}`);
}
const balanceEth = ethers.utils.formatUnits(await provider.getBalance(walletAddress), "wei");
return balanceEth;
}
}
219 changes: 218 additions & 1 deletion packages/integration-tests/tests/api/common/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as request from "supertest";
import { setTimeout } from "timers/promises";

import { environment, localConfig } from "../../../src/config";
import { Buffer } from "../../../src/entities";
import { Buffer, Token, Wallets } from "../../../src/entities";
import { Helper } from "../../../src/helper";

describe("Endpoints", () => {
Expand Down Expand Up @@ -182,4 +182,221 @@ describe("Endpoints", () => {
);
});
});

describe("Accounts API", () => {
//@id1704
it(
"Verify /api?module=account&action=balancemulti response returns elements" +
" balancemulti => balancemulti&address={account_address1},{account_address2}",
async () => {
const apiRoute = `/api?module=account&action=balancemulti&address=${Wallets.richWalletAddress},${Wallets.mainWalletAddress}`;
const richWalletBalance = await helper.getBalanceETH(Wallets.richWalletAddress, "L2");
const mainWalletBalance = await helper.getBalanceETH(Wallets.mainWalletAddress, "L2");
const richWalletLowerCase = Wallets.richWalletAddress.toLowerCase();
const mainWalletLowerCase = Wallets.mainWalletAddress.toLowerCase();
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body.result.length).toBeGreaterThan(1))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) =>
expect(res.body.result[0]).toStrictEqual(
expect.objectContaining({ account: richWalletLowerCase, balance: richWalletBalance })
)
)
.expect((res) =>
expect(res.body.result[1]).toStrictEqual(
expect.objectContaining({ account: mainWalletLowerCase, balance: mainWalletBalance })
)
);
}
);

//@id1703
it(
"Verify /api?module=account&action=balance response returns elements" +
"balance => balance&address={account_address}",
async () => {
const apiRoute = `/api?module=account&action=balance&address=${Wallets.richWalletAddress}`;
const balance = await helper.getBalanceETH(Wallets.richWalletAddress, "L2");
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ result: balance })));
}
);

//@id1705
it(
"Verify /api?module=account&action=tokenbalance response returns elements" +
" tokenbalance => tokenbalance&contractaddress={contract_address}&address={account_address}",
async () => {
const apiRoute = `/api?module=account&action=tokenbalance&contractaddress=${Token.ETHER_ERC20_Address}&address=${Wallets.richWalletAddress}`;
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) => expect(typeof res.body.result).toStrictEqual("string"));
}
);

//@id1702
it(
"Verify /api?module=account&action=txlist response returns elements" +
" txlist => txlist&page=1&offset=10&sort=desc&endblock{block_number}&startblock=0&address={account_address}",
async () => {
const blocks = await request(environment.blockExplorerAPI).get("/blocks");

const blockNumber = blocks.body.items[0].number;
const apiRoute = `/api?module=account&action=txlist&page=1&offset=10&sort=desc&endblock${blockNumber}&startblock=0&address=${Wallets.richWalletAddress}`;

await setTimeout(localConfig.extendedPause); //works unstable without timeout

return (
request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body.result.length).toBeGreaterThan(1))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) => expect(typeof res.body.result[0].blockNumber).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].timeStamp).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].hash).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].nonce).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].blockHash).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].transactionIndex).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].from).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].to).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].value).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].gas).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].gasPrice).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].isError).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].txreceipt_status).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].input).toStrictEqual("string"))
// .expect((res) => expect(typeof res.body.result[0].contractAddress).toStrictEqual("string")) // can be null
.expect((res) => expect(typeof res.body.result[0].cumulativeGasUsed).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].gasUsed).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].confirmations).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].fee).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].commitTxHash).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].proveTxHash).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].executeTxHash).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].isL1Originated).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].l1BatchNumber).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].methodId).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result[0].functionName).toStrictEqual("string"))
);
}
);
});

describe("Transactions API", () => {
//@id1697
it(
"Verify /api?module=transaction&action=getstatus response returns elements" +
" getstatus => getstatus&txhash={tx_hash}",
async () => {
const blocks = await request(environment.blockExplorerAPI).get("/transactions");

const txHash = blocks.body.items[0].hash;
const apiRoute = `/api?module=transaction&action=getstatus&txhash=${txHash}`;
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) =>
expect(res.body.result).toStrictEqual(expect.objectContaining({ isError: "0", errDescription: "" }))
);
}
);

//@id1698
it(
"Verify /api?module=transaction&action=gettxreceiptstatus response returns elements" +
" gettxreceiptstatus => gettxreceiptstatus&txhash={tx_hash}",
async () => {
const blocks = await request(environment.blockExplorerAPI).get("/transactions");

const txHash = blocks.body.items[0].hash;
const apiRoute = `/api?module=transaction&action=gettxreceiptstatus&txhash=${txHash}`;
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) => expect(typeof res.body.result.status).toStrictEqual("string"));
}
);
});

describe("Block API", () => {
const bufferRoute = "src/playbook/";
//@id1700
it("Verify /api?module=block&action=getblockcountdown&blockno={block_number} response returns elements", async () => {
const blocks = await request(environment.blockExplorerAPI).get("/blocks");

const blockNumber = blocks.body.items[0].number + 1;
const apiRoute = `/api?module=block&action=getblockcountdown&blockno=${blockNumber}`;
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) => expect(typeof res.body.result.CurrentBlock).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.CountdownBlock).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.RemainingBlock).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.EstimateTimeInSec).toStrictEqual("string"));
});

//@id1699
it("Verify /api?module=block&action=getblocknobytime&closest=before&timestamp={timestamp} response returns elements", async () => {
const apiRoute = `/api?module=block&action=getblocknobytime&closest=before&timestamp=1635934550`;
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) => expect(typeof res.body.result).toStrictEqual("string"));
});

//@id1701
it("Verify /api?module=block&action=getblockreward&blockno={blockNumber} response returns elements", async () => {
const blocks = await request(environment.blockExplorerAPI).get("/blocks");

const blockNumber = blocks.body.items[0].number;
const apiRoute = `/api?module=block&action=getblockreward&blockno=${blockNumber}`;
await setTimeout(localConfig.extendedPause); //works unstable without timeout

return request(environment.blockExplorerAPI)
.get(apiRoute)
.expect(200)
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ status: "1" })))
.expect((res) => expect(res.body).toStrictEqual(expect.objectContaining({ message: "OK" })))
.expect((res) => expect(typeof res.body.result.blockNumber).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.timeStamp).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.blockMiner).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.blockReward).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.uncleInclusionReward).toStrictEqual("string"))
.expect((res) => expect(typeof res.body.result.uncles).toStrictEqual("object"));
});
});
});

0 comments on commit 86e6244

Please sign in to comment.