Skip to content

Commit

Permalink
fix tests and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarinas committed Oct 10, 2023
1 parent f24c099 commit 71e0c20
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 70 deletions.
4 changes: 2 additions & 2 deletions modules/client-common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ export { Permissions };
Object.freeze(PermissionIds);
export { PermissionIds };
export const IPFS_CID_REGEX =
/^Qm([1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})$/;
/^((Qm[1-9A-HJ-NP-Za-km-z]{44,})|(b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,})|(z[1-9A-HJ-NP-Za-km-z]{48,})|(F[0-9A-F]{50,}))$/;

export const IPFS_URI_REGEX =
/^ipfs:\/\/(Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})$/;
/^ipfs:\/\/((Qm[1-9A-HJ-NP-Za-km-z]{44,})|(b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,})|(z[1-9A-HJ-NP-Za-km-z]{48,})|(F[0-9A-F]{50,}))$/;
export const OSX_PROPOSAL_ID_REGEX =
/^(0x[A-Fa-f0-9]{40})_(0x[A-Fa-f0-9]{1,64})$/;
export const HEX_STRING_REGEX = /^(0x)?[0-9a-fA-F]*$/;
Expand Down
2 changes: 0 additions & 2 deletions modules/client-common/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ export function decodeProposalId(
if (!isProposalId(proposalId)) {
throw new InvalidProposalIdError();
}
// const matchedRegexResult =
// proposalId.match(/^(0x[A-Fa-f0-9]{40})_(0x[A-Fa-f0-9]{1,64})$/) || [];

const matchedRegexResult = proposalId.match(OSX_PROPOSAL_ID_REGEX) || [];
if (matchedRegexResult.length !== 3) {
Expand Down
6 changes: 2 additions & 4 deletions modules/client-common/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
InvalidContractAbiError,
InvalidParameter,
InvalidSubdomainError,
isEnsName,
isIpfsUri,
isSubdomain,
} from ".";
} from "./errors";
import { array, mixed, number, object, string } from "yup";
import { isAddress } from "@ethersproject/address";
import { ANY_ADDRESS } from "./internal/constants";
import { isEnsName, isIpfsUri, isSubdomain } from "./validation";

