From 16a9c643b885f3a7a2e94391a3eab82deb3eb754 Mon Sep 17 00:00:00 2001 From: Elliu Date: Sun, 7 Sep 2025 15:34:23 +0900 Subject: [PATCH] WIP: add tests for /api/unlink-space --- src/web/routes/link.test.js | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/web/routes/link.test.js b/src/web/routes/link.test.js index 0d8d366..cc39354 100644 --- a/src/web/routes/link.test.js +++ b/src/web/routes/link.test.js @@ -630,3 +630,76 @@ test("web unlink room: checks that the channel is bridged", async t => { })) t.equal(error.data, "Channel ID 665310973967597573 is not currently bridged") }) + +// ***** + +test("web unlink space: access denied if not logged in to Discord", async t => { + const [error] = await tryToCatch(() => router.test("post", "/api/unlink-space", { + body: { + guild_id: "665289423482519565" + } + })) + t.equal(error.data, "Can't edit a guild you don't have Manage Server permissions in") +}) + +test("web unlink space: checks that guild exists", async t => { + const [error] = await tryToCatch(() => router.test("post", "/api/unlink-space", { + sessionData: { + managedGuilds: ["2"] + }, + body: { + guild_id: "2" + } + })) + t.equal(error.data, "Discord guild does not exist or bot has not joined it") +}) + +test("web unlink space: checks that a space is linked to the guild", async t => { + const row = db.prepare("SELECT * FROM guild_space WHERE guild_id = '665289423482519565'").get() + db.prepare("DELETE FROM guild_space WHERE guild_id = '665289423482519565'").run() + + const [error] = await tryToCatch(() => router.test("post", "/api/unlink-space", { + sessionData: { + managedGuilds: ["665289423482519565"] + }, + body: { + guild_id: "665289423482519565" + } + })) + t.equal(error.data, "Matrix space does not exist or bot has not linked it") + + db.prepare("INSERT INTO guild_space (guild_id, space_id, privacy_level, presence, url_preview) VALUES (?, ?, ?, ?, ?)").run(row.guild_id, row.space_id, row.privacy_level, row.presence, row.url_preview) + const new_row = db.prepare("SELECT * FROM guild_space WHERE guild_id = '665289423482519565'").get() + t.deepEqual(row, new_row) +}) + +test("web unlink space: successfully calls unbridgeDeletedChannel on linked channels in space", async t => { + // Need to re-link the room to check it is properly unlinked by the unlink-space + await router.test("post", "/api/link", { + sessionData: { + managedGuilds: ["665289423482519565"] + }, + body: { + discord: "665310973967597573", + matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe", + guild_id: "665289423482519565" + }, + }) + + let called = 0 + await router.test("post", "/api/unlink-space", { + sessionData: { + managedGuilds: ["665289423482519565"] + }, + body: { + guild_id: "665289423482519565", + }, + createRoom: { + async unbridgeDeletedChannel(channel) { + called++ + t.equal(channel.id, "665310973967597573") + } + } + }) + t.equal(called, 1) +})