mirror of
https://github.com/bitwarden/browser
synced 2025-12-06 00:13:28 +00:00
[PM-6404] Initial Clear Events Code (#8029)
* Add New KeyDefinitionOption * Add New Services * Add WebStorageServiceProvider Tests * Update Error Message * Add `UserKeyDefinition` * Fix Deserialization Helpers * Fix KeyDefinition * Add `UserKeyDefinition` * Fix Deserialization Helpers * Fix KeyDefinition * Move `ClearEvent` * Cleanup * Fix Imports * Remove `updateMock` * Call Super in Web Implementation * Use Better Type to Avoid Casting * Better Error Docs * Move StorageKey Creation to Function * Throw Aggregated Error for Failures
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { mock } from "jest-mock-extended";
|
||||
|
||||
import {
|
||||
AbstractStorageService,
|
||||
ObservableStorageService,
|
||||
} from "@bitwarden/common/platform/abstractions/storage.service";
|
||||
import { PossibleLocation } from "@bitwarden/common/platform/services/storage-service.provider";
|
||||
import {
|
||||
ClientLocations,
|
||||
StorageLocation,
|
||||
// eslint-disable-next-line import/no-restricted-paths
|
||||
} from "@bitwarden/common/platform/state/state-definition";
|
||||
|
||||
import { WebStorageServiceProvider } from "./web-storage-service.provider";
|
||||
|
||||
describe("WebStorageServiceProvider", () => {
|
||||
const mockDiskStorage = mock<AbstractStorageService & ObservableStorageService>();
|
||||
const mockMemoryStorage = mock<AbstractStorageService & ObservableStorageService>();
|
||||
const mockDiskLocalStorage = mock<AbstractStorageService & ObservableStorageService>();
|
||||
|
||||
const sut = new WebStorageServiceProvider(
|
||||
mockDiskStorage,
|
||||
mockMemoryStorage,
|
||||
mockDiskLocalStorage,
|
||||
);
|
||||
|
||||
describe("get", () => {
|
||||
const getTests = [
|
||||
{
|
||||
input: { default: "disk", overrides: {} },
|
||||
expected: "disk",
|
||||
},
|
||||
{
|
||||
input: { default: "memory", overrides: {} },
|
||||
expected: "memory",
|
||||
},
|
||||
{
|
||||
input: { default: "disk", overrides: { web: "disk-local" } },
|
||||
expected: "disk-local",
|
||||
},
|
||||
{
|
||||
input: { default: "disk", overrides: { web: "memory" } },
|
||||
expected: "memory",
|
||||
},
|
||||
{
|
||||
input: { default: "memory", overrides: { web: "disk" } },
|
||||
expected: "disk",
|
||||
},
|
||||
] satisfies {
|
||||
input: { default: StorageLocation; overrides: Partial<ClientLocations> };
|
||||
expected: PossibleLocation;
|
||||
}[];
|
||||
|
||||
it.each(getTests)("computes properly based on %s", ({ input, expected: expectedLocation }) => {
|
||||
const [actualLocation] = sut.get(input.default, input.overrides);
|
||||
expect(actualLocation).toStrictEqual(expectedLocation);
|
||||
});
|
||||
|
||||
it("throws on unsupported option", () => {
|
||||
expect(() => sut.get("blah" as any, {})).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
37
apps/web/src/app/platform/web-storage-service.provider.ts
Normal file
37
apps/web/src/app/platform/web-storage-service.provider.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
AbstractStorageService,
|
||||
ObservableStorageService,
|
||||
} from "@bitwarden/common/platform/abstractions/storage.service";
|
||||
import {
|
||||
PossibleLocation,
|
||||
StorageServiceProvider,
|
||||
} from "@bitwarden/common/platform/services/storage-service.provider";
|
||||
import {
|
||||
ClientLocations,
|
||||
// eslint-disable-next-line import/no-restricted-paths
|
||||
} from "@bitwarden/common/platform/state/state-definition";
|
||||
|
||||
export class WebStorageServiceProvider extends StorageServiceProvider {
|
||||
constructor(
|
||||
diskStorageService: AbstractStorageService & ObservableStorageService,
|
||||
memoryStorageService: AbstractStorageService & ObservableStorageService,
|
||||
private readonly diskLocalStorageService: AbstractStorageService & ObservableStorageService,
|
||||
) {
|
||||
super(diskStorageService, memoryStorageService);
|
||||
}
|
||||
|
||||
override get(
|
||||
defaultLocation: PossibleLocation,
|
||||
overrides: Partial<ClientLocations>,
|
||||
): [location: PossibleLocation, service: AbstractStorageService & ObservableStorageService] {
|
||||
const location = overrides["web"] ?? defaultLocation;
|
||||
switch (location) {
|
||||
case "disk-local":
|
||||
return ["disk-local", this.diskLocalStorageService];
|
||||
default:
|
||||
// Pass in computed location to super because they could have
|
||||
// overriden default "disk" with web "memory".
|
||||
return super.get(location, overrides);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user