wip testing

This commit is contained in:
Cadence Ember 2025-02-04 17:23:46 +13:00
parent fa8ce28f88
commit e45786d581
3 changed files with 35 additions and 15 deletions

View file

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

View file

@ -23,3 +23,8 @@ block body
!= icons.Spots.SpotAlertXL != icons.Spots.SpotAlertXL
p Either the selected server doesn't exist, or you don't have the Manage Server permission on Discord. 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.] p If you've checked your permissions, try #[a(href=rel("/oauth")) logging in again.]
else
.s-empty-state.wmx4.p48
!= icons.Spots.SpotAlertXL
p Access denied.

View file

@ -1,49 +1,62 @@
// @ts-check // @ts-check
const assert = require("assert").strict
const domino = require("domino")
const tryToCatch = require("try-to-catch") const tryToCatch = require("try-to-catch")
const {test} = require("supertape") const {test} = require("supertape")
const {router} = require("../../../test/web") const {router} = require("../../../test/web")
const {MatrixServerError} = require("../../matrix/mreq") const {MatrixServerError} = require("../../matrix/mreq")
/**
* @param {string} html
*/
function getContent(html) {
const doc = domino.createDocument(html)
console.error(Object.values(doc.querySelectorAll("svg")))
const content = doc.getElementById("content")
assert(content)
return content.innerHTML.trim()
}
let nonce let nonce
test("web guild: access denied when not logged in", async t => { 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: { 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 => { 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: { sessionData: {
user_id: "1", user_id: "1",
managedGuilds: [] 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 => { 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: { sessionData: {
user_id: "1", user_id: "1",
managedGuilds: [] 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 => { test("web invite: access denied with invalid nonce", async t => {
const content = await router.test("get", "/invite?nonce=1") const html = await router.test("get", "/invite?nonce=1")
t.match(content, /This QR code has expired./) t.match(html, /This QR code has expired./)
}) })
test("web guild: can view unbridged guild", async t => { 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: { sessionData: {
user_id: "1", user_id: "1",
managedGuilds: ["66192955777486848"] managedGuilds: ["66192955777486848"]
@ -60,11 +73,12 @@ test("web guild: can view unbridged guild", async t => {
} }
} }
}) })
const content = getContent(html)
t.match(content, /<h1[^<]*Function &amp; Arg/) t.match(content, /<h1[^<]*Function &amp; Arg/)
}) })
test("web guild: can view bridged guild", async t => { 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: { sessionData: {
managedGuilds: ["112760669178241024"] managedGuilds: ["112760669178241024"]
}, },
@ -80,14 +94,14 @@ test("web guild: can view bridged guild", async t => {
} }
} }
}) })
t.match(content, /<h1[^<]*Psychonauts 3/) t.match(html, /<h1[^<]*Psychonauts 3/)
nonce = content.match(/data-nonce="([a-f0-9-]+)"/)?.[1] nonce = html.match(/data-nonce="([a-f0-9-]+)"/)?.[1]
t.ok(nonce) t.ok(nonce)
}) })
test("web invite: page loads with valid nonce", async t => { test("web invite: page loads with valid nonce", async t => {
const content = await router.test("get", `/invite?nonce=${nonce}`) const html = await router.test("get", `/invite?nonce=${nonce}`)
t.match(content, /Invite a Matrix user/) t.match(html, /Invite a Matrix user/)
}) })