Skip to content

Commit 42cb716

Browse files
committed
Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom in ContentMessages.ts
1 parent 06d1239 commit 42cb716

3 files changed

Lines changed: 17 additions & 21 deletions

File tree

src/ContentMessages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export async function uploadFile(
337337
const abortController = controller ?? new AbortController();
338338

339339
// If the room is encrypted then encrypt the file before uploading it.
340-
if (matrixClient.isRoomEncrypted(roomId)) {
340+
if (await matrixClient.getCrypto()?.isEncryptionEnabledInRoom(roomId)) {
341341
// First read the file into memory.
342342
const data = await readFileAsArrayBuffer(file);
343343
if (abortController.signal.aborted) throw new UploadCanceledError();

test/test-utils/test-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function createTestClient(): MatrixClient {
125125
getUserVerificationStatus: jest.fn(),
126126
getDeviceVerificationStatus: jest.fn(),
127127
resetKeyBackup: jest.fn(),
128-
isEncryptionEnabledInRoom: jest.fn(),
128+
isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
129129
getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]),
130130
setDeviceIsolationMode: jest.fn(),
131131
prepareToEncrypt: jest.fn(),
@@ -273,6 +273,7 @@ export function createTestClient(): MatrixClient {
273273
isFallbackICEServerAllowed: jest.fn().mockReturnValue(false),
274274
getAuthIssuer: jest.fn(),
275275
getOrCreateFilter: jest.fn(),
276+
sendStickerMessage: jest.fn(),
276277
} as unknown as MatrixClient;
277278

278279
client.reEmitter = new ReEmitter(client);

test/unit-tests/ContentMessages-test.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import encrypt, { IEncryptedFile } from "matrix-encrypt-attachment";
1414

1515
import ContentMessages, { UploadCanceledError, uploadFile } from "../../src/ContentMessages";
1616
import { doMaybeLocalRoomAction } from "../../src/utils/local-room";
17-
import { createTestClient, mkEvent } from "../test-utils";
17+
import { createTestClient, flushPromises, mkEvent } from "../test-utils";
1818
import { BlurhashEncoder } from "../../src/BlurhashEncoder";
1919

2020
jest.mock("matrix-encrypt-attachment", () => ({ encryptAttachment: jest.fn().mockResolvedValue({}) }));
@@ -43,13 +43,7 @@ describe("ContentMessages", () => {
4343
let prom: Promise<ISendEventResponse>;
4444

4545
beforeEach(() => {
46-
client = {
47-
getSafeUserId: jest.fn().mockReturnValue("@alice:test"),
48-
sendStickerMessage: jest.fn(),
49-
sendMessage: jest.fn(),
50-
isRoomEncrypted: jest.fn().mockReturnValue(false),
51-
uploadContent: jest.fn().mockResolvedValue({ content_uri: "mxc://server/file" }),
52-
} as unknown as MatrixClient;
46+
client = createTestClient();
5347
contentMessages = new ContentMessages();
5448
prom = Promise.resolve<ISendEventResponse>({ event_id: "$event_id" });
5549
});
@@ -262,6 +256,7 @@ describe("ContentMessages", () => {
262256

263257
expect(upload.loaded).toBe(0);
264258
expect(upload.total).toBe(file.size);
259+
await flushPromises();
265260
const { progressHandler } = mocked(client.uploadContent).mock.calls[0][1]!;
266261
progressHandler!({ loaded: 123, total: 1234 });
267262
expect(upload.loaded).toBe(123);
@@ -342,6 +337,7 @@ describe("ContentMessages", () => {
342337
mocked(client.uploadContent).mockReturnValue(deferred.promise);
343338
const file1 = new File([], "file1");
344339
const prom = contentMessages.sendContentToRoom(file1, roomId, undefined, client, undefined);
340+
await flushPromises();
345341
const { abortController } = mocked(client.uploadContent).mock.calls[0][1]!;
346342
expect(abortController!.signal.aborted).toBeFalsy();
347343
const [upload] = contentMessages.getCurrentUploads();
@@ -354,14 +350,14 @@ describe("ContentMessages", () => {
354350
});
355351

356352
describe("uploadFile", () => {
353+
let client: MatrixClient;
354+
357355
beforeEach(() => {
358356
jest.clearAllMocks();
357+
client = createTestClient();
359358
});
360359

361-
const client = createTestClient();
362-
363360
it("should not encrypt the file if the room isn't encrypted", async () => {
364-
mocked(client.isRoomEncrypted).mockReturnValue(false);
365361
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
366362
const progressHandler = jest.fn();
367363
const file = new Blob([]);
@@ -375,7 +371,7 @@ describe("uploadFile", () => {
375371
});
376372

377373
it("should encrypt the file if the room is encrypted", async () => {
378-
mocked(client.isRoomEncrypted).mockReturnValue(true);
374+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
379375
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
380376
mocked(encrypt.encryptAttachment).mockResolvedValue({
381377
data: new ArrayBuffer(123),
@@ -405,14 +401,13 @@ describe("uploadFile", () => {
405401
});
406402

407403
it("should throw UploadCanceledError upon aborting the upload", async () => {
408-
mocked(client.isRoomEncrypted).mockReturnValue(false);
409-
const deferred = defer<UploadResponse>();
410-
mocked(client.uploadContent).mockReturnValue(deferred.promise);
404+
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://foo/bar" });
411405
const file = new Blob([]);
406+
const controller = new AbortController();
407+
controller.abort();
412408

413-
const prom = uploadFile(client, "!roomId:server", file);
414-
mocked(client.uploadContent).mock.calls[0][1]!.abortController!.abort();
415-
deferred.resolve({ content_uri: "mxc://foo/bar" });
416-
await expect(prom).rejects.toThrow(UploadCanceledError);
409+
await expect(uploadFile(client, "!roomId:server", file, undefined, controller)).rejects.toThrow(
410+
UploadCanceledError,
411+
);
417412
});
418413
});

0 commit comments

Comments
 (0)