Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cherry-pick (#8727) #8766

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions app/store/migrations/030.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import migrate from './030';
import { merge } from 'lodash';
import { captureException } from '@sentry/react-native';
import initialRootState from '../../util/test/initial-root-state';

const expectedState = {
engine: {
backgroundState: {
PreferencesController: {
securityAlertsEnabled: true,
},
},
},
};

jest.mock('@sentry/react-native', () => ({
captureException: jest.fn(),
}));
const mockedCaptureException = jest.mocked(captureException);

describe('Migration #30', () => {
beforeEach(() => {
jest.restoreAllMocks();
jest.resetAllMocks();
});

const invalidStates = [
{
state: null,
errorMessage: "Migration 30: Invalid root state: 'object'",
scenario: 'state is invalid',
},
{
state: merge({}, initialRootState, {
engine: null,
}),
errorMessage: "Migration 30: Invalid root engine state: 'object'",
scenario: 'engine state is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: null,
},
}),
errorMessage:
"Migration 30: Invalid root engine backgroundState: 'object'",
scenario: 'backgroundState is invalid',
},
];

for (const { errorMessage, scenario, state } of invalidStates) {
it(`should capture exception if ${scenario}`, () => {
const newState = migrate(state);

expect(newState).toStrictEqual(state);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
errorMessage,
);
});
}

it('should not change anything if security alert is already enabled', () => {
const oldState = {
engine: {
backgroundState: {
PreferencesController: {
securityAlertsEnabled: true,
},
},
},
};

const migratedState = migrate(oldState);
expect(migratedState).toStrictEqual(expectedState);
});

it('should enable security alert if it is not enabled', () => {
const oldState = {
engine: {
backgroundState: {
PreferencesController: {
securityAlertsEnabled: false,
},
},
},
};
const migratedState = migrate(oldState);
expect(migratedState).toStrictEqual(expectedState);
});
});
54 changes: 54 additions & 0 deletions app/store/migrations/030.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { PreferencesState } from '@metamask/preferences-controller';
import { captureException } from '@sentry/react-native';
import { isObject } from '@metamask/utils';

/**
* Enable security alerts by default.
* @param {any} state - Redux state.
* @returns Migrated Redux state.
*/
export default function migrate(state: unknown) {
if (!isObject(state)) {
captureException(
new Error(`Migration 30: Invalid root state: '${typeof state}'`),
);
return state;
}

if (!isObject(state.engine)) {
captureException(
new Error(
`Migration 30: Invalid root engine state: '${typeof state.engine}'`,
),
);
return state;
}

if (!isObject(state.engine.backgroundState)) {
captureException(
new Error(
`Migration 30: Invalid root engine backgroundState: '${typeof state
.engine.backgroundState}'`,
),
);
return state;
}

const preferencesControllerState = state.engine.backgroundState
.PreferencesController as PreferencesState;

if (!isObject(preferencesControllerState)) {
captureException(
new Error(
`Migration 30: Invalid PreferencesController state: '${typeof preferencesControllerState}'`,
),
);
return state;
}

if (!preferencesControllerState.securityAlertsEnabled) {
preferencesControllerState.securityAlertsEnabled = true;
}

return state;
}
2 changes: 2 additions & 0 deletions app/store/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import migration26 from './026';
import migration27 from './027';
import migration28 from './028';
import migration29 from './029';
import migration30 from './030';

// We do not keep track of the old state
// We create this type for better readability
Expand Down Expand Up @@ -66,6 +67,7 @@ export const migrations: MigrationManifest = {
27: migration27,
28: migration28 as unknown as (state: PersistedState) => PersistedState,
29: migration29 as unknown as (state: OldState) => PersistedState,
30: migration30 as unknown as (state: OldState) => PersistedState,
};

// The latest (i.e. highest) version number.
Expand Down
Loading