mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 21:19:13 +00:00

* Fix generic webhook 'user is already in the room error' * Add changelog * Cleanup to support usecases without slamming the homeserver. * Fix fallthrough * Add some tests * Add tests for intentutils * Refactor the rest of the app Co-authored-by: Half-Shot <will@half-shot.uk>
58 lines
2.4 KiB
TypeScript
58 lines
2.4 KiB
TypeScript
import { IntentMock, MatrixClientMock } from "./utils/IntentMock";
|
|
import { ensureUserIsInRoom } from "../src/IntentUtils";
|
|
import { expect } from "chai";
|
|
import { MatrixError } from "matrix-bot-sdk";
|
|
|
|
const ROOM_ID = "!foo:bar";
|
|
const SENDER_USER_ID = "@my_target:foo";
|
|
|
|
describe("IntentUtils", () => {
|
|
describe("ensureUserIsInRoom", () => {
|
|
it("no-ops if the user is already joined to the room", () => {
|
|
const targetIntent = IntentMock.create(SENDER_USER_ID);
|
|
targetIntent.ensureJoined = () => { /* No-op */ };
|
|
const matrixClient = MatrixClientMock.create();
|
|
ensureUserIsInRoom(targetIntent, matrixClient, ROOM_ID);
|
|
});
|
|
|
|
it("invites the user to the room and joins", () => {
|
|
const targetIntent = IntentMock.create(SENDER_USER_ID);
|
|
const matrixClient = MatrixClientMock.create();
|
|
let hasInvited = false;
|
|
// This should fail the first time, then pass once we've tried to invite the user
|
|
targetIntent.ensureJoined = (roomId: string) => {
|
|
if (hasInvited) {
|
|
return;
|
|
}
|
|
expect(roomId).to.equal(ROOM_ID);
|
|
throw new MatrixError({ errcode: "M_FORBIDDEN", error: "Test forced error"}, 401)
|
|
};
|
|
|
|
// This should invite the puppet user.
|
|
matrixClient.inviteUser = (userId: string, roomId: string) => {
|
|
expect(userId).to.equal(SENDER_USER_ID);
|
|
expect(roomId).to.equal(ROOM_ID);
|
|
hasInvited = true;
|
|
}
|
|
|
|
ensureUserIsInRoom(targetIntent, matrixClient, ROOM_ID);
|
|
// Only pass if we've actually bothered to invite the bot.
|
|
expect(hasInvited).to.be.true;
|
|
});
|
|
|
|
it("invites the user to the room and joins", () => {
|
|
const targetIntent = IntentMock.create(SENDER_USER_ID);
|
|
const matrixClient = MatrixClientMock.create();
|
|
|
|
// This should fail the first time, then pass once we've tried to invite the user
|
|
targetIntent.ensureJoined = () => {
|
|
throw new MatrixError({ errcode: "FORCED_FAILURE", error: "Test forced error"}, 500)
|
|
};
|
|
try {
|
|
ensureUserIsInRoom(targetIntent, matrixClient, ROOM_ID);
|
|
} catch (ex) {
|
|
expect(ex.message).to.contain(`Could not ensure that ${SENDER_USER_ID} is in ${ROOM_ID}`)
|
|
}
|
|
});
|
|
})
|
|
}); |