|
| 1 | +import { JsonRpcEngine } from '@metamask/json-rpc-engine'; |
| 2 | +import type { |
| 3 | + ListEntropySourcesParams, |
| 4 | + ListEntropySourcesResult, |
| 5 | +} from '@metamask/snaps-sdk'; |
| 6 | +import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; |
| 7 | + |
| 8 | +import { listEntropySourcesHandler } from './listEntropySources'; |
| 9 | + |
| 10 | +describe('snap_listEntropySources', () => { |
| 11 | + describe('listEntropySourcesHandler', () => { |
| 12 | + it('has the expected shape', () => { |
| 13 | + expect(listEntropySourcesHandler).toMatchObject({ |
| 14 | + methodNames: ['snap_listEntropySources'], |
| 15 | + implementation: expect.any(Function), |
| 16 | + hookNames: { |
| 17 | + hasPermission: true, |
| 18 | + getEntropySources: true, |
| 19 | + }, |
| 20 | + }); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('implementation', () => { |
| 25 | + it('returns the result from the `getEntropySources` hook', async () => { |
| 26 | + const { implementation } = listEntropySourcesHandler; |
| 27 | + |
| 28 | + const getEntropySources = jest.fn().mockReturnValue([ |
| 29 | + { |
| 30 | + name: 'Secret recovery phrase 1', |
| 31 | + id: 'foo', |
| 32 | + type: 'mnemonic', |
| 33 | + primary: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'Secret recovery phrase 2', |
| 37 | + id: 'bar', |
| 38 | + type: 'mnemonic', |
| 39 | + primary: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: 'Primary secret recovery phrase', |
| 43 | + id: 'baz', |
| 44 | + type: 'mnemonic', |
| 45 | + primary: true, |
| 46 | + }, |
| 47 | + ]); |
| 48 | + |
| 49 | + const hooks = { |
| 50 | + hasPermission: jest.fn().mockReturnValue(true), |
| 51 | + getEntropySources, |
| 52 | + }; |
| 53 | + |
| 54 | + const engine = new JsonRpcEngine(); |
| 55 | + |
| 56 | + engine.push((request, response, next, end) => { |
| 57 | + const result = implementation( |
| 58 | + request as JsonRpcRequest<ListEntropySourcesParams>, |
| 59 | + response as PendingJsonRpcResponse<ListEntropySourcesResult>, |
| 60 | + next, |
| 61 | + end, |
| 62 | + hooks, |
| 63 | + ); |
| 64 | + |
| 65 | + result?.catch(end); |
| 66 | + }); |
| 67 | + |
| 68 | + const response = await engine.handle({ |
| 69 | + jsonrpc: '2.0', |
| 70 | + id: 1, |
| 71 | + method: 'snap_listEntropySources', |
| 72 | + }); |
| 73 | + |
| 74 | + expect(response).toStrictEqual({ |
| 75 | + jsonrpc: '2.0', |
| 76 | + id: 1, |
| 77 | + result: [ |
| 78 | + { |
| 79 | + name: 'Secret recovery phrase 1', |
| 80 | + id: 'foo', |
| 81 | + type: 'mnemonic', |
| 82 | + primary: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'Secret recovery phrase 2', |
| 86 | + id: 'bar', |
| 87 | + type: 'mnemonic', |
| 88 | + primary: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: 'Primary secret recovery phrase', |
| 92 | + id: 'baz', |
| 93 | + type: 'mnemonic', |
| 94 | + primary: true, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('returns an unauthorized error if the requesting origin does not have the required permission', async () => { |
| 101 | + const { implementation } = listEntropySourcesHandler; |
| 102 | + |
| 103 | + const hooks = { |
| 104 | + hasPermission: jest.fn().mockReturnValue(false), |
| 105 | + getEntropySources: jest.fn(), |
| 106 | + }; |
| 107 | + |
| 108 | + const engine = new JsonRpcEngine(); |
| 109 | + |
| 110 | + engine.push((request, response, next, end) => { |
| 111 | + const result = implementation( |
| 112 | + request as JsonRpcRequest<ListEntropySourcesParams>, |
| 113 | + response as PendingJsonRpcResponse<ListEntropySourcesResult>, |
| 114 | + next, |
| 115 | + end, |
| 116 | + hooks, |
| 117 | + ); |
| 118 | + |
| 119 | + result?.catch(end); |
| 120 | + }); |
| 121 | + |
| 122 | + const response = await engine.handle({ |
| 123 | + jsonrpc: '2.0', |
| 124 | + id: 1, |
| 125 | + method: 'snap_listEntropySources', |
| 126 | + }); |
| 127 | + |
| 128 | + expect(response).toStrictEqual({ |
| 129 | + jsonrpc: '2.0', |
| 130 | + id: 1, |
| 131 | + error: { |
| 132 | + code: 4100, |
| 133 | + message: |
| 134 | + 'The requested account and/or method has not been authorized by the user.', |
| 135 | + stack: expect.any(String), |
| 136 | + }, |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments