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

tests: approvals tests #628

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"start:chrome:proxy": "NAMADA_INTERFACE_PROXY=true yarn start:chrome",
"start:firefox": "yarn clean:firefox && NODE_ENV=development TARGET=firefox yarn watch",
"start:firefox:proxy": "NAMADA_INTERFACE_PROXY=true yarn start:firefox",
"test": "./scripts/build-node.sh && yarn jest",
"test": "./scripts/build-node.sh && yarn jest --coverage",
"test:watch": "./scripts/build-node.sh && yarn jest --watchAll=true",
"test:ci": "jest",
"wasm:build": "node ./scripts/build.js --release",
Expand Down Expand Up @@ -88,6 +88,7 @@
"fake-indexeddb": "^4.0.1",
"file-loader": "^6.2.0",
"jest": "^29.0.1",
"jest-create-mock-instance": "^2.0.0",
"jest-environment-jsdom": "^29.3.1",
"leb128": "^0.0.5",
"merge-jsons-webpack-plugin": "^2.0.1",
Expand Down
98 changes: 98 additions & 0 deletions apps/extension/src/background/approvals/handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TxType } from "@namada/shared";
import { AccountType } from "@namada/types";
import createMockInstance from "jest-create-mock-instance";
import {
ApproveConnectInterfaceMsg,
ApproveSignArbitraryMsg,
ApproveTxMsg,
} from "provider";
import { Message } from "router";
import { getHandler } from "./handler";
import {
ConnectInterfaceResponseMsg,
RejectSignatureMsg,
RejectTxMsg,
RevokeConnectionMsg,
SubmitApprovedSignatureMsg,
SubmitApprovedTxMsg,
} from "./messages";
import { ApprovalsService } from "./service";

jest.mock("webextension-polyfill", () => ({}));

class UnknownMsg extends Message<unknown> {
validate(): void {}
route(): string {
return "unknown";
}
type(): string {
return "unknown";
}
}

describe.only("approvals handler", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("handlers switch", () => {
const service: jest.Mocked<ApprovalsService> = createMockInstance(
ApprovalsService as any
);
const handler = getHandler(service);
const env = {
isInternalMsg: true,
senderTabId: 1,
requestInteraction: () => {},
};

const approveTxMsg = new ApproveTxMsg(
TxType.Bond,
"txMsg",
"specificMsg",
AccountType.Mnemonic
);
handler(env, approveTxMsg);
expect(service.approveTx).toBeCalled();

const rejectTxMsg = new RejectTxMsg("msgId");
handler(env, rejectTxMsg);
expect(service.rejectTx).toBeCalled();

const submitApprovedTxMsg = new SubmitApprovedTxMsg(TxType.Bond, "msgId");
handler(env, submitApprovedTxMsg);
expect(service.submitTx).toBeCalled();

const approveConnectInterfaceMsg = new ApproveConnectInterfaceMsg();
handler(env, approveConnectInterfaceMsg);
expect(service.approveConnection).toBeCalled();

const connectInterfaceResponseMsg = new ConnectInterfaceResponseMsg(
0,
"",
true
);
handler(env, connectInterfaceResponseMsg);
expect(service.approveConnectionResponse).toBeCalled();

const revokeConnectionMsg = new RevokeConnectionMsg("");
handler(env, revokeConnectionMsg);
expect(service.revokeConnection).toBeCalled();

const approveSignArbitraryMsg = new ApproveSignArbitraryMsg("", "");
handler(env, approveSignArbitraryMsg);
expect(service.approveSignature).toBeCalled();

const rejectSignatureMsg = new RejectSignatureMsg("");
handler(env, rejectSignatureMsg);
expect(service.rejectSignature).toBeCalled();

const submitApprovedSignatureMsg = new SubmitApprovedSignatureMsg("", "");
handler(env, submitApprovedSignatureMsg);
expect(service.submitSignature).toBeCalled();

const unknownMsg = new UnknownMsg();
expect(() => handler(env, unknownMsg)).toThrow();
});
});
128 changes: 128 additions & 0 deletions apps/extension/src/background/approvals/messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TxType } from "@namada/shared";
import { ROUTE } from "./constants";
import {
ConnectInterfaceResponseMsg,
MessageType,
RejectSignatureMsg,
RejectTxMsg,
RevokeConnectionMsg,
SubmitApprovedSignatureMsg,
SubmitApprovedTxMsg,
} from "./messages";

jest.mock("webextension-polyfill", () => ({}));

