Skip to content

Commit

Permalink
rpc failing tests and code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaysharmajs committed Jun 15, 2023
1 parent 8607882 commit 235474b
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 85 deletions.
126 changes: 62 additions & 64 deletions packages/snap/tests/address-book-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -221,35 +239,15 @@ 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);
await failing_tests.addAddressFailTest(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);
Expand Down
133 changes: 112 additions & 21 deletions packages/snap/tests/rpc-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,23 +48,15 @@ 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",
address: "0x456789",
chain_id: "3",
};

const origin = "test-origin";

// Define the JSON-RPC request variable{}
let request: JsonRpcRequest<Json[] | Record<string, Json>> = {
method: "addAddress",
Expand All @@ -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<Json[] | Record<string, Json>> = {
method: "deleteAddress",
Expand All @@ -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<Json[] | Record<string, Json>> = {
method: "getAddresses",
Expand All @@ -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<Json[] | Record<string, Json>> = {
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<Json[] | Record<string, Json>> = {
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<Json[] | Record<string, Json>> = {
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);
});

0 comments on commit 235474b

Please sign in to comment.