|
|
|
|
@@ -5,8 +5,6 @@ import {
|
|
|
|
|
CollectionTypes,
|
|
|
|
|
} from "@bitwarden/common/admin-console/models/collections/collection";
|
|
|
|
|
import { CollectionView } from "@bitwarden/common/admin-console/models/collections/collection.view";
|
|
|
|
|
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
|
|
|
|
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
|
|
|
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
|
|
|
|
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
|
|
|
|
|
import { CollectionId, OrganizationId, UserId } from "@bitwarden/common/types/guid";
|
|
|
|
|
@@ -72,10 +70,6 @@ describe("DefaultCollectionEncryptionService", () => {
|
|
|
|
|
mark: jest.fn(),
|
|
|
|
|
} as unknown as LogService;
|
|
|
|
|
const sdkService = { userClient$: jest.fn() } as unknown as SdkService;
|
|
|
|
|
const configService = {
|
|
|
|
|
getFeatureFlag: jest.fn(),
|
|
|
|
|
getFeatureFlag$: jest.fn(),
|
|
|
|
|
} as unknown as ConfigService;
|
|
|
|
|
|
|
|
|
|
let mockDecrypt: jest.Mock;
|
|
|
|
|
let mockDecryptListWithFailures: jest.Mock;
|
|
|
|
|
@@ -105,347 +99,210 @@ describe("DefaultCollectionEncryptionService", () => {
|
|
|
|
|
const mockSdk = { take: jest.fn().mockReturnValue(mockRef) };
|
|
|
|
|
|
|
|
|
|
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(mockSdk));
|
|
|
|
|
service = new DefaultCollectionEncryptionService(sdkService, logService, configService);
|
|
|
|
|
service = new DefaultCollectionEncryptionService(sdkService, logService);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("when CollectionsDecryptListFailures is enabled", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(true));
|
|
|
|
|
describe("decryptMany", () => {
|
|
|
|
|
it("returns an empty array without calling the SDK for empty input", async () => {
|
|
|
|
|
const result = await firstValueFrom(service.decryptMany([], userId));
|
|
|
|
|
expect(result).toEqual([]);
|
|
|
|
|
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("decryptMany", () => {
|
|
|
|
|
it("checks the CollectionsDecryptListFailures feature flag", async () => {
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [makeSdkCollectionView()],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
it("decrypts all collections via a single batch call and returns views", async () => {
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
await firstValueFrom(service.decryptMany([makeCollection()], userId));
|
|
|
|
|
|
|
|
|
|
expect(configService.getFeatureFlag$).toHaveBeenCalledWith(
|
|
|
|
|
FeatureFlag.CollectionsDecryptListFailures,
|
|
|
|
|
);
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [
|
|
|
|
|
makeSdkCollectionView({ id: collectionId as any, name: "Collection 1" }),
|
|
|
|
|
makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" }),
|
|
|
|
|
],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns an empty array without calling the SDK for empty input", async () => {
|
|
|
|
|
const result = await firstValueFrom(service.decryptMany([], userId));
|
|
|
|
|
expect(result).toEqual([]);
|
|
|
|
|
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
const result = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
|
|
|
|
|
|
|
|
|
|
it("decrypts all collections via a single batch call and returns views", async () => {
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [
|
|
|
|
|
makeSdkCollectionView({ id: collectionId as any, name: "Collection 1" }),
|
|
|
|
|
makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" }),
|
|
|
|
|
],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(
|
|
|
|
|
service.decryptMany([collection1, collection2], userId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result).toHaveLength(2);
|
|
|
|
|
expect(result[0].name).toBe("Collection 1");
|
|
|
|
|
expect(result[1].name).toBe("Collection 2");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs and still returns a placeholder view for any collection the SDK returns as a failure", async () => {
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
// SDK reports collection1 as a failure and returns only collection2.
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" })],
|
|
|
|
|
failures: [{ id: collectionId as any }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(
|
|
|
|
|
service.decryptMany([collection1, collection2], userId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result).toHaveLength(2);
|
|
|
|
|
const failedView = result.find((v) => v.id === collectionId)!;
|
|
|
|
|
const successView = result.find((v) => v.id === collectionId2)!;
|
|
|
|
|
expect(failedView.decryptionFailure).toBe(true);
|
|
|
|
|
expect(failedView.name).toBe(DECRYPT_ERROR);
|
|
|
|
|
expect(successView.decryptionFailure).toBe(false);
|
|
|
|
|
expect(successView.name).toBe("Collection 2");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects when the batch SDK call throws", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockImplementation(() => {
|
|
|
|
|
throw new Error("batch failure");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves defaultUserCollectionEmail from the source collection", async () => {
|
|
|
|
|
const email = "offboarded@example.com";
|
|
|
|
|
const collection = makeCollection({
|
|
|
|
|
defaultUserCollectionEmail: email,
|
|
|
|
|
type: CollectionTypes.DefaultUserCollection,
|
|
|
|
|
});
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [makeSdkCollectionView({ type: CollectionTypes.DefaultUserCollection })],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
|
|
|
|
|
|
|
|
|
|
expect(result.defaultUserCollectionEmail).toBe(email);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("matches each decrypted view to its source collection by ID", async () => {
|
|
|
|
|
const email = "offboarded@example.com";
|
|
|
|
|
const collection1 = makeCollection({
|
|
|
|
|
defaultUserCollectionEmail: email,
|
|
|
|
|
type: CollectionTypes.DefaultUserCollection,
|
|
|
|
|
});
|
|
|
|
|
const collection2 = makeCollection({
|
|
|
|
|
id: collectionId2,
|
|
|
|
|
type: CollectionTypes.SharedCollection,
|
|
|
|
|
});
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
// Return views in reverse order to confirm ID-based matching, not index-based.
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [
|
|
|
|
|
makeSdkCollectionView({
|
|
|
|
|
id: collectionId2 as any,
|
|
|
|
|
type: CollectionTypes.SharedCollection,
|
|
|
|
|
}),
|
|
|
|
|
makeSdkCollectionView({
|
|
|
|
|
id: collectionId as any,
|
|
|
|
|
type: CollectionTypes.DefaultUserCollection,
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const results = await firstValueFrom(
|
|
|
|
|
service.decryptMany([collection1, collection2], userId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const view1 = results.find((v) => v.id === collectionId);
|
|
|
|
|
const view2 = results.find((v) => v.id === collectionId2);
|
|
|
|
|
expect(view1?.defaultUserCollectionEmail).toBe(email);
|
|
|
|
|
expect(view2?.defaultUserCollectionEmail).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Pairing is by ID, so a view whose ID does not round-trip cannot be mapped back to its
|
|
|
|
|
// source and drops out of the list - the one disappearance this path cannot represent.
|
|
|
|
|
it("logs a decrypted view whose ID matches no source collection", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [makeSdkCollectionView({ id: collectionId2 as any })],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const results = await firstValueFrom(service.decryptMany([collection], userId));
|
|
|
|
|
|
|
|
|
|
expect(results).toEqual([]);
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining(`Decrypted collection ${collectionId2} did not match`),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs the error and rejects when the SDK client is unavailable", async () => {
|
|
|
|
|
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
expect(result).toHaveLength(2);
|
|
|
|
|
expect(result[0].name).toBe("Collection 1");
|
|
|
|
|
expect(result[1].name).toBe("Collection 2");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("decrypt", () => {
|
|
|
|
|
it("decrypts a single collection via the batch SDK call and maps the result", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
it("logs and still returns a placeholder view for any collection the SDK returns as a failure", async () => {
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
const sdkView = makeSdkCollectionView({ name: "Decrypted Name" });
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({ successes: [sdkView], failures: [] });
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(service.decrypt(collection, userId));
|
|
|
|
|
|
|
|
|
|
expect(mockDecryptListWithFailures).toHaveBeenCalledWith([stubSdkCollection]);
|
|
|
|
|
expect(mockDecrypt).not.toHaveBeenCalled();
|
|
|
|
|
expect(result).toBeInstanceOf(CollectionView);
|
|
|
|
|
expect(result.name).toBe("Decrypted Name");
|
|
|
|
|
// SDK reports collection1 as a failure and returns only collection2.
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" })],
|
|
|
|
|
failures: [{ id: collectionId as any }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects when the batch call throws", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockImplementation(() => {
|
|
|
|
|
throw new Error("batch failure");
|
|
|
|
|
});
|
|
|
|
|
const result = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
|
|
|
|
|
|
|
|
|
|
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result).toHaveLength(2);
|
|
|
|
|
const failedView = result.find((v) => v.id === collectionId)!;
|
|
|
|
|
const successView = result.find((v) => v.id === collectionId2)!;
|
|
|
|
|
expect(failedView.decryptionFailure).toBe(true);
|
|
|
|
|
expect(failedView.name).toBe(DECRYPT_ERROR);
|
|
|
|
|
expect(successView.decryptionFailure).toBe(false);
|
|
|
|
|
expect(successView.name).toBe("Collection 2");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects when the batch SDK call throws", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockImplementation(() => {
|
|
|
|
|
throw new Error("batch failure");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs the error and rejects when the SDK client is unavailable", async () => {
|
|
|
|
|
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
it("preserves defaultUserCollectionEmail from the source collection", async () => {
|
|
|
|
|
const email = "offboarded@example.com";
|
|
|
|
|
const collection = makeCollection({
|
|
|
|
|
defaultUserCollectionEmail: email,
|
|
|
|
|
type: CollectionTypes.DefaultUserCollection,
|
|
|
|
|
});
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [makeSdkCollectionView({ type: CollectionTypes.DefaultUserCollection })],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
|
|
|
|
|
|
|
|
|
|
expect(result.defaultUserCollectionEmail).toBe(email);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("matches each decrypted view to its source collection by ID", async () => {
|
|
|
|
|
const email = "offboarded@example.com";
|
|
|
|
|
const collection1 = makeCollection({
|
|
|
|
|
defaultUserCollectionEmail: email,
|
|
|
|
|
type: CollectionTypes.DefaultUserCollection,
|
|
|
|
|
});
|
|
|
|
|
const collection2 = makeCollection({
|
|
|
|
|
id: collectionId2,
|
|
|
|
|
type: CollectionTypes.SharedCollection,
|
|
|
|
|
});
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
// Return views in reverse order to confirm ID-based matching, not index-based.
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [
|
|
|
|
|
makeSdkCollectionView({
|
|
|
|
|
id: collectionId2 as any,
|
|
|
|
|
type: CollectionTypes.SharedCollection,
|
|
|
|
|
}),
|
|
|
|
|
makeSdkCollectionView({
|
|
|
|
|
id: collectionId as any,
|
|
|
|
|
type: CollectionTypes.DefaultUserCollection,
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const results = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
|
|
|
|
|
|
|
|
|
|
const view1 = results.find((v) => v.id === collectionId);
|
|
|
|
|
const view2 = results.find((v) => v.id === collectionId2);
|
|
|
|
|
expect(view1?.defaultUserCollectionEmail).toBe(email);
|
|
|
|
|
expect(view2?.defaultUserCollectionEmail).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Pairing is by ID, so a view whose ID does not round-trip cannot be mapped back to its
|
|
|
|
|
// source and drops out of the list - the one disappearance this path cannot represent.
|
|
|
|
|
it("logs a decrypted view whose ID matches no source collection", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({
|
|
|
|
|
successes: [makeSdkCollectionView({ id: collectionId2 as any })],
|
|
|
|
|
failures: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const results = await firstValueFrom(service.decryptMany([collection], userId));
|
|
|
|
|
|
|
|
|
|
expect(results).toEqual([]);
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining(`Decrypted collection ${collectionId2} did not match`),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs the error and rejects when the SDK client is unavailable", async () => {
|
|
|
|
|
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("when CollectionsDecryptListFailures is disabled", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(false));
|
|
|
|
|
describe("decrypt", () => {
|
|
|
|
|
it("decrypts a single collection via the batch SDK call and maps the result", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
const sdkView = makeSdkCollectionView({ name: "Decrypted Name" });
|
|
|
|
|
mockDecryptListWithFailures.mockReturnValue({ successes: [sdkView], failures: [] });
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(service.decrypt(collection, userId));
|
|
|
|
|
|
|
|
|
|
expect(mockDecryptListWithFailures).toHaveBeenCalledWith([stubSdkCollection]);
|
|
|
|
|
expect(mockDecrypt).not.toHaveBeenCalled();
|
|
|
|
|
expect(result).toBeInstanceOf(CollectionView);
|
|
|
|
|
expect(result.name).toBe("Decrypted Name");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("decryptMany", () => {
|
|
|
|
|
it("returns an empty array without calling the SDK for empty input", async () => {
|
|
|
|
|
const result = await firstValueFrom(service.decryptMany([], userId));
|
|
|
|
|
expect(result).toEqual([]);
|
|
|
|
|
expect(mockDecrypt).not.toHaveBeenCalled();
|
|
|
|
|
it("rejects when the batch call throws", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockImplementation(() => {
|
|
|
|
|
throw new Error("batch failure");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("decrypts all collections one at a time and returns views", async () => {
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
mockDecrypt
|
|
|
|
|
.mockReturnValueOnce(makeSdkCollectionView({ name: "Collection 1" }))
|
|
|
|
|
.mockReturnValueOnce(makeSdkCollectionView({ name: "Collection 2" }));
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(
|
|
|
|
|
service.decryptMany([collection1, collection2], userId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
|
|
|
|
|
expect(result).toHaveLength(2);
|
|
|
|
|
expect(result[0].name).toBe("Collection 1");
|
|
|
|
|
expect(result[1].name).toBe("Collection 2");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs and drops items that fail to decrypt without aborting the rest", async () => {
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
mockDecrypt
|
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
|
throw new Error("key not found");
|
|
|
|
|
})
|
|
|
|
|
.mockReturnValueOnce(makeSdkCollectionView({ name: "Collection 2" }));
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(
|
|
|
|
|
service.decryptMany([collection1, collection2], userId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
|
|
|
|
|
);
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
|
expect(result[0].name).toBe("Collection 2");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves defaultUserCollectionEmail from the source collection", async () => {
|
|
|
|
|
const email = "offboarded@example.com";
|
|
|
|
|
const collection = makeCollection({
|
|
|
|
|
defaultUserCollectionEmail: email,
|
|
|
|
|
type: CollectionTypes.DefaultUserCollection,
|
|
|
|
|
});
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecrypt.mockReturnValue(
|
|
|
|
|
makeSdkCollectionView({ type: CollectionTypes.DefaultUserCollection }),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
|
|
|
|
|
|
|
|
|
|
expect(result.defaultUserCollectionEmail).toBe(email);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs the error and rejects when the SDK client is unavailable", async () => {
|
|
|
|
|
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("decrypt", () => {
|
|
|
|
|
it("decrypts a single collection using the original per-item SDK call", async () => {
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecrypt.mockReturnValue(makeSdkCollectionView({ name: "Decrypted Name" }));
|
|
|
|
|
it("logs the error and rejects when the SDK client is unavailable", async () => {
|
|
|
|
|
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(service.decrypt(collection, userId));
|
|
|
|
|
|
|
|
|
|
expect(mockDecrypt).toHaveBeenCalledWith(stubSdkCollection);
|
|
|
|
|
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
|
|
|
|
|
expect(result).toBeInstanceOf(CollectionView);
|
|
|
|
|
expect(result.name).toBe("Decrypted Name");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs the error and rejects when the SDK client is unavailable", async () => {
|
|
|
|
|
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Failed to decrypt collections in batch"),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The SDK bindings for these two calls are currently typed as synchronous, while the equivalent
|
|
|
|
|
* cipher bindings return promises. Both call sites are awaited so the service behaves correctly
|
|
|
|
|
* either way, and these specs pin that down: they hand the mocks a promise, which only produces
|
|
|
|
|
* the right result if the `await` is present. Dropping either `await` makes the spec below fail
|
|
|
|
|
* instead of silently returning an empty list.
|
|
|
|
|
* The SDK binding for `decrypt_list_with_failures` is currently typed as synchronous, while the
|
|
|
|
|
* equivalent cipher bindings return promises. The call site is awaited so the service behaves
|
|
|
|
|
* correctly either way, and these specs pin that down: they hand the mock a promise, which only
|
|
|
|
|
* produces the right result if the `await` is present. Dropping the `await` makes the spec below
|
|
|
|
|
* fail instead of silently returning an empty list.
|
|
|
|
|
*/
|
|
|
|
|
describe("when the SDK bindings return promises", () => {
|
|
|
|
|
it("awaits decrypt_list_with_failures and still separates successes from failures", async () => {
|
|
|
|
|
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(true));
|
|
|
|
|
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
@@ -463,43 +320,7 @@ describe("DefaultCollectionEncryptionService", () => {
|
|
|
|
|
expect(result.find((c) => c.id === collectionId)?.decryptionFailure).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("awaits the per-item decrypt on the V1 path", async () => {
|
|
|
|
|
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(false));
|
|
|
|
|
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecrypt.mockResolvedValue(makeSdkCollectionView({ name: "Decrypted Name" }));
|
|
|
|
|
|
|
|
|
|
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
|
|
|
|
|
|
|
|
|
|
expect(result).toBeInstanceOf(CollectionView);
|
|
|
|
|
expect(result.name).toBe("Decrypted Name");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("logs and drops an item whose per-item decrypt rejects, without aborting the rest", async () => {
|
|
|
|
|
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(false));
|
|
|
|
|
|
|
|
|
|
const collection1 = makeCollection();
|
|
|
|
|
const collection2 = makeCollection({ id: collectionId2 });
|
|
|
|
|
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
|
|
|
|
|
mockDecrypt
|
|
|
|
|
.mockRejectedValueOnce(new Error("key not found"))
|
|
|
|
|
.mockResolvedValueOnce(makeSdkCollectionView({ name: "Collection 2" }));
|
|
|
|
|
|
|
|
|
|
const result = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
|
|
|
|
|
|
|
|
|
|
expect(logService.error).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
|
|
|
|
|
);
|
|
|
|
|
expect(result).toHaveLength(1);
|
|
|
|
|
expect(result[0].name).toBe("Collection 2");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects when a promise-returning decrypt_list_with_failures rejects", async () => {
|
|
|
|
|
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(true));
|
|
|
|
|
|
|
|
|
|
const collection = makeCollection();
|
|
|
|
|
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
|
|
|
|
|
mockDecryptListWithFailures.mockRejectedValue(new Error("batch failure"));
|
|
|
|
|
|