Skip to content

Commit

Permalink
wait until window handles
Browse files Browse the repository at this point in the history
  • Loading branch information
seaona authored and hjetpoluru committed Jun 14, 2024
1 parent 2aaf565 commit f48a7dc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 42 deletions.
11 changes: 11 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5190,6 +5190,17 @@ export default class MetamaskController extends EventEmitter {
}),
);

// EVM requests and eth permissions should not be passed to non-EVM accounts
// this middleware intercepts these requests and returns an error.
engine.push(
createEvmMethodsToNonEvmAccountReqFilterMiddleware({
messenger: this.controllerMessenger.getRestricted({
name: 'EvmMethodsToNonEvmAccountFilterMessenger',
allowedActions: ['AccountsController:getSelectedAccount'],
}),
}),
);

// Unrestricted/permissionless RPC method implementations.
// They must nevertheless be placed _behind_ the permission middleware.
engine.push(
Expand Down
93 changes: 51 additions & 42 deletions test/e2e/tests/tokens/increase-token-allowance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,61 @@ const DEFAULT_TEST_DAPP_INCREASE_ALLOWANCE_SPENDING_CAP = '1';
describe('Increase Token Allowance', function () {
const smartContract = SMART_CONTRACTS.HST;

it('increases token spending cap to allow other accounts to transfer tokens @no-mmi', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
smartContract,
title: this.test.fullTitle(),
},
async ({ driver, contractRegistry }) => {
const ACCOUNT_1_NAME = 'Account 1';
const ACCOUNT_2_NAME = '2nd Account';

const initialSpendingCap = '1';
const additionalSpendingCap = '1';

await unlockWallet(driver);

const contractAddress = await contractRegistry.getContractAddress(
for (let i = 0; i < 10; i++) {
it(`increases token spending cap to allow other accounts to transfer tokens @no-mmi run${i}`, async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
smartContract,
);
await openDapp(driver, contractAddress);
title: this.test.fullTitle(),
},
async ({ driver, contractRegistry }) => {
const ACCOUNT_1_NAME = 'Account 1';
const ACCOUNT_2_NAME = '2nd Account';

await deployTokenContract(driver);
await approveTokenSpendingCapTo(driver, ACCOUNT_2, initialSpendingCap);
const initialSpendingCap = '1';
const additionalSpendingCap = '1';

await sendTransaction(driver, ACCOUNT_2, '1');
await addAccount(driver, ACCOUNT_2_NAME);
await unlockWallet(driver);

await triggerTransferFromTokens(driver, ACCOUNT_1, ACCOUNT_2);
// 'Transfer From Tokens' on the test dApp attempts to transfer 1.5 TST.
// Since this is higher than the 'initialSpendingCap', it should fail.
await pollForTokenAddressesError(
driver,
'reverted ERC20: insufficient allowance',
);
const contractAddress = await contractRegistry.getContractAddress(
smartContract,
);
await openDapp(driver, contractAddress);

await switchToAccountWithName(driver, ACCOUNT_1_NAME);
await deployTokenContract(driver);
await approveTokenSpendingCapTo(
driver,
ACCOUNT_2,
initialSpendingCap,
);

await increaseTokenAllowance(driver, additionalSpendingCap);
await sendTransaction(driver, ACCOUNT_2, '1');
await addAccount(driver, ACCOUNT_2_NAME);

await switchToAccountWithName(driver, ACCOUNT_2_NAME);
await triggerTransferFromTokens(driver, ACCOUNT_1, ACCOUNT_2);
await confirmTransferFromTokensSuccess(driver);
},
);
});
await triggerTransferFromTokens(driver, ACCOUNT_1, ACCOUNT_2);
// 'Transfer From Tokens' on the test dApp attempts to transfer 1.5 TST.
// Since this is higher than the 'initialSpendingCap', it should fail.
await pollForTokenAddressesError(
driver,
'reverted ERC20: insufficient allowance',
);

await switchToAccountWithName(driver, ACCOUNT_1_NAME);

await increaseTokenAllowance(driver, additionalSpendingCap);

await switchToAccountWithName(driver, ACCOUNT_2_NAME);
await triggerTransferFromTokens(driver, ACCOUNT_1, ACCOUNT_2);
await confirmTransferFromTokensSuccess(driver);
},
);
});
}
async function deployTokenContract(driver) {
await driver.findClickableElement('#deployButton');
}
Expand Down Expand Up @@ -232,6 +237,8 @@ describe('Increase Token Allowance', function () {
});
await driver.delay(2000);

// Windows: MetaMask, Test Dapp and Dialog
await driver.waitUntilXWindowHandles(3);
await driver.switchToWindowWithTitle(WINDOW_TITLES.Dialog);
let spendingCapElement = await driver.findElement(
'[data-testid="custom-spending-cap-input"]',
Expand Down Expand Up @@ -297,6 +304,8 @@ describe('Increase Token Allowance', function () {
}

async function confirmTransferFromTokensSuccess(driver) {
// Windows: MetaMask, Test Dapp and Dialog
await driver.waitUntilXWindowHandles(3);
await driver.switchToWindowWithTitle(WINDOW_TITLES.Dialog);
await driver.waitForSelector({ text: '1.5 TST', tag: 'h1' });
await driver.clickElement({ text: 'Confirm', tag: 'button' });
Expand Down

0 comments on commit f48a7dc

Please sign in to comment.