diff --git a/packages/snap/tests/address-book-test.ts b/packages/snap/tests/address-book-test.ts index 7f8303c..7319235 100644 --- a/packages/snap/tests/address-book-test.ts +++ b/packages/snap/tests/address-book-test.ts @@ -3,6 +3,47 @@ import { AddressState } from "../src/state"; import { Addresses, Address } from "../src//types/address"; class PassingAddressStateTests { + //Initialize Sample Address + address1: Address = { + name: "User1", + address: "0x123456", + chain_id: "1", + }; + + address2: Address = { + name: "User2", + address: "0xabcdef", + chain_id: "2", + }; + + //Initialize Sample Address Book + SampleAddresses = new Addresses([this.address1, this.address2]); + + // Mock the Metamask snap object and its request method + snapMock = { + request: (params: any) => { + if ( + params.method === "snap_manageState" && + params.params.operation === "get" + ) { + return { + addresses: this.SampleAddresses.string(), + }; + } + + if ( + params.method === "snap_manageState" && + params.params.operation === "update" + ) { + // Modify the SampleAddresses and return true + this.SampleAddresses.addresses = JSON.parse( + params.params.newState.addresses + ); + return true; + } + }, + }; + //getAddressBook function should return current state of address book async getAddressBookPassTest(t: any) { //get current state of AddressBook @@ -97,6 +138,24 @@ class PassingAddressStateTests { } class FailingAddressStateTests { + //Mock snap object for 'data?.addresses == undefined' + snapMock1 = { + request: (params: any) => { + return { + addresses: undefined, + }; + }, + }; + + //Mock snap object for 'typeof data?.addresses !== "string"' + snapMock2 = { + request: (params: any) => { + return { + addresses: 1, + }; + }, + }; + //getAddressBook function should throw error async getAddressBookFailTest(t: any) { await t.throwsAsync( @@ -170,48 +229,7 @@ const passing_tests = new PassingAddressStateTests(); const failing_tests = new FailingAddressStateTests(); test.serial("AddressState Passing Tests", async (t) => { - //Initialize Sample Address - const address1: Address = { - name: "User1", - address: "0x123456", - chain_id: "1", - }; - - const address2: Address = { - name: "User2", - address: "0xabcdef", - chain_id: "2", - }; - - //Initialize Sample Address Book - const SampleAddresses = new Addresses([address1, address2]); - - // Mock the Metamask snap object and its request method - const snapMock = { - request: (params: any) => { - if ( - params.method === "snap_manageState" && - params.params.operation === "get" - ) { - return { - addresses: SampleAddresses.string(), - }; - } - - if ( - params.method === "snap_manageState" && - params.params.operation === "update" - ) { - // Modify the SampleAddresses and return true - SampleAddresses.addresses = JSON.parse( - params.params.newState.addresses - ); - return true; - } - }, - }; - - (globalThis as any).snap = snapMock; + (globalThis as any).snap = passing_tests.snapMock; await passing_tests.getAddressBookPassTest(t); await passing_tests.getAddressPassTest(t); @@ -221,17 +239,7 @@ test.serial("AddressState Passing Tests", async (t) => { }); test.serial("AddressState Failing Tests", async (t) => { - // test data?.addresses == undefined - //Mock snap object - const snapMock1 = { - request: (params: any) => { - return { - addresses: undefined, - }; - }, - }; - - (globalThis as any).snap = snapMock1; + (globalThis as any).snap = failing_tests.snapMock1; await failing_tests.getAddressBookFailTest(t); await failing_tests.getAddressFailTest(t); @@ -239,17 +247,7 @@ test.serial("AddressState Failing Tests", async (t) => { await failing_tests.removeAddressFailTest(t); await failing_tests.addAddressesFailTest(t); - // test typeof data?.addresses !== "string" - //Mock snap object - const snapMock2 = { - request: (params: any) => { - return { - addresses: 1, - }; - }, - }; - - (globalThis as any).snap = snapMock2; + (globalThis as any).snap = failing_tests.snapMock2; await failing_tests.getAddressBookFailTest(t); await failing_tests.getAddressFailTest(t); diff --git a/packages/snap/tests/rpc-test.ts b/packages/snap/tests/rpc-test.ts index 905162f..d1ca70d 100644 --- a/packages/snap/tests/rpc-test.ts +++ b/packages/snap/tests/rpc-test.ts @@ -2,7 +2,8 @@ import test from "ava"; import { onRpcRequest } from "../src/index"; import { Address, Addresses } from "../src/types/address"; import { Json, JsonRpcRequest } from "@metamask/utils"; -import { boolean } from "superstruct"; + +const origin = "test-origin"; class PassingOnRpcRequestTests { //Initialize Sample Address @@ -47,14 +48,8 @@ class PassingOnRpcRequestTests { }, }; - constructor() { - test.before(() => { - (globalThis as any).snap = this.snapMock; - }); - } - //onRpcRequet function should handle the "addAddress" case correctly - async caseAddAddress(t: any) { + async casePassAddAddress(t: any) { //Initialize new address const new_address: Address = { name: "User3", @@ -62,8 +57,6 @@ class PassingOnRpcRequestTests { chain_id: "3", }; - const origin = "test-origin"; - // Define the JSON-RPC request variable{} let request: JsonRpcRequest> = { method: "addAddress", @@ -81,9 +74,7 @@ class PassingOnRpcRequestTests { } //onRpcRequet function should handle the "deleteAddress" case correctly - async caseDeleteAddress(t: any) { - const origin = "test-origin"; - + async casePassDeleteAddress(t: any) { // Define the JSON-RPC request variable{} let request: JsonRpcRequest> = { method: "deleteAddress", @@ -101,9 +92,7 @@ class PassingOnRpcRequestTests { } //onRpcRequest function should handle the "getAddresses" case correctly - async caseGetAddresses(t: any) { - const origin = "test-origin"; - + async casePassGetAddresses(t: any) { // Define the JSON-RPC request variable{} let request: JsonRpcRequest> = { method: "getAddresses", @@ -130,10 +119,112 @@ class PassingOnRpcRequestTests { } } -const tests = new PassingOnRpcRequestTests(); +class FailingOnRpcRequestTests { + //Mock snap object for 'data?.addresses == undefined' + snapMock1 = { + request: (params: any) => { + return { + addresses: undefined, + }; + }, + }; + + //Mock snap object for 'typeof data?.addresses !== "string"' + snapMock2 = { + request: (params: any) => { + return { + addresses: 1, + }; + }, + }; + + //onRpcRequet function should throw error on "addAddress" case + async caseFailAddAddress(t: any) { + //Initialize new address + const new_address: Address = { + name: "User3", + address: "0x456789", + chain_id: "3", + }; + + // Define the JSON-RPC request variable{} + let request: JsonRpcRequest> = { + method: "addAddress", + jsonrpc: "2.0", + id: null, + params: { + address: JSON.stringify(new_address), + }, + }; + + await t.throwsAsync( + async () => { + await onRpcRequest({ origin, request }); + }, + { instanceOf: Error } + ); + } + + //onRpcRequet function should throw error on "deleteAddress" case + async caseFailDeleteAddress(t: any) { + // Define the JSON-RPC request variable{} + let request: JsonRpcRequest> = { + method: "deleteAddress", + jsonrpc: "2.0", + id: null, + params: { + chain_id: "3", + }, + }; + + await t.throwsAsync( + async () => { + await onRpcRequest({ origin, request }); + }, + { instanceOf: Error } + ); + } + + //onRpcRequest function should throw error on "getAddresses" case + async caseFailGetAddresses(t: any) { + // Define the JSON-RPC request variable{} + let request: JsonRpcRequest> = { + method: "getAddresses", + jsonrpc: "2.0", + id: null, + params: [], + }; + + await t.throwsAsync( + async () => { + await onRpcRequest({ origin, request }); + }, + { instanceOf: Error } + ); + } +} + +const passing_tests = new PassingOnRpcRequestTests(); +const failing_tests = new FailingOnRpcRequestTests(); + +test.serial("onRpcRequest Passing Tests", async (t) => { + (globalThis as any).snap = passing_tests.snapMock; + + await passing_tests.casePassGetAddresses(t); + await passing_tests.casePassAddAddress(t); + await passing_tests.casePassDeleteAddress(t); +}); + +test.serial("onRpcRequest Failing Tests", async (t) => { + (globalThis as any).snap = failing_tests.snapMock1; + + await failing_tests.caseFailGetAddresses(t); + await failing_tests.caseFailAddAddress(t); + await failing_tests.caseFailDeleteAddress(t); + + (globalThis as any).snap = failing_tests.snapMock2; -test.serial("onRpcRequest Tests", async (t) => { - await tests.caseGetAddresses(t); - await tests.caseAddAddress(t); - await tests.caseDeleteAddress(t); + await failing_tests.caseFailGetAddresses(t); + await failing_tests.caseFailAddAddress(t); + await failing_tests.caseFailDeleteAddress(t); });