Test setup for new web pages

This commit is contained in:
Cadence Ember 2025-02-04 17:23:46 +13:00
commit f7e2c89e65
10 changed files with 233 additions and 37 deletions

View file

@ -59,7 +59,7 @@ test("invite: checks if guild exists", async t => { // it might not exist if the
})
test("invite: checks if channel exists or is autocreatable", async t => {
db.prepare("UPDATE guild_active SET autocreate = 0").run()
db.prepare("UPDATE guild_active SET autocreate = 0 WHERE guild_id = '112760669178241024'").run()
const msgs = await fromAsync(_interact({
data: {
options: [{
@ -72,7 +72,7 @@ test("invite: checks if channel exists or is autocreatable", async t => {
guild_id: "112760669178241024"
}, {}))
t.equal(msgs[0].createInteractionResponse.data.content, "This channel isn't bridged, so you can't invite Matrix users yet. Try turning on automatic room-creation or link a Matrix room in the website.")
db.prepare("UPDATE guild_active SET autocreate = 1").run()
db.prepare("UPDATE guild_active SET autocreate = 1 WHERE guild_id = '112760669178241024'").run()
})
test("invite: checks if user is already invited to space", async t => {

View file

@ -7,6 +7,7 @@ const getRelativePath = require("get-relative-path")
const h3 = require("h3")
const {defineEventHandler, defaultContentType, setResponseStatus, useSession, getQuery} = h3
const {compileFile} = require("@cloudrac3r/pug")
const pretty = process.argv.join(" ").includes("test")
const {reg} = require("../matrix/read-registration")
@ -31,7 +32,7 @@ function render(event, filename, locals) {
function compile() {
try {
const template = compileFile(path, {})
const template = compileFile(path, {pretty})
pugCache.set(path, async (event, locals) => {
defaultContentType(event, "text/html; charset=utf-8")
const session = await useSession(event, {password: reg.as_token})

View file

@ -23,3 +23,13 @@ block body
!= icons.Spots.SpotAlertXL
p Either the selected server doesn't exist, or you don't have the Manage Server permission on Discord.
p If you've checked your permissions, try #[a(href=rel("/oauth")) logging in again.]
else if !row
.s-empty-state.wmx4.p48
!= icons.Spots.SpotAlertXL
p Please add the bot to your server using the buttons on the home page.
else
.s-empty-state.wmx4.p48
!= icons.Spots.SpotAlertXL
p Access denied.

View file

@ -11,7 +11,7 @@ mixin space(space)
strong= space.name
if space.topic
ul.s-user-card--awards
li space.topic
li= space.topic
block body
.s-notice.s-notice__info.d-flex.g16

View file

@ -115,12 +115,12 @@ as.router.get("/guild", defineEventHandler(async event => {
// Permission problems
if (!guild_id || !guild || !(session.data.managedGuilds || []).concat(session.data.matrixGuilds || []).includes(guild_id) || !row) {
return pugSync.render(event, "guild_access_denied.pug", {guild_id})
return pugSync.render(event, "guild_access_denied.pug", {guild_id, row})
}
// Self-service guild that hasn't been linked yet - needs a special page encouraging the link flow
if (!row.space_id && row.autocreate === 0) {
const spaces = db.prepare("SELECT room_id, type, name, avatar FROM invite LEFT JOIN guild_space ON invite.room_id = guild_space.space_id WHERE mxid = ? AND space_id IS NULL and type = 'm.space'").all(session.data.mxid)
const spaces = db.prepare("SELECT room_id, type, name, topic, avatar FROM invite LEFT JOIN guild_space ON invite.room_id = guild_space.space_id WHERE mxid = ? AND space_id IS NULL and type = 'm.space'").all(session.data.mxid)
return pugSync.render(event, "guild_not_linked.pug", {guild, guild_id, spaces})
}

View file

@ -1,70 +1,96 @@
// @ts-check
const tryToCatch = require("try-to-catch")
const {test} = require("supertape")
const {router} = require("../../../test/web")
const {router, test} = require("../../../test/web")
const {MatrixServerError} = require("../../matrix/mreq")
let nonce
test("web guild: access denied when not logged in", async t => {
const content = await router.test("get", "/guild?guild_id=112760669178241024", {
const html = await router.test("get", "/guild?guild_id=112760669178241024", {
sessionData: {
},
})
t.match(content, /You need to log in to manage your servers./)
t.match(html, /You need to log in to manage your servers./)
})
test("web guild: asks to select guild if not selected", async t => {
const content = await router.test("get", "/guild", {
const html = await router.test("get", "/guild", {
sessionData: {
user_id: "1",
managedGuilds: []
},
})
t.match(content, /Select a server from the top right corner to continue./)
t.match(html, /Select a server from the top right corner to continue./)
})
test("web guild: access denied when guild id messed up", async t => {
const content = await router.test("get", "/guild?guild_id=1", {
const html = await router.test("get", "/guild?guild_id=1", {
sessionData: {
user_id: "1",
managedGuilds: []
},
})
t.match(content, /the selected server doesn't exist/)
t.match(html, /the selected server doesn't exist/)
})
test("web invite: access denied with invalid nonce", async t => {
const content = await router.test("get", "/invite?nonce=1")
t.match(content, /This QR code has expired./)
const html = await router.test("get", "/invite?nonce=1")
t.match(html, /This QR code has expired./)
})
test("web guild: can view unbridged guild", async t => {
const content = await router.test("get", "/guild?guild_id=66192955777486848", {
const html = await router.test("get", "/guild?guild_id=66192955777486848", {
sessionData: {
user_id: "1",
managedGuilds: ["66192955777486848"]
},
api: {
async getStateEvent(roomID, type, key) {
return {}
},
async getMembers(roomID, membership) {
return {chunk: []}
},
async getFullHierarchy(roomID) {
return []
}
}
})
t.match(content, /<h1[^<]*Function &amp; Arg/)
t.has(html, `<h1 class="s-page-title--header">Function &amp; Arg</h1>`)
})
test("web guild: unbridged self-service guild prompts log in to matrix", async t => {
const html = await router.test("get", "/guild?guild_id=665289423482519565", {
sessionData: {
user_id: "1",
managedGuilds: ["665289423482519565"]
}
})
t.has(html, `You picked self-service mode`)
t.has(html, `You need to log in with Matrix first`)
})
test("web guild: unbridged self-service guild asks to be invited", async t => {
const html = await router.test("get", "/guild?guild_id=665289423482519565", {
sessionData: {
mxid: "@user:example.org",
user_id: "1",
managedGuilds: ["665289423482519565"]
}
})
t.has(html, `On Matrix, invite <`)
})
test("web guild: unbridged self-service guild shows available spaces", async t => {
const html = await router.test("get", "/guild?guild_id=665289423482519565", {
sessionData: {
mxid: "@cadence:cadence.moe",
user_id: "1",
managedGuilds: ["665289423482519565"]
}
})
t.has(html, `<strong>Data Horde</strong>`)
t.has(html, `<li>here is the space topic</li>`)
// t.match(html, /<img class="avatar-image" src="https:\/\/bridge.cadence.moe\/download\/matrix\/cadence.moe\/TLqQOsTSrZkVKwBSWYTZNTrw">/)
// t.notMatch(html, /<strong>some room<\/strong>/)
// t.notMatch(html, /<strong>somebody else's space<\/strong>/)
})
test("web guild: can view bridged guild", async t => {
const content = await router.test("get", "/guild?guild_id=112760669178241024", {
const html = await router.test("get", "/guild?guild_id=112760669178241024", {
sessionData: {
managedGuilds: ["112760669178241024"]
},
@ -80,14 +106,14 @@ test("web guild: can view bridged guild", async t => {
}
}
})
t.match(content, /<h1[^<]*Psychonauts 3/)
nonce = content.match(/data-nonce="([a-f0-9-]+)"/)?.[1]
t.match(html, /<h1[^<]*Psychonauts 3/)
nonce = html.match(/data-nonce="([a-f0-9-]+)"/)?.[1]
t.ok(nonce)
})
test("web invite: page loads with valid nonce", async t => {
const content = await router.test("get", `/invite?nonce=${nonce}`)
t.match(content, /Invite a Matrix user/)
const html = await router.test("get", `/invite?nonce=${nonce}`)
t.match(html, /Invite a Matrix user/)
})