2021-06-29 12:11:58 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2020-02-13 14:13:10 +00:00
|
|
|
|
2021-10-22 22:23:32 +00:00
|
|
|
import { waitForMember, canEncryptToAllUsers } from '../src/createRoom';
|
|
|
|
|
2020-02-13 14:13:10 +00:00
|
|
|
/* Shorter timeout, we've got tests to run */
|
|
|
|
const timeout = 30;
|
|
|
|
|
|
|
|
describe("waitForMember", () => {
|
|
|
|
let client;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = new EventEmitter();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("resolves with false if the timeout is reached", (done) => {
|
2021-07-19 21:43:11 +00:00
|
|
|
waitForMember(client, "", "", { timeout: 0 }).then((r) => {
|
2020-02-13 14:13:10 +00:00
|
|
|
expect(r).toBe(false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("resolves with false if the timeout is reached, even if other RoomState.newMember events fire", (done) => {
|
|
|
|
const roomId = "!roomId:domain";
|
|
|
|
const userId = "@clientId:domain";
|
2021-07-19 21:43:11 +00:00
|
|
|
waitForMember(client, roomId, userId, { timeout }).then((r) => {
|
2020-02-13 14:13:10 +00:00
|
|
|
expect(r).toBe(false);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
client.emit("RoomState.newMember", undefined, undefined, { roomId, userId: "@anotherClient:domain" });
|
2020-02-17 16:00:25 +00:00
|
|
|
});
|
2020-02-13 14:13:10 +00:00
|
|
|
|
|
|
|
it("resolves with true if RoomState.newMember fires", (done) => {
|
|
|
|
const roomId = "!roomId:domain";
|
|
|
|
const userId = "@clientId:domain";
|
2021-07-19 21:43:11 +00:00
|
|
|
waitForMember(client, roomId, userId, { timeout }).then((r) => {
|
2020-02-13 14:13:10 +00:00
|
|
|
expect(r).toBe(true);
|
|
|
|
expect(client.listeners("RoomState.newMember").length).toBe(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
client.emit("RoomState.newMember", undefined, undefined, { roomId, userId });
|
2020-02-17 16:00:25 +00:00
|
|
|
});
|
|
|
|
});
|
2020-02-18 11:25:19 +00:00
|
|
|
|
|
|
|
describe("canEncryptToAllUsers", () => {
|
|
|
|
const trueUser = {
|
|
|
|
"@goodUser:localhost": {
|
|
|
|
"DEV1": {},
|
|
|
|
"DEV2": {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const falseUser = {
|
|
|
|
"@badUser:localhost": {},
|
|
|
|
};
|
|
|
|
|
2022-02-02 12:02:17 +00:00
|
|
|
it("returns true if all devices have crypto", async () => {
|
2020-02-18 11:25:19 +00:00
|
|
|
const client = {
|
|
|
|
downloadKeys: async function(userIds) { return trueUser; },
|
|
|
|
};
|
|
|
|
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost"]);
|
|
|
|
expect(response).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-02-02 12:02:17 +00:00
|
|
|
it("returns false if not all users have crypto", async () => {
|
2020-02-18 11:25:19 +00:00
|
|
|
const client = {
|
2021-06-29 12:11:58 +00:00
|
|
|
downloadKeys: async function(userIds) { return { ...trueUser, ...falseUser }; },
|
2020-02-18 11:25:19 +00:00
|
|
|
};
|
|
|
|
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost", "@badUser:localhost"]);
|
|
|
|
expect(response).toBe(false);
|
|
|
|
});
|
|
|
|
});
|