1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

[PM-5272] Migrate CollapsedGroupings to State Provider (#7954)

This commit is contained in:
SmithThe4th
2024-02-16 12:53:24 -05:00
committed by GitHub
parent b0dd64bab4
commit 5b652092cd
16 changed files with 245 additions and 81 deletions

View File

@@ -16,6 +16,7 @@ import { AutofillSettingsKeyMigrator } from "./migrations/18-move-autofill-setti
import { RequirePasswordOnStartMigrator } from "./migrations/19-migrate-require-password-on-start";
import { PrivateKeyMigrator } from "./migrations/20-move-private-key-to-state-providers";
import { CollectionMigrator } from "./migrations/21-move-collections-state-to-state-provider";
import { CollapsedGroupingsMigrator } from "./migrations/22-move-collapsed-groupings-to-state-provider";
import { FixPremiumMigrator } from "./migrations/3-fix-premium";
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
@@ -26,7 +27,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
import { MinVersionMigrator } from "./migrations/min-version";
export const MIN_VERSION = 2;
export const CURRENT_VERSION = 21;
export const CURRENT_VERSION = 22;
export type MinVersion = typeof MIN_VERSION;
export function createMigrationBuilder() {
@@ -50,7 +51,8 @@ export function createMigrationBuilder() {
.with(AutofillSettingsKeyMigrator, 17, 18)
.with(RequirePasswordOnStartMigrator, 18, 19)
.with(PrivateKeyMigrator, 19, 20)
.with(CollectionMigrator, 20, CURRENT_VERSION);
.with(CollectionMigrator, 20, 21)
.with(CollapsedGroupingsMigrator, 21, CURRENT_VERSION);
}
export async function currentVersion(

View File

@@ -0,0 +1,118 @@
import { MockProxy, any } from "jest-mock-extended";
import { MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { CollapsedGroupingsMigrator } from "./22-move-collapsed-groupings-to-state-provider";
function exampleJSON() {
return {
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2", "user-3"],
"user-1": {
settings: {
collapsedGroupings: ["grouping-1", "grouping-2"],
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
settings: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
function rollbackJSON() {
return {
"user_user-1_vaultFilter_collapsedGroupings": ["grouping-1", "grouping-2"],
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2"],
"user-1": {
settings: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
settings: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
describe("CollapsedGroupingsMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: CollapsedGroupingsMigrator;
const keyDefinitionLike = {
key: "collapsedGroupings",
stateDefinition: {
name: "vaultFilter",
},
};
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(exampleJSON(), 21);
sut = new CollapsedGroupingsMigrator(21, 22);
});
it("should remove collapsedGroupings from all accounts", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledWith("user-1", {
settings: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
});
});
it("should set collapsedGroupings values for each account", async () => {
await sut.migrate(helper);
expect(helper.setToUser).toHaveBeenCalledWith("user-1", keyDefinitionLike, [
"grouping-1",
"grouping-2",
]);
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackJSON(), 22);
sut = new CollapsedGroupingsMigrator(21, 22);
});
it.each(["user-1", "user-2"])("should null out new values", async (userId) => {
await sut.rollback(helper);
expect(helper.setToUser).toHaveBeenCalledWith(userId, keyDefinitionLike, null);
});
it("should add explicit value back to accounts", async () => {
await sut.rollback(helper);
expect(helper.set).toHaveBeenCalledTimes(2);
expect(helper.set).toHaveBeenCalledWith("user-1", {
settings: {
collapsedGroupings: ["grouping-1", "grouping-2"],
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
});
});
it("should not try to restore values to missing accounts", async () => {
await sut.rollback(helper);
expect(helper.set).not.toHaveBeenCalledWith("user-3", any());
});
});
});

View File

@@ -0,0 +1,47 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedAccountType = {
settings?: {
collapsedGroupings?: string[];
};
};
const COLLAPSED_GROUPINGS: KeyDefinitionLike = {
key: "collapsedGroupings",
stateDefinition: {
name: "vaultFilter",
},
};
export class CollapsedGroupingsMigrator extends Migrator<21, 22> {
async migrate(helper: MigrationHelper): Promise<void> {
const accounts = await helper.getAccounts<ExpectedAccountType>();
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
const value = account?.settings?.collapsedGroupings;
if (value != null) {
await helper.setToUser(userId, COLLAPSED_GROUPINGS, value);
delete account.settings.collapsedGroupings;
await helper.set(userId, account);
}
}
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
}
async rollback(helper: MigrationHelper): Promise<void> {
const accounts = await helper.getAccounts<ExpectedAccountType>();
async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise<void> {
const value = await helper.getFromUser(userId, COLLAPSED_GROUPINGS);
if (account) {
account.settings = Object.assign(account.settings ?? {}, {
collapsedGroupings: value,
});
await helper.set(userId, account);
}
await helper.setToUser(userId, COLLAPSED_GROUPINGS, null);
}
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
}
}