mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Making addPermittedAccount and removePermittedAccount methods idempotent (#15709)
This commit is contained in:
parent
cd5398b2b2
commit
8c8539d1f5
@ -14,9 +14,7 @@ export function getPermissionBackgroundApiMethods(permissionController) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (existing.value.includes(account)) {
|
if (existing.value.includes(account)) {
|
||||||
throw new Error(
|
return;
|
||||||
`eth_accounts permission for origin "${origin}" already permits account "${account}".`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
permissionController.updateCaveat(
|
permissionController.updateCaveat(
|
||||||
@ -35,9 +33,7 @@ export function getPermissionBackgroundApiMethods(permissionController) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!existing.value.includes(account)) {
|
if (!existing.value.includes(account)) {
|
||||||
throw new Error(
|
return;
|
||||||
`eth_accounts permission for origin "${origin}" already does not permit account "${account}".`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const remainingAccounts = existing.value.filter(
|
const remainingAccounts = existing.value.filter(
|
||||||
|
@ -34,7 +34,7 @@ describe('permission background API methods', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws if the specified account is already permitted', () => {
|
it('does not add a permitted account', () => {
|
||||||
const permissionController = {
|
const permissionController = {
|
||||||
getCaveat: jest.fn().mockImplementationOnce(() => {
|
getCaveat: jest.fn().mockImplementationOnce(() => {
|
||||||
return { type: CaveatTypes.restrictReturnedAccounts, value: ['0x1'] };
|
return { type: CaveatTypes.restrictReturnedAccounts, value: ['0x1'] };
|
||||||
@ -42,14 +42,9 @@ describe('permission background API methods', () => {
|
|||||||
updateCaveat: jest.fn(),
|
updateCaveat: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(() =>
|
getPermissionBackgroundApiMethods(
|
||||||
getPermissionBackgroundApiMethods(
|
permissionController,
|
||||||
permissionController,
|
).addPermittedAccount('foo.com', '0x1');
|
||||||
).addPermittedAccount('foo.com', '0x1'),
|
|
||||||
).toThrow(
|
|
||||||
`eth_accounts permission for origin "foo.com" already permits account "0x1".`,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(permissionController.getCaveat).toHaveBeenCalledTimes(1);
|
expect(permissionController.getCaveat).toHaveBeenCalledTimes(1);
|
||||||
expect(permissionController.getCaveat).toHaveBeenCalledWith(
|
expect(permissionController.getCaveat).toHaveBeenCalledWith(
|
||||||
'foo.com',
|
'foo.com',
|
||||||
@ -128,7 +123,7 @@ describe('permission background API methods', () => {
|
|||||||
expect(permissionController.updateCaveat).not.toHaveBeenCalled();
|
expect(permissionController.updateCaveat).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws if the specified account is not permitted', () => {
|
it('does not call permissionController.updateCaveat if the specified account is not permitted', () => {
|
||||||
const permissionController = {
|
const permissionController = {
|
||||||
getCaveat: jest.fn().mockImplementationOnce(() => {
|
getCaveat: jest.fn().mockImplementationOnce(() => {
|
||||||
return { type: CaveatTypes.restrictReturnedAccounts, value: ['0x1'] };
|
return { type: CaveatTypes.restrictReturnedAccounts, value: ['0x1'] };
|
||||||
@ -137,14 +132,9 @@ describe('permission background API methods', () => {
|
|||||||
updateCaveat: jest.fn(),
|
updateCaveat: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(() =>
|
getPermissionBackgroundApiMethods(
|
||||||
getPermissionBackgroundApiMethods(
|
permissionController,
|
||||||
permissionController,
|
).removePermittedAccount('foo.com', '0x2');
|
||||||
).removePermittedAccount('foo.com', '0x2'),
|
|
||||||
).toThrow(
|
|
||||||
`eth_accounts permission for origin "foo.com" already does not permit account "0x2".`,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(permissionController.getCaveat).toHaveBeenCalledTimes(1);
|
expect(permissionController.getCaveat).toHaveBeenCalledTimes(1);
|
||||||
expect(permissionController.getCaveat).toHaveBeenCalledWith(
|
expect(permissionController.getCaveat).toHaveBeenCalledWith(
|
||||||
'foo.com',
|
'foo.com',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user