1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-28 23:06:37 +01:00
metamask-extension/app/scripts/migrations/090.test.js
Dan J Miller b874a301f5
Capture exception with sentry when invariant conditions are met in migrations (#20427)
* capture exception for sentry when invariant conditions are met in migration 82

* Code cleanup

* Capture exceptions in invariant conditions for migrations 83,84,85,86,89,91,93,94

* Update app/scripts/migrations/082.test.js

Co-authored-by: Mark Stacey <markjstacey@gmail.com>

* Code cleanup

* Fix SentryObject type declaration

* Stop throwing error if preferences controller is undefined

* Refactor 084 and 086 to remove double negative

* Capture exceptions for invariant states in in migrations 87,88,90 and 92

* lint fix

* log warning in migration 82 when preferences controller is undefined

---------

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2023-08-16 16:56:20 -02:30

123 lines
2.8 KiB
JavaScript

import { migrate, version } from './090';
const PREVIOUS_VERSION = version - 1;
const sentryCaptureExceptionMock = jest.fn();
global.sentry = {
captureException: sentryCaptureExceptionMock,
};
describe('migration #90', () => {
afterEach(() => {
jest.resetAllMocks();
});
it('updates the version metadata', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: {},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.meta).toStrictEqual({
version,
});
});
it('does not change the state if the phishing controller state does not exist', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: { test: '123' },
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldStorage.data);
});
it('captures an exception if the phishing controller state is invalid', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: { PhishingController: 'this is not valid' },
};
await migrate(oldStorage);
expect(sentryCaptureExceptionMock).toHaveBeenCalledTimes(1);
expect(sentryCaptureExceptionMock).toHaveBeenCalledWith(
new Error(`typeof state.PhishingController is string`),
);
});
it('does not change the state if the listState property does not exist', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: {
PhishingController: { test: 123 },
},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldStorage.data);
});
it('deletes the "listState" property', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: { PhishingController: { listState: {} } },
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data.PhishingController.listState).toBeUndefined();
});
it('deletes the listState if present', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: { PhishingController: { listState: { test: 123 } } },
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
PhishingController: {},
});
});
it('does not delete the allowlist if present', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: {
PhishingController: {
whitelist: ['foobar.com'],
listState: { test: 123 },
},
},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({
PhishingController: { whitelist: ['foobar.com'] },
});
});
});