General code coverage

This commit is contained in:
Cadence Ember
2026-01-09 03:49:32 +13:00
parent 0d15865bcd
commit 045fdfdf27
12 changed files with 331 additions and 43 deletions

View File

@@ -31,14 +31,13 @@ function getSnow(event) {
/** @type {Map<string, Promise<string>>} */
const cache = new Map()
/** @param {string} url */
/** @param {string | undefined} url */
function timeUntilExpiry(url) {
const params = new URL(url).searchParams
const ex = params.get("ex")
assert(ex) // refreshed urls from the discord api always include this parameter
const time = parseInt(ex, 16)*1000 - Date.now()
if (time > 0) return time
return false
}
function defineMediaProxyHandler(domain) {
@@ -71,6 +70,7 @@ function defineMediaProxyHandler(domain) {
refreshed = await promise
const time = timeUntilExpiry(refreshed)
assert(time) // the just-refreshed URL will always be in the future
/* c8 ignore next 3 */
setTimeout(() => {
cache.delete(url)
}, time).unref()

View File

@@ -5,20 +5,6 @@ const {test} = require("supertape")
const {router} = require("../../../test/web")
const {MatrixServerError} = require("../../matrix/mreq")
const snow = {
channel: {
async refreshAttachmentURLs(attachments) {
if (typeof attachments === "string") attachments = [attachments]
return {
refreshed_urls: attachments.map(a => ({
original: a,
refreshed: a + `?ex=${Math.floor(Date.now() / 1000 + 3600).toString(16)}`
}))
}
}
}
}
test("web download discord: access denied if not a known attachment", async t => {
const [error] = await tryToCatch(() =>
router.test("get", "/download/discordcdn/:channel_id/:attachment_id/:file_name", {
@@ -27,7 +13,19 @@ test("web download discord: access denied if not a known attachment", async t =>
attachment_id: "2",
file_name: "image.png"
},
snow
snow: {
channel: {
async refreshAttachmentURLs(attachments) {
if (typeof attachments === "string") attachments = [attachments]
return {
refreshed_urls: attachments.map(a => ({
original: a,
refreshed: a + `?ex=${Math.floor(Date.now() / 1000 + 3600).toString(16)}`
}))
}
}
}
}
})
)
t.ok(error)
@@ -42,8 +40,43 @@ test("web download discord: works if a known attachment", async t => {
file_name: "image.png"
},
event,
snow
snow: {
channel: {
async refreshAttachmentURLs(attachments) {
if (typeof attachments === "string") attachments = [attachments]
return {
refreshed_urls: attachments.map(a => ({
original: a,
refreshed: a + `?ex=${Math.floor(Date.now() / 1000 + 3600).toString(16)}`
}))
}
}
}
}
})
t.equal(event.node.res.statusCode, 302)
t.match(event.node.res.getHeader("location"), /https:\/\/cdn.discordapp.com\/attachments\/655216173696286746\/1314358913482621010\/image\.png\?ex=/)
})
test("web download discord: uses cache", async t => {
let notCalled = true
const event = {}
await router.test("get", "/download/discordcdn/:channel_id/:attachment_id/:file_name", {
params: {
channel_id: "655216173696286746",
attachment_id: "1314358913482621010",
file_name: "image.png"
},
event,
snow: {
channel: {
// @ts-ignore
async refreshAttachmentURLs(attachments) {
notCalled = false
throw new Error("tried to refresh when it should be in cache")
}
}
}
})
t.ok(notCalled)
})

View File

@@ -314,6 +314,13 @@ test("api invite: can invite to a moderated guild", async t => {
guest_can_join: false,
num_joined_members: 2,
}
yield {
room_id: spaceID,
children_state: [],
guest_can_join: false,
num_joined_members: 2,
room_type: "m.space"
}
},
async sendState(roomID, type, key, content) {
called++

View File

@@ -68,8 +68,7 @@ as.router.get("/api/message", defineEventHandler(async event => {
}
}
if (!matrix_author.displayname) matrix_author.displayname = mxid
if (matrix_author.avatar_url) matrix_author.avatar_url = mUtils.getPublicUrlForMxc(matrix_author.avatar_url)
else matrix_author.avatar_url = null
matrix_author.avatar_url = mUtils.getPublicUrlForMxc(matrix_author.avatar_url) || null
matrix_author["mxid"] = mxid
}

View File

@@ -148,7 +148,7 @@ test("web link space: check that inviting user has PL 50", async t => {
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
t.equal(type, "m.room.power_levels")
t.equal(key, "")
return {users: {"@_ooye_bot:cadence.moe": 100}}
return {users: {"@_ooye_bot:cadence.moe": 100}, events: {"m.room.tombstone": 150}}
},
async getStateEventOuter(roomID, type, key) {
called++
@@ -163,7 +163,7 @@ test("web link space: check that inviting user has PL 50", async t => {
event_id: "$create",
origin_server_ts: 0,
content: {
room_version: "11"
room_version: "12"
}
}
}
@@ -194,7 +194,7 @@ test("web link space: successfully adds entry to database and loads page", async
t.equal(roomID, "!zTMspHVUBhFLLSdmnS:cadence.moe")
t.equal(type, "m.room.power_levels")
t.equal(key, "")
return {users: {"@_ooye_bot:cadence.moe": 100, "@cadence:cadence.moe": 50}}
return {users: {"@cadence:cadence.moe": 50}}
},
async getStateEventOuter(roomID, type, key) {
called++
@@ -204,12 +204,12 @@ test("web link space: successfully adds entry to database and loads page", async
return {
type: "m.room.create",
state_key: "",
sender: "@creator:cadence.moe",
sender: "@_ooye_bot:cadence.moe",
room_id: "!zTMspHVUBhFLLSdmnS:cadence.moe",
event_id: "$create",
origin_server_ts: 0,
content: {
room_version: "11"
room_version: "12"
}
}
}

View File

@@ -0,0 +1,16 @@
// @ts-check
const tryToCatch = require("try-to-catch")
const {test} = require("supertape")
const {router} = require("../../../test/web")
test("web password: stores password", async t => {
const event = {}
await router.test("post", "/api/password", {
body: {
password: "password123"
},
event
})
t.equal(event.node.res.statusCode, 302)
})