1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +00:00

[PM-5276] Migrate FolderService to state providers (#7682)

* added state definitionand key definition for folder service

* added data migrations

* created folder to house key definitions

* deleted browser-folder-service and added state provider to the browser

* exposed decrypt function so it can be used by the key definition, updated folder service to use state provider

* removed memory since derived state is now used

* updated test cases

* updated test cases

* updated migrations after merge conflict fix

* added state provider to the folder service constructor

* renamed migration file

* updated comments

* updated comments

* removed service registartion from browser service module and removed unused set and get encrypted folders from state service

* renamed files

* added storage location overides and removed extra methods
This commit is contained in:
SmithThe4th
2024-02-06 14:51:02 -05:00
committed by GitHub
parent f64092cc90
commit 7e00ece092
19 changed files with 473 additions and 241 deletions

View File

@@ -0,0 +1,163 @@
import { MockProxy, any } from "jest-mock-extended";
import { MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { FolderMigrator } from "./15-move-folder-state-to-state-provider";
function exampleJSON() {
return {
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2"],
"user-1": {
data: {
folders: {
encrypted: {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
},
otherStuff: "overStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
data: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
function rollbackJSON() {
return {
"user_user-1_folder_folders": {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
"user_user-2_folder_folders": null as any,
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2"],
"user-1": {
data: {
otherStuff: "overStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
data: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
describe("FolderMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: FolderMigrator;
const keyDefinitionLike = {
key: "folders",
stateDefinition: {
name: "folder",
},
};
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(exampleJSON(), 14);
sut = new FolderMigrator(14, 15);
});
it("should remove folders from all accounts", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledWith("user-1", {
data: {
otherStuff: "overStuff2",
},
otherStuff: "otherStuff3",
});
});
it("should set folders value for each account", async () => {
await sut.migrate(helper);
expect(helper.setToUser).toHaveBeenCalledWith("user-1", keyDefinitionLike, {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
});
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackJSON(), 14);
sut = new FolderMigrator(14, 15);
});
it.each(["user-1", "user-2"])("should null out new values", async (userId) => {
await sut.rollback(helper);
expect(helper.setToUser).toHaveBeenCalledWith(userId, keyDefinitionLike, null);
});
it("should add explicit value back to accounts", async () => {
await sut.rollback(helper);
expect(helper.set).toHaveBeenCalledWith("user-1", {
data: {
folders: {
encrypted: {
"folder-id-1": {
id: "folder-id-1",
name: "folder-name-1",
revisionDate: "folder-revision-date-1",
},
"folder-id-2": {
id: "folder-id-2",
name: "folder-name-2",
revisionDate: "folder-revision-date-2",
},
},
},
otherStuff: "overStuff2",
},
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,57 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type FolderDataType = {
id: string;
name: string;
revisionDate: string;
};
type ExpectedAccountType = {
data?: {
folders?: {
encrypted?: Record<string, FolderDataType>;
};
};
};
const USER_ENCRYPTED_FOLDERS: KeyDefinitionLike = {
key: "folders",
stateDefinition: {
name: "folder",
},
};
export class FolderMigrator extends Migrator<14, 15> {
async migrate(helper: MigrationHelper): Promise<void> {
const accounts = await helper.getAccounts<ExpectedAccountType>();
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
const value = account?.data?.folders?.encrypted;
if (value != null) {
await helper.setToUser(userId, USER_ENCRYPTED_FOLDERS, value);
delete account.data.folders;
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 value = await helper.getFromUser(userId, USER_ENCRYPTED_FOLDERS);
if (account) {
account.data = Object.assign(account.data ?? {}, {
folders: {
encrypted: value,
},
});
await helper.set(userId, account);
}
await helper.setToUser(userId, USER_ENCRYPTED_FOLDERS, null);
}
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
}
}