From e45786d581c674d9233e3857d64679e64e3d6d29 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 4 Feb 2025 17:23:46 +1300 Subject: [PATCH] wip testing --- src/web/pug-sync.js | 3 ++- src/web/pug/guild_access_denied.pug | 5 ++++ src/web/routes/guild.test.js | 42 +++++++++++++++++++---------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/web/pug-sync.js b/src/web/pug-sync.js index fb83caa..a966f06 100644 --- a/src/web/pug-sync.js +++ b/src/web/pug-sync.js @@ -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}) diff --git a/src/web/pug/guild_access_denied.pug b/src/web/pug/guild_access_denied.pug index 319d4de..b62a06d 100644 --- a/src/web/pug/guild_access_denied.pug +++ b/src/web/pug/guild_access_denied.pug @@ -23,3 +23,8 @@ 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 + .s-empty-state.wmx4.p48 + != icons.Spots.SpotAlertXL + p Access denied. diff --git a/src/web/routes/guild.test.js b/src/web/routes/guild.test.js index 3ef177e..93c394a 100644 --- a/src/web/routes/guild.test.js +++ b/src/web/routes/guild.test.js @@ -1,49 +1,62 @@ // @ts-check +const assert = require("assert").strict +const domino = require("domino") const tryToCatch = require("try-to-catch") const {test} = require("supertape") const {router} = require("../../../test/web") 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 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"] @@ -60,11 +73,12 @@ test("web guild: can view unbridged guild", async t => { } } }) + const content = getContent(html) t.match(content, / { - 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 +94,14 @@ test("web guild: can view bridged guild", async t => { } } }) - t.match(content, / { - 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/) })