1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

removeNetworkConfiguration validates given ID (#18650)

In the `core` version of NetworkController, `removeNetworkConfiguration`
throws an error if the given network configuration ID doesn't match an
existing network configuration. This commits adds the same check for
consistency. It also makes some minor changes to the implementation and
tests for `removeNetworkConfiguration` for consistency as well.
This commit is contained in:
Elliot Winkler 2023-04-19 09:47:33 -06:00 committed by GitHub
parent d362dbfacb
commit a876eaba23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 18 deletions

View File

@ -7308,14 +7308,14 @@ describe('NetworkController', () => {
});
describe('removeNetworkConfigurations', () => {
it('should remove a network configuration', async () => {
const networkConfigurationId = 'networkConfigurationId';
it('removes a network configuration', async () => {
const networkConfigurationId = 'testNetworkConfigurationId';
await withController(
{
state: {
networkConfigurations: {
[networkConfigurationId]: {
id: 'aaaaaa',
id: networkConfigurationId,
rpcUrl: 'https://test-rpc-url',
ticker: 'old_rpc_ticker',
nickname: 'old_rpc_chainName',
@ -7326,25 +7326,44 @@ describe('NetworkController', () => {
},
},
async ({ controller }) => {
expect(
Object.values(controller.store.getState().networkConfigurations),
).toStrictEqual([
{
id: 'aaaaaa',
rpcUrl: 'https://test-rpc-url',
ticker: 'old_rpc_ticker',
nickname: 'old_rpc_chainName',
rpcPrefs: { blockExplorerUrl: 'testchainscan.io' },
chainId: '0x1',
},
]);
controller.removeNetworkConfiguration(networkConfigurationId);
expect(
controller.store.getState().networkConfigurations,
).toStrictEqual({});
},
);
});
it('throws if the networkConfigurationId it is passed does not correspond to a network configuration in state', async () => {
const testNetworkConfigurationId = 'testNetworkConfigurationId';
const invalidNetworkConfigurationId = 'invalidNetworkConfigurationId';
await withController(
{
state: {
networkConfigurations: {
[testNetworkConfigurationId]: {
rpcUrl: 'https://rpc-url.com',
ticker: 'old_rpc_ticker',
nickname: 'old_rpc_nickname',
rpcPrefs: { blockExplorerUrl: 'testchainscan.io' },
chainId: '0x1',
id: testNetworkConfigurationId,
},
},
},
},
async ({ controller }) => {
expect(() =>
controller.removeNetworkConfiguration(
invalidNetworkConfigurationId,
),
).toThrow(
`networkConfigurationId ${invalidNetworkConfigurationId} does not match a configured networkConfiguration`,
);
},
);
});
});
});

View File

@ -1141,9 +1141,12 @@ export class NetworkController extends EventEmitter {
* @param networkConfigurationId - The unique id for the network configuration
* to remove.
*/
removeNetworkConfiguration(
networkConfigurationId: NetworkConfigurationId,
): void {
removeNetworkConfiguration(networkConfigurationId: NetworkConfigurationId) {
if (!this.store.getState().networkConfigurations[networkConfigurationId]) {
throw new Error(
`networkConfigurationId ${networkConfigurationId} does not match a configured networkConfiguration`,
);
}
const networkConfigurations = {
...this.store.getState().networkConfigurations,
};