Code coverage for link/unlink endpoints
This commit is contained in:
parent
a29d019d17
commit
a90d3b9055
13 changed files with 802 additions and 67 deletions
|
@ -412,7 +412,7 @@ async function unbridgeChannel(channelID) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {{id: string, topic?: string?}} channel
|
||||
* @param {{id: string, topic?: string?}} channel channel-ish (just needs an id, topic is optional)
|
||||
* @param {string} guildID
|
||||
*/
|
||||
async function unbridgeDeletedChannel(channel, guildID) {
|
||||
|
|
|
@ -14,7 +14,7 @@ test("channel2room: discoverable privacy room", async t => {
|
|||
let called = 0
|
||||
async function getStateEvent(roomID, type, key) { // getting power levels from space to apply to room
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {users: {"@example:matrix.org": 50}}
|
||||
|
@ -36,7 +36,7 @@ test("channel2room: linkable privacy room", async t => {
|
|||
let called = 0
|
||||
async function getStateEvent(roomID, type, key) { // getting power levels from space to apply to room
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {users: {"@example:matrix.org": 50}}
|
||||
|
@ -57,7 +57,7 @@ test("channel2room: invite-only privacy room", async t => {
|
|||
let called = 0
|
||||
async function getStateEvent(roomID, type, key) { // getting power levels from space to apply to room
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {users: {"@example:matrix.org": 50}}
|
||||
|
@ -76,7 +76,7 @@ test("channel2room: room where limited people can mention everyone", async t =>
|
|||
let called = 0
|
||||
async function getStateEvent(roomID, type, key) { // getting power levels from space to apply to room
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {users: {"@example:matrix.org": 50}}
|
||||
|
@ -98,7 +98,7 @@ test("channel2room: matrix room that already has a custom topic set", async t =>
|
|||
let called = 0
|
||||
async function getStateEvent(roomID, type, key) { // getting power levels from space to apply to room
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {}
|
||||
|
@ -118,7 +118,7 @@ test("channel2room: read-only discord channel", async t => {
|
|||
let called = 0
|
||||
async function getStateEvent(roomID, type, key) { // getting power levels from space to apply to room
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {}
|
||||
|
@ -139,7 +139,7 @@ test("channel2room: read-only discord channel", async t => {
|
|||
"m.room.join_rules/": {
|
||||
allow: [
|
||||
{
|
||||
room_id: "!jjWAGMeQdNrVZSSfvz:cadence.moe",
|
||||
room_id: "!jjmvBegULiLucuWEHU:cadence.moe",
|
||||
type: "m.room_membership",
|
||||
},
|
||||
],
|
||||
|
@ -160,7 +160,7 @@ test("channel2room: read-only discord channel", async t => {
|
|||
"@test_auto_invite:example.org": 100,
|
||||
},
|
||||
},
|
||||
"m.space.parent/!jjWAGMeQdNrVZSSfvz:cadence.moe": {
|
||||
"m.space.parent/!jjmvBegULiLucuWEHU:cadence.moe": {
|
||||
canonical: true,
|
||||
via: [
|
||||
"cadence.moe",
|
||||
|
|
|
@ -6,17 +6,17 @@ const data = require("../../test/data")
|
|||
const {db, select, from} = require("../passthrough")
|
||||
|
||||
test("orm: select: get works", t => {
|
||||
const row = select("guild_space", "guild_id", {}, "WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
const row = select("guild_space", "guild_id", {}, "WHERE space_id = ?").get("!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(row?.guild_id, data.guild.general.id)
|
||||
})
|
||||
|
||||
test("orm: from: get works", t => {
|
||||
const row = from("guild_space").select("guild_id").and("WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
const row = from("guild_space").select("guild_id").and("WHERE space_id = ?").get("!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(row?.guild_id, data.guild.general.id)
|
||||
})
|
||||
|
||||
test("orm: select: get pluck works", t => {
|
||||
const guildID = select("guild_space", "guild_id", {}, "WHERE space_id = ?").pluck().get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
const guildID = select("guild_space", "guild_id", {}, "WHERE space_id = ?").pluck().get("!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(guildID, data.guild.general.id)
|
||||
})
|
||||
|
||||
|
@ -36,7 +36,7 @@ test("orm: select: in array works", t => {
|
|||
})
|
||||
|
||||
test("orm: from: get pluck works", t => {
|
||||
const guildID = from("guild_space").pluck("guild_id").and("WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
const guildID = from("guild_space").pluck("guild_id").and("WHERE space_id = ?").get("!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
t.equal(guildID, data.guild.general.id)
|
||||
})
|
||||
|
||||
|
|
|
@ -91,7 +91,7 @@ test("invite: checks if user is already invited to space", async t => {
|
|||
api: {
|
||||
getStateEvent: async (roomID, type, stateKey) => {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
return {
|
||||
|
@ -121,14 +121,14 @@ test("invite: invites if user is not in space", async t => {
|
|||
api: {
|
||||
getStateEvent: async (roomID, type, stateKey) => {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
throw new MatrixServerError("State event doesn't exist or something")
|
||||
},
|
||||
inviteToRoom: async (roomID, mxid) => {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(mxid, "@cadence:cadence.moe")
|
||||
}
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ test("invite: prompts to invite to room (if never joined)", async t => {
|
|||
called++
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
if (roomID === "!jjWAGMeQdNrVZSSfvz:cadence.moe") { // space ID
|
||||
if (roomID === "!jjmvBegULiLucuWEHU:cadence.moe") { // space ID
|
||||
return {
|
||||
displayname: "cadence",
|
||||
membership: "join"
|
||||
|
@ -188,7 +188,7 @@ test("invite: prompts to invite to room (if left)", async t => {
|
|||
called++
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(stateKey, "@cadence:cadence.moe")
|
||||
if (roomID === "!jjWAGMeQdNrVZSSfvz:cadence.moe") { // space ID
|
||||
if (roomID === "!jjmvBegULiLucuWEHU:cadence.moe") { // space ID
|
||||
return {
|
||||
displayname: "cadence",
|
||||
membership: "join"
|
||||
|
|
|
@ -57,7 +57,7 @@ test("permissions: reports permissions of selected matrix user (implicit default
|
|||
},
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {
|
||||
|
@ -91,7 +91,7 @@ test("permissions: reports permissions of selected matrix user (moderator)", asy
|
|||
},
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {
|
||||
|
@ -127,7 +127,7 @@ test("permissions: reports permissions of selected matrix user (admin)", async t
|
|||
},
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(type, "m.room.power_levels")
|
||||
t.equal(key, "")
|
||||
return {
|
||||
|
@ -159,7 +159,7 @@ test("permissions: can update user to moderator", async t => {
|
|||
api: {
|
||||
async setUserPowerCascade(roomID, mxid, power) {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(mxid, "@cadence:cadence.moe")
|
||||
t.equal(power, 50)
|
||||
}
|
||||
|
@ -186,7 +186,7 @@ test("permissions: can update user to default", async t => {
|
|||
api: {
|
||||
async setUserPowerCascade(roomID, mxid, power) {
|
||||
called++
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe") // space ID
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe") // space ID
|
||||
t.equal(mxid, "@cadence:cadence.moe")
|
||||
t.equal(power, 0)
|
||||
}
|
||||
|
|
|
@ -159,7 +159,7 @@ test("api invite: can invite with valid nonce", async t => {
|
|||
return {membership: "leave"}
|
||||
},
|
||||
async inviteToRoom(roomID, mxidToInvite, mxid) {
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
called++
|
||||
},
|
||||
async setUserPowerCascade(roomID, mxid, power) {
|
||||
|
@ -192,7 +192,7 @@ test("api invite: can invite to a moderated guild", async t => {
|
|||
router.test("post", `/api/invite`, {
|
||||
body: {
|
||||
mxid: "@cadence:cadence.moe",
|
||||
permissions: "default",
|
||||
permissions: "admin",
|
||||
guild_id: "112760669178241024"
|
||||
},
|
||||
sessionData: {
|
||||
|
@ -204,14 +204,18 @@ test("api invite: can invite to a moderated guild", async t => {
|
|||
throw new MatrixServerError({errcode: "M_NOT_FOUND", error: "Event not found or something"})
|
||||
},
|
||||
async inviteToRoom(roomID, mxidToInvite, mxid) {
|
||||
t.equal(roomID, "!jjWAGMeQdNrVZSSfvz:cadence.moe")
|
||||
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
|
||||
called++
|
||||
},
|
||||
async setUserPowerCascade(roomID, mxid, power) {
|
||||
t.equal(power, 100) // moderator
|
||||
called++
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
t.notOk(error)
|
||||
t.equal(called, 2)
|
||||
t.equal(called, 3)
|
||||
})
|
||||
|
||||
test("api invite: does not reinvite joined users", async t => {
|
||||
|
|
|
@ -1,19 +1,39 @@
|
|||
// @ts-check
|
||||
|
||||
const {z} = require("zod")
|
||||
const {defineEventHandler, useSession, createError, readValidatedBody, setResponseHeader} = require("h3")
|
||||
const {defineEventHandler, useSession, createError, readValidatedBody, setResponseHeader, H3Event} = require("h3")
|
||||
const Ty = require("../../types")
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
|
||||
const {discord, db, as, sync, select, from} = require("../../passthrough")
|
||||
/** @type {import("../../d2m/actions/create-space")} */
|
||||
const createSpace = sync.require("../../d2m/actions/create-space")
|
||||
/** @type {import("../../d2m/actions/create-room")} */
|
||||
const createRoom = sync.require("../../d2m/actions/create-room")
|
||||
const {reg} = require("../../matrix/read-registration")
|
||||
|
||||
/** @type {import("../../matrix/api")} */
|
||||
const api = sync.require("../../matrix/api")
|
||||
/**
|
||||
* @param {H3Event} event
|
||||
* @returns {import("../../matrix/api")}
|
||||
*/
|
||||
function getAPI(event) {
|
||||
/* c8 ignore next */
|
||||
return event.context.api || sync.require("../../matrix/api")
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {H3Event} event
|
||||
* @returns {import("../../d2m/actions/create-room")}
|
||||
*/
|
||||
function getCreateRoom(event) {
|
||||
/* c8 ignore next */
|
||||
return event.context.createRoom || sync.require("../../d2m/actions/create-room")
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {H3Event} event
|
||||
* @returns {import("../../d2m/actions/create-space")}
|
||||
*/
|
||||
function getCreateSpace(event) {
|
||||
/* c8 ignore next */
|
||||
return event.context.createSpace || sync.require("../../d2m/actions/create-space")
|
||||
}
|
||||
|
||||
const schema = {
|
||||
linkSpace: z.object({
|
||||
|
@ -34,6 +54,7 @@ const schema = {
|
|||
as.router.post("/api/link-space", defineEventHandler(async event => {
|
||||
const parsedBody = await readValidatedBody(event, schema.linkSpace.parse)
|
||||
const session = await useSession(event, {password: reg.as_token})
|
||||
const api = getAPI(event)
|
||||
|
||||
// Check guild ID
|
||||
const guildID = parsedBody.guild_id
|
||||
|
@ -43,25 +64,33 @@ as.router.post("/api/link-space", defineEventHandler(async event => {
|
|||
if (!session.data.mxid) throw createError({status: 403, message: "Forbidden", data: "Can't link with your Matrix space if you aren't logged in to Matrix"})
|
||||
const spaceID = parsedBody.space_id
|
||||
const inviteType = select("invite", "type", {mxid: session.data.mxid, room_id: spaceID}).pluck().get()
|
||||
if (inviteType !== "m.space") throw createError({status: 403, message: "Forbidden", data: "No past invitations detected from your Matrix account for that space."})
|
||||
if (inviteType !== "m.space") throw createError({status: 403, message: "Forbidden", data: "You personally must invite OOYE to that space on Matrix"})
|
||||
|
||||
// Check they are not already bridged
|
||||
const existing = select("guild_space", "guild_id", {}, "WHERE guild_id = ? OR space_id = ?").get(guildID, spaceID)
|
||||
if (existing) throw createError({status: 400, message: "Bad Request", data: `Guild ID ${guildID} or space ID ${spaceID} are already bridged and cannot be reused`})
|
||||
|
||||
// Check space exists and bridge is joined and bridge has PL 100
|
||||
// Check space exists and bridge is joined
|
||||
const self = `@${reg.sender_localpart}:${reg.ooye.server_name}`
|
||||
/** @type {Ty.Event.M_Room_Member} */
|
||||
const memberEvent = await api.getStateEvent(spaceID, "m.room.member", self)
|
||||
if (memberEvent.membership !== "join") throw createError({status: 400, message: "Bad Request", data: "Matrix space does not exist"})
|
||||
/** @type {Ty.Event.M_Power_Levels} */
|
||||
const powerLevelsStateContent = await api.getStateEvent(spaceID, "m.room.power_levels", "")
|
||||
const selfPowerLevel = powerLevelsStateContent.users?.[self] || powerLevelsStateContent.users_default || 0
|
||||
if (selfPowerLevel < (powerLevelsStateContent.state_default || 50) || selfPowerLevel < 100) throw createError({status: 400, message: "Bad Request", data: "OOYE needs power level 100 (admin) in the target Matrix space"})
|
||||
/** @type {Ty.Event.M_Room_Member?} */
|
||||
let memberEvent = null
|
||||
try {
|
||||
memberEvent = await api.getStateEvent(spaceID, "m.room.member", self)
|
||||
} catch (e) {}
|
||||
if (memberEvent?.membership !== "join") throw createError({status: 400, message: "Bad Request", data: "Matrix space does not exist"})
|
||||
|
||||
// Check bridge has PL 100
|
||||
/** @type {Ty.Event.M_Power_Levels?} */
|
||||
let powerLevelsStateContent = null
|
||||
try {
|
||||
powerLevelsStateContent = await api.getStateEvent(spaceID, "m.room.power_levels", "")
|
||||
} catch (e) {}
|
||||
const selfPowerLevel = powerLevelsStateContent?.users?.[self] || powerLevelsStateContent?.users_default || 0
|
||||
if (selfPowerLevel < (powerLevelsStateContent?.state_default || 50) || selfPowerLevel < 100) throw createError({status: 400, message: "Bad Request", data: "OOYE needs power level 100 (admin) in the target Matrix space"})
|
||||
|
||||
// Check inviting user is a moderator in the space
|
||||
const invitingPowerLevel = powerLevelsStateContent.users?.[session.data.mxid] || powerLevelsStateContent.users_default || 0
|
||||
if (invitingPowerLevel < (powerLevelsStateContent.state_default || 50)) throw createError({status: 403, message: "Forbidden", data: `You need to be at least power level 50 (moderator) in the target Matrix space to use OOYE, but you are currently power level ${invitingPowerLevel}.`})
|
||||
const invitingPowerLevel = powerLevelsStateContent?.users?.[session.data.mxid] || powerLevelsStateContent?.users_default || 0
|
||||
if (invitingPowerLevel < (powerLevelsStateContent?.state_default || 50)) throw createError({status: 403, message: "Forbidden", data: `You need to be at least power level 50 (moderator) in the target Matrix space to set up OOYE, but you are currently power level ${invitingPowerLevel}.`})
|
||||
|
||||
// Insert database entry
|
||||
db.transaction(() => {
|
||||
|
@ -76,6 +105,9 @@ as.router.post("/api/link-space", defineEventHandler(async event => {
|
|||
as.router.post("/api/link", defineEventHandler(async event => {
|
||||
const parsedBody = await readValidatedBody(event, schema.link.parse)
|
||||
const session = await useSession(event, {password: reg.as_token})
|
||||
const api = getAPI(event)
|
||||
const createRoom = getCreateRoom(event)
|
||||
const createSpace = getCreateSpace(event)
|
||||
|
||||
// Check guild ID or nonce
|
||||
const guildID = parsedBody.guild_id
|
||||
|
@ -90,22 +122,41 @@ as.router.post("/api/link", defineEventHandler(async event => {
|
|||
const channel = discord.channels.get(parsedBody.discord)
|
||||
if (!channel) throw createError({status: 400, message: "Bad Request", data: "Discord channel does not exist"})
|
||||
|
||||
// Check channel and room are not already bridged
|
||||
const row = from("channel_room").select("channel_id", "room_id").and("WHERE channel_id = ? OR room_id = ?").get(parsedBody.discord, parsedBody.matrix)
|
||||
if (row) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${row.channel_id} and room ID ${row.room_id} are already bridged and cannot be reused`})
|
||||
// Check channel is part of the guild
|
||||
if (!("guild_id" in channel) || channel.guild_id !== guildID) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${channel.id} is not part of guild ${guildID}`})
|
||||
|
||||
// Check room exists and bridge is joined and bridge has PL 100
|
||||
// Check channel and room are not already bridged
|
||||
const row = from("channel_room").select("channel_id", "room_id").and("WHERE channel_id = ? OR room_id = ?").get(channel.id, parsedBody.matrix)
|
||||
if (row) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${row.channel_id} or room ID ${parsedBody.matrix} are already bridged and cannot be reused`})
|
||||
|
||||
// Check room is part of the guild's space
|
||||
/** @type {Ty.Event.M_Space_Child?} */
|
||||
let spaceChildEvent = null
|
||||
try {
|
||||
spaceChildEvent = await api.getStateEvent(spaceID, "m.space.child", parsedBody.matrix)
|
||||
} catch (e) {}
|
||||
if (!Array.isArray(spaceChildEvent?.via)) throw createError({status: 400, message: "Bad Request", data: "Matrix room needs to be part of the bridged space"})
|
||||
|
||||
// Check room exists and bridge is joined
|
||||
const self = `@${reg.sender_localpart}:${reg.ooye.server_name}`
|
||||
/** @type {Ty.Event.M_Room_Member} */
|
||||
const memberEvent = await api.getStateEvent(parsedBody.matrix, "m.room.member", self)
|
||||
if (memberEvent.membership !== "join") throw createError({status: 400, message: "Bad Request", data: "Matrix room does not exist"})
|
||||
/** @type {Ty.Event.M_Power_Levels} */
|
||||
const powerLevelsStateContent = await api.getStateEvent(parsedBody.matrix, "m.room.power_levels", "")
|
||||
const selfPowerLevel = powerLevelsStateContent.users?.[self] || powerLevelsStateContent.users_default || 0
|
||||
if (selfPowerLevel < (powerLevelsStateContent.state_default || 50) || selfPowerLevel < 100) throw createError({status: 400, message: "Bad Request", data: "OOYE needs power level 100 (admin) in the target Matrix room"})
|
||||
/** @type {Ty.Event.M_Room_Member?} */
|
||||
let memberEvent = null
|
||||
try {
|
||||
memberEvent = await api.getStateEvent(parsedBody.matrix, "m.room.member", self)
|
||||
} catch (e) {}
|
||||
if (memberEvent?.membership !== "join") throw createError({status: 400, message: "Bad Request", data: "Matrix room does not exist"})
|
||||
|
||||
// Check bridge has PL 100
|
||||
/** @type {Ty.Event.M_Power_Levels?} */
|
||||
let powerLevelsStateContent = null
|
||||
try {
|
||||
powerLevelsStateContent = await api.getStateEvent(parsedBody.matrix, "m.room.power_levels", "")
|
||||
} catch (e) {}
|
||||
const selfPowerLevel = powerLevelsStateContent?.users?.[self] || powerLevelsStateContent?.users_default || 0
|
||||
if (selfPowerLevel < (powerLevelsStateContent?.state_default || 50) || selfPowerLevel < 100) throw createError({status: 400, message: "Bad Request", data: "OOYE needs power level 100 (admin) in the target Matrix room"})
|
||||
|
||||
// Insert database entry
|
||||
db.prepare("INSERT INTO channel_room (channel_id, room_id, name, guild_id) VALUES (?, ?, ?, ?)").run(parsedBody.discord, parsedBody.matrix, channel.name, guildID)
|
||||
db.prepare("INSERT INTO channel_room (channel_id, room_id, name, guild_id) VALUES (?, ?, ?, ?)").run(channel.id, parsedBody.matrix, channel.name, guildID)
|
||||
|
||||
// Sync room data and space child
|
||||
await createRoom.syncRoom(parsedBody.discord)
|
||||
|
@ -125,14 +176,25 @@ as.router.post("/api/link", defineEventHandler(async event => {
|
|||
as.router.post("/api/unlink", defineEventHandler(async event => {
|
||||
const {channel_id, guild_id} = await readValidatedBody(event, schema.unlink.parse)
|
||||
const session = await useSession(event, {password: reg.as_token})
|
||||
const createRoom = getCreateRoom(event)
|
||||
|
||||
// Check guild ID or nonce
|
||||
if (!(session.data.managedGuilds || []).concat(session.data.matrixGuilds || []).includes(guild_id)) throw createError({status: 403, message: "Forbidden", data: "Can't edit a guild you don't have Manage Server permissions in"})
|
||||
|
||||
// Check channel is part of this guild
|
||||
const channel = discord.channels.get(channel_id)
|
||||
if (!channel) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${channel_id} does not exist`})
|
||||
if (!("guild_id" in channel) || channel.guild_id !== guild_id) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${channel_id} is not part of guild ${guild_id}`})
|
||||
// Check guild exists
|
||||
const guild = discord.guilds.get(guild_id)
|
||||
if (!guild) throw createError({status: 400, message: "Bad Request", data: "Discord guild does not exist or bot has not joined it"})
|
||||
|
||||
// Check that the channel (if it exists) is part of this guild
|
||||
/** @type {any} */
|
||||
let channel = discord.channels.get(channel_id)
|
||||
if (channel) {
|
||||
if (!("guild_id" in channel) || channel.guild_id !== guild_id) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${channel_id} is not part of guild ${guild_id}`})
|
||||
} else {
|
||||
// Otherwise, if the channel isn't cached, it must have been deleted.
|
||||
// There's no other authentication here - it's okay for anyone to unlink a deleted channel just by knowing its ID.
|
||||
channel = {id: channel_id}
|
||||
}
|
||||
|
||||
// Check channel is currently bridged
|
||||
const row = select("channel_room", "channel_id", {channel_id: channel_id}).get()
|
||||
|
|
661
src/web/routes/link.test.js
Normal file
661
src/web/routes/link.test.js
Normal file
|
@ -0,0 +1,661 @@
|
|||
// @ts-check
|
||||
|
||||
const tryToCatch = require("try-to-catch")
|
||||
const {router, test} = require("../../../test/web")
|
||||
const {MatrixServerError} = require("../../matrix/mreq")
|
||||
const {select, db} = require("../../passthrough")
|
||||
const assert = require("assert").strict
|
||||
|
||||
test("web link space: access denied when not logged in to Discord", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Can't edit a guild you don't have Manage Server permissions in")
|
||||
})
|
||||
|
||||
test("web link space: access denied when not logged in to Matrix", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Can't link with your Matrix space if you aren't logged in to Matrix")
|
||||
})
|
||||
|
||||
test("web link space: access denied when bot was invited by different user", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@user:example.org"
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "You personally must invite OOYE to that space on Matrix")
|
||||
})
|
||||
|
||||
test("web link space: access denied when guild is already in use", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["112760669178241024"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
body: {
|
||||
space_id: "!jjmvBegULiLucuWEHU:cadence.moe",
|
||||
guild_id: "112760669178241024"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Guild ID 112760669178241024 or space ID !jjmvBegULiLucuWEHU:cadence.moe are already bridged and cannot be reused")
|
||||
})
|
||||
|
||||
test("web link space: check that OOYE is joined", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
t.equal(type, "m.room.member")
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
throw new MatrixServerError({errcode: "M_NOT_FOUND", error: "join the room or something"})
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Matrix space does not exist")
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("web link space: check that OOYE has PL 100 (not missing)", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
if (type === "m.room.member") {
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
throw new MatrixServerError({errcode: "M_NOT_FOUND", error: "what if I told you that power levels never existed"})
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "OOYE needs power level 100 (admin) in the target Matrix space")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("web link space: check that OOYE has PL 100 (not users_default)", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
if (type === "m.room.member") {
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
t.equal(key, "")
|
||||
return {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "OOYE needs power level 100 (admin) in the target Matrix space")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("web link space: check that OOYE has PL 100 (not 50)", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
if (type === "m.room.member") {
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
t.equal(key, "")
|
||||
return {users: {"@_ooye_bot:cadence.moe": 50}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "OOYE needs power level 100 (admin) in the target Matrix space")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("web link space: check that inviting user has PL 50", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
if (type === "m.room.member") {
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
t.equal(key, "")
|
||||
return {users: {"@_ooye_bot:cadence.moe": 100}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "You need to be at least power level 50 (moderator) in the target Matrix space to set up OOYE, but you are currently power level 0.")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("web link space: successfully adds entry to database and loads page", async t => {
|
||||
let called = 0
|
||||
await router.test("post", "/api/link-space", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
body: {
|
||||
space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
if (type === "m.room.member") {
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
t.equal(key, "")
|
||||
return {users: {"@_ooye_bot:cadence.moe": 100, "@cadence:cadence.moe": 50}}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(called, 2)
|
||||
|
||||
// check that the entry was added to the database
|
||||
t.equal(select("guild_space", "privacy_level", {guild_id: "665289423482519565", space_id: "!zTMspHVUBhFLLSdmnS:cadence.moe"}).pluck().get(), 0)
|
||||
|
||||
// check that the guild info page now loads
|
||||
const html = await router.test("get", "/guild?guild_id=665289423482519565", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"],
|
||||
mxid: "@cadence:cadence.moe"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
return {}
|
||||
},
|
||||
async getMembers(roomID, membership) {
|
||||
return {chunk: []}
|
||||
},
|
||||
async getFullHierarchy(roomID) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
})
|
||||
t.has(html, `<h1 class="s-page-title--header">Data Horde</h1>`)
|
||||
})
|
||||
|
||||
// *****
|
||||
|
||||
test("web link room: access denied when not logged in to Discord", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Can't edit a guild you don't have Manage Server permissions in")
|
||||
})
|
||||
|
||||
test("web link room: check that guild exists", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["1"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "1"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Discord guild does not exist or bot has not joined it")
|
||||
})
|
||||
|
||||
test("web link room: check that channel exists", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "1",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Discord channel does not exist")
|
||||
})
|
||||
|
||||
test("web link room: check that channel is part of guild", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "112760669178241024",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Channel ID 112760669178241024 is not part of guild 665289423482519565")
|
||||
})
|
||||
|
||||
test("web link room: check that channel is not already linked", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["112760669178241024"]
|
||||
},
|
||||
body: {
|
||||
discord: "112760669178241024",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "112760669178241024"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Channel ID 112760669178241024 or room ID !NDbIqNpJyPvfKRnNcr:cadence.moe are already bridged and cannot be reused")
|
||||
})
|
||||
|
||||
test("web link room: checks the autocreate setting if the space doesn't exist yet", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
createSpace: {
|
||||
async ensureSpace(guild) {
|
||||
called++
|
||||
t.equal(guild.id, "665289423482519565")
|
||||
// simulate what ensureSpace is intended to check
|
||||
const autocreate = 0
|
||||
assert.equal(autocreate, 1, "refusing to implicitly create a space for guild 665289423482519565. set the guild_active data first before calling ensureSpace/syncSpace.")
|
||||
return ""
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.match(error.message, /refusing to implicitly create a space/)
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("web link room: check that room is part of space (event missing)", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
t.equal(type, "m.space.child")
|
||||
t.equal(key, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
throw new MatrixServerError({errcode: "M_NOT_FOUND", error: "what if I told you there was no such thing as a space"})
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Matrix room needs to be part of the bridged space")
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("web link room: check that room is part of space (event empty)", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
t.equal(type, "m.space.child")
|
||||
t.equal(key, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
return {}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Matrix room needs to be part of the bridged space")
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("web link room: check that bridge is joined to room", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
if (type === "m.room.member") {
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
throw new MatrixServerError({errcode: "M_NOT_FOUND", error: "not in the room I guess"})
|
||||
} else if (type === "m.space.child") {
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
t.equal(key, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
return {via: ["cadence.moe"]}
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Matrix room does not exist")
|
||||
t.equal(called, 2)
|
||||
})
|
||||
|
||||
test("web link room: check that bridge has PL 100 in target room (event missing)", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
if (type === "m.room.member") {
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.space.child") {
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
t.equal(key, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
return {via: ["cadence.moe"]}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(key, "")
|
||||
throw new MatrixServerError({errcode: "M_NOT_FOUND", error: "what if I told you there's no such thing as power levels"})
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "OOYE needs power level 100 (admin) in the target Matrix room")
|
||||
t.equal(called, 3)
|
||||
})
|
||||
|
||||
test("web link room: check that bridge has PL 100 in target room (users default)", async t => {
|
||||
let called = 0
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
if (type === "m.room.member") {
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.space.child") {
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
t.equal(key, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
return {via: ["cadence.moe"]}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(key, "")
|
||||
return {users_default: 50}
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "OOYE needs power level 100 (admin) in the target Matrix room")
|
||||
t.equal(called, 3)
|
||||
})
|
||||
|
||||
test("web link room: successfully calls createRoom", async t => {
|
||||
let called = 0
|
||||
await router.test("post", "/api/link", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
discord: "665310973967597573",
|
||||
matrix: "!NDbIqNpJyPvfKRnNcr:cadence.moe",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
api: {
|
||||
async getStateEvent(roomID, type, key) {
|
||||
called++
|
||||
if (type === "m.room.member") {
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(key, "@_ooye_bot:cadence.moe")
|
||||
return {membership: "join"}
|
||||
} else if (type === "m.room.power_levels") {
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(key, "")
|
||||
return {users: {"@_ooye_bot:cadence.moe": 100}}
|
||||
} else if (type === "m.space.child") {
|
||||
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
|
||||
t.equal(key, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
return {via: ["cadence.moe"]}
|
||||
}
|
||||
},
|
||||
async sendEvent(roomID, type, content) {
|
||||
called++
|
||||
t.equal(roomID, "!NDbIqNpJyPvfKRnNcr:cadence.moe")
|
||||
t.equal(type, "m.room.message")
|
||||
t.match(content.body, /👋/)
|
||||
return ""
|
||||
}
|
||||
},
|
||||
createRoom: {
|
||||
async syncRoom(channelID) {
|
||||
called++
|
||||
t.equal(channelID, "665310973967597573")
|
||||
return "!NDbIqNpJyPvfKRnNcr:cadence.moe"
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(called, 5)
|
||||
})
|
||||
|
||||
// *****
|
||||
|
||||
test("web unlink room: access denied if not logged in to Discord", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/unlink", {
|
||||
body: {
|
||||
channel_id: "665310973967597573",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Can't edit a guild you don't have Manage Server permissions in")
|
||||
})
|
||||
|
||||
test("web unlink room: checks that guild exists", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/unlink", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["2"]
|
||||
},
|
||||
body: {
|
||||
channel_id: "665310973967597573",
|
||||
guild_id: "2"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Discord guild does not exist or bot has not joined it")
|
||||
})
|
||||
|
||||
test("web unlink room: checks that the channel is part of the guild", async t => {
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/unlink", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
channel_id: "112760669178241024",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Channel ID 112760669178241024 is not part of guild 665289423482519565")
|
||||
})
|
||||
|
||||
test("web unlink room: successfully calls unbridgeDeletedChannel when the channel does exist", async t => {
|
||||
let called = 0
|
||||
await router.test("post", "/api/unlink", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
channel_id: "665310973967597573",
|
||||
guild_id: "665289423482519565"
|
||||
},
|
||||
createRoom: {
|
||||
async unbridgeDeletedChannel(channel) {
|
||||
called++
|
||||
t.equal(channel.id, "665310973967597573")
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("web unlink room: successfully calls unbridgeDeletedChannel when the channel does not exist", async t => {
|
||||
let called = 0
|
||||
await router.test("post", "/api/unlink", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["112760669178241024"]
|
||||
},
|
||||
body: {
|
||||
channel_id: "489237891895768942",
|
||||
guild_id: "112760669178241024"
|
||||
},
|
||||
createRoom: {
|
||||
async unbridgeDeletedChannel(channel) {
|
||||
called++
|
||||
t.equal(channel.id, "489237891895768942")
|
||||
}
|
||||
}
|
||||
})
|
||||
t.equal(called, 1)
|
||||
})
|
||||
|
||||
test("web unlink room: checks that the channel is bridged", async t => {
|
||||
db.prepare("DELETE FROM channel_room WHERE channel_id = '665310973967597573'").run()
|
||||
const [error] = await tryToCatch(() => router.test("post", "/api/unlink", {
|
||||
sessionData: {
|
||||
user_id: "1",
|
||||
managedGuilds: ["665289423482519565"]
|
||||
},
|
||||
body: {
|
||||
channel_id: "665310973967597573",
|
||||
guild_id: "665289423482519565"
|
||||
}
|
||||
}))
|
||||
t.equal(error.data, "Channel ID 665310973967597573 is not currently bridged")
|
||||
})
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
const {test} = require("supertape")
|
||||
const {router} = require("../../test/web")
|
||||
const assert = require("assert").strict
|
||||
|
||||
require("./server")
|
||||
|
||||
|
@ -28,5 +29,8 @@ test("web server: compresses static resources", async t => {
|
|||
"accept-encoding": "gzip"
|
||||
}
|
||||
})
|
||||
t.ok(content instanceof ReadableStream)
|
||||
assert(content instanceof ReadableStream)
|
||||
const firstChunk = await content.getReader().read()
|
||||
t.ok(firstChunk.value instanceof Uint8Array, "can get data")
|
||||
t.deepEqual(firstChunk.value.slice(0, 3), Uint8Array.from([31, 139, 8]), "has compressed gzip header")
|
||||
})
|
||||
|
|
|
@ -43,7 +43,7 @@ module.exports = {
|
|||
rate_limit_per_user: 0,
|
||||
position: 0,
|
||||
permission_overwrites: [],
|
||||
parent_id: "665289423482519566",
|
||||
parent_id: null,
|
||||
name: "saving-the-world",
|
||||
last_pin_timestamp: "2021-04-14T18:39:41+00:00",
|
||||
last_message_id: "1335828749479837750",
|
||||
|
@ -58,7 +58,7 @@ module.exports = {
|
|||
"m.room.topic/": {topic: "#collective-unconscious | https://docs.google.com/document/d/blah/edit | I spread, pipe, and whip because it is my will. :headstone:\n\nChannel ID: 112760669178241024\nGuild ID: 112760669178241024"},
|
||||
"m.room.guest_access/": {guest_access: "can_join"},
|
||||
"m.room.history_visibility/": {history_visibility: "shared"},
|
||||
"m.space.parent/!jjWAGMeQdNrVZSSfvz:cadence.moe": {
|
||||
"m.space.parent/!jjmvBegULiLucuWEHU:cadence.moe": {
|
||||
via: ["cadence.moe"],
|
||||
canonical: true
|
||||
},
|
||||
|
@ -66,7 +66,7 @@ module.exports = {
|
|||
join_rule: "restricted",
|
||||
allow: [{
|
||||
type: "m.room_membership",
|
||||
room_id: "!jjWAGMeQdNrVZSSfvz:cadence.moe"
|
||||
room_id: "!jjmvBegULiLucuWEHU:cadence.moe"
|
||||
}]
|
||||
},
|
||||
"m.room.avatar/": {
|
||||
|
|
|
@ -6,7 +6,7 @@ INSERT INTO guild_active (guild_id, autocreate) VALUES
|
|||
('665289423482519565', 0);
|
||||
|
||||
INSERT INTO guild_space (guild_id, space_id, privacy_level) VALUES
|
||||
('112760669178241024', '!jjWAGMeQdNrVZSSfvz:cadence.moe', 0);
|
||||
('112760669178241024', '!jjmvBegULiLucuWEHU:cadence.moe', 0);
|
||||
|
||||
INSERT INTO channel_room (channel_id, room_id, name, nick, thread_parent, custom_avatar) VALUES
|
||||
('112760669178241024', '!kLRqKKUQXcibIMtOpl:cadence.moe', 'heave', 'main', NULL, NULL),
|
||||
|
@ -176,6 +176,7 @@ INSERT INTO media_proxy (permitted_hash) VALUES
|
|||
|
||||
INSERT INTO invite (mxid, room_id, type, name, avatar, topic) VALUES
|
||||
('@cadence:cadence.moe', '!zTMspHVUBhFLLSdmnS:cadence.moe', 'm.space', 'Data Horde', 'mxc://cadence.moe/TLqQOsTSrZkVKwBSWYTZNTrw', 'here is the space topic'),
|
||||
('@cadence:cadence.moe', '!jjmvBegULiLucuWEHU:cadence.moe', 'm.space', 'Epicord', NULL, NULL),
|
||||
('@cadence:cadence.moe', '!room:cadence.moe', NULL, 'some room', NULL, NULL),
|
||||
('@rnl:cadence.moe', '!space:cadence.moe', NULL, 'somebody else''s space', NULL, NULL);
|
||||
|
||||
|
|
|
@ -157,4 +157,5 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not
|
|||
require("../src/web/routes/download-discord.test")
|
||||
require("../src/web/routes/download-matrix.test")
|
||||
require("../src/web/routes/guild.test")
|
||||
require("../src/web/routes/link.test")
|
||||
})()
|
||||
|
|
|
@ -47,7 +47,7 @@ class Router {
|
|||
/**
|
||||
* @param {string} method
|
||||
* @param {string} inputUrl
|
||||
* @param {{event?: any, params?: any, body?: any, sessionData?: any, api?: Partial<import("../src/matrix/api")>, snow?: {[k in keyof SnowTransfer]?: Partial<SnowTransfer[k]>}, headers?: any}} [options]
|
||||
* @param {{event?: any, params?: any, body?: any, sessionData?: any, api?: Partial<import("../src/matrix/api")>, snow?: {[k in keyof SnowTransfer]?: Partial<SnowTransfer[k]>}, createRoom?: Partial<import("../src/d2m/actions/create-room")>, createSpace?: Partial<import("../src/d2m/actions/create-space")>, headers?: any}} [options]
|
||||
*/
|
||||
test(method, inputUrl, options = {}) {
|
||||
const url = new URL(inputUrl, "http://a")
|
||||
|
@ -79,6 +79,8 @@ class Router {
|
|||
api: options.api,
|
||||
params: options.params,
|
||||
snow: options.snow,
|
||||
createRoom: options.createRoom,
|
||||
createSpace: options.createSpace,
|
||||
sessions: {
|
||||
h3: {
|
||||
id: "h3",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue