Making addPermittedAccount and removePermittedAccount methods idempotent (#15709)

feature/default_network_editable
Jyoti Puri 2 years ago committed by GitHub
parent cd5398b2b2
commit 8c8539d1f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/scripts/controllers/permissions/background-api.js
  2. 26
      app/scripts/controllers/permissions/background-api.test.js

@ -14,9 +14,7 @@ export function getPermissionBackgroundApiMethods(permissionController) {
);
if (existing.value.includes(account)) {
throw new Error(
`eth_accounts permission for origin "${origin}" already permits account "${account}".`,
);
return;
}
permissionController.updateCaveat(
@ -35,9 +33,7 @@ export function getPermissionBackgroundApiMethods(permissionController) {
);
if (!existing.value.includes(account)) {
throw new Error(
`eth_accounts permission for origin "${origin}" already does not permit account "${account}".`,
);
return;
}
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 = {
getCaveat: jest.fn().mockImplementationOnce(() => {
return { type: CaveatTypes.restrictReturnedAccounts, value: ['0x1'] };
@ -42,14 +42,9 @@ describe('permission background API methods', () => {
updateCaveat: jest.fn(),
};
expect(() =>
getPermissionBackgroundApiMethods(
permissionController,
).addPermittedAccount('foo.com', '0x1'),
).toThrow(
`eth_accounts permission for origin "foo.com" already permits account "0x1".`,
);
getPermissionBackgroundApiMethods(
permissionController,
).addPermittedAccount('foo.com', '0x1');
expect(permissionController.getCaveat).toHaveBeenCalledTimes(1);
expect(permissionController.getCaveat).toHaveBeenCalledWith(
'foo.com',
@ -128,7 +123,7 @@ describe('permission background API methods', () => {
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 = {
getCaveat: jest.fn().mockImplementationOnce(() => {
return { type: CaveatTypes.restrictReturnedAccounts, value: ['0x1'] };
@ -137,14 +132,9 @@ describe('permission background API methods', () => {
updateCaveat: jest.fn(),
};
expect(() =>
getPermissionBackgroundApiMethods(
permissionController,
).removePermittedAccount('foo.com', '0x2'),
).toThrow(
`eth_accounts permission for origin "foo.com" already does not permit account "0x2".`,
);
getPermissionBackgroundApiMethods(
permissionController,
).removePermittedAccount('foo.com', '0x2');
expect(permissionController.getCaveat).toHaveBeenCalledTimes(1);
expect(permissionController.getCaveat).toHaveBeenCalledWith(
'foo.com',

Loading…
Cancel
Save