-
Notifications
You must be signed in to change notification settings - Fork 542
/
invokeSnap.test.ts
105 lines (96 loc) · 3.07 KB
/
invokeSnap.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { PermissionType } from '@metamask/permission-controller';
import {
MOCK_SNAP_ID,
MOCK_ORIGIN,
getTruncatedSnap,
} from '@metamask/snaps-utils/test-utils';
import { invokeSnapBuilder, getInvokeSnapImplementation } from './invokeSnap';
describe('builder', () => {
it('has the expected shape', () => {
expect(invokeSnapBuilder).toMatchObject({
targetKey: 'wallet_snap_*',
specificationBuilder: expect.any(Function),
methodHooks: {
getSnap: true,
handleSnapRpcRequest: true,
},
});
});
it('builder outputs expected specification', () => {
expect(
invokeSnapBuilder.specificationBuilder({
methodHooks: {
getSnap: jest.fn(),
handleSnapRpcRequest: jest.fn(),
},
}),
).toMatchObject({
permissionType: PermissionType.RestrictedMethod,
targetKey: 'wallet_snap_*',
allowedCaveats: null,
methodImplementation: expect.any(Function),
});
});
});
describe('implementation', () => {
const getMockHooks = () =>
({
getSnap: jest.fn(),
handleSnapRpcRequest: jest.fn(),
} as any);
it('calls handleSnapRpcRequest', async () => {
const hooks = getMockHooks();
hooks.getSnap.mockImplementation(getTruncatedSnap);
const implementation = getInvokeSnapImplementation(hooks);
await implementation({
context: { origin: MOCK_ORIGIN },
method: `wallet_snap_${MOCK_SNAP_ID}`,
params: { method: 'foo', params: {} },
});
expect(hooks.getSnap).toHaveBeenCalledTimes(1);
expect(hooks.getSnap).toHaveBeenCalledWith(MOCK_SNAP_ID);
expect(hooks.handleSnapRpcRequest).toHaveBeenCalledWith({
handler: 'onRpcRequest',
origin: MOCK_ORIGIN,
request: {
jsonrpc: '2.0',
id: expect.any(String),
method: 'foo',
params: {},
},
snapId: MOCK_SNAP_ID,
});
});
it('throws if snap is not installed', async () => {
const hooks = getMockHooks();
const implementation = getInvokeSnapImplementation(hooks);
await expect(
implementation({
context: { origin: MOCK_ORIGIN },
method: `wallet_snap_${MOCK_SNAP_ID}`,
params: { method: 'foo', params: {} },
}),
).rejects.toThrow(
`The snap "${MOCK_SNAP_ID}" is not installed. This is a bug, please report it.`,
);
expect(hooks.getSnap).toHaveBeenCalledTimes(1);
expect(hooks.getSnap).toHaveBeenCalledWith(MOCK_SNAP_ID);
expect(hooks.handleSnapRpcRequest).not.toHaveBeenCalled();
});
it('throws if request is not valid', async () => {
const hooks = getMockHooks();
hooks.getSnap.mockImplementation(getTruncatedSnap);
const implementation = getInvokeSnapImplementation(hooks);
await expect(
implementation({
context: { origin: MOCK_ORIGIN },
method: `wallet_snap_${MOCK_SNAP_ID}`,
params: {},
}),
).rejects.toThrow(
'Must specify a valid JSON-RPC request object as single parameter.',
);
expect(hooks.getSnap).toHaveBeenCalledTimes(0);
expect(hooks.handleSnapRpcRequest).not.toHaveBeenCalled();
});
});