describe("approvals messages", () => {
test("valid RejectTxMsg", () => {
const msg = new RejectTxMsg("msgId");

expect(msg.type()).toBe(MessageType.RejectTx);
expect(msg.route()).toBe(ROUTE);
expect(msg.validate()).toBeUndefined();
});

test("invalid RejectTxMsg", () => {
const msg = new RejectTxMsg("msgId");
(msg as any).msgId = undefined;

expect(() => msg.validate()).toThrow();
});

test("valid SubmitApprovedTxMsg", () => {
const msg = new SubmitApprovedTxMsg(TxType.Bond, "msgId");

expect(msg.type()).toBe(MessageType.SubmitApprovedTx);
expect(msg.route()).toBe(ROUTE);
expect(msg.validate()).toBeUndefined();
});

test("invalid SubmitApprovedTxMsg", () => {
const msg = new SubmitApprovedTxMsg(TxType.Bond, "msgId");
(msg as any).msgId = undefined;

expect(() => msg.validate()).toThrow();

const msg2 = new SubmitApprovedTxMsg(TxType.Bond, "msgId");
(msg2 as any).txType = undefined;

expect(() => msg2.validate()).toThrow();
});

test("valid RejectSignatureMsg", () => {
const msg = new RejectSignatureMsg("msgId");

expect(msg.type()).toBe(MessageType.RejectSignature);
expect(msg.route()).toBe(ROUTE);
expect(msg.validate()).toBeUndefined();
});

test("invalid RejectSignatureMsg", () => {
const msg = new RejectSignatureMsg("msgId");
(msg as any).msgId = undefined;

expect(() => msg.validate()).toThrow();
});

test("valid SubmitApprovedSignatureMsg", () => {
const msg = new SubmitApprovedSignatureMsg("msgId", "signer");

expect(msg.type()).toBe(MessageType.SubmitApprovedSignature);
expect(msg.route()).toBe(ROUTE);
expect(msg.validate()).toBeUndefined();
});

test("invalid SubmitApprovedSignatureMsg", () => {
const msg = new SubmitApprovedSignatureMsg("msgId", "signer");
(msg as any).msgId = undefined;

expect(() => msg.validate()).toThrow();

const msg2 = new SubmitApprovedSignatureMsg("msgId", "signer");
(msg2 as any).signer = undefined;

expect(() => msg2.validate()).toThrow();
});

test("valid ConnectInterfaceResponseMsg", () => {
const msg = new ConnectInterfaceResponseMsg(0, "interface", true);

expect(msg.type()).toBe(MessageType.ConnectInterfaceResponse);
expect(msg.route()).toBe(ROUTE);
expect(msg.validate()).toBeUndefined();
});

test("invalid ConnectInterfaceResponseMsg", () => {
const msg = new ConnectInterfaceResponseMsg(0, "interface", true);

(msg as any).interfaceTabId = undefined;

expect(() => msg.validate()).toThrow();

const msg2 = new ConnectInterfaceResponseMsg(0, "interface", true);
(msg2 as any).interfaceOrigin = undefined;

expect(() => msg2.validate()).toThrow();

const msg3 = new ConnectInterfaceResponseMsg(0, "interface", true);
(msg3 as any).allowConnection = undefined;

expect(() => msg3.validate()).toThrow();
});

test("valid RevokeConnectionMsg", () => {
const msg = new RevokeConnectionMsg("");

expect(msg.type()).toBe(MessageType.RevokeConnection);
expect(msg.route()).toBe(ROUTE);
expect(msg.validate()).toBeUndefined();
});

test("invalid RevokeConnectionMsg", () => {
const msg = new RevokeConnectionMsg("");

(msg as any).originToRevoke = undefined;

expect(() => msg.validate()).toThrow();
});
});
10 changes: 7 additions & 3 deletions apps/extension/src/background/approvals/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ROUTE } from "./constants";

import { validateProps } from "utils";

enum MessageType {
export enum MessageType {
SubmitApprovedTx = "submit-approved-tx",
RejectTx = "reject-tx",
SubmitApprovedSignature = "submit-approved-signature",
Expand Down Expand Up @@ -127,13 +127,17 @@ export class ConnectInterfaceResponseMsg extends Message<void> {
}

validate(): void {
if (!this.interfaceTabId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be a few instances of these falsy checks that typecheck even when they're slightly wrong. I'm wondering if it might be worth adding a linting rule in the future to prevent that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah either that or we can just use io-ts also for those checks

if (typeof this.interfaceTabId === "undefined") {
throw new Error("interfaceTabId not set");
}

if (!this.interfaceOrigin) {
throw new Error("interfaceOrigin not set");
}

if (typeof this.allowConnection === "undefined") {
throw new Error("allowConnection not set");
}
}

route(): string {
Expand All @@ -155,7 +159,7 @@ export class RevokeConnectionMsg extends Message<void> {
}

validate(): void {
if (!this.originToRevoke) {
if (typeof this.originToRevoke === "undefined") {
throw new Error("originToRevoke not set");
}
}
Expand Down
Loading
Loading