1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Add e2e tests for queuing multiple signing operations (#19446)

This commit is contained in:
Vinicius Stevam 2023-06-28 08:21:02 +01:00 committed by GitHub
parent b36487ab52
commit 994a71d30e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 410 additions and 250 deletions

View File

@ -5,6 +5,7 @@ const {
DAPP_URL, DAPP_URL,
defaultGanacheOptions, defaultGanacheOptions,
unlockWallet, unlockWallet,
regularDelayMs,
} = require('../helpers'); } = require('../helpers');
const FixtureBuilder = require('../fixture-builder'); const FixtureBuilder = require('../fixture-builder');
@ -36,7 +37,7 @@ describe('Eth sign', function () {
}); });
it('can initiate and confirm a eth sign', async function () { it('can initiate and confirm a eth sign', async function () {
const expectedPersonalMessage = const expectedEthSignMessage =
'0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0'; '0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0';
const expectedEthSignResult = const expectedEthSignResult =
'"0x816ab6c5d5356548cc4e004ef35a37fdfab916742a2bbeda756cd064c3d3789a6557d41d49549be1de249e1937a8d048996dfcc70d0552111605dc7cc471e8531b"'; '"0x816ab6c5d5356548cc4e004ef35a37fdfab916742a2bbeda756cd064c3d3789a6557d41d49549be1de249e1937a8d048996dfcc70d0552111605dc7cc471e8531b"';
@ -69,23 +70,11 @@ describe('Eth sign', function () {
windowHandles, windowHandles,
); );
await driver.findElement({ await verifyAndAssertEthSign(driver, DAPP_URL, expectedEthSignMessage);
css: '.request-signature__content__title',
text: 'Signature request',
});
await driver.findElement({ await approveEthSign(
css: '.request-signature__origin', driver,
text: DAPP_URL, '[data-testid="page-container-footer-next"]',
});
await driver.findElement({
css: '.request-signature__row-value',
text: expectedPersonalMessage,
});
await driver.clickElement('[data-testid="page-container-footer-next"]');
await driver.clickElement(
'.signature-request-warning__footer__sign-button', '.signature-request-warning__footer__sign-button',
); );
// Switch to the Dapp // Switch to the Dapp
@ -101,4 +90,103 @@ describe('Eth sign', function () {
}, },
); );
}); });
it('can queue multiple eth sign and confirm', async function () {
const expectedEthSignMessage =
'0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0';
const expectedEthSignResult =
'"0x816ab6c5d5356548cc4e004ef35a37fdfab916742a2bbeda756cd064c3d3789a6557d41d49549be1de249e1937a8d048996dfcc70d0552111605dc7cc471e8531b"';
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPreferencesController({
disabledRpcMethodPreferences: {
eth_sign: true,
},
})
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test.title,
},
async ({ driver }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await openDapp(driver);
// Create eth sign
await driver.clickElement('#ethSign');
// Wait for Signature request popup
await driver.waitUntilXWindowHandles(3);
let windowHandles = await driver.getAllWindowHandles();
// Switch to Dapp
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
// Create second eth sign
await driver.clickElement('#ethSign');
await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
await driver.waitForSelector({
text: 'Reject 2 requests',
tag: 'a',
});
await verifyAndAssertEthSign(driver, DAPP_URL, expectedEthSignMessage);
// Confirm first eth sign
await approveEthSign(
driver,
'[data-testid="page-container-footer-next"]',
'.signature-request-warning__footer__sign-button',
);
// Confirm second eth sign
await approveEthSign(
driver,
'[data-testid="page-container-footer-next"]',
'.signature-request-warning__footer__sign-button',
);
// Switch to the Dapp
await driver.waitUntilXWindowHandles(2);
windowHandles = await driver.getAllWindowHandles();
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
// Verify last confirmed request
const result = await driver.findElement('#ethSignResult');
assert.equal(await result.getText(), expectedEthSignResult);
},
);
});
}); });
async function verifyAndAssertEthSign(driver, dappUrl, expectedMessage) {
await driver.findElement({
css: '.request-signature__content__title',
text: 'Signature request',
});
await driver.findElement({
css: '.request-signature__origin',
text: dappUrl,
});
await driver.findElement({
css: '.request-signature__row-value',
text: expectedMessage,
});
}
async function approveEthSign(driver, buttonTestId, signButtonClass) {
await driver.clickElement(buttonTestId);
await driver.clickElement(signButtonClass);
await driver.delay(regularDelayMs);
}

