Skip to content

Commit 99d42da

Browse files
committed
Add test for updating a Snap
1 parent a757d23 commit 99d42da

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

packages/snaps-controllers/src/snaps/SnapController.test.tsx

+133
Original file line numberDiff line numberDiff line change
@@ -5411,6 +5411,139 @@ describe('SnapController', () => {
54115411
snapController.destroy();
54125412
});
54135413

5414+
it('grants the `endowment:caip25` permission when updating a Snap with `endowment:ethereum-provider` if the `useCaip25Permission` feature flag is enabled', async () => {
5415+
const newVersion = '1.0.2';
5416+
const newVersionRange = '>=1.0.1';
5417+
5418+
const rootMessenger = getControllerMessenger();
5419+
const messenger = getSnapControllerMessenger(rootMessenger);
5420+
5421+
rootMessenger.registerActionHandler(
5422+
'SelectedNetworkController:getNetworkClientIdForDomain',
5423+
() => 'mainnet',
5424+
);
5425+
5426+
rootMessenger.registerActionHandler(
5427+
'NetworkController:getNetworkClientById',
5428+
() => ({
5429+
configuration: {
5430+
chainId: '0x1',
5431+
},
5432+
}),
5433+
);
5434+
5435+
const { manifest: originalManifest } =
5436+
await getMockSnapFilesWithUpdatedChecksum({
5437+
manifest: getSnapManifest({
5438+
initialPermissions: {
5439+
'endowment:page-home': {},
5440+
},
5441+
}),
5442+
});
5443+
5444+
const { manifest: updatedManifest } =
5445+
await getMockSnapFilesWithUpdatedChecksum({
5446+
manifest: getSnapManifest({
5447+
version: newVersion,
5448+
initialPermissions: {
5449+
'endowment:page-home': {},
5450+
'endowment:ethereum-provider': {},
5451+
},
5452+
}),
5453+
});
5454+
5455+
const detectLocationMock = jest
5456+
.fn()
5457+
.mockImplementationOnce(
5458+
() =>
5459+
new LoopbackLocation({
5460+
manifest: originalManifest,
5461+
}),
5462+
)
5463+
.mockImplementationOnce(
5464+
() =>
5465+
new LoopbackLocation({
5466+
manifest: updatedManifest,
5467+
}),
5468+
);
5469+
5470+
const controller = getSnapController(
5471+
getSnapControllerOptions({
5472+
messenger,
5473+
detectSnapLocation: detectLocationMock,
5474+
featureFlags: {
5475+
useCaip25Permission: true,
5476+
},
5477+
}),
5478+
);
5479+
5480+
await controller.installSnaps(MOCK_ORIGIN, { [MOCK_SNAP_ID]: {} });
5481+
await controller.stopSnap(MOCK_SNAP_ID);
5482+
5483+
const approvedPermissions = {
5484+
'endowment:page-home': {
5485+
caveats: null,
5486+
},
5487+
};
5488+
5489+
expect(messenger.call).toHaveBeenCalledWith(
5490+
'PermissionController:grantPermissions',
5491+
{
5492+
approvedPermissions,
5493+
subject: { origin: MOCK_SNAP_ID },
5494+
requestData: expect.any(Object),
5495+
},
5496+
);
5497+
5498+
jest.mocked(messenger.call).mockClear();
5499+
5500+
const result = await controller.installSnaps(MOCK_ORIGIN, {
5501+
[MOCK_SNAP_ID]: { version: newVersionRange },
5502+
});
5503+
5504+
const updatedApprovedPermissions = {
5505+
'endowment:page-home': {
5506+
caveats: null,
5507+
},
5508+
'endowment:ethereum-provider': {},
5509+
'endowment:caip25': {
5510+
caveats: [
5511+
{
5512+
type: 'authorizedScopes',
5513+
value: {
5514+
requiredScopes: {},
5515+
optionalScopes: {
5516+
'eip155:1': {
5517+
accounts: [],
5518+
},
5519+
},
5520+
sessionProperties: {},
5521+
isMultichainOrigin: false,
5522+
},
5523+
},
5524+
],
5525+
},
5526+
};
5527+
5528+
expect(messenger.call).toHaveBeenCalledWith(
5529+
'PermissionController:grantPermissions',
5530+
{
5531+
approvedPermissions: updatedApprovedPermissions,
5532+
subject: { origin: MOCK_SNAP_ID },
5533+
requestData: expect.any(Object),
5534+
},
5535+
);
5536+
5537+
expect(result).toStrictEqual({
5538+
[MOCK_SNAP_ID]: getTruncatedSnap({
5539+
version: newVersion,
5540+
initialPermissions: updatedManifest.result.initialPermissions,
5541+
}),
5542+
});
5543+
5544+
controller.destroy();
5545+
});
5546+
54145547
it('does not grant the `endowment:caip25` permission to a Snap with `endowment:ethereum-provider` if the `useCaip25Permission` feature flag is disabled', async () => {
54155548
const rootMessenger = getControllerMessenger();
54165549
const messenger = getSnapControllerMessenger(rootMessenger);

0 commit comments

Comments
 (0)