Skip to content

Commit

Permalink
Merge pull request #35 from openlawteam/offchain-proof-helpers
Browse files Browse the repository at this point in the history
Added offchain proof helpers
  • Loading branch information
fforbeck authored Aug 19, 2021
2 parents 51fcc2f + 53a8c68 commit 8a96fa8
Show file tree
Hide file tree
Showing 11 changed files with 566 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/jest-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ jobs:
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 14.x

- name: Cache node modules
id: cache-node-modules
uses: actions/cache@v2
with:
path: node_modules
key: node-modules-${{ hashFiles('package-lock.json') }}

- name: Install dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.class
*.log
.env

# sbt specific
.cache
Expand Down
13 changes: 9 additions & 4 deletions tests/buildVoteMessage.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DEFAULT_VERIFYING_CONTRACT,
} from "./utils";
import { buildVoteMessage, VoteChoices } from "../index";
import { DEFAULT_SNAPSHOT_HUB_API_URL } from "./msw-mocks/helpers";
import { server } from "./msw-mocks";

describe("buildVoteMessage unit tests", () => {
Expand All @@ -22,7 +23,7 @@ describe("buildVoteMessage unit tests", () => {
space: "tribute",
token: DEFAULT_VERIFYING_CONTRACT,
},
"http://localhost:3000"
DEFAULT_SNAPSHOT_HUB_API_URL
);

expect(result).toMatchObject({
Expand All @@ -36,14 +37,18 @@ describe("buildVoteMessage unit tests", () => {
space: "tribute",
token: DEFAULT_VERIFYING_CONTRACT,
type: "vote",
version: undefined,
version: "0.1.2",
});

expect(result.timestamp).toMatch(/^[0-9]{1,}$/);
});

test("should throw on error", async () => {
server.use(rest.get("*/api", (_req, res, ctx) => res(ctx.status(500))));
server.use(
rest.get(`${DEFAULT_SNAPSHOT_HUB_API_URL}/api`, (_req, res, ctx) =>
res(ctx.status(500))
)
);

try {
await buildVoteMessage(
Expand All @@ -57,7 +62,7 @@ describe("buildVoteMessage unit tests", () => {
space: "tribute",
token: DEFAULT_VERIFYING_CONTRACT,
},
"http://localhost:3000"
DEFAULT_SNAPSHOT_HUB_API_URL
);
} catch (error) {
expect(error.message.length > 0).toBe(true);
Expand Down
100 changes: 100 additions & 0 deletions tests/getOffchainVotingProof.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
getOffchainVotingProof,
SnapshotOffchainProofResponse,
} from "../utils";
import { DEFAULT_SNAPSHOT_HUB_API_URL } from "./msw-mocks/helpers";
import { rest, server } from "./msw-mocks/server";
import { snapshotAPIOffchainProofResponse } from "./msw-mocks/rest-responses/snapshot-api";

const DEFAULT_MERKLE_ROOT_HEX: string =
"0x2f6a1ec9f67c87e7956228a0838b0980748f2dda936a0ebaf3e929f192fa7b6c";

describe("getOffchainVotingProof unit tests", () => {
test("can fetch proof", async () => {
let testResponse: SnapshotOffchainProofResponse | undefined;

testResponse = await getOffchainVotingProof(
DEFAULT_SNAPSHOT_HUB_API_URL,
"space",
DEFAULT_MERKLE_ROOT_HEX
);

expect(testResponse).toMatchObject(snapshotAPIOffchainProofResponse);
});

test('can return "undefined" if no proof exists', async () => {
server.use(
rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`,
(_req, res, ctx) => res(ctx.status(404))
)
);

let testResponse: SnapshotOffchainProofResponse | undefined;
let testError: Error | undefined = undefined;

try {
testResponse = await getOffchainVotingProof(
DEFAULT_SNAPSHOT_HUB_API_URL,
"space",
DEFAULT_MERKLE_ROOT_HEX
);
} catch (error) {
testError = error;
}

expect(testResponse).toBe(undefined);
expect(testError).toBe(undefined);
});

test("can throw error when server error", async () => {
server.use(
rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`,
(_req, res, ctx) => res(ctx.status(500))
)
);

let testError: any;

try {
await getOffchainVotingProof(
DEFAULT_SNAPSHOT_HUB_API_URL,
"space",
DEFAULT_MERKLE_ROOT_HEX
);
} catch (error) {
testError = error;
}

expect(testError?.message).toMatch(
/something went wrong while getting the off-chain vote proof\./i
);
});

test("can throw error when client error", async () => {
server.use(
rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`,
(_req, res, ctx) => res(ctx.status(400))
)
);

let testError: any;

try {
// Using fake data
await getOffchainVotingProof(
DEFAULT_SNAPSHOT_HUB_API_URL,
"space",
DEFAULT_MERKLE_ROOT_HEX
);
} catch (error) {
testError = error;
}

expect(testError?.message).toMatch(
/something went wrong while getting the off-chain vote proof\./i
);
});
});
97 changes: 81 additions & 16 deletions tests/msw-mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,83 @@
import { rest } from "msw";

const snapshotAPIStatusHandler = rest.get("*/api", (_req, res, ctx) =>
res(
ctx.json({
data: {
name: "snapshot-hub",
network: "testnet",
version: "0.1.2",
tag: "alpha",
relayer: "0xEd7B3f2902f2E1B17B027bD0c125B674d293bDA0",
},
})
)
);

// Export all mock handlers
export const handlers = [snapshotAPIStatusHandler];
import {
snapshotAPIDraftResponse,
snapshotAPIOffchainProofResponse,
snapshotAPIProposalResponse,
snapshotAPIRootResponse,
snapshotAPISpaceResponse,
snapshotAPISubmitMessage,
} from "./rest-responses/snapshot-api";
import { DEFAULT_SNAPSHOT_HUB_API_URL } from "./helpers";

/**
* Snapshot API
*/

const getSnapshotAPIStatus = rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api`,
(_req, res, ctx) =>
res(
ctx.json({
data: {
name: "snapshot-hub",
network: "testnet",
version: "0.1.2",
tag: "alpha",
relayer: "0xEd7B3f2902f2E1B17B027bD0c125B674d293bDA0",
},
})
)
);

const getSnapshotAPIRoot = rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api`,
async (_req, res, ctx) => res(ctx.json(snapshotAPIRootResponse))
);

const getSnapshotAPISpace = rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/spaces/:spaceName`,
async (_req, res, ctx) => res(ctx.json(snapshotAPISpaceResponse))
);

const getSnapshotAPIDraft = rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/draft/:id`,
async (_req, res, ctx) => res(ctx.json(snapshotAPIDraftResponse))
);

const getSnapshotAPIProposal = rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/proposal/:id`,
async (_req, res, ctx) => res(ctx.json(snapshotAPIProposalResponse))
);

const postSnapshotAPIMessage = rest.post(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/message`,
async (_req, res, ctx) => res(ctx.json(snapshotAPISubmitMessage))
);

const postSnapshotAPIOffchainProof = rest.post(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proofs`,
(_req, res, ctx) => res(ctx.status(201))
);

const getSnapshotAPIOffchainProof = rest.get(
`${DEFAULT_SNAPSHOT_HUB_API_URL}/api/:spaceName/offchain_proof/:merkleRoot`,
(_req, res, ctx) => res(ctx.json(snapshotAPIOffchainProofResponse))
);

/**
* HANDLERS TO EXPORT
*/

const handlers = [
getSnapshotAPIDraft,
getSnapshotAPIOffchainProof,
getSnapshotAPIProposal,
getSnapshotAPIRoot,
getSnapshotAPIStatus,
getSnapshotAPISpace,
postSnapshotAPIMessage,
postSnapshotAPIOffchainProof,
];

export { handlers };
25 changes: 25 additions & 0 deletions tests/msw-mocks/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const DEFAULT_ETH_ADDRESS: string =
"0x04028Df0Cea639E97fDD3fC01bA5CC172613211D";

export const DEFAULT_SPACE: string = "tributetest";

export const DEFAULT_PROPOSAL_HASH: string =
"0x4662dd46b8ca7ce0852426f20bc53b02335432089bbe3a4c510b36741d81ca75";

export const DEFAULT_DRAFT_HASH: string =
"0xb22ca9af120bfddfc2071b5e86a9edee6e0e2ab76399e7c2d96a9d502f5c1241";

export const DEFAULT_SIG: string =
"0x042bfdd90cddaf67978fe3e3294f1c1aad017a06dde072c02ad1549914b8bf8e316c1addbad510247d2e5175006c272632a3b79120898482cf5e0187a1283b811b";

export const DEFAULT_DAO_NAME: string = "test-dao";

export const DEFAULT_ADAPTER_OR_EXTENSION_ID: string =
"0x86e4d415f8203aaabba34505e3ceb7b05f5255120bc6ae19dd5f4823ce4e1fb9";

export const DEFAULT_DELEGATED_ADDRESS: string =
"0xA51086a07AE92Ae8c29E7CD34421c16fd666595c";

// Fake Snapshot Hub host
export const DEFAULT_SNAPSHOT_HUB_API_URL: string =
"http://localhost:8080/snapshot-hub";
Loading

0 comments on commit 8a96fa8

Please sign in to comment.