mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00:00
[PM-5433] Migrate Showcards and Showidentities on current tab to state provider (#8252)
* added showCards and Identities to vault settings and then added migration file * added migration file and removed fields from domain * fixed merge conflicts
This commit is contained in:
@@ -31,6 +31,7 @@ import { PreferredLanguageMigrator } from "./migrations/32-move-preferred-langua
|
||||
import { AppIdMigrator } from "./migrations/33-move-app-id-to-state-providers";
|
||||
import { DomainSettingsMigrator } from "./migrations/34-move-domain-settings-to-state-providers";
|
||||
import { MoveThemeToStateProviderMigrator } from "./migrations/35-move-theme-to-state-providers";
|
||||
import { VaultSettingsKeyMigrator } from "./migrations/36-move-show-card-and-identity-to-state-provider";
|
||||
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
|
||||
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
|
||||
import { RemoveLegacyEtmKeyMigrator } from "./migrations/6-remove-legacy-etm-key";
|
||||
@@ -40,7 +41,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
|
||||
import { MinVersionMigrator } from "./migrations/min-version";
|
||||
|
||||
export const MIN_VERSION = 2;
|
||||
export const CURRENT_VERSION = 35;
|
||||
export const CURRENT_VERSION = 36;
|
||||
export type MinVersion = typeof MIN_VERSION;
|
||||
|
||||
export function createMigrationBuilder() {
|
||||
@@ -78,7 +79,8 @@ export function createMigrationBuilder() {
|
||||
.with(PreferredLanguageMigrator, 31, 32)
|
||||
.with(AppIdMigrator, 32, 33)
|
||||
.with(DomainSettingsMigrator, 33, 34)
|
||||
.with(MoveThemeToStateProviderMigrator, 34, CURRENT_VERSION);
|
||||
.with(MoveThemeToStateProviderMigrator, 34, 35)
|
||||
.with(VaultSettingsKeyMigrator, 35, CURRENT_VERSION);
|
||||
}
|
||||
|
||||
export async function currentVersion(
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
import { MockProxy, any } from "jest-mock-extended";
|
||||
|
||||
import { MigrationHelper, StateDefinitionLike } from "../migration-helper";
|
||||
import { mockMigrationHelper } from "../migration-helper.spec";
|
||||
|
||||
import { VaultSettingsKeyMigrator } from "./36-move-show-card-and-identity-to-state-provider";
|
||||
|
||||
function exampleJSON() {
|
||||
return {
|
||||
global: {
|
||||
otherStuff: "otherStuff1",
|
||||
},
|
||||
authenticatedAccounts: ["user-1", "user-2", "user-3"],
|
||||
"user-1": {
|
||||
settings: {
|
||||
dontShowCardsCurrentTab: true,
|
||||
dontShowIdentitiesCurrentTab: true,
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
},
|
||||
"user-2": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function rollbackJSON() {
|
||||
return {
|
||||
"user_user-1_vaultSettings_showCardsCurrentTab": true,
|
||||
"user_user-1_vaultSettings_showIdentitiesCurrentTab": true,
|
||||
global: {
|
||||
otherStuff: "otherStuff1",
|
||||
},
|
||||
authenticatedAccounts: ["user-1", "user-2", "user-3"],
|
||||
"user-1": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
},
|
||||
"user-2": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const vaultSettingsStateDefinition: {
|
||||
stateDefinition: StateDefinitionLike;
|
||||
} = {
|
||||
stateDefinition: {
|
||||
name: "vaultSettings",
|
||||
},
|
||||
};
|
||||
|
||||
describe("VaultSettingsKeyMigrator", () => {
|
||||
let helper: MockProxy<MigrationHelper>;
|
||||
let sut: VaultSettingsKeyMigrator;
|
||||
|
||||
describe("migrate", () => {
|
||||
beforeEach(() => {
|
||||
helper = mockMigrationHelper(exampleJSON(), 35);
|
||||
sut = new VaultSettingsKeyMigrator(35, 36);
|
||||
});
|
||||
|
||||
it("should remove dontShowCardsCurrentTab and dontShowIdentitiesCurrentTab from all accounts", async () => {
|
||||
await sut.migrate(helper);
|
||||
expect(helper.set).toHaveBeenCalledTimes(1);
|
||||
expect(helper.set).toHaveBeenCalledWith("user-1", {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
});
|
||||
});
|
||||
|
||||
it("should set showCardsCurrentTab and showIdentitiesCurrentTab values for each account", async () => {
|
||||
await sut.migrate(helper);
|
||||
|
||||
expect(helper.setToUser).toHaveBeenCalledTimes(2);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...vaultSettingsStateDefinition, key: "showCardsCurrentTab" },
|
||||
true,
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...vaultSettingsStateDefinition, key: "showIdentitiesCurrentTab" },
|
||||
true,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("rollback", () => {
|
||||
beforeEach(() => {
|
||||
helper = mockMigrationHelper(rollbackJSON(), 36);
|
||||
sut = new VaultSettingsKeyMigrator(35, 36);
|
||||
});
|
||||
|
||||
it("should null out new values for each account", async () => {
|
||||
await sut.rollback(helper);
|
||||
|
||||
expect(helper.setToUser).toHaveBeenCalledTimes(2);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...vaultSettingsStateDefinition, key: "showCardsCurrentTab" },
|
||||
null,
|
||||
);
|
||||
expect(helper.setToUser).toHaveBeenCalledWith(
|
||||
"user-1",
|
||||
{ ...vaultSettingsStateDefinition, key: "showIdentitiesCurrentTab" },
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
it("should add explicit value back to accounts", async () => {
|
||||
await sut.rollback(helper);
|
||||
|
||||
expect(helper.set).toHaveBeenCalledTimes(1);
|
||||
expect(helper.set).toHaveBeenCalledWith("user-1", {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
dontShowCardsCurrentTab: false,
|
||||
dontShowIdentitiesCurrentTab: false,
|
||||
},
|
||||
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());
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,105 @@
|
||||
import { MigrationHelper, StateDefinitionLike } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
type ExpectedAccountState = {
|
||||
settings?: {
|
||||
dontShowCardsCurrentTab?: boolean;
|
||||
dontShowIdentitiesCurrentTab?: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
const vaultSettingsStateDefinition: {
|
||||
stateDefinition: StateDefinitionLike;
|
||||
} = {
|
||||
stateDefinition: {
|
||||
name: "vaultSettings",
|
||||
},
|
||||
};
|
||||
|
||||
export class VaultSettingsKeyMigrator extends Migrator<35, 36> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
const accounts = await helper.getAccounts<ExpectedAccountState>();
|
||||
|
||||
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
|
||||
|
||||
async function migrateAccount(userId: string, account: ExpectedAccountState): Promise<void> {
|
||||
let updateAccount = false;
|
||||
const accountSettings = account?.settings;
|
||||
|
||||
if (accountSettings?.dontShowCardsCurrentTab != null) {
|
||||
await helper.setToUser(
|
||||
userId,
|
||||
{ ...vaultSettingsStateDefinition, key: "showCardsCurrentTab" },
|
||||
accountSettings.dontShowCardsCurrentTab,
|
||||
);
|
||||
delete account.settings.dontShowCardsCurrentTab;
|
||||
updateAccount = true;
|
||||
}
|
||||
|
||||
if (accountSettings?.dontShowIdentitiesCurrentTab != null) {
|
||||
await helper.setToUser(
|
||||
userId,
|
||||
{ ...vaultSettingsStateDefinition, key: "showIdentitiesCurrentTab" },
|
||||
accountSettings.dontShowIdentitiesCurrentTab,
|
||||
);
|
||||
delete account.settings.dontShowIdentitiesCurrentTab;
|
||||
updateAccount = true;
|
||||
}
|
||||
|
||||
if (updateAccount) {
|
||||
await helper.set(userId, account);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async rollback(helper: MigrationHelper): Promise<void> {
|
||||
const accounts = await helper.getAccounts<ExpectedAccountState>();
|
||||
|
||||
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
|
||||
|
||||
async function rollbackAccount(userId: string, account: ExpectedAccountState): Promise<void> {
|
||||
let updateAccount = false;
|
||||
let settings = account?.settings ?? {};
|
||||
|
||||
const showCardsCurrentTab = await helper.getFromUser<boolean>(userId, {
|
||||
...vaultSettingsStateDefinition,
|
||||
key: "showCardsCurrentTab",
|
||||
});
|
||||
|
||||
const showIdentitiesCurrentTab = await helper.getFromUser<boolean>(userId, {
|
||||
...vaultSettingsStateDefinition,
|
||||
key: "showIdentitiesCurrentTab",
|
||||
});
|
||||
|
||||
if (showCardsCurrentTab != null) {
|
||||
// invert the value to match the new naming convention
|
||||
settings = { ...settings, dontShowCardsCurrentTab: !showCardsCurrentTab };
|
||||
|
||||
await helper.setToUser(
|
||||
userId,
|
||||
{ ...vaultSettingsStateDefinition, key: "showCardsCurrentTab" },
|
||||
null,
|
||||
);
|
||||
|
||||
updateAccount = true;
|
||||
}
|
||||
|
||||
if (showIdentitiesCurrentTab != null) {
|
||||
// invert the value to match the new naming convention
|
||||
settings = { ...settings, dontShowIdentitiesCurrentTab: !showIdentitiesCurrentTab };
|
||||
|
||||
await helper.setToUser(
|
||||
userId,
|
||||
{ ...vaultSettingsStateDefinition, key: "showIdentitiesCurrentTab" },
|
||||
null,
|
||||
);
|
||||
|
||||
updateAccount = true;
|
||||
}
|
||||
|
||||
if (updateAccount) {
|
||||
await helper.set(userId, { ...account, settings });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user