mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
[PM-5535] Migrate Environment Service to StateProvider (#7621)
* Migrate EnvironmentService * Move Migration Test Helper * Claim StateDefinition * Add State Migration * Update StateServices * Update EnvironmentService Abstraction * Update DI * Update Browser Instantiation * Fix BrowserEnvironmentService * Update Desktop & CLI Instantiation * Update Usage * Create isStringRecord helper * Fix Old Tests * Use Existing AccountService * Don't Rely on Parameter Mutation * Fix Conflicts
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
import { runMigrator } from "../migration-helper.spec";
|
||||
|
||||
import { MoveEnvironmentStateToProviders } from "./12-move-environment-state-to-providers";
|
||||
|
||||
describe("MoveEnvironmentStateToProviders", () => {
|
||||
const migrator = new MoveEnvironmentStateToProviders(11, 12);
|
||||
|
||||
it("can migrate all data", async () => {
|
||||
const output = await runMigrator(migrator, {
|
||||
authenticatedAccounts: ["user1", "user2"] as const,
|
||||
global: {
|
||||
region: "US",
|
||||
environmentUrls: {
|
||||
base: "example.com",
|
||||
},
|
||||
extra: "data",
|
||||
},
|
||||
user1: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
extra: "data",
|
||||
region: "US",
|
||||
environmentUrls: {
|
||||
base: "example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
user2: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
region: "EU",
|
||||
environmentUrls: {
|
||||
base: "other.example.com",
|
||||
},
|
||||
extra: "data",
|
||||
},
|
||||
},
|
||||
extra: "data",
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
global: {
|
||||
extra: "data",
|
||||
},
|
||||
global_environment_region: "US",
|
||||
global_environment_urls: {
|
||||
base: "example.com",
|
||||
},
|
||||
user1: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
extra: "data",
|
||||
},
|
||||
},
|
||||
user2: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
extra: "data",
|
||||
},
|
||||
},
|
||||
extra: "data",
|
||||
user_user1_environment_region: "US",
|
||||
user_user2_environment_region: "EU",
|
||||
user_user1_environment_urls: {
|
||||
base: "example.com",
|
||||
},
|
||||
user_user2_environment_urls: {
|
||||
base: "other.example.com",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("handles missing parts", async () => {
|
||||
const output = await runMigrator(migrator, {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
global: {
|
||||
extra: "data",
|
||||
},
|
||||
user1: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
extra: "data",
|
||||
},
|
||||
},
|
||||
user2: null,
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
global: {
|
||||
extra: "data",
|
||||
},
|
||||
user1: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
extra: "data",
|
||||
},
|
||||
},
|
||||
user2: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("can migrate only global data", async () => {
|
||||
const output = await runMigrator(migrator, {
|
||||
authenticatedAccounts: [] as const,
|
||||
global: {
|
||||
region: "Self-Hosted",
|
||||
},
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
authenticatedAccounts: [],
|
||||
global_environment_region: "Self-Hosted",
|
||||
global: {},
|
||||
});
|
||||
});
|
||||
|
||||
it("can migrate only user state", async () => {
|
||||
const output = await runMigrator(migrator, {
|
||||
authenticatedAccounts: ["user1"] as const,
|
||||
global: null,
|
||||
user1: {
|
||||
settings: {
|
||||
region: "Self-Hosted",
|
||||
environmentUrls: {
|
||||
base: "some-base-url",
|
||||
api: "some-api-url",
|
||||
identity: "some-identity-url",
|
||||
icons: "some-icons-url",
|
||||
notifications: "some-notifications-url",
|
||||
events: "some-events-url",
|
||||
webVault: "some-webVault-url",
|
||||
keyConnector: "some-keyConnector-url",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
authenticatedAccounts: ["user1"] as const,
|
||||
global: null,
|
||||
user1: { settings: {} },
|
||||
user_user1_environment_region: "Self-Hosted",
|
||||
user_user1_environment_urls: {
|
||||
base: "some-base-url",
|
||||
api: "some-api-url",
|
||||
identity: "some-identity-url",
|
||||
icons: "some-icons-url",
|
||||
notifications: "some-notifications-url",
|
||||
events: "some-events-url",
|
||||
webVault: "some-webVault-url",
|
||||
keyConnector: "some-keyConnector-url",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
import { KeyDefinitionLike, MigrationHelper, StateDefinitionLike } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
type EnvironmentUrls = Record<string, string>;
|
||||
|
||||
type ExpectedAccountType = {
|
||||
settings?: { region?: string; environmentUrls?: EnvironmentUrls };
|
||||
};
|
||||
|
||||
type ExpectedGlobalType = { region?: string; environmentUrls?: EnvironmentUrls };
|
||||
|
||||
const ENVIRONMENT_STATE: StateDefinitionLike = { name: "environment" };
|
||||
|
||||
const REGION_KEY: KeyDefinitionLike = { key: "region", stateDefinition: ENVIRONMENT_STATE };
|
||||
const URLS_KEY: KeyDefinitionLike = { key: "urls", stateDefinition: ENVIRONMENT_STATE };
|
||||
|
||||
export class MoveEnvironmentStateToProviders extends Migrator<11, 12> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
const legacyGlobal = await helper.get<ExpectedGlobalType>("global");
|
||||
|
||||
// Move global data
|
||||
if (legacyGlobal?.region != null) {
|
||||
await helper.setToGlobal(REGION_KEY, legacyGlobal.region);
|
||||
}
|
||||
|
||||
if (legacyGlobal?.environmentUrls != null) {
|
||||
await helper.setToGlobal(URLS_KEY, legacyGlobal.environmentUrls);
|
||||
}
|
||||
|
||||
const legacyAccounts = await helper.getAccounts<ExpectedAccountType>();
|
||||
|
||||
await Promise.all(
|
||||
legacyAccounts.map(async ({ userId, account }) => {
|
||||
// Move account data
|
||||
if (account?.settings?.region != null) {
|
||||
await helper.setToUser(userId, REGION_KEY, account.settings.region);
|
||||
}
|
||||
|
||||
if (account?.settings?.environmentUrls != null) {
|
||||
await helper.setToUser(userId, URLS_KEY, account.settings.environmentUrls);
|
||||
}
|
||||
|
||||
// Delete old account data
|
||||
delete account?.settings?.region;
|
||||
delete account?.settings?.environmentUrls;
|
||||
await helper.set(userId, account);
|
||||
}),
|
||||
);
|
||||
|
||||
// Delete legacy global data
|
||||
delete legacyGlobal?.region;
|
||||
delete legacyGlobal?.environmentUrls;
|
||||
await helper.set("global", legacyGlobal);
|
||||
}
|
||||
|
||||
async rollback(helper: MigrationHelper): Promise<void> {
|
||||
let legacyGlobal = await helper.get<ExpectedGlobalType>("global");
|
||||
|
||||
let updatedLegacyGlobal = false;
|
||||
|
||||
const globalRegion = await helper.getFromGlobal<string>(REGION_KEY);
|
||||
|
||||
if (globalRegion) {
|
||||
if (!legacyGlobal) {
|
||||
legacyGlobal = {};
|
||||
}
|
||||
|
||||
updatedLegacyGlobal = true;
|
||||
legacyGlobal.region = globalRegion;
|
||||
await helper.setToGlobal(REGION_KEY, null);
|
||||
}
|
||||
|
||||
const globalUrls = await helper.getFromGlobal<EnvironmentUrls>(URLS_KEY);
|
||||
|
||||
if (globalUrls) {
|
||||
if (!legacyGlobal) {
|
||||
legacyGlobal = {};
|
||||
}
|
||||
|
||||
updatedLegacyGlobal = true;
|
||||
legacyGlobal.environmentUrls = globalUrls;
|
||||
await helper.setToGlobal(URLS_KEY, null);
|
||||
}
|
||||
|
||||
if (updatedLegacyGlobal) {
|
||||
await helper.set("global", legacyGlobal);
|
||||
}
|
||||
|
||||
async function rollbackUser(userId: string, account: ExpectedAccountType) {
|
||||
let updatedAccount = false;
|
||||
const userRegion = await helper.getFromUser<string>(userId, REGION_KEY);
|
||||
|
||||
if (userRegion) {
|
||||
if (!account) {
|
||||
account = {};
|
||||
}
|
||||
|
||||
if (!account.settings) {
|
||||
account.settings = {};
|
||||
}
|
||||
|
||||
updatedAccount = true;
|
||||
account.settings.region = userRegion;
|
||||
await helper.setToUser(userId, REGION_KEY, null);
|
||||
}
|
||||
|
||||
const userUrls = await helper.getFromUser<EnvironmentUrls>(userId, URLS_KEY);
|
||||
|
||||
if (userUrls) {
|
||||
if (!account) {
|
||||
account = {};
|
||||
}
|
||||
|
||||
if (!account.settings) {
|
||||
account.settings = {};
|
||||
}
|
||||
|
||||
updatedAccount = true;
|
||||
account.settings.environmentUrls = userUrls;
|
||||
await helper.setToUser(userId, URLS_KEY, null);
|
||||
}
|
||||
|
||||
if (updatedAccount) {
|
||||
await helper.set(userId, account);
|
||||
}
|
||||
}
|
||||
|
||||
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
||||
|
||||
await Promise.all(accounts.map(({ userId, account }) => rollbackUser(userId, account)));
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,14 @@
|
||||
import { mock } from "jest-mock-extended";
|
||||
|
||||
import { FakeStorageService } from "../../../spec/fake-storage.service";
|
||||
import { MigrationHelper } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
import { runMigrator } from "../migration-helper.spec";
|
||||
|
||||
import { MoveBrowserSettingsToGlobal } from "./9-move-browser-settings-to-global";
|
||||
|
||||
type TestState = { authenticatedAccounts: string[] } & { [key: string]: unknown };
|
||||
|
||||
// This could become a helper available to anyone
|
||||
const runMigrator = async <TMigrator extends Migrator<number, number>>(
|
||||
migrator: TMigrator,
|
||||
initalData?: Record<string, unknown>,
|
||||
): Promise<Record<string, unknown>> => {
|
||||
const fakeStorageService = new FakeStorageService(initalData);
|
||||
const helper = new MigrationHelper(migrator.fromVersion, fakeStorageService, mock());
|
||||
await migrator.migrate(helper);
|
||||
return fakeStorageService.internalStore;
|
||||
};
|
||||
|
||||
describe("MoveBrowserSettingsToGlobal", () => {
|
||||
const myMigrator = new MoveBrowserSettingsToGlobal(8, 9);
|
||||
|
||||
// This could be the state for a browser client who has never touched the settings or this could
|
||||
// be a different client who doesn't make it possible to toggle these settings
|
||||
it("doesn't set any value to global if there is no equivalent settings on the account", async () => {
|
||||
const testInput: TestState = {
|
||||
const output = await runMigrator(myMigrator, {
|
||||
authenticatedAccounts: ["user1"],
|
||||
global: {
|
||||
theme: "system", // A real global setting that should persist after migration
|
||||
@@ -35,9 +18,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
region: "Self-hosted",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const output = await runMigrator(myMigrator, testInput);
|
||||
});
|
||||
|
||||
// No additions to the global state
|
||||
expect(output["global"]).toEqual({
|
||||
@@ -55,7 +36,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
// This could be a user who opened up the settings page and toggled the checkbox, since this setting infers undefined
|
||||
// as false this is essentially the default value.
|
||||
it("sets the setting from the users settings if they have toggled the setting but placed it back to it's inferred", async () => {
|
||||
const testInput: TestState = {
|
||||
const output = await runMigrator(myMigrator, {
|
||||
authenticatedAccounts: ["user1"],
|
||||
global: {
|
||||
theme: "system", // A real global setting that should persist after migration
|
||||
@@ -71,9 +52,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
region: "Self-hosted",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const output = await runMigrator(myMigrator, testInput);
|
||||
});
|
||||
|
||||
// User settings should have moved to global
|
||||
expect(output["global"]).toEqual({
|
||||
@@ -94,7 +73,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
|
||||
// The user has set a value and it's not the default, we should respect that choice globally
|
||||
it("should take the only users settings", async () => {
|
||||
const testInput: TestState = {
|
||||
const output = await runMigrator(myMigrator, {
|
||||
authenticatedAccounts: ["user1"],
|
||||
global: {
|
||||
theme: "system", // A real global setting that should persist after migration
|
||||
@@ -110,9 +89,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
region: "Self-hosted",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const output = await runMigrator(myMigrator, testInput);
|
||||
});
|
||||
|
||||
// The value for the single user value should be set to global
|
||||
expect(output["global"]).toEqual({
|
||||
@@ -134,7 +111,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
// but in the bizzare case, we should interpret any user having the feature turned on as the value for
|
||||
// all the accounts.
|
||||
it("should take the false value if there are conflicting choices", async () => {
|
||||
const testInput: TestState = {
|
||||
const output = await runMigrator(myMigrator, {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
global: {
|
||||
theme: "system", // A real global setting that should persist after migration
|
||||
@@ -161,9 +138,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
region: "Self-hosted",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const output = await runMigrator(myMigrator, testInput);
|
||||
});
|
||||
|
||||
// The false settings should be respected over the true values
|
||||
// neverDomains should be combined into a single object
|
||||
@@ -191,7 +166,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
// if one user has toggled the setting back to on and one user has never touched the setting,
|
||||
// persist the false value into the global state.
|
||||
it("should persist the false value if one user has that in their settings", async () => {
|
||||
const testInput: TestState = {
|
||||
const output = await runMigrator(myMigrator, {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
global: {
|
||||
theme: "system", // A real global setting that should persist after migration
|
||||
@@ -212,9 +187,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
region: "Self-hosted",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const output = await runMigrator(myMigrator, testInput);
|
||||
});
|
||||
|
||||
// The false settings should be respected over the true values
|
||||
// neverDomains should be combined into a single object
|
||||
@@ -241,7 +214,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
// if one user has toggled the setting off and one user has never touched the setting,
|
||||
// persist the false value into the global state.
|
||||
it("should persist the false value from a user with no settings since undefined is inferred as false", async () => {
|
||||
const testInput: TestState = {
|
||||
const output = await runMigrator(myMigrator, {
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
global: {
|
||||
theme: "system", // A real global setting that should persist after migration
|
||||
@@ -262,9 +235,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
region: "Self-hosted",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const output = await runMigrator(myMigrator, testInput);
|
||||
});
|
||||
|
||||
// The false settings should be respected over the true values
|
||||
// neverDomains should be combined into a single object
|
||||
@@ -292,7 +263,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
// id of the non-current account isn't saved to the authenticatedAccounts array so we don't have a great way to
|
||||
// get the state and include it in our calculations for what the global state should be.
|
||||
it("only cares about users defined in authenticatedAccounts", async () => {
|
||||
const testInput: TestState = {
|
||||
const output = await runMigrator(myMigrator, {
|
||||
authenticatedAccounts: ["user1"],
|
||||
global: {
|
||||
theme: "system", // A real global setting that should persist after migration
|
||||
@@ -319,9 +290,7 @@ describe("MoveBrowserSettingsToGlobal", () => {
|
||||
region: "Self-hosted",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const output = await runMigrator(myMigrator, testInput);
|
||||
});
|
||||
|
||||
// The true settings should be respected over the false values because that whole users values
|
||||
// shouldn't be respected.
|
||||
|
||||
Reference in New Issue
Block a user