View File

@ -1,5 +1,10 @@
const { strict: assert } = require('assert'); const { strict: assert } = require('assert');
const { convertToHexValue, withFixtures, openDapp } = require('../helpers'); const {
convertToHexValue,
withFixtures,
openDapp,
regularDelayMs,
} = require('../helpers');
const FixtureBuilder = require('../fixture-builder'); const FixtureBuilder = require('../fixture-builder');
describe('Personal sign', function () { describe('Personal sign', function () {
@ -33,7 +38,7 @@ describe('Personal sign', function () {
await driver.clickElement('#personalSign'); await driver.clickElement('#personalSign');
await driver.waitUntilXWindowHandles(3); await driver.waitUntilXWindowHandles(3);
let windowHandles = await driver.getAllWindowHandles(); const windowHandles = await driver.getAllWindowHandles();
await driver.switchToWindowWithTitle( await driver.switchToWindowWithTitle(
'MetaMask Notification', 'MetaMask Notification',
windowHandles, windowHandles,
@ -47,23 +52,92 @@ describe('Personal sign', function () {
await driver.clickElement('[data-testid="page-container-footer-next"]'); await driver.clickElement('[data-testid="page-container-footer-next"]');
// Switch to the Dapp await verifyAndAssertPersonalMessage(driver, publicAddress);
await driver.waitUntilXWindowHandles(2); },
windowHandles = await driver.getAllWindowHandles(); );
});
it('can queue multiple personal signs and confirm', async function () {
const ganacheOptions = {
accounts: [
{
secretKey:
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC',
balance: convertToHexValue(25000000000000000000),
},
],
};
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await openDapp(driver);
// Create personal sign
await driver.clickElement('#personalSign');
await driver.waitUntilXWindowHandles(3);
const windowHandles = await driver.getAllWindowHandles();
// Switch to Dapp
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles); await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
// Verify // Create second personal sign
await driver.clickElement('#personalSignVerify'); await driver.clickElement('#personalSign');
const verifySigUtil = await driver.findElement(
'#personalSignVerifySigUtilResult', await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
); );
const verifyECRecover = await driver.waitForSelector({
css: '#personalSignVerifyECRecoverResult', await driver.waitForSelector({
text: publicAddress, text: 'Reject 2 requests',
tag: 'a',
}); });
assert.equal(await verifySigUtil.getText(), publicAddress);
assert.equal(await verifyECRecover.getText(), publicAddress); const personalMessageRow = await driver.findElement(
'.request-signature__row-value',
);
const personalMessage = await personalMessageRow.getText();
assert.equal(personalMessage, 'Example `personal_sign` message');
// Confirm first personal sign
await driver.clickElement('[data-testid="page-container-footer-next"]');
await driver.delay(regularDelayMs);
// Confirm second personal sign
await driver.clickElement('[data-testid="page-container-footer-next"]');
await verifyAndAssertPersonalMessage(driver, publicAddress);
}, },
); );
}); });
}); });
async function verifyAndAssertPersonalMessage(driver, publicAddress) {
// Switch to the Dapp
await driver.waitUntilXWindowHandles(2);
const windowHandles = await driver.getAllWindowHandles();
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
// Verify last confirmed personal sign
await driver.clickElement('#personalSignVerify');
const verifySigUtil = await driver.findElement(
'#personalSignVerifySigUtilResult',
);
const verifyECRecover = await driver.waitForSelector({
css: '#personalSignVerifyECRecoverResult',
text: publicAddress,
});
assert.equal(await verifySigUtil.getText(), publicAddress);
assert.equal(await verifyECRecover.getText(), publicAddress);
}

