Skip to content

Commit

Permalink
adding unit test for setupPhishingCommunication and setUpCookieHandle…
Browse files Browse the repository at this point in the history
…rCommunication
  • Loading branch information
NiranjanaBinoy committed Oct 9, 2024
1 parent 420e4a6 commit db813f3
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
import { BalancesTracker as MultichainBalancesTracker } from './lib/accounts/BalancesTracker';
import { deferredPromise } from './lib/util';
import MetaMaskController from './metamask-controller';
import { METAMASK_COOKIE_HANDLER } from './constants/stream';

const { Ganache } = require('../../test/e2e/seeder/ganache');

Expand All @@ -61,6 +62,7 @@ const browserPolyfillMock = {
addListener: jest.fn(),
},
getPlatformInfo: jest.fn().mockResolvedValue('mac'),
setUninstallURL: () => undefined,
},
storage: {
session: {
Expand Down Expand Up @@ -1222,6 +1224,116 @@ describe('MetaMaskController', () => {
expect(mockKeyring.destroy).toHaveBeenCalledTimes(1);
});
});
describe('#setupPhishingCommunication', () => {
beforeEach(() => {
jest.spyOn(metamaskController, 'safelistPhishingDomain');
jest.spyOn(metamaskController, 'backToSafetyPhishingWarning');
metamaskController.preferencesController.setUsePhishDetect(true);
});

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 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();
});

await promise;
streamTest.end();
});
});

describe('#setUpCookieHandlerCommunication', () => {
const 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,
});
beforeEach(() => {
jest.spyOn(localMetaMaskController, 'getCookieFromMarketingPage');
});

it('creates a cookie handler communication stream with getCookieFromMarketingPage handler', async () => {
const attrinutionRequest = {
name: METAMASK_COOKIE_HANDLER,
data: {
id: 1,
method: 'getCookieFromMarketingPage',
params: [{ ga_client_id: 'XYZ.ABC' }],
},
};

const { promise, resolve } = deferredPromise();
const streamTest = createThroughStream((chunk, _, cb) => {
if (chunk.name !== METAMASK_COOKIE_HANDLER) {
cb();
return;
}
resolve();
cb(null, chunk);
});

localMetaMaskController.setUpCookieHandlerCommunication({
connectionStream: streamTest,
});

streamTest.write(attrinutionRequest, null, () => {
expect(
localMetaMaskController.getCookieFromMarketingPage,
).toHaveBeenCalledWith({ ga_client_id: 'XYZ.ABC' });
});

await promise;
streamTest.end();
});
});

describe('#setupUntrustedCommunicationEip1193', () => {
const mockTxParams = { from: TEST_ADDRESS };
Expand Down

0 comments on commit db813f3

Please sign in to comment.