From 3d2d551677a02d1c04348db81eeb4c5b12e5b971 Mon Sep 17 00:00:00 2001 From: Jiexi Luan Date: Tue, 26 Nov 2024 11:12:52 -0800 Subject: [PATCH 1/4] add missing dispatch to network-list-menu --- .../multichain/network-list-menu/network-list-menu.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/components/multichain/network-list-menu/network-list-menu.tsx b/ui/components/multichain/network-list-menu/network-list-menu.tsx index 8b18170adc6c..2ca946bbd1f5 100644 --- a/ui/components/multichain/network-list-menu/network-list-menu.tsx +++ b/ui/components/multichain/network-list-menu/network-list-menu.tsx @@ -297,7 +297,7 @@ export const NetworkListMenu = ({ onClose }: { onClose: () => void }) => { } if (permittedAccountAddresses.length > 0) { - grantPermittedChain(selectedTabOrigin, network.chainId); + dispatch(grantPermittedChain(selectedTabOrigin, network.chainId)); if (!permittedChainIds.includes(network.chainId)) { dispatch(showPermittedNetworkToast()); } From df4e65966d1bb5a8853bedc527881cebf9b1f4df Mon Sep 17 00:00:00 2001 From: Jiexi Luan Date: Tue, 26 Nov 2024 11:13:06 -0800 Subject: [PATCH 2/4] move openPopupWithActiveTabOrigin to helpers --- test/e2e/helpers.js | 14 ++++++++++++++ test/e2e/tests/request-queuing/ui.spec.js | 7 +------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index 75eefb12f7c1..1b49a70e219c 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -913,6 +913,19 @@ async function openMenuSafe(driver) { } } +/** + * Opens the extension popup view in the context of the origin passed. + * + * @param {WebDriver} driver - The WebDriver instance used to interact with the browser. + * @param {string} origin - The origin to open the popup view from. + * @returns {Promise} A promise that resolves when the popup view is opened. + */ +async function openPopupWithActiveTabOrigin(driver, origin) { + await driver.openNewPage( + `${driver.extensionUrl}/${PAGES.POPUP}.html?activeTabOrigin=${origin}`, + ); +} + const sentryRegEx = /^https:\/\/sentry\.io\/api\/\d+\/envelope/gu; module.exports = { @@ -967,5 +980,6 @@ module.exports = { tempToggleSettingRedesignedConfirmations, tempToggleSettingRedesignedTransactionConfirmations, openMenuSafe, + openPopupWithActiveTabOrigin, sentryRegEx, }; diff --git a/test/e2e/tests/request-queuing/ui.spec.js b/test/e2e/tests/request-queuing/ui.spec.js index 707c252396b7..b52461ab470f 100644 --- a/test/e2e/tests/request-queuing/ui.spec.js +++ b/test/e2e/tests/request-queuing/ui.spec.js @@ -15,6 +15,7 @@ const { veryLargeDelayMs, DAPP_TWO_URL, tempToggleSettingRedesignedTransactionConfirmations, + openPopupWithActiveTabOrigin, } = require('../../helpers'); const { PAGES } = require('../../webdriver/driver'); const { @@ -192,12 +193,6 @@ async function switchToNetworkByName(driver, networkName) { await driver.clickElement(`[data-testid="${networkName}"]`); } -async function openPopupWithActiveTabOrigin(driver, origin) { - await driver.openNewPage( - `${driver.extensionUrl}/${PAGES.POPUP}.html?activeTabOrigin=${origin}`, - ); -} - async function validateBalanceAndActivity( driver, expectedBalance, From 5690670c1056acf5eb514dae3d6bd5f66face8e0 Mon Sep 17 00:00:00 2001 From: Jiexi Luan Date: Tue, 26 Nov 2024 11:14:17 -0800 Subject: [PATCH 3/4] add new e2e spec --- .../switch-auto-grant-networks-flow.spec.js | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 test/e2e/tests/connections/switch-auto-grant-networks-flow.spec.js diff --git a/test/e2e/tests/connections/switch-auto-grant-networks-flow.spec.js b/test/e2e/tests/connections/switch-auto-grant-networks-flow.spec.js new file mode 100644 index 000000000000..a674ab47cc55 --- /dev/null +++ b/test/e2e/tests/connections/switch-auto-grant-networks-flow.spec.js @@ -0,0 +1,121 @@ +const { strict: assert } = require('assert'); +const { + withFixtures, + WINDOW_TITLES, + defaultGanacheOptions, + openPopupWithActiveTabOrigin, + openDapp, + unlockWallet, + DAPP_URL, +} = require('../../helpers'); +const FixtureBuilder = require('../../fixture-builder'); +const { + PermissionNames, +} = require('../../../../app/scripts/controllers/permissions'); +const { CaveatTypes } = require('../../../../shared/constants/permissions'); + +describe('Switch Auto Grant Networks Flow', function () { + it('should be able to automatically grant additional `permitted-chains` for an unpermitted network being switched to for a dapp via the network menu', async function () { + await withFixtures( + { + dapp: true, + fixtures: new FixtureBuilder() + .withNetworkController() + .withPreferencesControllerUseRequestQueueEnabled() + .withSelectedNetworkControllerPerDomain() + .build(), + title: this.test.fullTitle(), + ganacheOptions: defaultGanacheOptions, + driverOptions: { constrainWindowSize: true }, + }, + async ({ driver }) => { + await unlockWallet(driver); + + await openDapp(driver); + + await driver.clickElement({ + text: 'Connect', + tag: 'button', + }); + + await driver.switchToWindowWithTitle(WINDOW_TITLES.Dialog); + const editButtons = await driver.findElements('[data-testid="edit"]'); + + // Click the edit button for networks + await editButtons[1].click(); + + // Disconnect Testnet + await driver.clickElement({ + text: 'Localhost 8545', + tag: 'p', + }); + + await driver.clickElement('[data-testid="connect-more-chains-button"]'); + await driver.clickElementAndWaitForWindowToClose({ + text: 'Connect', + tag: 'button', + }); + + await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp); + + const getPermissionsRequest = JSON.stringify({ + method: 'wallet_getPermissions', + }); + const getPermissionsResultBeforeSwitch = await driver.executeScript( + `return window.ethereum.request(${getPermissionsRequest})`, + ); + + const permittedChainsBeforeSwitch = + getPermissionsResultBeforeSwitch + ?.find( + (permission) => + permission.parentCapability === PermissionNames.permittedChains, + ) + ?.caveats.find( + (caveat) => caveat.type === CaveatTypes.restrictNetworkSwitching, + )?.value || []; + + assert.ok( + !permittedChainsBeforeSwitch.includes('0x1337'), + 'Localhost 8545 is not connected.', + ); + + await openPopupWithActiveTabOrigin(driver, DAPP_URL); + + // Network Selector + await driver.clickElement('[data-testid="network-display"]'); + + // Switch network + await driver.clickElement({ + text: 'Localhost 8545', + css: 'p', + }); + + await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp); + + const getPermissionsResultAfterSwitch = await driver.executeScript( + `return window.ethereum.request(${getPermissionsRequest})`, + ); + + const permittedChainsAfterSwitch = + getPermissionsResultAfterSwitch + ?.find( + (permission) => + permission.parentCapability === PermissionNames.permittedChains, + ) + ?.caveats.find( + (caveat) => caveat.type === CaveatTypes.restrictNetworkSwitching, + )?.value || []; + + assert.equal( + permittedChainsBeforeSwitch.length + 1, + permittedChainsAfterSwitch.length, + ); + assert.ok( + permittedChainsBeforeSwitch.includes('0x1337'), + 'Localhost 8545 is connected.', + ); + }, + ); + }); +}); From 2267e8c7665599abaa3ac7e456b61019fa1ca863 Mon Sep 17 00:00:00 2001 From: Jiexi Luan Date: Tue, 26 Nov 2024 11:48:07 -0800 Subject: [PATCH 4/4] revert dispatch --- .../multichain/network-list-menu/network-list-menu.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/components/multichain/network-list-menu/network-list-menu.tsx b/ui/components/multichain/network-list-menu/network-list-menu.tsx index 2ca946bbd1f5..8b18170adc6c 100644 --- a/ui/components/multichain/network-list-menu/network-list-menu.tsx +++ b/ui/components/multichain/network-list-menu/network-list-menu.tsx @@ -297,7 +297,7 @@ export const NetworkListMenu = ({ onClose }: { onClose: () => void }) => { } if (permittedAccountAddresses.length > 0) { - dispatch(grantPermittedChain(selectedTabOrigin, network.chainId)); + grantPermittedChain(selectedTabOrigin, network.chainId); if (!permittedChainIds.includes(network.chainId)) { dispatch(showPermittedNetworkToast()); }