You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-28 05:03:59 +03:00
Add delays to tests to wait for things to decrypt
Prepare for some refactoring which will add an extra tick to decryption by adding some `awaitDecryption` calls in the integration tests.
This commit is contained in:
@@ -314,31 +314,37 @@ function recvMessage(httpBackend, client, sender, message) {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
httpBackend.when("GET", "/sync").respond(200, syncData);
|
httpBackend.when("GET", "/sync").respond(200, syncData);
|
||||||
const deferred = Promise.defer();
|
|
||||||
const onEvent = function(event) {
|
|
||||||
console.log(client.credentials.userId + " received event",
|
|
||||||
event);
|
|
||||||
|
|
||||||
|
const eventPromise = new Promise((resolve, reject) => {
|
||||||
|
const onEvent = function(event) {
|
||||||
// ignore the m.room.member events
|
// ignore the m.room.member events
|
||||||
if (event.getType() == "m.room.member") {
|
if (event.getType() == "m.room.member") {
|
||||||
return;
|
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.getType()).toEqual("m.room.message");
|
||||||
expect(event.getContent()).toEqual({
|
expect(event.getContent()).toEqual({
|
||||||
msgtype: "m.text",
|
msgtype: "m.text",
|
||||||
body: "Hello, World",
|
body: "Hello, World",
|
||||||
});
|
});
|
||||||
expect(event.isEncrypted()).toBeTruthy();
|
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);
|
bobTestClient.httpBackend.when("GET", "/sync").respond(200, syncData);
|
||||||
|
|
||||||
const deferred = Promise.defer();
|
const eventPromise = new Promise((resolve, reject) => {
|
||||||
const onEvent = function(event) {
|
const onEvent = function(event) {
|
||||||
console.log(bobUserId + " received event",
|
console.log(bobUserId + " received event",
|
||||||
event);
|
event);
|
||||||
|
resolve(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);
|
bobTestClient.client.once("event", onEvent);
|
||||||
|
});
|
||||||
|
|
||||||
bobTestClient.httpBackend.flush();
|
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");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ function encryptMegolmEvent(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
event_id: 'test_megolm_event',
|
||||||
content: {
|
content: {
|
||||||
algorithm: "m.megolm.v1.aes-sha2",
|
algorithm: "m.megolm.v1.aes-sha2",
|
||||||
ciphertext: opts.groupSession.encrypt(JSON.stringify(plaintext)),
|
ciphertext: opts.groupSession.encrypt(JSON.stringify(plaintext)),
|
||||||
@@ -342,6 +343,9 @@ describe("megolm", function() {
|
|||||||
}).then(function() {
|
}).then(function() {
|
||||||
const room = aliceTestClient.client.getRoom(ROOM_ID);
|
const room = aliceTestClient.client.getRoom(ROOM_ID);
|
||||||
const event = room.getLiveTimeline().getEvents()[0];
|
const event = room.getLiveTimeline().getEvents()[0];
|
||||||
|
expect(event.isEncrypted()).toBe(true);
|
||||||
|
return testUtils.awaitDecryption(event);
|
||||||
|
}).then((event) => {
|
||||||
expect(event.getContent().body).toEqual('42');
|
expect(event.getContent().body).toEqual('42');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -219,3 +219,25 @@ module.exports.MockStorageApi.prototype = {
|
|||||||
delete this.data[k];
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user