1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +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:
SmithThe4th
2024-03-14 11:37:57 -04:00
committed by GitHub
parent d28634b068
commit ebf51ebaaf
12 changed files with 343 additions and 64 deletions

View File

@@ -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());
});
});
});

View File

@@ -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 });
}
}
}
}