Skip to content

Commit 71e51a0

Browse files
committed
megolm tests: Make DeviceList stubbing conditional
In preparation for running these tests against the rust crypto backend, make some of the stubbing conditional on whether we have a `client.crypto`. Really, we need to do something better here.
1 parent 85b34b4 commit 71e51a0

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

spec/integ/megolm-integ.spec.ts

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,18 @@ describe("megolm", () => {
424424

425425
it("Alice receives a megolm message", async () => {
426426
await aliceTestClient.start();
427-
aliceTestClient.client.crypto!.deviceList.downloadKeys = () => Promise.resolve({});
427+
428+
// if we're using the old crypto impl, stub out some methods in the device manager.
429+
// TODO: replace this with intercepts of the /keys/query endpoint to make it impl agnostic.
430+
if (aliceTestClient.client.crypto) {
431+
aliceTestClient.client.crypto.deviceList.downloadKeys = () => Promise.resolve({});
432+
aliceTestClient.client.crypto.deviceList.getUserByIdentityKey = () => "@bob:xyz";
433+
}
434+
428435
const p2pSession = await createOlmSession(testOlmAccount, aliceTestClient);
429436
const groupSession = new Olm.OutboundGroupSession();
430437
groupSession.create();
431438

432-
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => "@bob:xyz";
433-
434439
// make the room_key event
435440
const roomKeyEncrypted = encryptGroupSessionKey({
436441
recipient: aliceTestClient,
@@ -472,16 +477,21 @@ describe("megolm", () => {
472477
expect(decryptedEvent.getContent().body).toEqual("42");
473478
});
474479

475-
it("Alice receives a megolm message before the session keys", async () => {
480+
oldBackendOnly("Alice receives a megolm message before the session keys", async () => {
476481
// https://github.com/vector-im/element-web/issues/2273
477482
await aliceTestClient.start();
478-
aliceTestClient.client.crypto!.deviceList.downloadKeys = () => Promise.resolve({});
483+
484+
// if we're using the old crypto impl, stub out some methods in the device manager.
485+
// TODO: replace this with intercepts of the /keys/query endpoint to make it impl agnostic.
486+
if (aliceTestClient.client.crypto) {
487+
aliceTestClient.client.crypto.deviceList.downloadKeys = () => Promise.resolve({});
488+
aliceTestClient.client.crypto.deviceList.getUserByIdentityKey = () => "@bob:xyz";
489+
}
490+
479491
const p2pSession = await createOlmSession(testOlmAccount, aliceTestClient);
480492
const groupSession = new Olm.OutboundGroupSession();
481493
groupSession.create();
482494

483-
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => "@bob:xyz";
484-
485495
// make the room_key event, but don't send it yet
486496
const roomKeyEncrypted = encryptGroupSessionKey({
487497
recipient: aliceTestClient,
@@ -535,13 +545,18 @@ describe("megolm", () => {
535545

536546
it("Alice gets a second room_key message", async () => {
537547
await aliceTestClient.start();
538-
aliceTestClient.client.crypto!.deviceList.downloadKeys = () => Promise.resolve({});
548+
549+
// if we're using the old crypto impl, stub out some methods in the device manager.
550+
// TODO: replace this with intercepts of the /keys/query endpoint to make it impl agnostic.
551+
if (aliceTestClient.client.crypto) {
552+
aliceTestClient.client.crypto.deviceList.downloadKeys = () => Promise.resolve({});
553+
aliceTestClient.client.crypto.deviceList.getUserByIdentityKey = () => "@bob:xyz";
554+
}
555+
539556
const p2pSession = await createOlmSession(testOlmAccount, aliceTestClient);
540557
const groupSession = new Olm.OutboundGroupSession();
541558
groupSession.create();
542559

543-
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => "@bob:xyz";
544-
545560
// make the room_key event
546561
const roomKeyEncrypted1 = encryptGroupSessionKey({
547562
recipient: aliceTestClient,
@@ -972,15 +987,20 @@ describe("megolm", () => {
972987
await Promise.all([downloadPromise, sendPromise]);
973988
});
974989

975-
it("Alice exports megolm keys and imports them to a new device", async () => {
990+
oldBackendOnly("Alice exports megolm keys and imports them to a new device", async () => {
976991
aliceTestClient.expectKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
977992
await aliceTestClient.start();
978-
aliceTestClient.client.crypto!.deviceList.downloadKeys = () => Promise.resolve({});
993+
994+
// if we're using the old crypto impl, stub out some methods in the device manager.
995+
// TODO: replace this with intercepts of the /keys/query endpoint to make it impl agnostic.
996+
if (aliceTestClient.client.crypto) {
997+
aliceTestClient.client.crypto.deviceList.downloadKeys = () => Promise.resolve({});
998+
aliceTestClient.client.crypto.deviceList.getUserByIdentityKey = () => "@bob:xyz";
999+
}
1000+
9791001
// establish an olm session with alice
9801002
const p2pSession = await createOlmSession(testOlmAccount, aliceTestClient);
9811003

982-
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => "@bob:xyz";
983-
9841004
const groupSession = new Olm.OutboundGroupSession();
9851005
groupSession.create();
9861006

@@ -1031,7 +1051,11 @@ describe("megolm", () => {
10311051
await aliceTestClient.client.importRoomKeys(exported);
10321052
await aliceTestClient.start();
10331053

1034-
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => "@bob:xyz";
1054+
// if we're using the old crypto impl, stub out some methods in the device manager.
1055+
// TODO: replace this with intercepts of the /keys/query endpoint to make it impl agnostic.
1056+
if (aliceTestClient.client.crypto) {
1057+
aliceTestClient.client.crypto.deviceList.getUserByIdentityKey = () => "@bob:xyz";
1058+
}
10351059

10361060
const syncResponse = {
10371061
next_batch: 1,
@@ -1107,13 +1131,18 @@ describe("megolm", () => {
11071131

11081132
it("Alice can decrypt a message with falsey content", async () => {
11091133
await aliceTestClient.start();
1110-
aliceTestClient.client.crypto!.deviceList.downloadKeys = () => Promise.resolve({});
1134+
1135+
// if we're using the old crypto impl, stub out some methods in the device manager.
1136+
// TODO: replace this with intercepts of the /keys/query endpoint to make it impl agnostic.
1137+
if (aliceTestClient.client.crypto) {
1138+
aliceTestClient.client.crypto.deviceList.downloadKeys = () => Promise.resolve({});
1139+
aliceTestClient.client.crypto.deviceList.getUserByIdentityKey = () => "@bob:xyz";
1140+
}
1141+
11111142
const p2pSession = await createOlmSession(testOlmAccount, aliceTestClient);
11121143
const groupSession = new Olm.OutboundGroupSession();
11131144
groupSession.create();
11141145

1115-
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => "@bob:xyz";
1116-
11171146
// make the room_key event
11181147
const roomKeyEncrypted = encryptGroupSessionKey({
11191148
recipient: aliceTestClient,
@@ -1165,9 +1194,16 @@ describe("megolm", () => {
11651194
await beccaTestClient.client.initCrypto();
11661195

11671196
await aliceTestClient.start();
1168-
aliceTestClient.client.crypto!.deviceList.downloadKeys = () => Promise.resolve({});
11691197
await beccaTestClient.start();
11701198

1199+
// if we're using the old crypto impl, stub out some methods in the device manager.
1200+
// TODO: replace this with intercepts of the /keys/query endpoint to make it impl agnostic.
1201+
if (aliceTestClient.client.crypto) {
1202+
aliceTestClient.client.crypto!.deviceList.downloadKeys = () => Promise.resolve({});
1203+
aliceTestClient.client.crypto!.deviceList.getDeviceByIdentityKey = () => device;
1204+
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => beccaTestClient.client.getUserId()!;
1205+
}
1206+
11711207
const beccaRoom = new Room(ROOM_ID, beccaTestClient.client, "@becca:localhost", {});
11721208
beccaTestClient.client.store.storeRoom(beccaRoom);
11731209
await beccaTestClient.client.setRoomEncryption(ROOM_ID, { algorithm: "m.megolm.v1.aes-sha2" });
@@ -1193,8 +1229,6 @@ describe("megolm", () => {
11931229
event.claimedEd25519Key = null;
11941230

11951231
const device = new DeviceInfo(beccaTestClient.client.deviceId!);
1196-
aliceTestClient.client.crypto!.deviceList.getDeviceByIdentityKey = () => device;
1197-
aliceTestClient.client.crypto!.deviceList.getUserByIdentityKey = () => beccaTestClient.client.getUserId()!;
11981232

11991233
// Create an olm session for Becca and Alice's devices
12001234
const aliceOtks = await aliceTestClient.awaitOneTimeKeyUpload();

0 commit comments

Comments
 (0)