import test from "node:test"; import assert from "node:assert/strict"; Object.assign(process.env, { NODE_ENV: "test", DISCORD_GUILD_ID: "1", SHINUWA_USER_ID: "1", WEB_SERVER_PORT: "3000", WEB_API_KEY: "web-key", NOTIFICATIONS_API_KEY: "notifications-key", DISCORD_APPLICATION_ID: "1", DISCORD_TOKEN: "test-token", VOICE_CHANNEL_ID: "1", TEXT_CHANNEL_ID: "1", GAME_SERVERS_MESSAGE_ID: "1", }); const { createApp } = await import("../src/web/server.js"); const { isLoopbackAddress } = await import("../src/web/middlewares/auth.js"); const USER_ID = "123456789012345678"; const CLOSED_USER_ID = "223456789012345678"; function fakeClient() { const sent = []; return { sent, users: { async fetch(userId) { return { async send(message) { if (userId === CLOSED_USER_ID) { throw Object.assign(new Error("Cannot send messages to this user"), { code: 50007 }); } sent.push({ userId, ...message }); }, }; }, }, }; } const fakeQueue = { snapshot: () => ({ current: null, upcoming: [], status: "idle" }), }; async function withServer(options, run) { const client = fakeClient(); const app = createApp({ queue: fakeQueue, client }, options); const server = app.listen(0, "127.0.0.1"); await new Promise((resolve) => server.once("listening", resolve)); const base = `http://127.0.0.1:${server.address().port}`; try { await run({ base, client }); } finally { await new Promise((resolve) => server.close(resolve)); } } function sendDm(base, key, userId = USER_ID) { return fetch(`${base}/api/notifications/dm`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}` }, body: JSON.stringify({ userId, content: "Votre expédition est de retour !" }), }); } test("the DM route accepts only the notifications key", async () => { await withServer({}, async ({ base, client }) => { assert.equal((await sendDm(base, "notifications-key")).status, 204); assert.equal((await sendDm(base, "web-key")).status, 401); assert.equal(client.sent.length, 1); }); }); test("refused DMs do not lock out an authenticated application", async () => { await withServer({}, async ({ base, client }) => { for (let index = 0; index < 25; index++) { assert.equal((await sendDm(base, "notifications-key", CLOSED_USER_ID)).status, 422); } assert.equal((await sendDm(base, "notifications-key")).status, 204); assert.equal(client.sent.length, 1); }); }); test("repeated wrong keys are still rate limited", async () => { await withServer({}, async ({ base }) => { for (let index = 0; index < 20; index++) { const response = await fetch(`${base}/api/queue`, { headers: { Authorization: "Bearer wrong-key" }, }); assert.equal(response.status, 401); } const blocked = await fetch(`${base}/api/queue`, { headers: { Authorization: "Bearer web-key" }, }); assert.equal(blocked.status, 429); }); }); test("the DM route is disabled without a notifications key", async () => { await withServer({ notificationsApiKey: null }, async ({ base, client }) => { const response = await sendDm(base, "notifications-key"); assert.equal(response.status, 503); assert.equal(client.sent.length, 0); }); }); test("only loopback connections count as local", () => { for (const address of ["127.0.0.1", "::1", "::ffff:127.0.0.1"]) { assert.equal(isLoopbackAddress(address), true); } for (const address of ["192.168.0.104", "213.251.163.57", "::ffff:192.168.0.104", undefined]) { assert.equal(isLoopbackAddress(address), false); } });