1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 09:43:23 +00:00

[PM-5568] Implement Badge Settings state provider (#8112)

* create badge settings state provider

* replace state service get/set disableBadgeCounter with badge settings service equivalent

* migrate disableBadgeCounter account setting to badge settings state provider

* cleanup and address PR suggestions
This commit is contained in:
Jonathan Prusik
2024-02-27 16:03:12 -05:00
committed by GitHub
parent e833e93b3b
commit 929b5ebec3
15 changed files with 334 additions and 35 deletions

View File

@@ -73,7 +73,7 @@ const autofillSettingsStateDefinition: {
},
};
describe("ProviderKeysMigrator", () => {
describe("AutofillSettingsKeyMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: AutofillSettingsKeyMigrator;

View File

@@ -82,7 +82,7 @@ const autofillSettingsLocalStateDefinition: {
},
};
describe("ProviderKeysMigrator", () => {
describe("ClearClipboardDelayMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: ClearClipboardDelayMigrator;

View File

@@ -0,0 +1,166 @@
import { any, MockProxy } from "jest-mock-extended";
import { StateDefinitionLike, MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { BadgeSettingsMigrator } from "./26-move-badge-settings-to-state-providers";
function exampleJSON() {
return {
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2", "user-3"],
"user-1": {
settings: {
disableBadgeCounter: true,
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
settings: {
disableBadgeCounter: false,
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
"user-3": {
settings: {
otherStuff: "otherStuff6",
},
otherStuff: "otherStuff7",
},
};
}
function rollbackJSON() {
return {
"user_user-1_badgeSettings_enableBadgeCounter": false,
"user_user-2_badgeSettings_enableBadgeCounter": 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",
},
"user-3": {
settings: {
otherStuff: "otherStuff6",
},
otherStuff: "otherStuff7",
},
};
}
const badgeSettingsStateDefinition: {
stateDefinition: StateDefinitionLike;
} = {
stateDefinition: {
name: "badgeSettings",
},
};
describe("BadgeSettingsMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: BadgeSettingsMigrator;
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(exampleJSON(), 25);
sut = new BadgeSettingsMigrator(25, 26);
});
it("should remove disableBadgeCounter setting from all accounts", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledTimes(2);
expect(helper.set).toHaveBeenCalledWith("user-1", {
settings: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
});
expect(helper.set).toHaveBeenCalledWith("user-2", {
settings: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
});
});
it("should set badge setting values for each account", async () => {
await sut.migrate(helper);
expect(helper.setToUser).toHaveBeenCalledTimes(2);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-1",
{ ...badgeSettingsStateDefinition, key: "enableBadgeCounter" },
false,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-2",
{ ...badgeSettingsStateDefinition, key: "enableBadgeCounter" },
true,
);
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackJSON(), 24);
sut = new BadgeSettingsMigrator(25, 26);
});
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",
{ ...badgeSettingsStateDefinition, key: "enableBadgeCounter" },
null,
);
expect(helper.setToUser).toHaveBeenCalledWith(
"user-2",
{ ...badgeSettingsStateDefinition, key: "enableBadgeCounter" },
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: {
disableBadgeCounter: true,
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
});
expect(helper.set).toHaveBeenCalledWith("user-2", {
settings: {
disableBadgeCounter: false,
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
});
});
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,71 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedAccountState = {
settings?: {
disableBadgeCounter?: boolean;
};
};
const enableBadgeCounterKeyDefinition: KeyDefinitionLike = {
stateDefinition: {
name: "badgeSettings",
},
key: "enableBadgeCounter",
};
export class BadgeSettingsMigrator extends Migrator<25, 26> {
async migrate(helper: MigrationHelper): Promise<void> {
// account state (e.g. account settings -> state provider framework keys)
const accounts = await helper.getAccounts<ExpectedAccountState>();
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
// migrate account state
async function migrateAccount(userId: string, account: ExpectedAccountState): Promise<void> {
const accountSettings = account?.settings;
if (accountSettings?.disableBadgeCounter != undefined) {
await helper.setToUser(
userId,
enableBadgeCounterKeyDefinition,
!accountSettings.disableBadgeCounter,
);
delete account.settings.disableBadgeCounter;
// update the state account settings with the migrated values deleted
await helper.set(userId, account);
}
}
}
async rollback(helper: MigrationHelper): Promise<void> {
// account state (e.g. state provider framework keys -> account settings)
const accounts = await helper.getAccounts<ExpectedAccountState>();
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
// rollback account state
async function rollbackAccount(userId: string, account: ExpectedAccountState): Promise<void> {
let settings = account?.settings || {};
const enableBadgeCounter: boolean = await helper.getFromUser(
userId,
enableBadgeCounterKeyDefinition,
);
// update new settings and remove the account state provider framework keys for the rolled back values
if (enableBadgeCounter != undefined) {
settings = { ...settings, disableBadgeCounter: !enableBadgeCounter };
await helper.setToUser(userId, enableBadgeCounterKeyDefinition, null);
// commit updated settings to state
await helper.set(userId, {
...account,
settings,
});
}
}
}
}