1
1
import type { SnapId } from '@metamask/snaps-sdk' ;
2
- import { form , input } from '@metamask/snaps-sdk' ;
2
+ import { form , input , panel , text } from '@metamask/snaps-sdk' ;
3
3
import { MOCK_SNAP_ID } from '@metamask/snaps-utils/test-utils' ;
4
4
5
5
import {
@@ -10,7 +10,7 @@ import { SnapInterfaceController } from './SnapInterfaceController';
10
10
11
11
describe ( 'SnapInterfaceController' , ( ) => {
12
12
describe ( 'createInterface' , ( ) => {
13
- it ( 'can create a new interface' , ( ) => {
13
+ it ( 'can create a new interface' , async ( ) => {
14
14
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
15
15
const controllerMessenger =
16
16
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -20,12 +20,15 @@ describe('SnapInterfaceController', () => {
20
20
messenger : controllerMessenger ,
21
21
} ) ;
22
22
23
- const components = form ( {
24
- name : 'foo' ,
25
- children : [ input ( { name : 'bar' } ) ] ,
26
- } ) ;
23
+ const components = panel ( [
24
+ text ( '[foo](https://foo.bar)' ) ,
25
+ form ( {
26
+ name : 'foo' ,
27
+ children : [ input ( { name : 'bar' } ) ] ,
28
+ } ) ,
29
+ ] ) ;
27
30
28
- const id = rootMessenger . call (
31
+ const id = await rootMessenger . call (
29
32
'SnapInterfaceController:createInterface' ,
30
33
MOCK_SNAP_ID ,
31
34
components ,
@@ -37,13 +40,74 @@ describe('SnapInterfaceController', () => {
37
40
id ,
38
41
) ;
39
42
43
+ expect ( rootMessenger . call ) . toHaveBeenNthCalledWith (
44
+ 2 ,
45
+ 'PhishingController:maybeUpdateState' ,
46
+ ) ;
47
+
48
+ expect ( rootMessenger . call ) . toHaveBeenNthCalledWith (
49
+ 3 ,
50
+ 'PhishingController:testOrigin' ,
51
+ 'foo.bar' ,
52
+ ) ;
53
+
40
54
expect ( content ) . toStrictEqual ( components ) ;
41
55
expect ( state ) . toStrictEqual ( { foo : { bar : null } } ) ;
42
56
} ) ;
57
+
58
+ it ( 'throws if a link is on the phishing list' , async ( ) => {
59
+ const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
60
+ const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger (
61
+ rootMessenger ,
62
+ false ,
63
+ ) ;
64
+
65
+ rootMessenger . registerActionHandler (
66
+ 'PhishingController:maybeUpdateState' ,
67
+ jest . fn ( ) ,
68
+ ) ;
69
+
70
+ rootMessenger . registerActionHandler (
71
+ 'PhishingController:testOrigin' ,
72
+ ( ) => ( { result : true , type : 'all' } ) ,
73
+ ) ;
74
+
75
+ /* eslint-disable-next-line no-new */
76
+ new SnapInterfaceController ( {
77
+ messenger : controllerMessenger ,
78
+ } ) ;
79
+
80
+ const components = panel ( [
81
+ text ( '[foo](https://foo.bar)' ) ,
82
+ form ( {
83
+ name : 'foo' ,
84
+ children : [ input ( { name : 'bar' } ) ] ,
85
+ } ) ,
86
+ ] ) ;
87
+
88
+ await expect (
89
+ rootMessenger . call (
90
+ 'SnapInterfaceController:createInterface' ,
91
+ MOCK_SNAP_ID ,
92
+ components ,
93
+ ) ,
94
+ ) . rejects . toThrow ( 'Invalid URL: The specified URL is not allowed.' ) ;
95
+
96
+ expect ( rootMessenger . call ) . toHaveBeenNthCalledWith (
97
+ 2 ,
98
+ 'PhishingController:maybeUpdateState' ,
99
+ ) ;
100
+
101
+ expect ( rootMessenger . call ) . toHaveBeenNthCalledWith (
102
+ 3 ,
103
+ 'PhishingController:testOrigin' ,
104
+ 'foo.bar' ,
105
+ ) ;
106
+ } ) ;
43
107
} ) ;
44
108
45
109
describe ( 'getInterface' , ( ) => {
46
- it ( 'gets the interface' , ( ) => {
110
+ it ( 'gets the interface' , async ( ) => {
47
111
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
48
112
const controllerMessenger =
49
113
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -58,7 +122,7 @@ describe('SnapInterfaceController', () => {
58
122
children : [ input ( { name : 'bar' } ) ] ,
59
123
} ) ;
60
124
61
- const id = rootMessenger . call (
125
+ const id = await rootMessenger . call (
62
126
'SnapInterfaceController:createInterface' ,
63
127
MOCK_SNAP_ID ,
64
128
components ,
@@ -72,7 +136,7 @@ describe('SnapInterfaceController', () => {
72
136
expect ( content ) . toStrictEqual ( components ) ;
73
137
} ) ;
74
138
75
- it ( 'throws if the snap requesting the interface is not the one that created it' , ( ) => {
139
+ it ( 'throws if the snap requesting the interface is not the one that created it' , async ( ) => {
76
140
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
77
141
const controllerMessenger =
78
142
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -87,7 +151,7 @@ describe('SnapInterfaceController', () => {
87
151
children : [ input ( { name : 'bar' } ) ] ,
88
152
} ) ;
89
153
90
- const id = rootMessenger . call (
154
+ const id = await rootMessenger . call (
91
155
'SnapInterfaceController:createInterface' ,
92
156
MOCK_SNAP_ID ,
93
157
components ,
@@ -123,7 +187,7 @@ describe('SnapInterfaceController', () => {
123
187
} ) ;
124
188
125
189
describe ( 'updateInterface' , ( ) => {
126
- it ( 'can update an interface' , ( ) => {
190
+ it ( 'can update an interface' , async ( ) => {
127
191
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
128
192
const controllerMessenger =
129
193
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -143,13 +207,13 @@ describe('SnapInterfaceController', () => {
143
207
children : [ input ( { name : 'baz' } ) ] ,
144
208
} ) ;
145
209
146
- const id = rootMessenger . call (
210
+ const id = await rootMessenger . call (
147
211
'SnapInterfaceController:createInterface' ,
148
212
MOCK_SNAP_ID ,
149
213
components ,
150
214
) ;
151
215
152
- rootMessenger . call (
216
+ await rootMessenger . call (
153
217
'SnapInterfaceController:updateInterface' ,
154
218
MOCK_SNAP_ID ,
155
219
id ,
@@ -166,7 +230,69 @@ describe('SnapInterfaceController', () => {
166
230
expect ( state ) . toStrictEqual ( { foo : { baz : null } } ) ;
167
231
} ) ;
168
232
169
- it ( 'throws if the interface does not exist' , ( ) => {
233
+ it ( 'throws if a link is on the phishing list' , async ( ) => {
234
+ const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
235
+ const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger (
236
+ rootMessenger ,
237
+ false ,
238
+ ) ;
239
+
240
+ rootMessenger . registerActionHandler (
241
+ 'PhishingController:maybeUpdateState' ,
242
+ jest . fn ( ) ,
243
+ ) ;
244
+
245
+ rootMessenger . registerActionHandler (
246
+ 'PhishingController:testOrigin' ,
247
+ ( ) => ( { result : true , type : 'all' } ) ,
248
+ ) ;
249
+
250
+ /* eslint-disable-next-line no-new */
251
+ new SnapInterfaceController ( {
252
+ messenger : controllerMessenger ,
253
+ } ) ;
254
+
255
+ const components = form ( {
256
+ name : 'foo' ,
257
+ children : [ input ( { name : 'bar' } ) ] ,
258
+ } ) ;
259
+
260
+ const newContent = panel ( [
261
+ text ( '[foo](https://foo.bar)' ) ,
262
+ form ( {
263
+ name : 'foo' ,
264
+ children : [ input ( { name : 'baz' } ) ] ,
265
+ } ) ,
266
+ ] ) ;
267
+
268
+ const id = await rootMessenger . call (
269
+ 'SnapInterfaceController:createInterface' ,
270
+ MOCK_SNAP_ID ,
271
+ components ,
272
+ ) ;
273
+
274
+ await expect (
275
+ rootMessenger . call (
276
+ 'SnapInterfaceController:updateInterface' ,
277
+ MOCK_SNAP_ID ,
278
+ id ,
279
+ newContent ,
280
+ ) ,
281
+ ) . rejects . toThrow ( 'Invalid URL: The specified URL is not allowed.' ) ;
282
+
283
+ expect ( rootMessenger . call ) . toHaveBeenNthCalledWith (
284
+ 4 ,
285
+ 'PhishingController:maybeUpdateState' ,
286
+ ) ;
287
+
288
+ expect ( rootMessenger . call ) . toHaveBeenNthCalledWith (
289
+ 5 ,
290
+ 'PhishingController:testOrigin' ,
291
+ 'foo.bar' ,
292
+ ) ;
293
+ } ) ;
294
+
295
+ it ( 'throws if the interface does not exist' , async ( ) => {
170
296
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
171
297
const controllerMessenger =
172
298
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -178,17 +304,17 @@ describe('SnapInterfaceController', () => {
178
304
179
305
const content = form ( { name : 'foo' , children : [ input ( { name : 'bar' } ) ] } ) ;
180
306
181
- expect ( ( ) =>
307
+ await expect (
182
308
rootMessenger . call (
183
309
'SnapInterfaceController:updateInterface' ,
184
310
MOCK_SNAP_ID ,
185
311
'foo' ,
186
312
content ,
187
313
) ,
188
- ) . toThrow ( "Interface with id 'foo' not found." ) ;
314
+ ) . rejects . toThrow ( "Interface with id 'foo' not found." ) ;
189
315
} ) ;
190
316
191
- it ( 'throws if the interface is updated by another snap' , ( ) => {
317
+ it ( 'throws if the interface is updated by another snap' , async ( ) => {
192
318
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
193
319
const controllerMessenger =
194
320
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -205,25 +331,25 @@ describe('SnapInterfaceController', () => {
205
331
children : [ input ( { name : 'baz' } ) ] ,
206
332
} ) ;
207
333
208
- const id = rootMessenger . call (
334
+ const id = await rootMessenger . call (
209
335
'SnapInterfaceController:createInterface' ,
210
336
MOCK_SNAP_ID ,
211
337
content ,
212
338
) ;
213
339
214
- expect ( ( ) =>
340
+ await expect (
215
341
rootMessenger . call (
216
342
'SnapInterfaceController:updateInterface' ,
217
343
'foo' as SnapId ,
218
344
id ,
219
345
newContent ,
220
346
) ,
221
- ) . toThrow ( 'Interface not created by foo.' ) ;
347
+ ) . rejects . toThrow ( 'Interface not created by foo.' ) ;
222
348
} ) ;
223
349
} ) ;
224
350
225
351
describe ( 'updateInterfaceState' , ( ) => {
226
- it ( 'updates the interface state' , ( ) => {
352
+ it ( 'updates the interface state' , async ( ) => {
227
353
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
228
354
const controllerMessenger =
229
355
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -237,7 +363,7 @@ describe('SnapInterfaceController', () => {
237
363
238
364
const newState = { foo : { bar : 'baz' } } ;
239
365
240
- const id = rootMessenger . call (
366
+ const id = await rootMessenger . call (
241
367
'SnapInterfaceController:createInterface' ,
242
368
MOCK_SNAP_ID ,
243
369
content ,
@@ -260,7 +386,7 @@ describe('SnapInterfaceController', () => {
260
386
} ) ;
261
387
262
388
describe ( 'deleteInterface' , ( ) => {
263
- it ( 'can delete an interface' , ( ) => {
389
+ it ( 'can delete an interface' , async ( ) => {
264
390
const rootMessenger = getRootSnapInterfaceControllerMessenger ( ) ;
265
391
const controllerMessenger =
266
392
getRestrictedSnapInterfaceControllerMessenger ( rootMessenger ) ;
@@ -272,7 +398,7 @@ describe('SnapInterfaceController', () => {
272
398
273
399
const content = form ( { name : 'foo' , children : [ input ( { name : 'bar' } ) ] } ) ;
274
400
275
- const id = rootMessenger . call (
401
+ const id = await rootMessenger . call (
276
402
'SnapInterfaceController:createInterface' ,
277
403
MOCK_SNAP_ID ,
278
404
content ,
0 commit comments