mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
Ps/pm 5965/better config polling (#8325)
* Create tracker that can await until expected observables are received. * Test dates are almost equal * Remove unused class method * Allow for updating active account in accout service fake * Correct observable tracker behavior Clarify documentation * Transition config service to state provider Updates the config fetching behavior to be lazy and ensure that any emitted value has been updated if older than a configurable value (statically compiled). If desired, config fetching can be ensured fresh through an async. * Update calls to config service in DI and bootstrapping * Migrate account server configs * Fix global config fetching * Test migration rollback * Adhere to implementation naming convention * Adhere to abstract class naming convention * Complete config abstraction rename * Remove unnecessary cli config service * Fix builds * Validate observable does not complete * Use token service to determine authed or unauthed config pull * Remove superfluous factory config * Name describe blocks after the thing they test * Remove implementation documentation Unfortunately the experience when linking to external documentation is quite poor. Instead of following the link and retrieving docs, you get a link that can be clicked to take you out of context to the docs. No link _does_ retrieve docs, but lacks indication in the implementation that documentation exists at all. On the balance, removing the link is the better experience. * Fix storybook
This commit is contained in:
@@ -44,6 +44,7 @@ import { MergeEnvironmentState } from "./migrations/45-merge-environment-state";
|
||||
import { DeleteBiometricPromptCancelledData } from "./migrations/46-delete-orphaned-biometric-prompt-data";
|
||||
import { MoveDesktopSettingsMigrator } from "./migrations/47-move-desktop-settings";
|
||||
import { MoveDdgToStateProviderMigrator } from "./migrations/48-move-ddg-to-state-provider";
|
||||
import { AccountServerConfigMigrator } from "./migrations/49-move-account-server-configs";
|
||||
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
|
||||
import { RemoveLegacyEtmKeyMigrator } from "./migrations/6-remove-legacy-etm-key";
|
||||
import { MoveBiometricAutoPromptToAccount } from "./migrations/7-move-biometric-auto-prompt-to-account";
|
||||
@@ -52,8 +53,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
|
||||
import { MinVersionMigrator } from "./migrations/min-version";
|
||||
|
||||
export const MIN_VERSION = 3;
|
||||
export const CURRENT_VERSION = 48;
|
||||
|
||||
export const CURRENT_VERSION = 49;
|
||||
export type MinVersion = typeof MIN_VERSION;
|
||||
|
||||
export function createMigrationBuilder() {
|
||||
@@ -103,7 +103,8 @@ export function createMigrationBuilder() {
|
||||
.with(MergeEnvironmentState, 44, 45)
|
||||
.with(DeleteBiometricPromptCancelledData, 45, 46)
|
||||
.with(MoveDesktopSettingsMigrator, 46, 47)
|
||||
.with(MoveDdgToStateProviderMigrator, 47, CURRENT_VERSION);
|
||||
.with(MoveDdgToStateProviderMigrator, 47, 48)
|
||||
.with(AccountServerConfigMigrator, 48, CURRENT_VERSION);
|
||||
}
|
||||
|
||||
export async function currentVersion(
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import { runMigrator } from "../migration-helper.spec";
|
||||
|
||||
import { AccountServerConfigMigrator } from "./49-move-account-server-configs";
|
||||
|
||||
describe("AccountServerConfigMigrator", () => {
|
||||
const migrator = new AccountServerConfigMigrator(48, 49);
|
||||
|
||||
describe("all data", () => {
|
||||
function toMigrate() {
|
||||
return {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
user1: {
|
||||
settings: {
|
||||
serverConfig: {
|
||||
config: "user1 server config",
|
||||
},
|
||||
},
|
||||
},
|
||||
user2: {
|
||||
settings: {
|
||||
serverConfig: {
|
||||
config: "user2 server config",
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function migrated() {
|
||||
return {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
|
||||
user1: {
|
||||
settings: {},
|
||||
},
|
||||
user2: {
|
||||
settings: {},
|
||||
},
|
||||
user_user1_config_serverConfig: {
|
||||
config: "user1 server config",
|
||||
},
|
||||
user_user2_config_serverConfig: {
|
||||
config: "user2 server config",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function rolledBack(previous: object) {
|
||||
return {
|
||||
...previous,
|
||||
user_user1_config_serverConfig: null as unknown,
|
||||
user_user2_config_serverConfig: null as unknown,
|
||||
};
|
||||
}
|
||||
|
||||
it("migrates", async () => {
|
||||
const output = await runMigrator(migrator, toMigrate(), "migrate");
|
||||
expect(output).toEqual(migrated());
|
||||
});
|
||||
|
||||
it("rolls back", async () => {
|
||||
const output = await runMigrator(migrator, migrated(), "rollback");
|
||||
expect(output).toEqual(rolledBack(toMigrate()));
|
||||
});
|
||||
});
|
||||
|
||||
describe("missing parts", () => {
|
||||
function toMigrate() {
|
||||
return {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
user1: {
|
||||
settings: {
|
||||
serverConfig: {
|
||||
config: "user1 server config",
|
||||
},
|
||||
},
|
||||
},
|
||||
user2: null as unknown,
|
||||
};
|
||||
}
|
||||
|
||||
function migrated() {
|
||||
return {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
user1: {
|
||||
settings: {},
|
||||
},
|
||||
user2: null as unknown,
|
||||
user_user1_config_serverConfig: {
|
||||
config: "user1 server config",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function rollback(previous: object) {
|
||||
return {
|
||||
...previous,
|
||||
user_user1_config_serverConfig: null as unknown,
|
||||
};
|
||||
}
|
||||
|
||||
it("migrates", async () => {
|
||||
const output = await runMigrator(migrator, toMigrate(), "migrate");
|
||||
expect(output).toEqual(migrated());
|
||||
});
|
||||
|
||||
it("rolls back", async () => {
|
||||
const output = await runMigrator(migrator, migrated(), "rollback");
|
||||
expect(output).toEqual(rollback(toMigrate()));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { KeyDefinitionLike, MigrationHelper, StateDefinitionLike } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
const CONFIG_DISK: StateDefinitionLike = { name: "config" };
|
||||
export const USER_SERVER_CONFIG: KeyDefinitionLike = {
|
||||
stateDefinition: CONFIG_DISK,
|
||||
key: "serverConfig",
|
||||
};
|
||||
|
||||
// Note: no need to migrate global configs, they don't currently exist
|
||||
|
||||
type ExpectedAccountType = {
|
||||
settings?: {
|
||||
serverConfig?: unknown;
|
||||
};
|
||||
};
|
||||
|
||||
export class AccountServerConfigMigrator extends Migrator<48, 49> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
||||
|
||||
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
|
||||
if (account?.settings?.serverConfig != null) {
|
||||
await helper.setToUser(userId, USER_SERVER_CONFIG, account.settings.serverConfig);
|
||||
delete account.settings.serverConfig;
|
||||
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 serverConfig = await helper.getFromUser(userId, USER_SERVER_CONFIG);
|
||||
|
||||
if (serverConfig) {
|
||||
account ??= {};
|
||||
account.settings ??= {};
|
||||
|
||||
account.settings.serverConfig = serverConfig;
|
||||
await helper.setToUser(userId, USER_SERVER_CONFIG, null);
|
||||
await helper.set(userId, account);
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user