Files
teamspeak-music-bot/src/web/api/session.test.ts
T

369 lines
15 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import express from "express";
import cookieParser from "cookie-parser";
import request from "supertest";
import pino from "pino";
import { createDatabase, type BotDatabase } from "../../data/database.js";
import { createUserStore, type UserStore } from "../../data/users.js";
import { createSessionStore, type SessionStore } from "../../data/sessions.js";
import { createApiKeyStore, type ApiKeyStore } from "../../data/api-keys.js";
import { createAuditStore } from "../../data/audit.js";
import { createPermissionStore } from "../../data/permissions.js";
import { getDefaultConfig, type GuestModeConfig } from "../../data/config.js";
import type { GuestPermissions, BotAccess } from "../../data/permissions.js";
import { createSessionRouter } from "./session.js";
import { SESSION_COOKIE_NAME } from "../auth/validateSession.js";
function makeApp(botDb: BotDatabase, users: UserStore, sessions: SessionStore, apiKeys?: ApiKeyStore) {
const app = express();
app.use(express.json());
app.use(cookieParser());
const audit = createAuditStore(botDb.db);
const permissions = createPermissionStore(botDb.db);
app.use(
"/api/session",
createSessionRouter(
users,
sessions,
audit,
pino({ level: "silent" }),
permissions,
() => getDefaultConfig().guestMode,
apiKeys
)
);
return app;
}
function extractCookie(res: request.Response): string {
const header = res.headers["set-cookie"];
const arr = Array.isArray(header) ? header : header ? [header] : [];
const found = arr.find((c) => c.startsWith(`${SESSION_COOKIE_NAME}=`));
if (!found) throw new Error("no session cookie set");
return found.split(";")[0]; // "tsmb_session=xxxx"
}
describe("session router", () => {
let botDb: BotDatabase;
let users: UserStore;
let sessions: SessionStore;
let app: express.Express;
beforeEach(() => {
botDb = createDatabase(":memory:");
users = createUserStore(botDb.db);
sessions = createSessionStore(botDb.db);
app = makeApp(botDb, users, sessions);
});
afterEach(() => botDb.close());
it("GET /needs-setup returns true on an empty db", async () => {
const res = await request(app).get("/api/session/needs-setup");
expect(res.status).toBe(200);
expect(res.body).toEqual({ needsSetup: true, guestAllowed: false });
});
it("POST /setup creates the first admin, logs them in, and returns false from /needs-setup afterwards", async () => {
const setupRes = await request(app)
.post("/api/session/setup")
.send({ username: "alice", password: "hunter2-hunter2" });
expect(setupRes.status).toBe(200);
expect(setupRes.body.username).toBe("alice");
extractCookie(setupRes);
const needs = await request(app).get("/api/session/needs-setup");
expect(needs.body).toEqual({ needsSetup: false, guestAllowed: false });
});
it("POST /setup returns 409 once a user already exists", async () => {
await users.createUser("admin", "pw-admin-pw", "admin");
const res = await request(app)
.post("/api/session/setup")
.send({ username: "alice", password: "pw" });
expect(res.status).toBe(409);
expect(res.body).toEqual({ error: "already initialized" });
});
it("POST /login returns 401 with constant-time delay on bad credentials", async () => {
await users.createUser("alice", "correct-pw-pw", "admin");
const start = Date.now();
const res = await request(app)
.post("/api/session/login")
.send({ username: "alice", password: "wrong" });
expect(res.status).toBe(401);
expect(res.body).toEqual({ error: "invalid credentials" });
expect(Date.now() - start).toBeGreaterThanOrEqual(200);
}, 10_000);
it("POST /login sets a session cookie on success", async () => {
await users.createUser("alice", "pw-alice", "admin");
const res = await request(app)
.post("/api/session/login")
.send({ username: "alice", password: "pw-alice" });
expect(res.status).toBe(200);
expect(res.body.username).toBe("alice");
extractCookie(res);
});
it("GET /me returns the current user when cookie is present, 401 otherwise", async () => {
await users.createUser("alice", "pw-alice", "admin");
const loginRes = await request(app)
.post("/api/session/login")
.send({ username: "alice", password: "pw-alice" });
const cookie = extractCookie(loginRes);
const me = await request(app).get("/api/session/me").set("Cookie", cookie);
expect(me.status).toBe(200);
expect(me.body.username).toBe("alice");
// alice is the first user (an admin), so /me exposes all capabilities and full bot access.
expect(Array.isArray(me.body.capabilities)).toBe(true);
expect(me.body.capabilities).toEqual(
expect.arrayContaining(["player.control", "player.queue", "bot.manage", "platform.auth", "quality"])
);
expect(me.body.bots).toBe("all");
const anon = await request(app).get("/api/session/me");
expect(anon.status).toBe(401);
});
it("stores accent preferences per account and returns them to other sessions", async () => {
const alice = await users.createUser("alice", "pw-alice", "member");
const bob = await users.createUser("bob", "pw-bob", "member");
const cookieA = `${SESSION_COOKIE_NAME}=${sessions.createSession(alice.id).token}`;
const cookieB = `${SESSION_COOKIE_NAME}=${sessions.createSession(alice.id).token}`;
const updated = await request(app).put('/api/session/preferences').set('Cookie', cookieA)
.send({ accentColor: '#8B5CF6', userId: bob.id });
expect(updated.status).toBe(200);
expect(updated.body.accentColor).toBe('#8b5cf6');
expect((await request(app).get('/api/session/me').set('Cookie', cookieB)).body.accentColor).toBe('#8b5cf6');
expect(users.getAccentColor(bob.id)).toBe('#335eea');
});
it("rejects anonymous and invalid preference updates without altering the account", async () => {
const alice = await users.createUser("alice", "pw-alice", "member");
const cookie = `${SESSION_COOKIE_NAME}=${sessions.createSession(alice.id).token}`;
expect((await request(app).put('/api/session/preferences').send({ accentColor: '#8b5cf6' })).status).toBe(401);
for (const accentColor of ['red', '#fff', '#zzzzzz', null, 123]) {
expect((await request(app).put('/api/session/preferences').set('Cookie', cookie).send({ accentColor })).status).toBe(400);
}
expect(users.getAccentColor(alice.id)).toBe('#335eea');
});
it("POST /logout deletes the session and clears the cookie", async () => {
await users.createUser("alice", "pw-alice", "admin");
const loginRes = await request(app)
.post("/api/session/login")
.send({ username: "alice", password: "pw-alice" });
const cookie = extractCookie(loginRes);
const logout = await request(app).post("/api/session/logout").set("Cookie", cookie);
expect(logout.status).toBe(204);
const me = await request(app).get("/api/session/me").set("Cookie", cookie);
expect(me.status).toBe(401);
});
it("POST /change-password requires old password and invalidates other sessions", async () => {
const u = await users.createUser("alice", "old-pw-pw", "admin");
const cookieA = extractCookie(
await request(app).post("/api/session/login").send({ username: "alice", password: "old-pw-pw" })
);
const cookieB = extractCookie(
await request(app).post("/api/session/login").send({ username: "alice", password: "old-pw-pw" })
);
const wrongOld = await request(app)
.post("/api/session/change-password")
.set("Cookie", cookieA)
.send({ oldPassword: "WRONG", newPassword: "newpassword" });
expect(wrongOld.status).toBe(401);
const ok = await request(app)
.post("/api/session/change-password")
.set("Cookie", cookieA)
.send({ oldPassword: "old-pw-pw", newPassword: "newpassword" });
expect(ok.status).toBe(204);
const meA = await request(app).get("/api/session/me").set("Cookie", cookieA);
expect(meA.status).toBe(200);
const meB = await request(app).get("/api/session/me").set("Cookie", cookieB);
expect(meB.status).toBe(401);
expect(u.id).toBe(meA.body.id);
// 20s, not the 5s default: this case runs SIX bcryptjs rounds (one hash to
// create the user, four verifies, one hash for the new password), and
// bcryptjs is pure JS. It takes ~4.5s on an idle machine — close enough to
// the default that it tipped over whenever the full suite saturated the
// CPU, which made it look like a real intermittent failure. The work is
// genuinely slow, not hung, so the timeout is what was wrong.
}, 20000);
});
describe("session router — API key revocation", () => {
let botDb: BotDatabase;
let users: UserStore;
let sessions: SessionStore;
let apiKeys: ApiKeyStore;
let app: express.Express;
let userId: string;
let currentCookie: string;
let rawKey: string;
beforeEach(async () => {
botDb = createDatabase(":memory:");
users = createUserStore(botDb.db);
sessions = createSessionStore(botDb.db);
apiKeys = createApiKeyStore(botDb.db);
const member = await users.createUser("alice", "old-password", "member");
userId = member.id;
currentCookie = `${SESSION_COOKIE_NAME}=${sessions.createSession(userId).token}`;
rawKey = apiKeys.create(userId, "integration")!.rawKey;
app = makeApp(botDb, users, sessions, apiKeys);
});
afterEach(() => botDb.close());
it("successful password change revokes all owned keys and preserves only the active browser session", async () => {
const secondKey = apiKeys.create(userId, "another-integration")!.rawKey;
const otherSession = `${SESSION_COOKIE_NAME}=${sessions.createSession(userId).token}`;
const otherUser = await users.createUser("bob", "other-password", "member");
const otherUserKey = apiKeys.create(otherUser.id, "other-user-integration")!.rawKey;
const changed = await request(app).post("/api/session/change-password")
.set("Cookie", currentCookie)
.send({ oldPassword: "old-password", newPassword: "new-password" });
expect(changed.status).toBe(204);
expect(apiKeys.validateAndTouch(rawKey)).toBeNull();
expect(apiKeys.validateAndTouch(secondKey)).toBeNull();
expect(apiKeys.listForUser(userId)).toEqual([]);
expect(apiKeys.validateAndTouch(otherUserKey)?.userId).toBe(otherUser.id);
expect((await request(app).get("/api/session/me").set("Cookie", currentCookie)).status).toBe(200);
expect((await request(app).get("/api/session/me").set("Cookie", otherSession)).status).toBe(401);
}, 20_000);
it.each([
{ oldPassword: "wrong-password", newPassword: "new-password", status: 401 },
{ oldPassword: "old-password", newPassword: "short", status: 400 },
])("failed password change ($status) leaves API keys valid", async ({ oldPassword, newPassword, status }) => {
const changed = await request(app).post("/api/session/change-password")
.set("Cookie", currentCookie)
.send({ oldPassword, newPassword });
expect(changed.status).toBe(status);
expect(apiKeys.validateAndTouch(rawKey)?.userId).toBe(userId);
expect((await request(app).get("/api/session/me").set("Cookie", currentCookie)).status).toBe(200);
});
it("unauthenticated password change leaves API keys valid", async () => {
const changed = await request(app).post("/api/session/change-password")
.send({ oldPassword: "old-password", newPassword: "new-password" });
expect(changed.status).toBe(401);
expect(apiKeys.validateAndTouch(rawKey)?.userId).toBe(userId);
});
});
describe("session router — guest mode", () => {
let botDb: BotDatabase;
afterEach(() => botDb.close());
function makeApp(opts: {
guestEnabled: boolean;
guestPermissions?: GuestPermissions;
guestBots?: BotAccess;
}) {
botDb = createDatabase(":memory:");
const users = createUserStore(botDb.db);
const sessions = createSessionStore(botDb.db);
const audit = createAuditStore(botDb.db);
const permissions = createPermissionStore(botDb.db);
const guestCfg: GuestModeConfig = {
enabled: opts.guestEnabled,
bots: opts.guestBots ?? getDefaultConfig().guestMode.bots,
permissions: opts.guestPermissions ?? getDefaultConfig().guestMode.permissions,
};
const app = express();
app.use(express.json());
app.use(cookieParser());
app.use(
"/api/session",
createSessionRouter(users, sessions, audit, pino({ level: "silent" }), permissions, () => guestCfg)
);
return { app, users, sessions };
}
it("POST /guest is 403 when guest mode disabled", async () => {
const { app } = makeApp({ guestEnabled: false });
const res = await request(app).post("/api/session/guest");
expect(res.status).toBe(403);
});
it("POST /guest mints a guest session when enabled, and /me reports role guest + flags", async () => {
const { app } = makeApp({
guestEnabled: true,
guestPermissions: {
addToQueue: true,
playNext: true,
playNow: false,
skip: false,
transport: false,
removeClear: false,
playMode: false,
playCollection: false,
},
guestBots: "all",
});
const login = await request(app).post("/api/session/guest");
expect(login.status).toBe(200);
expect(login.body.role).toBe("guest");
const cookie = login.headers["set-cookie"];
const me = await request(app).get("/api/session/me").set("Cookie", cookie);
expect(me.body.role).toBe("guest");
expect(me.body.guest.addToQueue).toBe(true);
expect(me.body.guest.playNext).toBe(true);
expect(me.body.capabilities).toEqual([]);
expect(me.body.accentColor).toBeNull();
expect((await request(app).put('/api/session/preferences').set('Cookie', cookie).send({ accentColor: '#8b5cf6' })).status).toBe(403);
});
it("GET /needs-setup exposes guestAllowed", async () => {
const { app } = makeApp({ guestEnabled: true });
const res = await request(app).get("/api/session/needs-setup");
expect(res.body.guestAllowed).toBe(true);
});
it("GET /me returns 401 for a guest session once guest mode is disabled", async () => {
// Build an app whose guest config can be toggled at runtime, mirroring an
// admin flipping the setting mid-session (requireAuthInline must reject).
botDb = createDatabase(":memory:");
const users = createUserStore(botDb.db);
const sessions = createSessionStore(botDb.db);
const audit = createAuditStore(botDb.db);
const permissions = createPermissionStore(botDb.db);
const guestCfg: GuestModeConfig = {
enabled: true,
bots: getDefaultConfig().guestMode.bots,
permissions: getDefaultConfig().guestMode.permissions,
};
const app = express();
app.use(express.json());
app.use(cookieParser());
app.use(
"/api/session",
createSessionRouter(users, sessions, audit, pino({ level: "silent" }), permissions, () => guestCfg)
);
const login = await request(app).post("/api/session/guest");
expect(login.status).toBe(200);
const cookie = login.headers["set-cookie"];
// While enabled, /me works for the guest.
expect((await request(app).get("/api/session/me").set("Cookie", cookie)).status).toBe(200);
// Admin disables guest mode → the in-flight guest session is now invalid.
guestCfg.enabled = false;
expect((await request(app).get("/api/session/me").set("Cookie", cookie)).status).toBe(401);
});
});