-
Notifications
You must be signed in to change notification settings - Fork 121
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
128
apps/extension/src/background/approvals/messages.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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