The last split PR of docs/website/API_V2_PLAN.md § Phase 2. public.routes.js,
player.routes.js and auth.routes.js are deleted; each group is now a directory
whose index.js owns the group gate and the mount table and declares no routes.
Every one of the 200 manifest routes is now in a capability router.
public/ posts (2) wiki (4) pages (2) shard (12) site (4, group root)
player/ account (8) shard (8) appeals (4), behind noindex + requireAuth
auth/ login (2) register (1) invite (2) password (3) session (2, root)
No URL moves. All four gates zero-diff: routes.manifest.json (200 public + 2
internal), routes.guards.json, swagger-output.json (198 operations), and
docs/website/api-route-inventory.json was already in sync. 434 tests green.
Notes on the non-mechanical parts:
- public/index.js and auth/index.js carry no group gate, deliberately, and say
so. The public surface is anonymous by contract (logged-out SPA, Discord bot,
Android ShardStreamClient on /public/shard/stream); /auth is where a caller
becomes authenticated. player/index.js gates on requireAuth only, never
requireRole('player') — staff are a superset of players.
- GET /auth/me has a mount-order dependency: use('/me', meRouter) matches the
bare /me, so the request runs meRouter's noindex + requireAuth and falls
through. session.router.js must stay mounted last. Verified by the
counterfactual — mounting it first still 401s but drops X-Robots-Tag, which
no manifest or guards file can see.
- loginGuards moved to auth/loginGuards.js (frozen) rather than being copied
into the three routers that spread it; sso.routes.js drops its duplicate.
- The :param shadowing check was re-run in dispatch order against the built
stack: 86 routes, 64 literal, none shadowed. /public/wiki/{categories,tags}
ahead of /:slug is the only ordering-sensitive pair.
Co-Authored-By: Claude <noreply@anthropic.com>
85 lines
3.4 KiB
JavaScript
85 lines
3.4 KiB
JavaScript
// Point the DB at a closed port BEFORE requiring anything that builds the pool,
|
|
// so any stray query fails fast instead of holding the process open. The session
|
|
// service, users model, and shardLinks model are all stubbed, so no query runs.
|
|
process.env.DB_HOST = '127.0.0.1'
|
|
process.env.DB_PORT = '59999'
|
|
|
|
const { test, after, afterEach } = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
|
|
const { startApp } = require('./_helper')
|
|
const playerRouter = require('../src/router/v1/player')
|
|
const sessionService = require('../src/auth/session.service')
|
|
const users = require('../src/model/users/users.model')
|
|
const shardLinks = require('../src/model/shardLinks/shardLinks.model')
|
|
const db = require('../src/utils/db')
|
|
|
|
after(() => db.close())
|
|
|
|
const originals = {
|
|
validateSession: sessionService.validateSession,
|
|
isSessionRevoked: sessionService.isSessionRevoked,
|
|
sessionMeta: sessionService.sessionMeta,
|
|
getById: users.getById,
|
|
listForUser: shardLinks.listForUser,
|
|
}
|
|
afterEach(() => {
|
|
sessionService.validateSession = originals.validateSession
|
|
sessionService.isSessionRevoked = originals.isSessionRevoked
|
|
sessionService.sessionMeta = originals.sessionMeta
|
|
users.getById = originals.getById
|
|
shardLinks.listForUser = originals.listForUser
|
|
})
|
|
|
|
// Sign every request in as the given DB user (role decides the gate outcome).
|
|
function signInAs(user) {
|
|
sessionService.validateSession = () => ({ userId: user.id, sessionId: 's1', createdAt: Date.now(), authMethod: 'jwt' })
|
|
sessionService.isSessionRevoked = async () => false
|
|
sessionService.sessionMeta = () => ({})
|
|
users.getById = async () => user
|
|
}
|
|
|
|
// The player self-service group is intentionally role-agnostic (staff are a
|
|
// superset of players): any authenticated, active account reaches the self-scoped
|
|
// handlers. Regression guard for the fix that dropped requireRole('player') so a
|
|
// staff/admin account is no longer 403'd out of its own characters.
|
|
for (const role of ['player', 'admin', 'editor', 'moderator']) {
|
|
test(`GET /player/shard/accounts is reachable by an authenticated ${role}`, async () => {
|
|
signInAs({ id: 7, username: 'u', role, status: 'active' })
|
|
shardLinks.listForUser = async (id) => {
|
|
assert.equal(id, 7) // self-scoped to the caller regardless of role
|
|
return [{ account: 'acctA' }]
|
|
}
|
|
const app = await startApp((a) => a.use('/api/v1/player', playerRouter))
|
|
try {
|
|
const res = await fetch(app.url + '/api/v1/player/shard/accounts')
|
|
assert.equal(res.status, 200, `${role} should reach the handler, got ${res.status}`)
|
|
assert.deepEqual(await res.json(), [{ account: 'acctA' }])
|
|
} finally {
|
|
await app.close()
|
|
}
|
|
})
|
|
}
|
|
|
|
test('GET /player/shard/accounts still rejects an unauthenticated caller with 401', async () => {
|
|
sessionService.validateSession = () => null
|
|
const app = await startApp((a) => a.use('/api/v1/player', playerRouter))
|
|
try {
|
|
const res = await fetch(app.url + '/api/v1/player/shard/accounts')
|
|
assert.equal(res.status, 401)
|
|
} finally {
|
|
await app.close()
|
|
}
|
|
})
|
|
|
|
test('a disabled account is still rejected with 403 (status gate, not role)', async () => {
|
|
signInAs({ id: 8, username: 'banned', role: 'player', status: 'disabled' })
|
|
const app = await startApp((a) => a.use('/api/v1/player', playerRouter))
|
|
try {
|
|
const res = await fetch(app.url + '/api/v1/player/shard/accounts')
|
|
assert.equal(res.status, 403)
|
|
} finally {
|
|
await app.close()
|
|
}
|
|
})
|