|
| 1 | +import { JsonRpcEngine } from '@metamask/json-rpc-engine'; |
| 2 | +import { errorCodes } from '@metamask/rpc-errors'; |
| 3 | +import type { ClearStateResult } from '@metamask/snaps-sdk'; |
| 4 | +import { MOCK_SNAP_ID } from '@metamask/snaps-utils/test-utils'; |
| 5 | +import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; |
| 6 | + |
| 7 | +import type { ClearStateParameters } from './clearState'; |
| 8 | +import { clearStateHandler } from './clearState'; |
| 9 | + |
| 10 | +describe('snap_clearState', () => { |
| 11 | + describe('clearStateHandler', () => { |
| 12 | + it('has the expected shape', () => { |
| 13 | + expect(clearStateHandler).toMatchObject({ |
| 14 | + methodNames: ['snap_clearState'], |
| 15 | + implementation: expect.any(Function), |
| 16 | + hookNames: { |
| 17 | + clearSnapState: true, |
| 18 | + hasPermission: true, |
| 19 | + }, |
| 20 | + }); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('implementation', () => { |
| 25 | + const createOriginMiddleware = |
| 26 | + (origin: string) => |
| 27 | + (request: any, _response: unknown, next: () => void, _end: unknown) => { |
| 28 | + request.origin = origin; |
| 29 | + next(); |
| 30 | + }; |
| 31 | + |
| 32 | + it('returns the result from the `clearSnapState` hook', async () => { |
| 33 | + const { implementation } = clearStateHandler; |
| 34 | + |
| 35 | + const clearSnapState = jest.fn().mockReturnValue(null); |
| 36 | + const hasPermission = jest.fn().mockReturnValue(true); |
| 37 | + |
| 38 | + const hooks = { |
| 39 | + clearSnapState, |
| 40 | + hasPermission, |
| 41 | + }; |
| 42 | + |
| 43 | + const engine = new JsonRpcEngine(); |
| 44 | + |
| 45 | + engine.push(createOriginMiddleware(MOCK_SNAP_ID)); |
| 46 | + engine.push((request, response, next, end) => { |
| 47 | + const result = implementation( |
| 48 | + request as JsonRpcRequest<ClearStateParameters>, |
| 49 | + response as PendingJsonRpcResponse<ClearStateResult>, |
| 50 | + next, |
| 51 | + end, |
| 52 | + hooks, |
| 53 | + ); |
| 54 | + |
| 55 | + result?.catch(end); |
| 56 | + }); |
| 57 | + |
| 58 | + const response = await engine.handle({ |
| 59 | + jsonrpc: '2.0', |
| 60 | + id: 1, |
| 61 | + method: 'snap_clearState', |
| 62 | + params: {}, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(clearSnapState).toHaveBeenCalledWith(MOCK_SNAP_ID, true); |
| 66 | + expect(response).toStrictEqual({ |
| 67 | + jsonrpc: '2.0', |
| 68 | + id: 1, |
| 69 | + result: null, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('clears unencrypted state if specified', async () => { |
| 74 | + const { implementation } = clearStateHandler; |
| 75 | + |
| 76 | + const clearSnapState = jest.fn().mockReturnValue(null); |
| 77 | + const hasPermission = jest.fn().mockReturnValue(true); |
| 78 | + |
| 79 | + const hooks = { |
| 80 | + clearSnapState, |
| 81 | + hasPermission, |
| 82 | + }; |
| 83 | + |
| 84 | + const engine = new JsonRpcEngine(); |
| 85 | + |
| 86 | + engine.push(createOriginMiddleware(MOCK_SNAP_ID)); |
| 87 | + engine.push((request, response, next, end) => { |
| 88 | + const result = implementation( |
| 89 | + request as JsonRpcRequest<ClearStateParameters>, |
| 90 | + response as PendingJsonRpcResponse<ClearStateResult>, |
| 91 | + next, |
| 92 | + end, |
| 93 | + hooks, |
| 94 | + ); |
| 95 | + |
| 96 | + result?.catch(end); |
| 97 | + }); |
| 98 | + |
| 99 | + const response = await engine.handle({ |
| 100 | + jsonrpc: '2.0', |
| 101 | + id: 1, |
| 102 | + method: 'snap_clearState', |
| 103 | + params: { |
| 104 | + encrypted: false, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(clearSnapState).toHaveBeenCalledWith(MOCK_SNAP_ID, false); |
| 109 | + expect(response).toStrictEqual({ |
| 110 | + jsonrpc: '2.0', |
| 111 | + id: 1, |
| 112 | + result: null, |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('throws if the requesting origin does not have the required permission', async () => { |
| 117 | + const { implementation } = clearStateHandler; |
| 118 | + |
| 119 | + const clearSnapState = jest.fn(); |
| 120 | + const hasPermission = jest.fn().mockReturnValue(false); |
| 121 | + |
| 122 | + const hooks = { |
| 123 | + clearSnapState, |
| 124 | + hasPermission, |
| 125 | + }; |
| 126 | + |
| 127 | + const engine = new JsonRpcEngine(); |
| 128 | + |
| 129 | + engine.push((request, response, next, end) => { |
| 130 | + const result = implementation( |
| 131 | + request as JsonRpcRequest<ClearStateParameters>, |
| 132 | + response as PendingJsonRpcResponse<ClearStateResult>, |
| 133 | + next, |
| 134 | + end, |
| 135 | + hooks, |
| 136 | + ); |
| 137 | + |
| 138 | + result?.catch(end); |
| 139 | + }); |
| 140 | + |
| 141 | + const response = await engine.handle({ |
| 142 | + jsonrpc: '2.0', |
| 143 | + id: 1, |
| 144 | + method: 'snap_clearState', |
| 145 | + params: {}, |
| 146 | + }); |
| 147 | + |
| 148 | + expect(clearSnapState).not.toHaveBeenCalled(); |
| 149 | + expect(response).toStrictEqual({ |
| 150 | + jsonrpc: '2.0', |
| 151 | + id: 1, |
| 152 | + error: { |
| 153 | + code: errorCodes.provider.unauthorized, |
| 154 | + message: |
| 155 | + 'The requested account and/or method has not been authorized by the user.', |
| 156 | + stack: expect.any(String), |
| 157 | + }, |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('throws if the parameters are invalid', async () => { |
| 162 | + const { implementation } = clearStateHandler; |
| 163 | + |
| 164 | + const clearSnapState = jest.fn(); |
| 165 | + const hasPermission = jest.fn().mockReturnValue(true); |
| 166 | + |
| 167 | + const hooks = { |
| 168 | + clearSnapState, |
| 169 | + hasPermission, |
| 170 | + }; |
| 171 | + |
| 172 | + const engine = new JsonRpcEngine(); |
| 173 | + |
| 174 | + engine.push((request, response, next, end) => { |
| 175 | + const result = implementation( |
| 176 | + request as JsonRpcRequest<ClearStateParameters>, |
| 177 | + response as PendingJsonRpcResponse<ClearStateResult>, |
| 178 | + next, |
| 179 | + end, |
| 180 | + hooks, |
| 181 | + ); |
| 182 | + |
| 183 | + result?.catch(end); |
| 184 | + }); |
| 185 | + |
| 186 | + const response = await engine.handle({ |
| 187 | + jsonrpc: '2.0', |
| 188 | + id: 1, |
| 189 | + method: 'snap_clearState', |
| 190 | + params: { |
| 191 | + encrypted: 'foo', |
| 192 | + }, |
| 193 | + }); |
| 194 | + |
| 195 | + expect(response).toStrictEqual({ |
| 196 | + jsonrpc: '2.0', |
| 197 | + id: 1, |
| 198 | + error: { |
| 199 | + code: errorCodes.rpc.invalidParams, |
| 200 | + message: |
| 201 | + 'Invalid params: At path: encrypted -- Expected a value of type `boolean`, but received: `"foo"`.', |
| 202 | + stack: expect.any(String), |
| 203 | + }, |
| 204 | + }); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments