Skip to content

Commit

Permalink
Merge branch 'develop' into fix-change-language-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chloeYue authored Nov 28, 2024
2 parents 05e2c45 + 5430c89 commit 8041302
Show file tree
Hide file tree
Showing 168 changed files with 2,325 additions and 2,369 deletions.

This file was deleted.

30 changes: 17 additions & 13 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ import {
FakeLedgerBridge,
FakeTrezorBridge,
} from '../../test/stub/keyring-bridge';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getCurrentChainId } from '../../ui/selectors';
import { getCurrentChainId } from '../../shared/modules/selectors/networks';
import { addNonceToCsp } from '../../shared/modules/add-nonce-to-csp';
import { checkURLForProviderInjection } from '../../shared/modules/provider-injection';
import migrations from './migrations';
Expand Down Expand Up @@ -286,24 +284,27 @@ function maybeDetectPhishing(theController) {

// Determine the block reason based on the type
let blockReason;
let blockedUrl = hostname;
if (phishingTestResponse?.result && blockedRequestResponse.result) {
blockReason = `${phishingTestResponse.type} and ${blockedRequestResponse.type}`;
} else if (phishingTestResponse?.result) {
blockReason = phishingTestResponse.type;
} else {
blockReason = blockedRequestResponse.type;
blockedUrl = details.initiator;
}

theController.metaMetricsController.trackEvent({
// should we differentiate between background redirection and content script redirection?
event: MetaMetricsEventName.PhishingPageDisplayed,
category: MetaMetricsEventCategory.Phishing,
properties: {
url: hostname,
url: blockedUrl,
referrer: {
url: hostname,
url: blockedUrl,
},
reason: blockReason,
requestDomain: blockedRequestResponse.result ? hostname : undefined,
},
});
const querystring = new URLSearchParams({ hostname, href });
Expand Down Expand Up @@ -1048,11 +1049,6 @@ export function setupController(
updateBadge,
);

controller.controllerMessenger.subscribe(
METAMASK_CONTROLLER_EVENTS.NOTIFICATIONS_STATE_CHANGE,
updateBadge,
);

/**
* Formats a count for display as a badge label.
*
Expand Down Expand Up @@ -1121,8 +1117,14 @@ export function setupController(
controller.notificationServicesController.state;

const snapNotificationCount = Object.values(
controller.notificationController.state.notifications,
).filter((notification) => notification.readDate === null).length;
controller.notificationServicesController.state
.metamaskNotificationsList,
).filter(
(notification) =>
notification.type ===
NotificationServicesController.Constants.TRIGGER_TYPES.SNAP &&
notification.readDate === null,
).length;

const featureAnnouncementCount = isFeatureAnnouncementsEnabled
? controller.notificationServicesController.state.metamaskNotificationsList.filter(
Expand All @@ -1140,7 +1142,9 @@ export function setupController(
!notification.isRead &&
notification.type !==
NotificationServicesController.Constants.TRIGGER_TYPES
.FEATURES_ANNOUNCEMENT,
.FEATURES_ANNOUNCEMENT &&
notification.type !==
NotificationServicesController.Constants.TRIGGER_TYPES.SNAP,
).length
: 0;

Expand Down
3 changes: 0 additions & 3 deletions app/scripts/constants/sentry-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,6 @@ export const SENTRY_BACKGROUND_STATE = {
allNfts: false,
ignoredNfts: false,
},
NotificationController: {
notifications: false,
},
OnboardingController: {
completedOnboarding: true,
firstTimeFlowType: true,
Expand Down
197 changes: 125 additions & 72 deletions app/scripts/controllers/app-metadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import AppMetadataController from './app-metadata';

const EXPECTED_DEFAULT_STATE = {
currentAppVersion: '',
previousAppVersion: '',
previousMigrationVersion: 0,
currentMigrationVersion: 0,
};
import { ControllerMessenger } from '@metamask/base-controller';
import AppMetadataController, {
getDefaultAppMetadataControllerState,
type AppMetadataControllerOptions,
} from './app-metadata';

describe('AppMetadataController', () => {
describe('constructor', () => {
Expand All @@ -16,86 +13,142 @@ describe('AppMetadataController', () => {
previousMigrationVersion: 1,
currentMigrationVersion: 1,
};
const appMetadataController = new AppMetadataController({
state: initState,
currentMigrationVersion: 1,
currentAppVersion: '1',
});
expect(appMetadataController.store.getState()).toStrictEqual(initState);
withController(
{
state: initState,
currentMigrationVersion: 1,
currentAppVersion: '1',
},
({ controller }) => {
expect(controller.state).toStrictEqual(initState);
},
);
});

it('sets default state and does not modify it', async () => {
const appMetadataController = new AppMetadataController({
state: {},
it('sets default state and does not modify it', () => {
withController({ state: {} }, ({ controller }) => {
expect(controller.state).toStrictEqual(
getDefaultAppMetadataControllerState(),
);
});
expect(appMetadataController.store.getState()).toStrictEqual(
EXPECTED_DEFAULT_STATE,
);
});

it('sets default state and does not modify it if options version parameters match respective default values', async () => {
const appMetadataController = new AppMetadataController({
state: {},
currentMigrationVersion: 0,
currentAppVersion: '',
});
expect(appMetadataController.store.getState()).toStrictEqual(
EXPECTED_DEFAULT_STATE,
it('sets default state and does not modify it if options version parameters match respective default values', () => {
withController(
{
state: {},
currentMigrationVersion: 0,
currentAppVersion: '',
},
({ controller }) => {
expect(controller.state).toStrictEqual(
getDefaultAppMetadataControllerState(),
);
},
);
});

it('updates the currentAppVersion state property if options.currentAppVersion does not match the default value', async () => {
const appMetadataController = new AppMetadataController({
state: {},
currentMigrationVersion: 0,
currentAppVersion: '1',
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentAppVersion: '1',
});
it('updates the currentAppVersion state property if options.currentAppVersion does not match the default value', () => {
withController(
{
state: {},
currentMigrationVersion: 0,
currentAppVersion: '1',
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentAppVersion: '1',
});
},
);
});

it('updates the currentAppVersion and previousAppVersion state properties if options.currentAppVersion, currentAppVersion and previousAppVersion are all different', async () => {
const appMetadataController = new AppMetadataController({
state: {
currentAppVersion: '2',
previousAppVersion: '1',
it('updates the currentAppVersion and previousAppVersion state properties if options.currentAppVersion, currentAppVersion and previousAppVersion are all different', () => {
withController(
{
state: {
currentAppVersion: '2',
previousAppVersion: '1',
},
currentAppVersion: '3',
currentMigrationVersion: 0,
},
currentAppVersion: '3',
currentMigrationVersion: 0,
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentAppVersion: '3',
previousAppVersion: '2',
});
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentAppVersion: '3',
previousAppVersion: '2',
});
},
);
});

it('updates the currentMigrationVersion state property if the currentMigrationVersion param does not match the default value', async () => {
const appMetadataController = new AppMetadataController({
state: {},
currentMigrationVersion: 1,
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentMigrationVersion: 1,
});
it('updates the currentMigrationVersion state property if the currentMigrationVersion param does not match the default value', () => {
withController(
{
state: {},
currentMigrationVersion: 1,
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentMigrationVersion: 1,
});
},
);
});

it('updates the currentMigrationVersion and previousMigrationVersion state properties if the currentMigrationVersion param, the currentMigrationVersion state property and the previousMigrationVersion state property are all different', async () => {
const appMetadataController = new AppMetadataController({
state: {
currentMigrationVersion: 2,
previousMigrationVersion: 1,
it('updates the currentMigrationVersion and previousMigrationVersion state properties if the currentMigrationVersion param, the currentMigrationVersion state property and the previousMigrationVersion state property are all different', () => {
withController(
{
state: {
currentMigrationVersion: 2,
previousMigrationVersion: 1,
},
currentMigrationVersion: 3,
},
currentMigrationVersion: 3,
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentMigrationVersion: 3,
previousMigrationVersion: 2,
});
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentMigrationVersion: 3,
previousMigrationVersion: 2,
});
},
);
});
});
});

type WithControllerOptions = Partial<AppMetadataControllerOptions>;

type WithControllerCallback<ReturnValue> = ({
controller,
}: {
controller: AppMetadataController;
}) => ReturnValue;

type WithControllerArgs<ReturnValue> =
| [WithControllerCallback<ReturnValue>]
| [WithControllerOptions, WithControllerCallback<ReturnValue>];

function withController<ReturnValue>(
...args: WithControllerArgs<ReturnValue>
): ReturnValue {
const [options = {}, fn] = args.length === 2 ? args : [{}, args[0]];

const controllerMessenger = new ControllerMessenger<never, never>();

const messenger = controllerMessenger.getRestricted({
name: 'AppMetadataController',
allowedActions: [],
allowedEvents: [],
});

return fn({
controller: new AppMetadataController({
messenger,
...options,
}),
});
}
Loading

0 comments on commit 8041302

Please sign in to comment.