2023-03-16 16:03:03 +01:00
|
|
|
const { strict: assert } = require('assert');
|
2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
2022-08-09 20:36:32 +02:00
|
|
|
const { promises: fs } = require('fs');
|
2022-01-19 00:08:41 +01:00
|
|
|
const BigNumber = require('bignumber.js');
|
2022-02-16 15:21:41 +01:00
|
|
|
const mockttp = require('mockttp');
|
2021-02-04 19:15:23 +01:00
|
|
|
const createStaticServer = require('../../development/create-static-server');
|
2022-03-15 17:17:48 +01:00
|
|
|
const { setupMocking } = require('./mock-e2e');
|
2021-02-04 19:15:23 +01:00
|
|
|
const Ganache = require('./ganache');
|
|
|
|
const FixtureServer = require('./fixture-server');
|
2022-05-06 00:28:48 +02:00
|
|
|
const PhishingWarningPageServer = require('./phishing-warning-page-server');
|
2021-02-04 19:15:23 +01:00
|
|
|
const { buildWebDriver } = require('./webdriver');
|
2022-12-08 13:58:34 +01:00
|
|
|
const { PAGES } = require('./webdriver/driver');
|
2022-07-21 19:08:01 +02:00
|
|
|
const GanacheSeeder = require('./seeder/ganache-seeder');
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tinyDelayMs = 200;
|
|
|
|
const regularDelayMs = tinyDelayMs * 2;
|
|
|
|
const largeDelayMs = regularDelayMs * 2;
|
2022-03-09 15:38:12 +01:00
|
|
|
const veryLargeDelayMs = largeDelayMs * 2;
|
2022-03-29 17:47:45 +02:00
|
|
|
const dappBasePort = 8080;
|
2020-07-10 05:57:54 +02:00
|
|
|
|
2022-08-09 20:36:32 +02:00
|
|
|
const createDownloadFolder = async (downloadsFolder) => {
|
|
|
|
await fs.rm(downloadsFolder, { recursive: true, force: true });
|
|
|
|
await fs.mkdir(downloadsFolder, { recursive: true });
|
|
|
|
};
|
|
|
|
|
2022-01-19 00:08:41 +01:00
|
|
|
const convertToHexValue = (val) => `0x${new BigNumber(val, 10).toString(16)}`;
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function withFixtures(options, testSuite) {
|
2020-12-01 22:24:56 +01:00
|
|
|
const {
|
|
|
|
dapp,
|
|
|
|
fixtures,
|
|
|
|
ganacheOptions,
|
2022-07-21 19:08:01 +02:00
|
|
|
smartContract,
|
2020-12-01 22:24:56 +01:00
|
|
|
driverOptions,
|
2022-03-29 17:47:45 +02:00
|
|
|
dappOptions,
|
2020-12-01 22:24:56 +01:00
|
|
|
title,
|
2021-04-15 15:58:51 +02:00
|
|
|
failOnConsoleError = true,
|
2021-04-26 17:02:29 +02:00
|
|
|
dappPath = undefined,
|
2022-05-06 00:28:48 +02:00
|
|
|
dappPaths,
|
2022-02-22 17:48:12 +01:00
|
|
|
testSpecificMock = function () {
|
|
|
|
// do nothing.
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = options;
|
|
|
|
const fixtureServer = new FixtureServer();
|
|
|
|
const ganacheServer = new Ganache();
|
2022-03-07 20:23:04 +01:00
|
|
|
const https = await mockttp.generateCACertificate();
|
2022-03-15 17:17:48 +01:00
|
|
|
const mockServer = mockttp.getLocal({ https, cors: true });
|
2021-06-04 15:52:07 +02:00
|
|
|
let secondaryGanacheServer;
|
2022-03-29 17:47:45 +02:00
|
|
|
let numberOfDapps = dapp ? 1 : 0;
|
|
|
|
const dappServer = [];
|
2022-05-06 00:28:48 +02:00
|
|
|
const phishingPageServer = new PhishingWarningPageServer();
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let webDriver;
|
2022-11-14 15:35:08 +01:00
|
|
|
let driver;
|
2021-06-16 17:12:20 +02:00
|
|
|
let failed = false;
|
2020-01-20 19:50:25 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await ganacheServer.start(ganacheOptions);
|
2022-07-21 19:08:01 +02:00
|
|
|
let contractRegistry;
|
|
|
|
|
|
|
|
if (smartContract) {
|
2022-08-09 15:59:20 +02:00
|
|
|
const ganacheSeeder = new GanacheSeeder(ganacheServer.getProvider());
|
2022-07-21 19:08:01 +02:00
|
|
|
await ganacheSeeder.deploySmartContract(smartContract);
|
|
|
|
contractRegistry = ganacheSeeder.getContractRegistry();
|
|
|
|
}
|
|
|
|
|
2021-06-04 15:52:07 +02:00
|
|
|
if (ganacheOptions?.concurrent) {
|
2023-01-31 13:58:39 +01:00
|
|
|
const { port, chainId, ganacheOptions2 } = ganacheOptions.concurrent;
|
2021-06-04 15:52:07 +02:00
|
|
|
secondaryGanacheServer = new Ganache();
|
|
|
|
await secondaryGanacheServer.start({
|
|
|
|
blockTime: 2,
|
2022-01-19 00:08:41 +01:00
|
|
|
chain: { chainId },
|
2021-06-04 15:52:07 +02:00
|
|
|
port,
|
|
|
|
vmErrorsOnRPCResponse: false,
|
2023-01-31 13:58:39 +01:00
|
|
|
...ganacheOptions2,
|
2021-06-04 15:52:07 +02:00
|
|
|
});
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
await fixtureServer.start();
|
2023-03-13 15:31:14 +01:00
|
|
|
fixtureServer.loadJsonState(fixtures, contractRegistry);
|
2022-05-06 00:28:48 +02:00
|
|
|
await phishingPageServer.start();
|
2020-07-10 05:57:54 +02:00
|
|
|
if (dapp) {
|
2022-03-29 17:47:45 +02:00
|
|
|
if (dappOptions?.numberOfDapps) {
|
|
|
|
numberOfDapps = dappOptions.numberOfDapps;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < numberOfDapps; i++) {
|
|
|
|
let dappDirectory;
|
2022-05-06 00:28:48 +02:00
|
|
|
if (dappPath || (dappPaths && dappPaths[i])) {
|
|
|
|
dappDirectory = path.resolve(__dirname, dappPath || dappPaths[i]);
|
2022-03-29 17:47:45 +02:00
|
|
|
} else {
|
|
|
|
dappDirectory = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'node_modules',
|
|
|
|
'@metamask',
|
|
|
|
'test-dapp',
|
|
|
|
'dist',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
dappServer.push(createStaticServer(dappDirectory));
|
|
|
|
dappServer[i].listen(`${dappBasePort + i}`);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer[i].on('listening', resolve);
|
|
|
|
dappServer[i].on('error', reject);
|
|
|
|
});
|
2021-04-26 17:02:29 +02:00
|
|
|
}
|
2020-07-10 05:57:54 +02:00
|
|
|
}
|
2023-04-19 16:36:23 +02:00
|
|
|
const mockedEndpoint = await setupMocking(mockServer, testSpecificMock);
|
2022-02-16 15:21:41 +01:00
|
|
|
await mockServer.start(8000);
|
2023-05-02 17:10:53 +02:00
|
|
|
|
2022-11-14 15:35:08 +01:00
|
|
|
driver = (await buildWebDriver(driverOptions)).driver;
|
|
|
|
webDriver = driver.driver;
|
|
|
|
|
|
|
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
2023-02-23 15:27:36 +01:00
|
|
|
await driver.checkBrowserForExceptions(failOnConsoleError);
|
|
|
|
await driver.checkBrowserForConsoleErrors(failOnConsoleError);
|
2022-11-14 15:35:08 +01:00
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2022-12-13 20:30:47 +01:00
|
|
|
let driverProxy;
|
|
|
|
if (process.env.E2E_DEBUG === 'true') {
|
|
|
|
driverProxy = new Proxy(driver, {
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
const originalProperty = target[prop];
|
|
|
|
if (typeof originalProperty === 'function') {
|
|
|
|
return (...args) => {
|
|
|
|
console.log(
|
|
|
|
`[driver] Called '${prop}' with arguments ${JSON.stringify(
|
|
|
|
args,
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
return originalProperty.bind(target)(...args);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-21 20:24:51 +02:00
|
|
|
await testSuite({
|
2022-12-13 20:30:47 +01:00
|
|
|
driver: driverProxy ?? driver,
|
2022-07-21 19:08:01 +02:00
|
|
|
contractRegistry,
|
2023-03-20 10:29:18 +01:00
|
|
|
ganacheServer,
|
|
|
|
secondaryGanacheServer,
|
2023-04-19 16:36:23 +02:00
|
|
|
mockedEndpoint,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-07-09 17:42:56 +02:00
|
|
|
} catch (error) {
|
2021-06-16 17:12:20 +02:00
|
|
|
failed = true;
|
2020-07-22 20:25:04 +02:00
|
|
|
if (webDriver) {
|
2020-12-01 22:24:56 +01:00
|
|
|
try {
|
2022-11-14 15:35:08 +01:00
|
|
|
await driver.verboseReportOnFailure(title);
|
2020-12-01 22:24:56 +01:00
|
|
|
} catch (verboseReportError) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(verboseReportError);
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2023-02-23 15:27:36 +01:00
|
|
|
if (driver.errors.length > 0 || driver.exceptions.length > 0) {
|
2022-12-08 13:58:34 +01:00
|
|
|
/**
|
|
|
|
* Navigate to the background
|
|
|
|
* forcing background exceptions to be captured
|
|
|
|
* proving more helpful context
|
|
|
|
*/
|
|
|
|
await driver.navigate(PAGES.BACKGROUND);
|
2022-11-14 15:35:08 +01:00
|
|
|
}
|
2020-07-22 20:25:04 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
throw error;
|
2020-01-20 19:50:25 +01:00
|
|
|
} finally {
|
2021-06-16 17:12:20 +02:00
|
|
|
if (!failed || process.env.E2E_LEAVE_RUNNING !== 'true') {
|
|
|
|
await fixtureServer.stop();
|
|
|
|
await ganacheServer.quit();
|
|
|
|
if (ganacheOptions?.concurrent) {
|
|
|
|
await secondaryGanacheServer.quit();
|
|
|
|
}
|
|
|
|
if (webDriver) {
|
2022-11-14 15:35:08 +01:00
|
|
|
await driver.quit();
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
2022-03-29 17:47:45 +02:00
|
|
|
if (dapp) {
|
|
|
|
for (let i = 0; i < numberOfDapps; i++) {
|
|
|
|
if (dappServer[i] && dappServer[i].listening) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer[i].close((error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
2022-05-06 00:28:48 +02:00
|
|
|
if (phishingPageServer.isRunning()) {
|
|
|
|
await phishingPageServer.quit();
|
|
|
|
}
|
2022-03-07 20:23:04 +01:00
|
|
|
await mockServer.stop();
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:34:43 +01:00
|
|
|
/**
|
|
|
|
* @param {*} driver - selinium driver
|
|
|
|
* @param {*} handlesCount - total count of windows that should be loaded
|
|
|
|
* @returns handles - an object with window handles, properties in object represent windows:
|
|
|
|
* 1. extension: metamask extension window
|
|
|
|
* 2. dapp: test-app window
|
|
|
|
* 3. popup: metsmask extension popup window
|
|
|
|
*/
|
|
|
|
const getWindowHandles = async (driver, handlesCount) => {
|
|
|
|
await driver.waitUntilXWindowHandles(handlesCount);
|
|
|
|
const windowHandles = await driver.getAllWindowHandles();
|
|
|
|
|
|
|
|
const extension = windowHandles[0];
|
|
|
|
const dapp = await driver.switchToWindowWithTitle(
|
|
|
|
'E2E Test Dapp',
|
|
|
|
windowHandles,
|
|
|
|
);
|
|
|
|
const popup = windowHandles.find(
|
|
|
|
(handle) => handle !== extension && handle !== dapp,
|
|
|
|
);
|
|
|
|
return { extension, dapp, popup };
|
|
|
|
};
|
|
|
|
|
2023-01-31 13:58:39 +01:00
|
|
|
const importSRPOnboardingFlow = async (driver, seedPhrase, password) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-06-08 15:21:21 +02:00
|
|
|
await driver.waitForSelector('.import-srp__actions');
|
2023-01-13 17:25:01 +01:00
|
|
|
// import with recovery phrase
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
'[data-testid="import-srp__srp-word-0"]',
|
|
|
|
seedPhrase,
|
|
|
|
);
|
|
|
|
await driver.clickElement('[data-testid="import-srp-confirm"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// create password
|
|
|
|
await driver.fill('[data-testid="create-password-new"]', password);
|
|
|
|
await driver.fill('[data-testid="create-password-confirm"]', password);
|
|
|
|
await driver.clickElement('[data-testid="create-password-terms"]');
|
|
|
|
await driver.clickElement('[data-testid="create-password-import"]');
|
2023-01-31 13:58:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const completeImportSRPOnboardingFlow = async (
|
|
|
|
driver,
|
|
|
|
seedPhrase,
|
|
|
|
password,
|
|
|
|
) => {
|
|
|
|
await importSRPOnboardingFlow(driver, seedPhrase, password);
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// complete
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
};
|
|
|
|
|
2022-05-09 14:46:20 +02:00
|
|
|
const completeImportSRPOnboardingFlowWordByWord = async (
|
|
|
|
driver,
|
|
|
|
seedPhrase,
|
|
|
|
password,
|
|
|
|
) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
2022-07-22 22:39:48 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// import with recovery phrase, word by word
|
2022-05-09 14:46:20 +02:00
|
|
|
const words = seedPhrase.split(' ');
|
|
|
|
for (const word of words) {
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
`[data-testid="import-srp__srp-word-${words.indexOf(word)}"]`,
|
|
|
|
word,
|
|
|
|
);
|
|
|
|
}
|
2023-01-13 17:25:01 +01:00
|
|
|
await driver.clickElement('[data-testid="import-srp-confirm"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// create password
|
|
|
|
await driver.fill('[data-testid="create-password-new"]', password);
|
|
|
|
await driver.fill('[data-testid="create-password-confirm"]', password);
|
|
|
|
await driver.clickElement('[data-testid="create-password-terms"]');
|
|
|
|
await driver.clickElement('[data-testid="create-password-import"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// complete
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
};
|
|
|
|
|
2023-03-16 16:03:03 +01:00
|
|
|
const completeCreateNewWalletOnboardingFlow = async (driver, password) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-03-16 16:03:03 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-create-wallet"]');
|
|
|
|
|
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
|
|
|
|
|
|
|
// create password
|
|
|
|
await driver.fill('[data-testid="create-password-new"]', password);
|
|
|
|
await driver.fill('[data-testid="create-password-confirm"]', password);
|
|
|
|
await driver.clickElement('[data-testid="create-password-terms"]');
|
|
|
|
await driver.clickElement('[data-testid="create-password-wallet"]');
|
|
|
|
|
|
|
|
// secure my wallet
|
|
|
|
await driver.clickElement('[data-testid="secure-wallet-recommended"]');
|
|
|
|
|
|
|
|
// reveal SRP
|
|
|
|
await driver.clickElement('[data-testid="recovery-phrase-reveal"]');
|
|
|
|
|
|
|
|
const revealedSeedPhrase = await driver.findElement(
|
|
|
|
'[data-testid="recovery-phrase-chips"]',
|
|
|
|
);
|
|
|
|
|
|
|
|
const recoveryPhrase = await revealedSeedPhrase.getText();
|
|
|
|
|
|
|
|
await driver.clickElement('[data-testid="recovery-phrase-next"]');
|
|
|
|
|
|
|
|
// confirm SRP
|
|
|
|
const words = recoveryPhrase.split(/\s*(?:[0-9)]+|\n|\.|^$|$)\s*/u);
|
|
|
|
const finalWords = words.filter((str) => str !== '');
|
|
|
|
assert.equal(finalWords.length, 12);
|
|
|
|
|
|
|
|
await driver.fill('[data-testid="recovery-phrase-input-2"]', finalWords[2]);
|
|
|
|
await driver.fill('[data-testid="recovery-phrase-input-3"]', finalWords[3]);
|
|
|
|
await driver.fill('[data-testid="recovery-phrase-input-7"]', finalWords[7]);
|
|
|
|
|
|
|
|
await driver.clickElement('[data-testid="confirm-recovery-phrase"]');
|
|
|
|
|
|
|
|
await driver.clickElement({ text: 'Confirm', tag: 'button' });
|
|
|
|
|
|
|
|
// complete
|
|
|
|
await driver.findElement({ text: 'Wallet creation successful', tag: 'h2' });
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
|
|
|
|
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
|
|
|
};
|
|
|
|
|
|
|
|
const importWrongSRPOnboardingFlow = async (driver, seedPhrase) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-03-16 16:03:03 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
|
|
|
|
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
|
|
|
|
|
|
|
// import with recovery phrase
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
'[data-testid="import-srp__srp-word-0"]',
|
|
|
|
seedPhrase,
|
|
|
|
);
|
|
|
|
|
|
|
|
const warningText = 'Invalid Secret Recovery Phrase';
|
|
|
|
const warnings = await driver.findElements('.actionable-message__message');
|
|
|
|
const warning = warnings[1];
|
|
|
|
|
|
|
|
assert.equal(await warning.getText(), warningText);
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectDropdownByNum = async (elements, index) => {
|
|
|
|
await elements[index].click();
|
|
|
|
};
|
|
|
|
|
|
|
|
const testSRPDropdownIterations = async (options, driver, iterations) => {
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
|
|
|
await selectDropdownByNum(options, i);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
|
|
|
|
const formFields = await driver.findElements('.import-srp__srp-word-label');
|
|
|
|
const expectedNumFields = 12 + i * 3;
|
|
|
|
const actualNumFields = formFields.length;
|
|
|
|
assert.equal(actualNumFields, expectedNumFields);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-05 15:56:08 +02:00
|
|
|
const DAPP_URL = 'http://127.0.0.1:8080';
|
|
|
|
const DAPP_ONE_URL = 'http://127.0.0.1:8081';
|
|
|
|
|
|
|
|
const openDapp = async (driver, contract = null, dappURL = DAPP_URL) => {
|
|
|
|
contract
|
|
|
|
? await driver.openNewPage(`${dappURL}/?contract=${contract}`)
|
|
|
|
: await driver.openNewPage(dappURL);
|
|
|
|
};
|
2023-05-19 12:17:53 +02:00
|
|
|
const STALELIST_URL =
|
|
|
|
'https://static.metafi.codefi.network/api/v1/lists/stalelist.json';
|
|
|
|
|
|
|
|
const emptyHtmlPage = `<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>title</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Empty page
|
|
|
|
</body>
|
|
|
|
</html>`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup fetch mocks for the phishing detection feature.
|
|
|
|
*
|
|
|
|
* The mock configuration will show that "127.0.0.1" is blocked. The dynamic lookup on the warning
|
|
|
|
* page can be customized, so that we can test both the MetaMask and PhishFort block cases.
|
|
|
|
*
|
|
|
|
* @param {import('mockttp').Mockttp} mockServer - The mock server.
|
|
|
|
* @param {object} metamaskPhishingConfigResponse - The response for the dynamic phishing
|
|
|
|
* configuration lookup performed by the warning page.
|
|
|
|
*/
|
|
|
|
async function setupPhishingDetectionMocks(
|
|
|
|
mockServer,
|
|
|
|
metamaskPhishingConfigResponse,
|
|
|
|
) {
|
|
|
|
await mockServer.forGet(STALELIST_URL).thenCallback(() => {
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
|
|
|
json: {
|
|
|
|
version: 2,
|
|
|
|
tolerance: 2,
|
|
|
|
fuzzylist: [],
|
|
|
|
allowlist: [],
|
|
|
|
blocklist: ['127.0.0.1'],
|
|
|
|
lastUpdated: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
await mockServer
|
|
|
|
.forGet('https://github.com/MetaMask/eth-phishing-detect/issues/new')
|
|
|
|
.thenCallback(() => {
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
|
|
|
body: emptyHtmlPage,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
await mockServer
|
|
|
|
.forGet('https://github.com/phishfort/phishfort-lists/issues/new')
|
|
|
|
.thenCallback(() => {
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
|
|
|
body: emptyHtmlPage,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
await mockServer
|
|
|
|
.forGet(
|
|
|
|
'https://raw.githubusercontent.com/MetaMask/eth-phishing-detect/master/src/config.json',
|
|
|
|
)
|
|
|
|
.thenCallback(() => metamaskPhishingConfigResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockPhishingDetection(mockServer) {
|
|
|
|
setupPhishingDetectionMocks(mockServer, {
|
|
|
|
statusCode: 200,
|
|
|
|
json: {
|
|
|
|
version: 2,
|
|
|
|
tolerance: 2,
|
|
|
|
fuzzylist: [],
|
|
|
|
whitelist: [],
|
|
|
|
blacklist: ['127.0.0.1'],
|
|
|
|
lastUpdated: 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const PRIVATE_KEY =
|
|
|
|
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC';
|
|
|
|
|
|
|
|
const generateETHBalance = (eth) => convertToHexValue(eth * 10 ** 18);
|
|
|
|
const defaultGanacheOptions = {
|
|
|
|
accounts: [{ secretKey: PRIVATE_KEY, balance: generateETHBalance(25) }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const SERVICE_WORKER_URL = 'chrome://inspect/#service-workers';
|
2023-05-05 15:56:08 +02:00
|
|
|
|
2023-05-24 19:50:31 +02:00
|
|
|
const sendTransaction = async (driver, recipientAddress, quantity) => {
|
|
|
|
await driver.clickElement('[data-testid="eth-overview-send"]');
|
|
|
|
await driver.fill('[data-testid="ens-input"]', recipientAddress);
|
|
|
|
await driver.fill('.unit-input__input', quantity);
|
|
|
|
await driver.clickElement('[data-testid="page-container-footer-next"]');
|
|
|
|
await driver.clickElement('[data-testid="page-container-footer-next"]');
|
|
|
|
await driver.clickElement('[data-testid="home__activity-tab"]');
|
2023-06-08 15:21:21 +02:00
|
|
|
await driver.waitForElementNotPresent('.transaction-list-item--unconfirmed');
|
2023-05-24 19:50:31 +02:00
|
|
|
await driver.findElement('.transaction-list-item');
|
|
|
|
};
|
|
|
|
|
|
|
|
const findAnotherAccountFromAccountList = async (
|
|
|
|
driver,
|
|
|
|
itemNumber,
|
|
|
|
accountName,
|
|
|
|
) => {
|
2023-06-01 23:14:38 +02:00
|
|
|
await driver.clickElement('[data-testid="account-menu-icon"]');
|
|
|
|
const accountMenuItemSelector = `.multichain-account-list-item:nth-child(${itemNumber})`;
|
2023-06-20 15:45:40 +02:00
|
|
|
|
|
|
|
await driver.findElement({
|
|
|
|
css: `${accountMenuItemSelector} .multichain-account-list-item__account-name__button`,
|
|
|
|
text: accountName,
|
|
|
|
});
|
|
|
|
|
2023-05-24 19:50:31 +02:00
|
|
|
return accountMenuItemSelector;
|
|
|
|
};
|
|
|
|
|
2023-05-26 15:05:34 +02:00
|
|
|
const TEST_SEED_PHRASE =
|
|
|
|
'forum vessel pink push lonely enact gentle tail admit parrot grunt dress';
|
|
|
|
|
|
|
|
const TEST_SEED_PHRASE_TWO =
|
|
|
|
'phrase upgrade clock rough situate wedding elder clever doctor stamp excess tent';
|
|
|
|
|
2023-05-30 14:44:56 +02:00
|
|
|
// Usually happens when onboarded to make sure the state is retrieved from metamaskState properly
|
|
|
|
const assertAccountBalanceForDOM = async (driver, ganacheServer) => {
|
|
|
|
const balance = await ganacheServer.getBalance();
|
|
|
|
const balanceElement = await driver.findElement(
|
|
|
|
'[data-testid="eth-overview__primary-currency"]',
|
|
|
|
);
|
|
|
|
assert.equal(`${balance}\nETH`, await balanceElement.getText());
|
|
|
|
};
|
|
|
|
|
|
|
|
// Usually happens after txn is made
|
|
|
|
const locateAccountBalanceDOM = async (driver, ganacheServer) => {
|
|
|
|
const balance = await ganacheServer.getBalance();
|
|
|
|
await driver.waitForSelector({
|
|
|
|
css: '[data-testid="eth-overview__primary-currency"]',
|
|
|
|
text: `${balance} ETH`,
|
|
|
|
});
|
|
|
|
};
|
2023-06-20 12:17:08 +02:00
|
|
|
const DEFAULT_PRIVATE_KEY =
|
|
|
|
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC';
|
|
|
|
const WALLET_PASSWORD = 'correct horse battery staple';
|
2023-05-30 14:44:56 +02:00
|
|
|
|
2023-06-20 12:17:08 +02:00
|
|
|
const DEFAULT_GANACHE_OPTIONS = {
|
|
|
|
accounts: [
|
|
|
|
{
|
|
|
|
secretKey: DEFAULT_PRIVATE_KEY,
|
|
|
|
balance: generateETHBalance(25),
|
|
|
|
},
|
|
|
|
],
|
2023-06-08 15:21:21 +02:00
|
|
|
};
|
|
|
|
|
2023-06-20 12:17:08 +02:00
|
|
|
const generateGanacheOptions = (overrides) => ({
|
|
|
|
...DEFAULT_GANACHE_OPTIONS,
|
|
|
|
...overrides,
|
|
|
|
});
|
|
|
|
|
2023-06-08 15:21:21 +02:00
|
|
|
async function waitForAccountRendered(driver) {
|
|
|
|
await driver.waitForSelector(
|
|
|
|
'[data-testid="eth-overview__primary-currency"]',
|
|
|
|
);
|
|
|
|
}
|
2023-06-20 12:17:08 +02:00
|
|
|
const WINDOW_TITLES = Object.freeze({
|
|
|
|
ExtensionInFullScreenView: 'MetaMask',
|
|
|
|
TestDApp: 'E2E Test Dapp',
|
|
|
|
Notification: 'MetaMask Notification',
|
|
|
|
ServiceWorkerSettings: 'Inspect with Chrome Developer Tools',
|
|
|
|
InstalledExtensions: 'Extensions',
|
|
|
|
});
|
|
|
|
|
|
|
|
const unlockWallet = async (driver) => {
|
2023-06-09 16:01:12 +02:00
|
|
|
await driver.fill('#password', 'correct horse battery staple');
|
|
|
|
await driver.press('#password', driver.Key.ENTER);
|
2023-06-15 14:13:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const logInWithBalanceValidation = async (driver, ganacheServer) => {
|
2023-06-20 12:17:08 +02:00
|
|
|
await unlockWallet(driver);
|
2023-06-09 16:01:12 +02:00
|
|
|
await assertAccountBalanceForDOM(driver, ganacheServer);
|
|
|
|
};
|
|
|
|
|
2023-06-20 12:17:08 +02:00
|
|
|
function roundToXDecimalPlaces(number, decimalPlaces) {
|
|
|
|
return Math.round(number * 10 ** decimalPlaces) / 10 ** decimalPlaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateRandNumBetween(x, y) {
|
|
|
|
const min = Math.min(x, y);
|
|
|
|
const max = Math.max(x, y);
|
|
|
|
const randomNumber = Math.random() * (max - min) + min;
|
|
|
|
|
|
|
|
return randomNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function switchToWindow(driver, windowTitle) {
|
|
|
|
const windowHandles = await driver.getAllWindowHandles();
|
|
|
|
|
|
|
|
return await driver.switchToWindowWithTitle(windowTitle, windowHandles);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sleepSeconds(sec) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, sec * 1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function terminateServiceWorker(driver) {
|
|
|
|
await driver.openNewPage(SERVICE_WORKER_URL);
|
|
|
|
|
|
|
|
await driver.waitForSelector({
|
|
|
|
text: 'Service workers',
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
await driver.clickElement({
|
|
|
|
text: 'Service workers',
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
|
|
|
|
const serviceWorkerElements = await driver.findElements({
|
|
|
|
text: 'terminate',
|
|
|
|
tag: 'span',
|
|
|
|
});
|
|
|
|
|
|
|
|
// 1st one is app-init.js; while 2nd one is service-worker.js
|
|
|
|
await serviceWorkerElements[serviceWorkerElements.length - 1].click();
|
|
|
|
|
|
|
|
const serviceWorkerTab = await switchToWindow(
|
|
|
|
driver,
|
|
|
|
WINDOW_TITLES.ServiceWorkerSettings,
|
|
|
|
);
|
|
|
|
|
|
|
|
await driver.closeWindowHandle(serviceWorkerTab);
|
|
|
|
}
|
|
|
|
|
2018-05-25 03:17:26 +02:00
|
|
|
module.exports = {
|
2023-05-05 15:56:08 +02:00
|
|
|
DAPP_URL,
|
|
|
|
DAPP_ONE_URL,
|
2023-05-19 12:17:53 +02:00
|
|
|
SERVICE_WORKER_URL,
|
2023-05-26 15:05:34 +02:00
|
|
|
TEST_SEED_PHRASE,
|
|
|
|
TEST_SEED_PHRASE_TWO,
|
2023-06-08 15:21:21 +02:00
|
|
|
PRIVATE_KEY,
|
2022-01-19 19:34:43 +01:00
|
|
|
getWindowHandles,
|
2022-01-19 00:08:41 +01:00
|
|
|
convertToHexValue,
|
2019-09-07 09:13:58 +02:00
|
|
|
tinyDelayMs,
|
|
|
|
regularDelayMs,
|
|
|
|
largeDelayMs,
|
2022-03-09 15:38:12 +01:00
|
|
|
veryLargeDelayMs,
|
2020-01-20 19:50:25 +01:00
|
|
|
withFixtures,
|
2023-01-31 13:58:39 +01:00
|
|
|
importSRPOnboardingFlow,
|
2022-02-23 18:27:13 +01:00
|
|
|
completeImportSRPOnboardingFlow,
|
2022-05-09 14:46:20 +02:00
|
|
|
completeImportSRPOnboardingFlowWordByWord,
|
2023-03-16 16:03:03 +01:00
|
|
|
completeCreateNewWalletOnboardingFlow,
|
2022-08-09 20:36:32 +02:00
|
|
|
createDownloadFolder,
|
2023-03-16 16:03:03 +01:00
|
|
|
importWrongSRPOnboardingFlow,
|
|
|
|
testSRPDropdownIterations,
|
2023-05-05 15:56:08 +02:00
|
|
|
openDapp,
|
2023-05-19 12:17:53 +02:00
|
|
|
mockPhishingDetection,
|
|
|
|
setupPhishingDetectionMocks,
|
|
|
|
defaultGanacheOptions,
|
2023-05-24 19:50:31 +02:00
|
|
|
sendTransaction,
|
|
|
|
findAnotherAccountFromAccountList,
|
2023-06-20 12:17:08 +02:00
|
|
|
unlockWallet,
|
2023-06-09 16:01:12 +02:00
|
|
|
logInWithBalanceValidation,
|
2023-05-30 14:44:56 +02:00
|
|
|
assertAccountBalanceForDOM,
|
|
|
|
locateAccountBalanceDOM,
|
2023-06-08 15:21:21 +02:00
|
|
|
waitForAccountRendered,
|
2023-06-20 12:17:08 +02:00
|
|
|
generateGanacheOptions,
|
|
|
|
WALLET_PASSWORD,
|
|
|
|
WINDOW_TITLES,
|
|
|
|
DEFAULT_GANACHE_OPTIONS,
|
|
|
|
generateETHBalance,
|
|
|
|
roundToXDecimalPlaces,
|
|
|
|
generateRandNumBetween,
|
|
|
|
switchToWindow,
|
|
|
|
sleepSeconds,
|
|
|
|
terminateServiceWorker,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|