export const BigintSchema = mixed().test(
"isBigint",
Expand Down
21 changes: 9 additions & 12 deletions modules/client-common/test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ export const TEST_WALLET =
"0xdf57089febbacf7ba0bc227dafbffa9fc08a93fdc68e1e42411a14efcf23656e";
export const TEST_WALLET_ADDRESS = "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199";
export const ADDRESS_ONE = "0x0000000000000000000000000000000000000001";
export const IPFS_CID = "QmSH4tFQd6zqPW9b8ryvtrnbhr9HATyetmWxtGgWUJ33ik";
export const INVALID_IPFS_CID = "1nv4l1d_c1d";
export const INVALID_IPFS_URI = `ipfs://${INVALID_IPFS_CID}`;
export const IPFS_URI = `ipfs://${IPFS_CID}`;
export const HTTP_URI = "https://aragon.org";

export const web3endpoints = {
working: [
Expand Down Expand Up @@ -115,20 +110,22 @@ export const TEST_ABI: MetadataAbiInput[] = [
},
];


export const TEST_ADDRESS = "0x0000000000000000000000000000000000000001";
export const TEST_INVALID_ADDRESS =
"0x000000000000000000000000000000000000000P";

export const TEST_ENS_NAME = "test.eth";
export const TEST_INVALID_ENS_NAME = "test.invalid";

export const TEST_IPFS_URI_V0 =
"ipfs://QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR";
export const TEST_IPFS_URI_V1 =
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi";
export const TEST_INVALID_IPFS_URI =
"ipfs://QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR-invalid";
export const TEST_IPFS_CID_V0 =
"QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR";
export const TEST_IPFS_CID_V1 =
"bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi";

export const TEST_IPFS_URI_V0 = `ipfs://${TEST_IPFS_CID_V0}`;
export const TEST_IPFS_URI_V1 = `ipfs://${TEST_IPFS_CID_V1}`;
export const TEST_INVALID_IPFS_CID = "1nv4l1d_c1d";
export const TEST_INVALID_IPFS_URI = `ipfs://${TEST_INVALID_IPFS_CID}`;

export const TEST_HTTP_URI = "https://test.com";
export const TEST_INVALID_HTTP_URI = "https://te?st.com-invalid";
Expand Down
27 changes: 16 additions & 11 deletions modules/client-common/test/unit/multiuri.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { MultiUri } from "../../src";
import { HTTP_URI, INVALID_IPFS_CID, IPFS_CID, IPFS_URI } from "../constants";
import {
TEST_HTTP_URI,
TEST_INVALID_IPFS_CID,
TEST_IPFS_CID_V0,
TEST_IPFS_URI_V0,
} from "../constants";

describe("MultiUri", () => {
it("Should get http and ipfs urls", () => {
const uris = [
IPFS_URI,
HTTP_URI,
TEST_IPFS_URI_V0,
TEST_HTTP_URI,
];
const cid = uris[0].substring(7);
const multiuri = new MultiUri(uris.join(","));
Expand All @@ -15,21 +20,21 @@ describe("MultiUri", () => {
expect(multiuri.ipfsCid!).toBe(cid);
});
it("Should get an ipfs cid", () => {
const cid = IPFS_CID;
const cid = TEST_IPFS_CID_V0;
const multiuri = new MultiUri(cid);
expect(multiuri.http.length).toBe(0);
expect(multiuri.ipfs!.cid).toBe(cid);
expect(multiuri.ipfsCid!).toBe(cid);
});
it("Should get an invalid ipfs cid and return an empty string", () => {
const cid = INVALID_IPFS_CID;
const cid = TEST_INVALID_IPFS_CID;
const multiuri = new MultiUri(cid);
expect(multiuri.http.length).toBe(0);
expect(multiuri.ipfs).toBe(null);
expect(multiuri.ipfsCid).toBe(null);
});
it("Should get a valid ipfs uri", () => {
const uri = IPFS_URI;
const uri = TEST_IPFS_URI_V0;
const cid = uri.substring(7);
const multiuri = new MultiUri(uri);
expect(multiuri.http.length).toBe(0);
Expand All @@ -38,9 +43,9 @@ describe("MultiUri", () => {
});
it("Should get multiple http urls", () => {
const uris = [
`${HTTP_URI}/1`,
`${HTTP_URI}/2`,
`${HTTP_URI}/3`,
`${TEST_HTTP_URI}/1`,
`${TEST_HTTP_URI}/2`,
`${TEST_HTTP_URI}/3`,
];
const multiuri = new MultiUri(uris.join(","));
expect(multiuri.http.length).toBe(uris.length);
Expand All @@ -51,7 +56,7 @@ describe("MultiUri", () => {
expect(multiuri.ipfsCid!).toBe(null);
});
it("Should get an ipfs uri with path", () => {
const uri = `${IPFS_URI}/some/path`;
const uri = `${TEST_IPFS_URI_V0}/some/path`;
const pathCid = uri.substring(7);
let pathIdx = pathCid.indexOf("/");
const cid = pathCid.substring(0, pathIdx);
Expand All @@ -63,7 +68,7 @@ describe("MultiUri", () => {
expect(multiuri.ipfsCid!).toBe(cid);
});
it("Should get an ipfs cid with path", () => {
const pathCid = `${IPFS_CID}/some/path`;
const pathCid = `${TEST_IPFS_CID_V0}/some/path`;
let pathIdx = pathCid.indexOf("/");
const cid = pathCid.substring(0, pathIdx);
const path = pathCid.substring(pathIdx);
Expand Down
148 changes: 112 additions & 36 deletions modules/client-common/test/unit/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,121 @@
import { InvalidCidError, resolveIpfsCid } from "../../src";
import {
INVALID_IPFS_CID,
INVALID_IPFS_URI,
IPFS_CID,
IPFS_URI,
InvalidCidError,
isEnsName,
isIpfsUri,
isProposalId,
isSubdomain,
resolveIpfsCid,
} from "../../src";
import {
ADDRESS_ONE,
TEST_ENS_NAME,
TEST_INVALID_ENS_NAME,
TEST_INVALID_IPFS_CID,
TEST_INVALID_IPFS_URI,
TEST_INVALID_SUBDOMAIN,
TEST_IPFS_CID_V0,
TEST_IPFS_CID_V1,
TEST_IPFS_URI_V0,
TEST_IPFS_URI_V1,
TEST_SUBDOMAIN,
} from "../constants";
describe("Test Validations", () => {
describe("resolveIpfsCid", () => {
it("Should resolve IPFS CiD's", () => {
let cid = TEST_IPFS_CID_V0;
let result = resolveIpfsCid(cid);
expect(result).toEqual(cid);

describe("resolveIpfsCid", () => {
it("Should resolve IPFS CiD's", () => {
let cid = IPFS_CID;
let result = resolveIpfsCid(cid);
expect(result).toEqual(cid);
cid = TEST_IPFS_CID_V0;
result = resolveIpfsCid(cid);
expect(result).toEqual(cid);
});
it("Should resolve IPFS URI's V0", () => {
let cid = TEST_IPFS_CID_V0;
let result = resolveIpfsCid(cid);
expect(result).toEqual(TEST_IPFS_CID_V0);

cid = IPFS_CID;
result = resolveIpfsCid(cid);
expect(result).toEqual(cid);
});
it("Should resolve IPFS URI's", () => {
let cid = IPFS_CID;
let result = resolveIpfsCid(cid);
expect(result).toEqual(IPFS_CID);
cid = TEST_IPFS_URI_V0;
result = resolveIpfsCid(cid);
expect(result).toEqual(TEST_IPFS_CID_V0);

cid = IPFS_URI;
result = resolveIpfsCid(cid);
expect(result).toEqual(IPFS_CID);
cid = `${TEST_IPFS_URI_V0}/path`;
result = resolveIpfsCid(cid);
expect(result).toEqual(
TEST_IPFS_CID_V0,
);
});
it("Should resolve IPFS URI's V1", () => {
let cid = TEST_IPFS_CID_V1;
let result = resolveIpfsCid(cid);
expect(result).toEqual(TEST_IPFS_CID_V1);

cid = `${IPFS_URI}/path`;
result = resolveIpfsCid(cid);
expect(result).toEqual(
IPFS_CID,
);
cid = TEST_IPFS_URI_V1;
result = resolveIpfsCid(cid);
expect(result).toEqual(TEST_IPFS_CID_V1);

cid = `${TEST_IPFS_URI_V1}/path`;
result = resolveIpfsCid(cid);
expect(result).toEqual(
TEST_IPFS_CID_V1,
);
});
it("Should fail to recognize a non-IPFS origin", () => {
expect(() => resolveIpfsCid(TEST_INVALID_IPFS_CID)).toThrow(
new InvalidCidError(),
);
expect(() => resolveIpfsCid(TEST_INVALID_IPFS_URI)).toThrow(
new InvalidCidError(),
);
expect(() => resolveIpfsCid(`${TEST_INVALID_IPFS_URI}/path`)).toThrow(
new InvalidCidError(),
);
});
});
describe("isProposalId", () => {
it("Should recognize invalid proposal IDs", () => {
const inputs = [
{ in: "1", out: false },
{ in: "0x1", out: false },
{ in: ADDRESS_ONE + "_0x1", out: true },
{ in: ADDRESS_ONE, out: false },
{ in: "0x1_0x1", out: false },
{ in: ADDRESS_ONE + "_0x" + "0".repeat(64), out: true },
];
for (const input of inputs) {
expect(isProposalId(input.in)).toBe(input.out);
}
});
});
describe("isIpfsUri", () => {
const inputs = [
{ in: TEST_IPFS_CID_V0, out: false },
{ in: TEST_IPFS_CID_V1, out: false },
{ in: TEST_INVALID_IPFS_URI, out: false },
{ in: TEST_IPFS_URI_V0, out: true },
{ in: TEST_IPFS_URI_V1, out: true },
];
for (const input of inputs) {

expect(isIpfsUri(input.in)).toBe(input.out);
}
});
describe("isSubdomain", () => {
const inputs = [
{ in: TEST_SUBDOMAIN, out: true },
{ in: TEST_INVALID_SUBDOMAIN, out: false },
];
for (const input of inputs) {
expect(isSubdomain(input.in)).toBe(input.out);
}
});
it("Should fail to recognize a non-IPFS origin", () => {
expect(() => resolveIpfsCid(INVALID_IPFS_CID)).toThrow(
new InvalidCidError(),
);
expect(() => resolveIpfsCid(INVALID_IPFS_URI)).toThrow(
new InvalidCidError(),
);
expect(() => resolveIpfsCid(`${INVALID_IPFS_URI}/path`)).toThrow(
new InvalidCidError(),
);
describe("isEnsName", () => {
const inputs = [
{ in: TEST_ENS_NAME, out: true },
{ in: TEST_INVALID_ENS_NAME, out: false },
];
for (const input of inputs) {
expect(isEnsName(input.in)).toBe(input.out);
}
});
});
5 changes: 2 additions & 3 deletions modules/client/test/integration/client/decoding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ import { defaultAbiCoder } from "@ethersproject/abi";
import { AddressZero } from "@ethersproject/constants";
import {
ApplyInstallationParams,
ApplyUpdateParams,
ApplyUninstallationParams,
Context,
PermissionIds,
ApplyUpdateParams,
bytesToHex,
Context,
hexToBytes,
PermissionIds,
PermissionOperationType,
Permissions,
TokenType,
Expand Down

0 comments on commit 71e0c20

Please sign in to comment.