View File

@ -1,240 +1,238 @@
const { strict: assert } = require('assert'); const { strict: assert } = require('assert');
const { const {
convertToHexValue,
withFixtures, withFixtures,
regularDelayMs, regularDelayMs,
openDapp, openDapp,
DAPP_URL, DAPP_URL,
convertToHexValue,
} = require('../helpers'); } = require('../helpers');
const FixtureBuilder = require('../fixture-builder'); const FixtureBuilder = require('../fixture-builder');
describe('Sign Typed Data V4 Signature Request', function () { const signatureRequestType = {
it('can initiate and confirm a Signature Request', async function () { signTypedData: 'Sign Typed Data',
const ganacheOptions = { signTypedDataV3: 'Sign Typed Data V3',
accounts: [ signTypedDataV4: 'Sign Typed Data V4',
{ };
secretKey:
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC',
balance: convertToHexValue(25000000000000000000),
},
],
};
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await openDapp(driver);
// creates a sign typed data signature request
await driver.clickElement('#signTypedDataV4');
await driver.waitUntilXWindowHandles(3);
let windowHandles = await driver.getAllWindowHandles();
await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
const title = await driver.findElement(
'.signature-request__content__title',
);
const origin = await driver.findElement('.signature-request__origin');
const verifyContractDetailsButton = await driver.findElement(
'.signature-request-content__verify-contract-details',
);
const message = await driver.findElement(
'.signature-request-data__node__value',
);
assert.equal(await title.getText(), 'Signature request');
assert.equal(await origin.getText(), DAPP_URL);
verifyContractDetailsButton.click();
await driver.findElement({ text: 'Third-party details', tag: 'h5' });
await driver.findElement('[data-testid="recipient"]');
await driver.clickElement({ text: 'Got it', tag: 'button' });
assert.equal(await message.getText(), 'Hello, Bob!');
// Approve signing typed data
await driver.clickElement(
'[data-testid="signature-request-scroll-button"]',
);
await driver.delay(regularDelayMs);
await driver.clickElement({ text: 'Sign', tag: 'button' });
await driver.waitUntilXWindowHandles(2);
windowHandles = await driver.getAllWindowHandles();
// switch to the Dapp and verify the signed addressed
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
await driver.clickElement('#signTypedDataV4Verify');
const recoveredAddress = await driver.findElement(
'#signTypedDataV4VerifyResult',
);
assert.equal(await recoveredAddress.getText(), publicAddress);
},
);
});
});
/* eslint-disable-next-line mocha/max-top-level-suites */
describe('Sign Typed Data V3 Signature Request', function () {
it('can initiate and confirm a Signature Request', async function () {
const ganacheOptions = {
accounts: [
{
secretKey:
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC',
balance: convertToHexValue(25000000000000000000),
},
],
};
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await openDapp(driver);
// creates a sign typed data signature request
await driver.clickElement('#signTypedDataV3');
await driver.waitUntilXWindowHandles(3);
let windowHandles = await driver.getAllWindowHandles();
await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
const title = await driver.findElement(
'.signature-request__content__title',
);
const origin = await driver.findElement('.signature-request__origin');
const verifyContractDetailsButton = await driver.findElement(
'.signature-request-content__verify-contract-details',
);
const messages = await driver.findElements(
'.signature-request-data__node__value',
);
assert.equal(await title.getText(), 'Signature request');
assert.equal(await origin.getText(), DAPP_URL);
verifyContractDetailsButton.click();
await driver.findElement({ text: 'Third-party details', tag: 'h5' });
await driver.findElement('[data-testid="recipient"]');
await driver.clickElement({ text: 'Got it', tag: 'button' });
assert.equal(await messages[4].getText(), 'Hello, Bob!');
// Approve signing typed data
await driver.clickElement(
'[data-testid="signature-request-scroll-button"]',
);
await driver.delay(regularDelayMs);
await driver.clickElement({ text: 'Sign', tag: 'button' });
await driver.waitUntilXWindowHandles(2);
windowHandles = await driver.getAllWindowHandles();
// switch to the Dapp and verify the signed addressed
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
await driver.clickElement('#signTypedDataV3Verify');
const recoveredAddress = await driver.findElement(
'#signTypedDataV3VerifyResult',
);
assert.equal(await recoveredAddress.getText(), publicAddress);
},
);
});
});
const testData = [
{
type: signatureRequestType.signTypedData,
buttonId: '#signTypedData',
verifyId: '#signTypedDataVerify',
verifyResultId: '#signTypedDataVerifyResult',
expectedMessage: 'Hi, Alice!',
verifyAndAssertMessage: {
titleClass: '.request-signature__content__title',
originClass: '.request-signature__origin',
messageClass: '.request-signature__row-value',
},
},
{
type: signatureRequestType.signTypedDataV3,
buttonId: '#signTypedDataV3',
verifyId: '#signTypedDataV3Verify',
verifyResultId: '#signTypedDataV3VerifyResult',
expectedMessage: 'Hello, Bob!',
verifyAndAssertMessage: {
titleClass: '.signature-request__content__title',
originClass: '.signature-request__origin',
messageClass: '.signature-request-data__node__value',
},
},
{
type: signatureRequestType.signTypedDataV4,
buttonId: '#signTypedDataV4',
verifyId: '#signTypedDataV4Verify',
verifyResultId: '#signTypedDataV4VerifyResult',
expectedMessage: 'Hello, Bob!',
verifyAndAssertMessage: {
titleClass: '.signature-request__content__title',
originClass: '.signature-request__origin',
messageClass: '.signature-request-data__node__value',
},
},
];
const ganacheOptions = {
accounts: [
{
secretKey:
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC',
balance: convertToHexValue(25000000000000000000),
},
],
};
describe('Sign Typed Data Signature Request', function () { describe('Sign Typed Data Signature Request', function () {
it('can initiate and confirm a Signature Request', async function () { testData.forEach((data) => {
const ganacheOptions = { it(`can initiate and confirm a Signature Request of ${data.type}`, async function () {
accounts: [ await withFixtures(
{ {
secretKey: dapp: true,
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC', fixtures: new FixtureBuilder()
balance: convertToHexValue(25000000000000000000), .withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
}, },
], async ({ driver, ganacheServer }) => {
}; const addresses = await ganacheServer.getAccounts();
await withFixtures( const publicAddress = addresses[0];
{ await driver.navigate();
dapp: true, await driver.fill('#password', 'correct horse battery staple');
fixtures: new FixtureBuilder() await driver.press('#password', driver.Key.ENTER);
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await openDapp(driver); await openDapp(driver);
// creates a sign typed data signature request // creates a sign typed data signature request
await driver.clickElement('#signTypedData'); await driver.clickElement(data.buttonId);
await driver.waitUntilXWindowHandles(3); await driver.waitUntilXWindowHandles(3);
let windowHandles = await driver.getAllWindowHandles(); let windowHandles = await driver.getAllWindowHandles();
await driver.switchToWindowWithTitle( await driver.switchToWindowWithTitle(
'MetaMask Notification', 'MetaMask Notification',
windowHandles, windowHandles,
); );
const title = await driver.findElement( await verifyAndAssertSignTypedData(
'.request-signature__content__title', driver,
); data.type,
const origin = await driver.findElement('.request-signature__origin'); data.verifyAndAssertMessage.titleClass,
const message = await driver.findElements( data.verifyAndAssertMessage.originClass,
'.request-signature__row-value', data.verifyAndAssertMessage.messageClass,
); data.expectedMessage,
assert.equal(await title.getText(), 'Signature request'); );
assert.equal(await origin.getText(), DAPP_URL);
assert.equal(await message[0].getText(), 'Hi, Alice!');
assert.equal(await message[1].getText(), '1337');
// Approve signing typed data // Approve signing typed data
await driver.clickElement({ text: 'Sign', tag: 'button' }); await approveSignatureRequest(
await driver.waitUntilXWindowHandles(2); driver,
windowHandles = await driver.getAllWindowHandles(); data.type,
'[data-testid="signature-request-scroll-button"]',
);
await driver.waitUntilXWindowHandles(2);
windowHandles = await driver.getAllWindowHandles();
// switch to the Dapp and verify the signed addressed // switch to the Dapp and verify the signed address
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles); await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
await driver.clickElement('#signTypedDataVerify'); await driver.clickElement(data.verifyId);
const recoveredAddress = await driver.findElement( const recoveredAddress = await driver.findElement(
'#signTypedDataVerifyResult', data.verifyResultId,
); );
assert.equal(await recoveredAddress.getText(), publicAddress);
}, assert.equal(await recoveredAddress.getText(), publicAddress);
); },
);
});
});
testData.forEach((data) => {
it(`can queue multiple Signature Requests of ${data.type} and confirm`, async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await openDapp(driver);
// creates multiple sign typed data signature requests
await driver.clickElement(data.buttonId);
await driver.waitUntilXWindowHandles(3);
const windowHandles = await driver.getAllWindowHandles();
// switches to Dapp
await driver.switchToWindowWithTitle('E2E Test Dapp', windowHandles);
// creates second sign typed data signature request
await driver.clickElement(data.buttonId);
await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
await driver.waitForSelector({
text: 'Reject 2 requests',
tag: 'a',
});
await verifyAndAssertSignTypedData(
driver,
data.type,
data.verifyAndAssertMessage.titleClass,
data.verifyAndAssertMessage.originClass,
data.verifyAndAssertMessage.messageClass,
data.expectedMessage,
);
// approve first signature request
await approveSignatureRequest(
driver,
data.type,
'[data-testid="signature-request-scroll-button"]',
);
await driver.waitUntilXWindowHandles(3);
// approve second signature request
await approveSignatureRequest(
driver,
data.type,
'[data-testid="signature-request-scroll-button"]',
);
await driver.waitUntilXWindowHandles(2);
// switch to the Dapp and verify the signed address for each request
await driver.switchToWindowWithTitle('E2E Test Dapp');
await driver.clickElement(data.verifyId);
const recoveredAddress = await driver.findElement(
data.verifyResultId,
);
assert.equal(await recoveredAddress.getText(), publicAddress);
},
);
});
}); });
}); });
async function verifyAndAssertSignTypedData(
driver,
type,
titleClass,
originClass,
messageClass,
expectedMessage,
) {
const title = await driver.findElement(titleClass);
const origin = await driver.findElement(originClass);
assert.equal(await title.getText(), 'Signature request');
assert.equal(await origin.getText(), DAPP_URL);
const messages = await driver.findElements(messageClass);
if (type !== signatureRequestType.signTypedData) {
const verifyContractDetailsButton = await driver.findElement(
'.signature-request-content__verify-contract-details',
);
verifyContractDetailsButton.click();
await driver.findElement({ text: 'Third-party details', tag: 'h5' });
await driver.findElement('[data-testid="recipient"]');
await driver.clickElement({ text: 'Got it', tag: 'button' });
}
const messageNumber = type === signatureRequestType.signTypedDataV3 ? 4 : 0;
assert.equal(await messages[messageNumber].getText(), expectedMessage);
}
async function approveSignatureRequest(driver, type, buttonElementId) {
if (type !== signatureRequestType.signTypedData) {
await driver.clickElement(buttonElementId);
}
await driver.delay(regularDelayMs);
await driver.clickElement({ text: 'Sign', tag: 'button' });
}