diff --git a/spec/integ/matrix-client-crypto.spec.js b/spec/integ/matrix-client-crypto.spec.js index 7d9fb8181..80fb15f45 100644 --- a/spec/integ/matrix-client-crypto.spec.js +++ b/spec/integ/matrix-client-crypto.spec.js @@ -314,31 +314,37 @@ function recvMessage(httpBackend, client, sender, message) { }, }; httpBackend.when("GET", "/sync").respond(200, syncData); - const deferred = Promise.defer(); - const onEvent = function(event) { - console.log(client.credentials.userId + " received event", - event); - // ignore the m.room.member events - if (event.getType() == "m.room.member") { - return; - } + const eventPromise = new Promise((resolve, reject) => { + const onEvent = function(event) { + // ignore the m.room.member events + if (event.getType() == "m.room.member") { + return; + } + console.log(client.credentials.userId + " received event", + event); + client.removeListener("event", onEvent); + resolve(event); + }; + client.on("event", onEvent); + }); + + httpBackend.flush(); + + return eventPromise.then((event) => { + expect(event.isEncrypted()).toBeTruthy(); + + // it may still be being decrypted + return testUtils.awaitDecryption(event); + }).then((event) => { expect(event.getType()).toEqual("m.room.message"); expect(event.getContent()).toEqual({ msgtype: "m.text", body: "Hello, World", }); expect(event.isEncrypted()).toBeTruthy(); - - client.removeListener("event", onEvent); - deferred.resolve(); - }; - - client.on("event", onEvent); - - httpBackend.flush(); - return deferred.promise; + }); } @@ -583,27 +589,25 @@ describe("MatrixClient crypto", function() { }; bobTestClient.httpBackend.when("GET", "/sync").respond(200, syncData); - const deferred = Promise.defer(); - const onEvent = function(event) { - console.log(bobUserId + " received event", - event); - - // ignore the m.room.member events - if (event.getType() == "m.room.member") { - return; - } - - expect(event.isEncrypted()).toBeTruthy(); - - expect(event.getType()).toEqual("m.room.message"); - expect(event.getContent().msgtype).toEqual("m.bad.encrypted"); - deferred.resolve(); - }; - - bobTestClient.client.once("event", onEvent); + const eventPromise = new Promise((resolve, reject) => { + const onEvent = function(event) { + console.log(bobUserId + " received event", + event); + resolve(event); + }; + bobTestClient.client.once("event", onEvent); + }); bobTestClient.httpBackend.flush(); - return deferred.promise; + return eventPromise; + }).then((event) => { + expect(event.isEncrypted()).toBeTruthy(); + + // it may still be being decrypted + return testUtils.awaitDecryption(event); + }).then((event) => { + expect(event.getType()).toEqual("m.room.message"); + expect(event.getContent().msgtype).toEqual("m.bad.encrypted"); }); }); diff --git a/spec/integ/megolm-integ.spec.js b/spec/integ/megolm-integ.spec.js index 639be831d..e77a0e332 100644 --- a/spec/integ/megolm-integ.spec.js +++ b/spec/integ/megolm-integ.spec.js @@ -119,6 +119,7 @@ function encryptMegolmEvent(opts) { } return { + event_id: 'test_megolm_event', content: { algorithm: "m.megolm.v1.aes-sha2", ciphertext: opts.groupSession.encrypt(JSON.stringify(plaintext)), @@ -342,6 +343,9 @@ describe("megolm", function() { }).then(function() { const room = aliceTestClient.client.getRoom(ROOM_ID); const event = room.getLiveTimeline().getEvents()[0]; + expect(event.isEncrypted()).toBe(true); + return testUtils.awaitDecryption(event); + }).then((event) => { expect(event.getContent().body).toEqual('42'); }); }); diff --git a/spec/test-utils.js b/spec/test-utils.js index 2448d9874..d0b673568 100644 --- a/spec/test-utils.js +++ b/spec/test-utils.js @@ -219,3 +219,25 @@ module.exports.MockStorageApi.prototype = { delete this.data[k]; }, }; + + +/** + * If an event is being decrypted, wait for it to finish being decrypted. + * + * @param {MatrixEvent} event + * @returns {Promise} promise which resolves (to `event`) when the event has been decrypted + */ +module.exports.awaitDecryption = function(event) { + if (!event.isBeingDecrypted()) { + return Promise.resolve(event); + } + + console.log(`${Date.now()} event ${event.getId()} is being decrypted; waiting`); + + return new Promise((resolve, reject) => { + event.once('Event.decrypted', (ev) => { + console.log(`${Date.now()} event ${event.getId()} now decrypted`); + resolve(ev); + }); + }); +};