Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add E2E spec for auto grant new permitted chain to connected dapp when network is switched via popup #28741

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>} 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 = {
Expand Down Expand Up @@ -967,5 +980,6 @@ module.exports = {
tempToggleSettingRedesignedConfirmations,
tempToggleSettingRedesignedTransactionConfirmations,
openMenuSafe,
openPopupWithActiveTabOrigin,
sentryRegEx,
};
121 changes: 121 additions & 0 deletions test/e2e/tests/connections/switch-auto-grant-networks-flow.spec.js
Original file line number Diff line number Diff line change
@@ -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.',
);
},
);
});
});
7 changes: 1 addition & 6 deletions test/e2e/tests/request-queuing/ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
veryLargeDelayMs,
DAPP_TWO_URL,
tempToggleSettingRedesignedTransactionConfirmations,
openPopupWithActiveTabOrigin,
} = require('../../helpers');
const { PAGES } = require('../../webdriver/driver');
const {
Expand Down Expand Up @@ -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,
Expand Down
Loading