Skip to content

Commit

Permalink
refactor: Replace type casts with type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed Apr 25, 2024
1 parent ee2ad01 commit 834d764
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 50 deletions.
47 changes: 29 additions & 18 deletions packages/permission-controller/src/PermissionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import type {
import { ControllerMessenger } from '@metamask/base-controller';
import { isPlainObject } from '@metamask/controller-utils';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type {
Json,
JsonRpcFailure,
JsonRpcRequest,
JsonRpcSuccess,
import type { Json, JsonRpcRequest } from '@metamask/utils';
import {
assertIsJsonRpcFailure,
assertIsJsonRpcSuccess,
hasProperty,
} from '@metamask/utils';
import { hasProperty } from '@metamask/utils';
import assert from 'assert';

import type {
Expand Down Expand Up @@ -5331,11 +5330,12 @@ describe('PermissionController', () => {
const engine = new JsonRpcEngine();
engine.push(controller.createPermissionMiddleware({ origin }));

const response = (await engine.handle({
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: PermissionNames.wallet_getSecretArray,
})) as JsonRpcSuccess<string[]>;
});
assertIsJsonRpcSuccess(response);

expect(response.result).toStrictEqual(['a', 'b', 'c']);
});
Expand All @@ -5356,11 +5356,12 @@ describe('PermissionController', () => {
const engine = new JsonRpcEngine();
engine.push(controller.createPermissionMiddleware({ origin }));

const response = (await engine.handle({
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: PermissionNames.wallet_getSecretArray,
})) as JsonRpcSuccess<string[]>;
});
assertIsJsonRpcSuccess(response);

expect(response.result).toStrictEqual(['b']);
});
Expand All @@ -5384,11 +5385,12 @@ describe('PermissionController', () => {
const engine = new JsonRpcEngine();
engine.push(controller.createPermissionMiddleware({ origin }));

const response = (await engine.handle({
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: PermissionNames.wallet_getSecretArray,
})) as JsonRpcSuccess<string[]>;
});
assertIsJsonRpcSuccess(response);

expect(response.result).toStrictEqual(['c', 'a']);
});
Expand All @@ -5404,11 +5406,12 @@ describe('PermissionController', () => {
end();
});

const response = (await engine.handle({
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'wallet_unrestrictedMethod',
})) as JsonRpcSuccess<string[]>;
});
assertIsJsonRpcSuccess(response);

expect(response.result).toBe('success');
});
Expand Down Expand Up @@ -5451,8 +5454,11 @@ describe('PermissionController', () => {
'Unauthorized to perform action. Try requesting the required permission(s) first. For more information, see: https://docs.metamask.io/guide/rpc-api.html#permissions',
});

const { error } = (await engine.handle(request)) as JsonRpcFailure;
expect(error).toMatchObject(expect.objectContaining(expectedError));
const response = await engine.handle(request);
assertIsJsonRpcFailure(response);
expect(response.error).toMatchObject(
expect.objectContaining(expectedError),
);
});

it('returns an error if the method does not exist', async () => {
Expand All @@ -5470,7 +5476,9 @@ describe('PermissionController', () => {

const expectedError = errors.methodNotFound('wallet_foo', { origin });

const { error } = (await engine.handle(request)) as JsonRpcFailure;
const response = await engine.handle(request);
assertIsJsonRpcFailure(response);
const { error } = response;

expect(error.message).toStrictEqual(expectedError.message);
// @ts-expect-error We do expect this property to exist.
Expand Down Expand Up @@ -5517,7 +5525,10 @@ describe('PermissionController', () => {
{ request: { ...request } },
);

const { error } = (await engine.handle(request)) as JsonRpcFailure;
const response = await engine.handle(request);
assertIsJsonRpcFailure(response);
const { error } = response;

expect(error.message).toStrictEqual(expectedError.message);
// @ts-expect-error We do expect this property to exist.
expect(error.data?.cause).toBeNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type {
Json,
JsonRpcRequest,
JsonRpcSuccess,
PendingJsonRpcResponse,
} from '@metamask/utils';
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
import { assertIsJsonRpcSuccess } from '@metamask/utils';

import type { PermissionConstraint } from '../Permission';
import { getPermissionsHandler } from './getPermissions';
Expand Down Expand Up @@ -34,11 +30,8 @@ describe('getPermissions RPC method', () => {
id: 1,
method: 'arbitraryName',
});
expect((response as JsonRpcSuccess<Json>).result).toStrictEqual([
'a',
'b',
'c',
]);
assertIsJsonRpcSuccess(response);
expect(response.result).toStrictEqual(['a', 'b', 'c']);
expect(mockGetPermissionsForOrigin).toHaveBeenCalledTimes(1);
});

Expand Down Expand Up @@ -66,7 +59,8 @@ describe('getPermissions RPC method', () => {
id: 1,
method: 'arbitraryName',
});
expect((response as JsonRpcSuccess<Json>).result).toStrictEqual([]);
assertIsJsonRpcSuccess(response);
expect(response.result).toStrictEqual([]);
expect(mockGetPermissionsForOrigin).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import {
createAsyncMiddleware,
} from '@metamask/json-rpc-engine';
import { rpcErrors, serializeError } from '@metamask/rpc-errors';
import type { JsonRpcFailure, JsonRpcSuccess } from '@metamask/utils';
import { hasProperty } from '@metamask/utils';
import {
assertIsJsonRpcFailure,
assertIsJsonRpcSuccess,
hasProperty,
} from '@metamask/utils';
import type {
PermissionConstraint,
RequestedPermissions,
Expand Down Expand Up @@ -35,12 +38,13 @@ describe('requestPermissions RPC method', () => {
}),
);

const response = (await engine.handle({
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'arbitraryName',
params: [{}],
})) as JsonRpcSuccess<string[]>;
});
assertIsJsonRpcSuccess(response);

expect(response.result).toStrictEqual(['a', 'b', 'c']);
expect(mockRequestPermissionsForOrigin).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -68,12 +72,13 @@ describe('requestPermissions RPC method', () => {
),
);

const response = (await engine.handle({
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'arbitraryName',
params: [{}],
})) as JsonRpcFailure;
});
assertIsJsonRpcFailure(response);

