mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 01:03:35 +00:00
[PM-5979] Refactor EnvironmentService (#8040)
Refactor environment service to emit a single observable. This required significant changes to how the environment service behaves and tackles much of the tech debt planned for it.
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
import { runMigrator } from "../migration-helper.spec";
|
||||
|
||||
import { MergeEnvironmentState } from "./45-merge-environment-state";
|
||||
|
||||
describe("MergeEnvironmentState", () => {
|
||||
const migrator = new MergeEnvironmentState(44, 45);
|
||||
|
||||
it("can migrate all data", async () => {
|
||||
const output = await runMigrator(migrator, {
|
||||
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",
|
||||
},
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
authenticatedAccounts: ["user1", "user2"],
|
||||
global: {
|
||||
extra: "data",
|
||||
},
|
||||
global_environment_environment: {
|
||||
region: "US",
|
||||
urls: {
|
||||
base: "example.com",
|
||||
},
|
||||
},
|
||||
user1: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
extra: "data",
|
||||
},
|
||||
},
|
||||
user2: {
|
||||
extra: "data",
|
||||
settings: {
|
||||
extra: "data",
|
||||
},
|
||||
},
|
||||
extra: "data",
|
||||
user_user1_environment_environment: {
|
||||
region: "US",
|
||||
urls: {
|
||||
base: "example.com",
|
||||
},
|
||||
},
|
||||
user_user2_environment_environment: {
|
||||
region: "EU",
|
||||
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: [],
|
||||
global_environment_region: "Self-Hosted",
|
||||
global: {},
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
authenticatedAccounts: [],
|
||||
global_environment_environment: {
|
||||
region: "Self-Hosted",
|
||||
urls: undefined,
|
||||
},
|
||||
global: {},
|
||||
});
|
||||
});
|
||||
|
||||
it("can migrate only user state", async () => {
|
||||
const output = await runMigrator(migrator, {
|
||||
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",
|
||||
},
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
authenticatedAccounts: ["user1"] as const,
|
||||
global: null,
|
||||
user1: { settings: {} },
|
||||
user_user1_environment_environment: {
|
||||
region: "Self-Hosted",
|
||||
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,83 @@
|
||||
import { KeyDefinitionLike, MigrationHelper, StateDefinitionLike } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
const ENVIRONMENT_STATE: StateDefinitionLike = { name: "environment" };
|
||||
|
||||
const ENVIRONMENT_REGION: KeyDefinitionLike = {
|
||||
key: "region",
|
||||
stateDefinition: ENVIRONMENT_STATE,
|
||||
};
|
||||
|
||||
const ENVIRONMENT_URLS: KeyDefinitionLike = {
|
||||
key: "urls",
|
||||
stateDefinition: ENVIRONMENT_STATE,
|
||||
};
|
||||
|
||||
const ENVIRONMENT_ENVIRONMENT: KeyDefinitionLike = {
|
||||
key: "environment",
|
||||
stateDefinition: ENVIRONMENT_STATE,
|
||||
};
|
||||
|
||||
export class MergeEnvironmentState extends Migrator<44, 45> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
const accounts = await helper.getAccounts<unknown>();
|
||||
|
||||
async function migrateAccount(userId: string, account: unknown): Promise<void> {
|
||||
const region = await helper.getFromUser(userId, ENVIRONMENT_REGION);
|
||||
const urls = await helper.getFromUser(userId, ENVIRONMENT_URLS);
|
||||
|
||||
if (region == null && urls == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
await helper.setToUser(userId, ENVIRONMENT_ENVIRONMENT, {
|
||||
region,
|
||||
urls,
|
||||
});
|
||||
await helper.removeFromUser(userId, ENVIRONMENT_REGION);
|
||||
await helper.removeFromUser(userId, ENVIRONMENT_URLS);
|
||||
}
|
||||
|
||||
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
|
||||
|
||||
const region = await helper.getFromGlobal(ENVIRONMENT_REGION);
|
||||
const urls = await helper.getFromGlobal(ENVIRONMENT_URLS);
|
||||
|
||||
if (region == null && urls == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
await helper.setToGlobal(ENVIRONMENT_ENVIRONMENT, {
|
||||
region,
|
||||
urls,
|
||||
});
|
||||
await helper.removeFromGlobal(ENVIRONMENT_REGION);
|
||||
await helper.removeFromGlobal(ENVIRONMENT_URLS);
|
||||
}
|
||||
|
||||
async rollback(helper: MigrationHelper): Promise<void> {
|
||||
const accounts = await helper.getAccounts<unknown>();
|
||||
|
||||
async function rollbackAccount(userId: string, account: unknown): Promise<void> {
|
||||
const state = (await helper.getFromUser(userId, ENVIRONMENT_ENVIRONMENT)) as {
|
||||
region: string;
|
||||
urls: string;
|
||||
} | null;
|
||||
|
||||
await helper.setToUser(userId, ENVIRONMENT_REGION, state?.region);
|
||||
await helper.setToUser(userId, ENVIRONMENT_URLS, state?.urls);
|
||||
await helper.removeFromUser(userId, ENVIRONMENT_ENVIRONMENT);
|
||||
}
|
||||
|
||||
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
|
||||
|
||||
const state = (await helper.getFromGlobal(ENVIRONMENT_ENVIRONMENT)) as {
|
||||
region: string;
|
||||
urls: string;
|
||||
} | null;
|
||||
|
||||
await helper.setToGlobal(ENVIRONMENT_REGION, state?.region);
|
||||
await helper.setToGlobal(ENVIRONMENT_URLS, state?.urls);
|
||||
await helper.removeFromGlobal(ENVIRONMENT_ENVIRONMENT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user