Skip to content

Commit f660a6f

Browse files
committed
Add a test for setRoomEncryption
1 parent 9d20db1 commit f660a6f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

spec/unit/crypto.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { MemoryStore } from "../../src";
1919
import { RoomKeyRequestState } from '../../src/crypto/OutgoingRoomKeyRequestManager';
2020
import { RoomMember } from '../../src/models/room-member';
2121
import { IStore } from '../../src/store';
22+
import { IRoomEncryption, RoomList } from "../../src/crypto/RoomList";
2223

2324
const Olm = global.Olm;
2425

@@ -1140,4 +1141,48 @@ describe("Crypto", function() {
11401141
await client!.client.crypto!.start();
11411142
});
11421143
});
1144+
1145+
describe("setRoomEncryption", () => {
1146+
let mockClient: MatrixClient;
1147+
let mockRoomList: RoomList;
1148+
let clientStore: IStore;
1149+
let crypto: Crypto;
1150+
1151+
beforeEach(async function() {
1152+
mockClient = {} as MatrixClient;
1153+
const mockStorage = new MockStorageApi() as unknown as Storage;
1154+
clientStore = new MemoryStore({ localStorage: mockStorage }) as unknown as IStore;
1155+
const cryptoStore = new MemoryCryptoStore();
1156+
1157+
mockRoomList = {
1158+
getRoomEncryption: jest.fn().mockReturnValue(null),
1159+
setRoomEncryption: jest.fn().mockResolvedValue(undefined),
1160+
} as unknown as RoomList;
1161+
1162+
crypto = new Crypto(
1163+
mockClient,
1164+
"@alice:home.server",
1165+
"FLIBBLE",
1166+
clientStore,
1167+
cryptoStore,
1168+
mockRoomList,
1169+
[],
1170+
);
1171+
});
1172+
1173+
it("should set the algorithm if called for a known room", async () => {
1174+
const room = new Room("!room:id", mockClient, "@my.user:id");
1175+
await clientStore.storeRoom(room);
1176+
await crypto.setRoomEncryption("!room:id", { algorithm: "m.megolm.v1.aes-sha2" } as IRoomEncryption);
1177+
expect(mockRoomList!.setRoomEncryption).toHaveBeenCalledTimes(1);
1178+
expect(jest.mocked(mockRoomList!.setRoomEncryption).mock.calls[0][0]).toEqual("!room:id");
1179+
});
1180+
1181+
it("should raise if called for an unknown room", async () => {
1182+
await expect(async () => {
1183+
await crypto.setRoomEncryption("!room:id", { algorithm: "m.megolm.v1.aes-sha2" } as IRoomEncryption);
1184+
}).rejects.toThrow(/unknown room/);
1185+
expect(mockRoomList!.setRoomEncryption).not.toHaveBeenCalled();
1186+
});
1187+
});
11431188
});

0 commit comments

Comments
 (0)