expect(hasProperty(response, 'result')).toBe(false);
delete response.error.stack;
Expand Down Expand Up @@ -102,7 +107,7 @@ describe('requestPermissions RPC method', () => {
);

for (const invalidParams of ['foo', ['bar']]) {
const req = {
const request = {
jsonrpc: '2.0',
id: 1,
method: 'arbitraryName',
Expand All @@ -111,13 +116,14 @@ describe('requestPermissions RPC method', () => {

const expectedError = rpcErrors
.invalidParams({
data: { request: { ...req } },
data: { request: { ...request } },
})
.serialize();
delete expectedError.stack;

// @ts-expect-error Intentional destructive testing
const response = (await engine.handle(req)) as JsonRpcFailure;
const response = await engine.handle(request);
assertIsJsonRpcFailure(response);
delete response.error.stack;
expect(response.error).toStrictEqual(expectedError);
expect(mockRequestPermissionsForOrigin).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { rpcErrors } from '@metamask/rpc-errors';
import type {
Json,
JsonRpcFailure,
JsonRpcRequest,
JsonRpcSuccess,
import type { Json, JsonRpcRequest } from '@metamask/utils';
import {
assertIsJsonRpcFailure,
assertIsJsonRpcSuccess,
} from '@metamask/utils';

import type { RevokePermissionArgs } from './revokePermissions';
Expand Down Expand Up @@ -35,7 +34,7 @@ describe('revokePermissions RPC method', () => {
}),
);

const response = (await engine.handle({
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'wallet_revokePermissions',
Expand All @@ -44,7 +43,8 @@ describe('revokePermissions RPC method', () => {
snap_dialog: {},
},
],
})) as JsonRpcSuccess<null>;
});
assertIsJsonRpcSuccess(response);

expect(response.result).toBeNull();
expect(mockRevokePermissionsForOrigin).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -79,7 +79,8 @@ describe('revokePermissions RPC method', () => {
.serialize();
delete expectedError.stack;

const response = (await engine.handle(req)) as JsonRpcFailure;
const response = await engine.handle(req);
assertIsJsonRpcFailure(response);
delete response.error.stack;
expect(response.error).toStrictEqual(expectedError);
expect(mockRevokePermissionsForOrigin).not.toHaveBeenCalled();
Expand Down Expand Up @@ -111,7 +112,8 @@ describe('revokePermissions RPC method', () => {
.serialize();
delete expectedError.stack;

const response = (await engine.handle(req)) as JsonRpcFailure;
const response = await engine.handle(req);
assertIsJsonRpcFailure(response);
delete response.error.stack;
expect(response.error).toStrictEqual(expectedError);
expect(mockRevokePermissionsForOrigin).not.toHaveBeenCalled();
Expand Down Expand Up @@ -142,7 +144,8 @@ describe('revokePermissions RPC method', () => {
.serialize();
delete expectedError.stack;

const response = (await engine.handle(req)) as JsonRpcFailure;
const response = await engine.handle(req);
assertIsJsonRpcFailure(response);
delete response.error.stack;
expect(response.error).toStrictEqual(expectedError);
expect(mockRevokePermissionsForOrigin).not.toHaveBeenCalled();
Expand Down Expand Up @@ -174,7 +177,8 @@ describe('revokePermissions RPC method', () => {
.serialize();
delete expectedError.stack;

const response = (await engine.handle(req)) as JsonRpcFailure;
const response = await engine.handle(req);
assertIsJsonRpcFailure(response);
delete response.error.stack;
expect(response.error).toStrictEqual(expectedError);
expect(mockRevokePermissionsForOrigin).not.toHaveBeenCalled();
Expand Down

0 comments on commit 834d764

Please sign in to comment.