Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom in ContentMessages.ts (#28238)

This commit is contained in:
Florian Duros 2024-10-18 16:44:56 +02:00 committed by GitHub
parent 4e93233a3d
commit fad457362d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 21 deletions

View file

@ -337,7 +337,7 @@ export async function uploadFile(
const abortController = controller ?? new AbortController(); const abortController = controller ?? new AbortController();
// If the room is encrypted then encrypt the file before uploading it. // If the room is encrypted then encrypt the file before uploading it.
if (matrixClient.isRoomEncrypted(roomId)) { if (await matrixClient.getCrypto()?.isEncryptionEnabledInRoom(roomId)) {
// First read the file into memory. // First read the file into memory.
const data = await readFileAsArrayBuffer(file); const data = await readFileAsArrayBuffer(file);
if (abortController.signal.aborted) throw new UploadCanceledError(); if (abortController.signal.aborted) throw new UploadCanceledError();

View file

@ -125,7 +125,7 @@ export function createTestClient(): MatrixClient {
getUserVerificationStatus: jest.fn(), getUserVerificationStatus: jest.fn(),
getDeviceVerificationStatus: jest.fn(), getDeviceVerificationStatus: jest.fn(),
resetKeyBackup: jest.fn(), resetKeyBackup: jest.fn(),
isEncryptionEnabledInRoom: jest.fn(), isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]), getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]),
setDeviceIsolationMode: jest.fn(), setDeviceIsolationMode: jest.fn(),
prepareToEncrypt: jest.fn(), prepareToEncrypt: jest.fn(),
@ -273,6 +273,7 @@ export function createTestClient(): MatrixClient {
isFallbackICEServerAllowed: jest.fn().mockReturnValue(false), isFallbackICEServerAllowed: jest.fn().mockReturnValue(false),
getAuthIssuer: jest.fn(), getAuthIssuer: jest.fn(),
getOrCreateFilter: jest.fn(), getOrCreateFilter: jest.fn(),
sendStickerMessage: jest.fn(),
} as unknown as MatrixClient; } as unknown as MatrixClient;
client.reEmitter = new ReEmitter(client); client.reEmitter = new ReEmitter(client);

View file

@ -14,7 +14,7 @@ import encrypt, { IEncryptedFile } from "matrix-encrypt-attachment";
import ContentMessages, { UploadCanceledError, uploadFile } from "../../src/ContentMessages"; import ContentMessages, { UploadCanceledError, uploadFile } from "../../src/ContentMessages";
import { doMaybeLocalRoomAction } from "../../src/utils/local-room"; import { doMaybeLocalRoomAction } from "../../src/utils/local-room";
import { createTestClient, mkEvent } from "../test-utils"; import { createTestClient, flushPromises, mkEvent } from "../test-utils";
import { BlurhashEncoder } from "../../src/BlurhashEncoder"; import { BlurhashEncoder } from "../../src/BlurhashEncoder";
jest.mock("matrix-encrypt-attachment", () => ({ encryptAttachment: jest.fn().mockResolvedValue({}) })); jest.mock("matrix-encrypt-attachment", () => ({ encryptAttachment: jest.fn().mockResolvedValue({}) }));
@ -43,13 +43,7 @@ describe("ContentMessages", () => {
let prom: Promise<ISendEventResponse>; let prom: Promise<ISendEventResponse>;
beforeEach(() => { beforeEach(() => {
client = { client = createTestClient();
getSafeUserId: jest.fn().mockReturnValue("@alice:test"),
sendStickerMessage: jest.fn(),
sendMessage: jest.fn(),
isRoomEncrypted: jest.fn().mockReturnValue(false),
uploadContent: jest.fn().mockResolvedValue({ content_uri: "mxc://server/file" }),
} as unknown as MatrixClient;
contentMessages = new ContentMessages(); contentMessages = new ContentMessages();
prom = Promise.resolve<ISendEventResponse>({ event_id: "$event_id" }); prom = Promise.resolve<ISendEventResponse>({ event_id: "$event_id" });
}); });
@ -262,6 +256,7 @@ describe("ContentMessages", () => {
expect(upload.loaded).toBe(0); expect(upload.loaded).toBe(0);
expect(upload.total).toBe(file.size); expect(upload.total).toBe(file.size);
await flushPromises();
const { progressHandler } = mocked(client.uploadContent).mock.calls[0][1]!; const { progressHandler } = mocked(client.uploadContent).mock.calls[0][1]!;
progressHandler!({ loaded: 123, total: 1234 }); progressHandler!({ loaded: 123, total: 1234 });
expect(upload.loaded).toBe(123); expect(upload.loaded).toBe(123);
@ -342,6 +337,7 @@ describe("ContentMessages", () => {
mocked(client.uploadContent).mockReturnValue(deferred.promise); mocked(client.uploadContent).mockReturnValue(deferred.promise);
const file1 = new File([], "file1"); const file1 = new File([], "file1");
const prom = contentMessages.sendContentToRoom(file1, roomId, undefined, client, undefined); const prom = contentMessages.sendContentToRoom(file1, roomId, undefined, client, undefined);
await flushPromises();
const { abortController } = mocked(client.uploadContent).mock.calls[0][1]!; const { abortController } = mocked(client.uploadContent).mock.calls[0][1]!;
expect(abortController!.signal.aborted).toBeFalsy(); expect(abortController!.signal.aborted).toBeFalsy();
const [upload] = contentMessages.getCurrentUploads(); const [upload] = contentMessages.getCurrentUploads();
@ -354,14 +350,14 @@ describe("ContentMessages", () => {
}); });
describe("uploadFile", () => { describe("uploadFile", () => {
let client: MatrixClient;
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
client = createTestClient();
}); });
const client = createTestClient();
it("should not encrypt the file if the room isn't encrypted", async () => { it("should not encrypt the file if the room isn't encrypted", async () => {
mocked(client.isRoomEncrypted).mockReturnValue(false);
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" }); mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
const progressHandler = jest.fn(); const progressHandler = jest.fn();
const file = new Blob([]); const file = new Blob([]);
@ -375,7 +371,7 @@ describe("uploadFile", () => {
}); });
it("should encrypt the file if the room is encrypted", async () => { it("should encrypt the file if the room is encrypted", async () => {
mocked(client.isRoomEncrypted).mockReturnValue(true); jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" }); mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
mocked(encrypt.encryptAttachment).mockResolvedValue({ mocked(encrypt.encryptAttachment).mockResolvedValue({
data: new ArrayBuffer(123), data: new ArrayBuffer(123),
@ -405,14 +401,13 @@ describe("uploadFile", () => {
}); });
it("should throw UploadCanceledError upon aborting the upload", async () => { it("should throw UploadCanceledError upon aborting the upload", async () => {
mocked(client.isRoomEncrypted).mockReturnValue(false); mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://foo/bar" });
const deferred = defer<UploadResponse>();
mocked(client.uploadContent).mockReturnValue(deferred.promise);
const file = new Blob([]); const file = new Blob([]);
const controller = new AbortController();
controller.abort();
const prom = uploadFile(client, "!roomId:server", file); await expect(uploadFile(client, "!roomId:server", file, undefined, controller)).rejects.toThrow(
mocked(client.uploadContent).mock.calls[0][1]!.abortController!.abort(); UploadCanceledError,
deferred.resolve({ content_uri: "mxc://foo/bar" }); );
await expect(prom).rejects.toThrow(UploadCanceledError);
}); });
}); });