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/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.', + ); + }, + ); + }); +}); 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,