From 9eff2412521120fa0a7bc0bbbc5f71bd7e244453 Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Sun, 24 Nov 2024 11:47:23 -0500 Subject: [PATCH] test: Adding unit test for setupPhishingCommunication and setUpCookieHandlerCommunication (#27736) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding unit test for setupPhishingCommunication and setUpCookieHandlerCommunication. ## **Description** [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27736?quickstart=1) ## **Related issues** Fixes: https://github.com/MetaMask/metamask-extension/issues/27119 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- app/scripts/metamask-controller.test.js | 124 ++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/app/scripts/metamask-controller.test.js b/app/scripts/metamask-controller.test.js index 0cd4fba34589..880df69aa00f 100644 --- a/app/scripts/metamask-controller.test.js +++ b/app/scripts/metamask-controller.test.js @@ -45,6 +45,7 @@ import { } from './lib/accounts/BalancesController'; import { BalancesTracker as MultichainBalancesTracker } from './lib/accounts/BalancesTracker'; import { deferredPromise } from './lib/util'; +import { METAMASK_COOKIE_HANDLER } from './constants/stream'; import MetaMaskController, { ONE_KEY_VIA_TREZOR_MINOR_VERSION, } from './metamask-controller'; @@ -1273,6 +1274,129 @@ describe('MetaMaskController', () => { expect(mockKeyring.destroy).toHaveBeenCalledTimes(1); }); }); + describe('#setupPhishingCommunication', () => { + beforeEach(() => { + jest.spyOn(metamaskController, 'safelistPhishingDomain'); + jest.spyOn(metamaskController, 'backToSafetyPhishingWarning'); + metamaskController.preferencesController.setUsePhishDetect(true); + }); + afterEach(() => { + jest.clearAllMocks(); + }); + it('creates a phishing stream with safelistPhishingDomain and backToSafetyPhishingWarning handler', async () => { + const safelistPhishingDomainRequest = { + name: 'metamask-phishing-safelist', + data: { + id: 1, + method: 'safelistPhishingDomain', + params: ['mockHostname'], + }, + }; + const backToSafetyPhishingWarningRequest = { + name: 'metamask-phishing-safelist', + data: { id: 2, method: 'backToSafetyPhishingWarning', params: [] }, + }; + + const { promise, resolve } = deferredPromise(); + const { promise: promiseStream, resolve: resolveStream } = + deferredPromise(); + const streamTest = createThroughStream((chunk, _, cb) => { + if (chunk.name !== 'metamask-phishing-safelist') { + cb(); + return; + } + resolve(); + cb(null, chunk); + }); + + metamaskController.setupPhishingCommunication({ + connectionStream: streamTest, + }); + + streamTest.write(safelistPhishingDomainRequest, null, () => { + expect( + metamaskController.safelistPhishingDomain, + ).toHaveBeenCalledWith('mockHostname'); + }); + streamTest.write(backToSafetyPhishingWarningRequest, null, () => { + expect( + metamaskController.backToSafetyPhishingWarning, + ).toHaveBeenCalled(); + resolveStream(); + }); + + await promise; + streamTest.end(); + await promiseStream; + }); + }); + + describe('#setUpCookieHandlerCommunication', () => { + let localMetaMaskController; + beforeEach(() => { + localMetaMaskController = new MetaMaskController({ + showUserConfirmation: noop, + encryptor: mockEncryptor, + initState: { + ...cloneDeep(firstTimeState), + MetaMetricsController: { + metaMetricsId: 'MOCK_METRICS_ID', + participateInMetaMetrics: true, + dataCollectionForMarketing: true, + }, + }, + initLangCode: 'en_US', + platform: { + showTransactionNotification: () => undefined, + getVersion: () => 'foo', + }, + browser: browserPolyfillMock, + infuraProjectId: 'foo', + isFirstMetaMaskControllerSetup: true, + }); + jest.spyOn(localMetaMaskController, 'getCookieFromMarketingPage'); + }); + afterEach(() => { + jest.clearAllMocks(); + }); + it('creates a cookie handler communication stream with getCookieFromMarketingPage handler', async () => { + const attributionRequest = { + name: METAMASK_COOKIE_HANDLER, + data: { + id: 1, + method: 'getCookieFromMarketingPage', + params: [{ ga_client_id: 'XYZ.ABC' }], + }, + }; + + const { promise, resolve } = deferredPromise(); + const { promise: promiseStream, resolve: resolveStream } = + deferredPromise(); + const streamTest = createThroughStream((chunk, _, cb) => { + if (chunk.name !== METAMASK_COOKIE_HANDLER) { + cb(); + return; + } + resolve(); + cb(null, chunk); + }); + + localMetaMaskController.setUpCookieHandlerCommunication({ + connectionStream: streamTest, + }); + + streamTest.write(attributionRequest, null, () => { + expect( + localMetaMaskController.getCookieFromMarketingPage, + ).toHaveBeenCalledWith({ ga_client_id: 'XYZ.ABC' }); + resolveStream(); + }); + + await promise; + streamTest.end(); + await promiseStream; + }); + }); describe('#setupUntrustedCommunicationEip1193', () => { const mockTxParams = { from: TEST_ADDRESS };