refactor: update letter decryption test to look for key request properties

This commit is contained in:
ramvignesh-b
2026-04-26 19:22:58 +05:30
parent b1d466fb11
commit f242977be3
2 changed files with 30 additions and 15 deletions
+8 -6
View File
@@ -152,9 +152,10 @@ describe("letterLogic image helpers", () => {
crypto, crypto,
); );
expect(api.get).toHaveBeenCalledWith("https://remote/photo.png.bin", { expect(api.get).toHaveBeenCalledWith(
responseType: "blob", "https://remote/photo.png.bin",
}); expect.objectContaining({ responseType: "blob" }),
);
expect(CryptoUtils.prototype.decryptImage).toHaveBeenCalledWith( expect(CryptoUtils.prototype.decryptImage).toHaveBeenCalledWith(
expect.any(Blob), expect.any(Blob),
"wrapped-dek", "wrapped-dek",
@@ -238,9 +239,10 @@ describe("letterLogic image helpers", () => {
crypto, crypto,
); );
expect(api.get).toHaveBeenCalledWith("https://remote/photo.png.bin", { expect(api.get).toHaveBeenCalledWith(
responseType: "blob", "https://remote/photo.png.bin",
}); expect.objectContaining({ responseType: "blob" }),
);
expect( expect(
CryptoUtils.prototype.decryptImageWithSharingKey, CryptoUtils.prototype.decryptImageWithSharingKey,
).toHaveBeenCalledWith(expect.any(Blob), "raw-sharing-key"); ).toHaveBeenCalledWith(expect.any(Blob), "raw-sharing-key");
+22 -9
View File
@@ -28,14 +28,18 @@ export async function decryptCanvasImages(
remoteImages.map((img) => [img.file_name, img.file]), remoteImages.map((img) => [img.file_name, img.file]),
); );
const decryptionPromises = canvasData.objects.map(async (obj, index) => { const imageDecryptionPromises = canvasData.objects.map(async (obj, index) => {
if (obj.type !== "Image") return; if (obj.type !== "Image") return;
const imgObj = obj as FabricImageJSON; const imgObj = obj as FabricImageJSON;
const remoteUrl = imageMap.get(imgObj.src); const remoteUrl = imageMap.get(imgObj.src);
if (!remoteUrl) return; if (!remoteUrl) return;
try { try {
const res = await api.get(remoteUrl, { responseType: "blob" }); // HACK: For S3 Storage fetch and avoiding CORS error
const res = await api.get(remoteUrl, {
responseType: "blob",
withCredentials: false,
});
const originalSrc = imgObj.src; const originalSrc = imgObj.src;
const blobUrl = await cryptoUtils.decryptImage( const blobUrl = await cryptoUtils.decryptImage(
@@ -56,7 +60,7 @@ export async function decryptCanvasImages(
} }
}); });
await Promise.all(decryptionPromises); await Promise.all(imageDecryptionPromises);
canvasData.objects = canvasData.objects.filter(Boolean); canvasData.objects = canvasData.objects.filter(Boolean);
return { isDecryptionPartialFailure, error }; return { isDecryptionPartialFailure, error };
} }
@@ -66,14 +70,16 @@ export async function decryptCanvasImagesWithSharingKey(
remoteImages: { file_name: string; file: string }[], remoteImages: { file_name: string; file: string }[],
sharingKey: string, sharingKey: string,
cryptoUtils: CryptoUtils, cryptoUtils: CryptoUtils,
) { ): Promise<{ isDecryptionPartialFailure: boolean; error: string }> {
if (!canvasData?.objects) return; if (!canvasData?.objects)
return { isDecryptionPartialFailure: false, error: "" };
let isDecryptionPartialFailure = false;
let error = "";
const imageMap = new Map( const imageMap = new Map(
remoteImages.map((img) => [img.file_name, img.file]), remoteImages.map((img) => [img.file_name, img.file]),
); );
const decryptionPromises = canvasData.objects.map(async (obj) => { const decryptionPromises = canvasData.objects.map(async (obj, index) => {
if (obj.type !== "Image") return; if (obj.type !== "Image") return;
const imgObj = obj as FabricImageJSON; const imgObj = obj as FabricImageJSON;
@@ -81,17 +87,24 @@ export async function decryptCanvasImagesWithSharingKey(
if (!remoteUrl) return; if (!remoteUrl) return;
try { try {
const res = await api.get(remoteUrl, { responseType: "blob" }); const res = await api.get(remoteUrl, {
responseType: "blob",
withCredentials: false,
});
imgObj.src = await cryptoUtils.decryptImageWithSharingKey( imgObj.src = await cryptoUtils.decryptImageWithSharingKey(
res.data, res.data,
sharingKey, sharingKey,
); );
} catch (_error) { } catch (_error) {
// Keep original or handle failure delete canvasData.objects[index];
isDecryptionPartialFailure = true;
error = _error instanceof Error ? _error.message : "Unknown error";
} }
}); });
await Promise.all(decryptionPromises); await Promise.all(decryptionPromises);
canvasData.objects = canvasData.objects.filter(Boolean);
return { isDecryptionPartialFailure, error };
} }
export async function encryptCanvasImages( export async function encryptCanvasImages(