Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add validation test case #621

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/__tests__/validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { BigNumber } from "bignumber.js";

import { fetchTokenList, MinimalTokenInfo } from "../hooks/token";
import { UnknownTransfer, AssetTransfer, CollectibleTransfer } from "../hooks/useCsvParser";
import { validateRow, validateAssetRow, validateCollectibleRow } from "../parser/validation";
import { testData } from "../test/util";

const dummySafeInfo = testData.dummySafeInfo;
let listedToken: MinimalTokenInfo;

describe("validateRow", () => {
it("should return warnings for unknown token_type", () => {
const unknownTransfer: UnknownTransfer = { token_type: "unknown" };
const result = validateRow(unknownTransfer);
expect(result).toEqual(["Unknown token_type: Must be one of erc20, native or nft"]);
});

it("should call validateAssetRow for erc20", () => {
const assetTransfer: AssetTransfer = {
token_type: "erc20",
tokenAddress: null,
receiver: testData.addresses.receiver1,
amount: "1",
decimals: 18,
receiverEnsName: null,
};
const result = validateRow(assetTransfer);
expect(result).toEqual([]);
});

it("should call validateCollectibleRow for erc721", () => {
const collectibleTransfer: CollectibleTransfer = {
token_type: "erc721",
tokenAddress: testData.addresses.dummyErc721Address,
receiver: testData.addresses.receiver1,
tokenId: new BigNumber("69").toFixed(),
from: testData.dummySafeInfo.safeAddress,
receiverEnsName: null,
};
const result = validateRow(collectibleTransfer);
expect(result).toEqual([]);
});
});

describe("validateAssetRow", () => {
beforeAll(async () => {
const tokenList = await fetchTokenList(dummySafeInfo.chainId);
let listedTokens = Array.from(tokenList.keys());
const tokenInfo = tokenList.get(listedTokens[0]);
if (typeof tokenInfo !== "undefined") {
listedToken = tokenInfo;
}
});

it("should validate positive amount", () => {
const assetTransfer: AssetTransfer = {
token_type: "erc20",
tokenAddress: listedToken.address,
receiver: testData.addresses.receiver1,
amount: "-1",
decimals: listedToken.decimals,
receiverEnsName: null,
};
const result = validateAssetRow(assetTransfer);
expect(result).toContain("Only positive amounts/values possible: -1");
});

it("should validate if token is found", () => {
const assetTransfer: AssetTransfer = {
token_type: "erc20",
tokenAddress: listedToken.address,
receiver: testData.addresses.receiver1,
amount: "10",
decimals: -1,
symbol: "TOKEN_NOT_FOUND",
receiverEnsName: null,
};
const result = validateAssetRow(assetTransfer);
expect(result).toContain("No token contract was found at " + listedToken.address);
});
});

describe("validateCollectibleRow", () => {
it("should validate positive token ID", () => {
const collectibleTransfer: CollectibleTransfer = {
token_type: "erc721",
tokenAddress: testData.addresses.dummyErc721Address,
receiver: testData.addresses.receiver1,
tokenId: "-1",
from: testData.dummySafeInfo.safeAddress,
receiverEnsName: null,
};
const result = validateCollectibleRow(collectibleTransfer);
expect(result).toContain("Only positive Token IDs possible: -1");
});

it("should validate integer token ID", () => {
const collectibleTransfer: CollectibleTransfer = {
token_type: "erc721",
tokenAddress: testData.addresses.dummyErc721Address,
receiver: testData.addresses.receiver1,
tokenId: "1.5",
from: testData.dummySafeInfo.safeAddress,
receiverEnsName: null,
};
const result = validateCollectibleRow(collectibleTransfer);
expect(result).toContain("Token IDs must be integer numbers: 1.5");
});

it("should validate value for ERC1155 tokens", () => {
const erc1155Transfer: CollectibleTransfer = {
token_type: "erc1155",
tokenAddress: testData.addresses.dummyErc721Address,
receiver: testData.addresses.receiver1,
tokenId: "1",
amount: "0",
from: testData.dummySafeInfo.safeAddress,
receiverEnsName: null,
};
const result = validateCollectibleRow(erc1155Transfer);
expect(result).toContain("ERC1155 Tokens need a defined value > 0: 0");
});
});