Compare commits

..

5 commits

2 changed files with 13 additions and 87 deletions

View file

@ -259,7 +259,7 @@ as.router.post("/api/unlink-space", defineEventHandler(async event => {
const remainingLinkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {guild_id: guild_id}).all() const remainingLinkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {guild_id: guild_id}).all()
if (remainingLinkedChannels.length !== 0) if (remainingLinkedChannels.length !== 0)
throw createError({status: 500, message: "Internal Server Error", data: "Some linked room still exists after trying to unlink all of them. Aborting the space unlinking..."}) throw createError({status: 400, message: "Bad Request", data: "Some linked room still exists after trying to unlink all of them. Aborting the space unlinking..."})
await api.setUserPower(spaceID, me, 0) await api.setUserPower(spaceID, me, 0)
await api.leaveRoom(spaceID) await api.leaveRoom(spaceID)

View file

@ -618,9 +618,7 @@ test("web unlink room: successfully calls unbridgeDeletedChannel when the channe
}) })
test("web unlink room: checks that the channel is bridged", async t => { test("web unlink room: checks that the channel is bridged", async t => {
const row = db.prepare("SELECT * FROM channel_room WHERE channel_id = '665310973967597573'").get()
db.prepare("DELETE FROM channel_room WHERE channel_id = '665310973967597573'").run() db.prepare("DELETE FROM channel_room WHERE channel_id = '665310973967597573'").run()
const [error] = await tryToCatch(() => router.test("post", "/api/unlink", { const [error] = await tryToCatch(() => router.test("post", "/api/unlink", {
sessionData: { sessionData: {
managedGuilds: ["665289423482519565"] managedGuilds: ["665289423482519565"]
@ -631,10 +629,6 @@ test("web unlink room: checks that the channel is bridged", async t => {
} }
})) }))
t.equal(error.data, "Channel ID 665310973967597573 is not currently bridged") t.equal(error.data, "Channel ID 665310973967597573 is not currently bridged")
db.prepare("INSERT INTO channel_room (channel_id, room_id, name, nick, thread_parent, custom_avatar, last_bridged_pin_timestamp, speedbump_id, speedbump_checked, speedbump_webhook_id, guild_id, custom_topic) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").run(row.channel_id, row.room_id, row.name, row.nick, row.thread_parent, row.custom_avatar, row.last_bridged_pin_timestamp, row.speedbump_id, row.speedbump_checked, row.speedbump_webhook_id, row.guild_id, row.custom_topic)
const new_row = db.prepare("SELECT * FROM channel_room WHERE channel_id = '665310973967597573'").get()
t.deepEqual(row, new_row)
}) })
// ***** // *****
@ -660,7 +654,7 @@ test("web unlink space: checks that guild exists", async t => {
t.equal(error.data, "Discord guild does not exist or bot has not joined it") 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 before trying to unlink the space", async t => { 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() const row = db.prepare("SELECT * FROM guild_space WHERE guild_id = '665289423482519565'").get()
db.prepare("DELETE FROM guild_space WHERE guild_id = '665289423482519565'").run() db.prepare("DELETE FROM guild_space WHERE guild_id = '665289423482519565'").run()
@ -679,54 +673,20 @@ test("web unlink space: checks that a space is linked to the guild before trying
t.deepEqual(row, new_row) t.deepEqual(row, new_row)
}) })
test("web unlink space: correctly abort unlinking if some linked channels remain after trying to unlink them all", async t => { test("web unlink space: successfully calls unbridgeDeletedChannel on linked channels in space", async t => {
let unbridgedChannel = false // Need to re-link the room to check it is properly unlinked by the unlink-space
await router.test("post", "/api/link", {
const [error] = await tryToCatch(() => router.test("post", "/api/unlink-space", {
sessionData: { sessionData: {
managedGuilds: ["665289423482519565"] managedGuilds: ["665289423482519565"]
}, },
body: { body: {
guild_id: "665289423482519565", discord: "665310973967597573",
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
guild_id: "665289423482519565"
}, },
createRoom: { })
async unbridgeDeletedChannel(channel, guildID) {
unbridgedChannel = true
t.equal(channel.id, "665310973967597573")
t.equal(guildID, "665289423482519565")
// Do not actually delete the link from DB, should trigger error later in check
}
},
api: {
async *generateFullHierarchy(spaceID) {
t.equal(spaceID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
yield {
room_id: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
children_state: {},
guest_can_join: false,
num_joined_members: 2
}
/* c8 ignore next */
},
}
}))
t.equal(error.data, "Some linked room still exists after trying to unlink all of them. Aborting the space unlinking...") let called = 0
t.equal(unbridgedChannel, true)
})
test("web unlink space: successfully calls unbridgeDeletedChannel on linked channels in space, self-downgrade power level, leave space, and delete link from DB", async t => {
const {reg} = require("../../matrix/read-registration")
const me = `@${reg.sender_localpart}:${reg.ooye.server_name}`
const getLinkRowQuery = "SELECT * FROM guild_space WHERE guild_id = '665289423482519565'"
const row = db.prepare(getLinkRowQuery).get()
t.equal(row.space_id, "!zTMspHVUBhFLLSdmnS:cadence.moe")
let unbridgedChannel = false
let downgradedPowerLevel = false
let leftRoom = false
await router.test("post", "/api/unlink-space", { await router.test("post", "/api/unlink-space", {
sessionData: { sessionData: {
managedGuilds: ["665289423482519565"] managedGuilds: ["665289423482519565"]
@ -735,45 +695,11 @@ test("web unlink space: successfully calls unbridgeDeletedChannel on linked chan
guild_id: "665289423482519565", guild_id: "665289423482519565",
}, },
createRoom: { createRoom: {
async unbridgeDeletedChannel(channel, guildID) { async unbridgeDeletedChannel(channel) {
unbridgedChannel = true called++
t.equal(channel.id, "665310973967597573") t.equal(channel.id, "665310973967597573")
t.equal(guildID, "665289423482519565")
// In order to not simulate channel deletion and not trigger the post unlink channels, pre-unlink space check
db.prepare("DELETE FROM channel_room WHERE guild_id = '665289423482519565' AND channel_id = '665310973967597573'").run()
} }
},
api: {
async *generateFullHierarchy(spaceID) {
t.equal(spaceID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
yield {
room_id: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
children_state: {},
guest_can_join: false,
num_joined_members: 2
}
/* c8 ignore next */
},
async setUserPower(spaceID, targetUser, powerLevel) {
downgradedPowerLevel = true
t.equal(spaceID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
t.equal(targetUser, me)
t.equal(powerLevel, 0)
},
async leaveRoom(spaceID) {
leftRoom = true
t.equal(spaceID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
},
} }
}) })
t.equal(called, 1)
t.equal(unbridgedChannel, true)
t.equal(downgradedPowerLevel, true)
t.equal(leftRoom, true)
const missed_row = db.prepare(getLinkRowQuery).get()
t.equal(missed_row, undefined)
}) })