diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index c95a60ce7..81af515e3 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -391,10 +391,100 @@ const openDapp = async (driver, contract = null, dappURL = DAPP_URL) => { ? await driver.openNewPage(`${dappURL}/?contract=${contract}`) : await driver.openNewPage(dappURL); }; +const STALELIST_URL = + 'https://static.metafi.codefi.network/api/v1/lists/stalelist.json'; + +const emptyHtmlPage = ` + + + + title + + + Empty page + +`; + +/** + * 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'; module.exports = { DAPP_URL, DAPP_ONE_URL, + SERVICE_WORKER_URL, getWindowHandles, convertToHexValue, tinyDelayMs, @@ -410,4 +500,7 @@ module.exports = { importWrongSRPOnboardingFlow, testSRPDropdownIterations, openDapp, + mockPhishingDetection, + setupPhishingDetectionMocks, + defaultGanacheOptions, }; diff --git a/test/e2e/mv3/dapp-interactions.spec.js b/test/e2e/mv3/dapp-interactions.spec.js index 71b51f722..96dee4d8a 100644 --- a/test/e2e/mv3/dapp-interactions.spec.js +++ b/test/e2e/mv3/dapp-interactions.spec.js @@ -1,5 +1,10 @@ const { strict: assert } = require('assert'); -const { convertToHexValue, withFixtures, openDapp } = require('../helpers'); +const { + convertToHexValue, + withFixtures, + openDapp, + SERVICE_WORKER_URL, +} = require('../helpers'); const FixtureBuilder = require('../fixture-builder'); describe('MV3 - Dapp interactions', function () { @@ -34,7 +39,7 @@ describe('MV3 - Dapp interactions', function () { await openDapp(driver); // Terminate Service Worker - await driver.openNewPage('chrome://inspect/#service-workers/'); + await driver.openNewPage(SERVICE_WORKER_URL); await driver.clickElement({ text: 'Service workers', tag: 'button', diff --git a/test/e2e/mv3/phishing-warning-sw-restart.spec.js b/test/e2e/mv3/phishing-warning-sw-restart.spec.js new file mode 100644 index 000000000..4557a0e57 --- /dev/null +++ b/test/e2e/mv3/phishing-warning-sw-restart.spec.js @@ -0,0 +1,52 @@ +const { strict: assert } = require('assert'); +const { + withFixtures, + mockPhishingDetection, + SERVICE_WORKER_URL, + openDapp, + defaultGanacheOptions, +} = require('../helpers'); +const FixtureBuilder = require('../fixture-builder'); + +describe('Phishing warning page', function () { + it('should restore the transaction when service worker restarts', async function () { + await withFixtures( + { + dapp: true, + fixtures: new FixtureBuilder().build(), + ganacheOptions: defaultGanacheOptions, + title: this.test.title, + testSpecificMock: mockPhishingDetection, + }, + async ({ driver }) => { + await driver.navigate(); + // log in wallet + await driver.fill('#password', 'correct horse battery staple'); + await driver.press('#password', driver.Key.ENTER); + + // Restart service worker + await driver.openNewPage(SERVICE_WORKER_URL); + + await driver.clickElement({ + text: 'Service workers', + tag: 'button', + }); + + await driver.clickElement({ + text: 'terminate', + tag: 'span', + }); + + // Open the dapp site and extension detect it as phishing warning page + await openDapp(driver); + + await driver.switchToWindowWithTitle('MetaMask Phishing Detection'); + const phishingPageHeader = await driver.findElements({ + text: 'Deceptive site ahead', + tag: 'h1', + }); + assert.ok(phishingPageHeader.length, 1); + }, + ); + }); +}); diff --git a/test/e2e/mv3/service-worker-restart.spec.js b/test/e2e/mv3/service-worker-restart.spec.js index 9a69d0f34..20061834a 100644 --- a/test/e2e/mv3/service-worker-restart.spec.js +++ b/test/e2e/mv3/service-worker-restart.spec.js @@ -1,5 +1,12 @@ const { strict: assert } = require('assert'); -const { convertToHexValue, withFixtures, openDapp } = require('../helpers'); + +const { + convertToHexValue, + withFixtures, + openDapp, + SERVICE_WORKER_URL, + defaultGanacheOptions, +} = require('../helpers'); const FixtureBuilder = require('../fixture-builder'); const { ACTION_QUEUE_METRICS_E2E_TEST, @@ -9,14 +16,6 @@ const { MetaMetricsEventCategory, } = require('../../../shared/constants/metametrics'); -const PRIVATE_KEY = - '0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC'; - -const generateETHBalance = (eth) => convertToHexValue(eth * 10 ** 18); -const defaultGanacheOptions = { - accounts: [{ secretKey: PRIVATE_KEY, balance: generateETHBalance(25) }], -}; - const numberOfSegmentRequests = 3; async function mockSegment(mockServer) { @@ -76,7 +75,7 @@ describe('MV3 - Service worker restart', function () { await driver.clickElement({ text: 'Create', tag: 'button' }); - await driver.openNewPage('chrome://inspect/#service-workers/'); + await driver.openNewPage(SERVICE_WORKER_URL); await driver.clickElement({ text: 'Service workers', tag: 'button', @@ -159,7 +158,7 @@ describe('MV3 - Service worker restart', function () { // Restart service worker in a new window // Because if we stay in the same window we will lose the popup when opening a new tab await driver.switchToNewWindow(); - await driver.openNewURL('chrome://inspect/#service-workers'); + await driver.openNewURL(SERVICE_WORKER_URL); windowHandles = await driver.getAllWindowHandles(); // MM expanded view, Dapp, Notification popup, console and service worker await driver.waitUntilXWindowHandles(5); diff --git a/test/e2e/tests/phishing-detection.spec.js b/test/e2e/tests/phishing-detection.spec.js index 0775a68a7..735d2bfb2 100644 --- a/test/e2e/tests/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-detection.spec.js @@ -1,88 +1,14 @@ const { strict: assert } = require('assert'); -const { convertToHexValue, withFixtures, openDapp } = require('../helpers'); +const { + convertToHexValue, + withFixtures, + openDapp, + setupPhishingDetectionMocks, + mockPhishingDetection, +} = require('../helpers'); const FixtureBuilder = require('../fixture-builder'); -const STALELIST_URL = - 'https://static.metafi.codefi.network/api/v1/lists/stalelist.json'; - -const emptyHtmlPage = ` - - - - title - - - Empty page - -`; - -/** - * 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); -} - describe('Phishing Detection', function () { - function mockPhishingDetection(mockServer) { - setupPhishingDetectionMocks(mockServer, { - statusCode: 200, - json: { - version: 2, - tolerance: 2, - fuzzylist: [], - whitelist: [], - blacklist: ['127.0.0.1'], - lastUpdated: 0, - }, - }); - } - const ganacheOptions = { accounts: [ { diff --git a/test/e2e/webdriver/driver.js b/test/e2e/webdriver/driver.js index ba555177a..cebeae935 100644 --- a/test/e2e/webdriver/driver.js +++ b/test/e2e/webdriver/driver.js @@ -403,6 +403,10 @@ class Driver { throw new Error(`No window with title: ${title}`); } + async closeWindow() { + await this.driver.close(); + } + // Close Alert Popup async closeAlertPopup() { return await this.driver.switchTo().alert().accept();