// 사이트 "내 정보" (표시 이름) · 관리 화면 API (v1.6.0) import { test } from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import http from 'node:http'; import os from 'node:os'; import path from 'node:path'; import { buildServer } from '../src/app.ts'; import type { Config } from '../src/config.ts'; const SUPER = '293719842274541579'; const ADMIN = '111111111111111111'; const MEMBER = '222222222222222222'; const OTHER = '333333333333333333'; const tmp = () => fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-profile-')); const NICK: Record = { [SUPER]: '사금향', [ADMIN]: '관리', [MEMBER]: '멤버닉', [OTHER]: '다른' }; async function fakeDiscord() { const srv = http.createServer((req, res) => { const u = new URL(req.url!, 'http://x'); res.setHeader('content-type', 'application/json'); const m = /^\/guilds\/111\/members\/(\d+)$/.exec(u.pathname); if (m && NICK[m[1]!]) return res.end(JSON.stringify({ nick: NICK[m[1]!], avatar: null })); const usr = /^\/users\/(\d+)$/.exec(u.pathname); if (usr && NICK[usr[1]!]) return res.end(JSON.stringify({ id: usr[1], username: 'u' + usr[1], global_name: NICK[usr[1]!], avatar: null })); if (u.pathname === '/releases/latest') return res.end(JSON.stringify({ tag_name: 'v1.6.0', html_url: 'https://git.example/releases/tag/v1.6.0', published_at: '2026-10-17T00:00:00Z' })); res.statusCode = 404; res.end('{}'); }); await new Promise((r) => srv.listen(0, '127.0.0.1', () => r())); return { url: `http://127.0.0.1:${(srv.address() as any).port}`, close: () => srv.close() }; } async function stack() { const dc = await fakeDiscord(); const cfg: Config = { port: 0, host: '127.0.0.1', publicUrl: 'https://gvr.example', clientId: 'cid', clientSecret: 's', botToken: 'bot', allowedGuilds: ['111'], owners: [SUPER], superAdmins: [SUPER], sessionSecret: 'x'.repeat(32), dataDir: tmp(), webDir: '/nonexistent', version: 'test', discordApi: dc.url, assetUpstream: {}, releasesApi: `${dc.url}/releases/latest`, }; const { app, store } = await buildServer(cfg); store.upsertAppUser({ id: ADMIN, name: '관리', avatar: null, role: 'admin', addedBy: SUPER }); await app.listen({ host: '127.0.0.1', port: 0 }); const base = `http://127.0.0.1:${(app.server.address() as any).port}`; const cookieOf = (id: string) => { const s = store.createSession({ id, name: NICK[id]!, avatar: null }); return `gvr_sid=${app.signCookie(s.token)}`; }; const as = (id: string) => { const cookie = cookieOf(id); const call = async (method: string, p: string, body?: unknown, origin = 'https://gvr.example') => { const r = await fetch(base + p, { method, headers: { cookie, ...(body !== undefined ? { 'content-type': 'application/json', origin } : {}) }, body: body === undefined ? undefined : JSON.stringify(body) }); return { status: r.status, json: (await r.json().catch(() => null)) as any }; }; return { get: (p: string) => call('GET', p), put: (p: string, b: unknown, o?: string) => call('PUT', p, b, o), post: (p: string, b: unknown) => call('POST', p, b), patch: (p: string, b: unknown) => call('PATCH', p, b), del: (p: string) => call('DELETE', p, {}) }; }; return { store, as, close: async () => (await app.close(), dc.close()) }; } test('내 정보: 누구나 자기 표시 이름을 바꾸고, 지우면 디스코드 별명으로 돌아간다', async () => { const s = await stack(); try { const me = s.as(MEMBER); assert.deepEqual((await me.get('/site/profile')).json, { user: { id: MEMBER, name: '멤버닉', avatar: null }, customName: null, role: null }); const r = await me.put('/site/profile/name', { name: ' 하늘 별 ' }); assert.equal(r.status, 200); assert.equal(r.json.user.name, '하늘 별'); assert.equal((await me.get('/auth/me')).json.user.name, '하늘 별'); assert.equal(s.store.displayNames()[MEMBER], '하늘 별'); // 지우기 → 디스코드 서버 별명 const back = await me.put('/site/profile/name', { name: '' }); assert.equal(back.status, 200); assert.equal(back.json.customName, null); assert.equal((await me.get('/auth/me')).json.user.name, '멤버닉'); assert.equal(s.store.displayNames()[MEMBER], undefined); } finally { await s.close(); } }); test('내 정보: 다른 사람 이름 · 너무 긴 이름 · 보이지 않는 글자 · 다른 사이트에서 온 요청은 막는다', async () => { const s = await stack(); try { assert.equal((await s.as(OTHER).put('/site/profile/name', { name: '하늘' })).status, 200); const me = s.as(MEMBER); const dup = await me.put('/site/profile/name', { name: '하늘' }); assert.equal(dup.status, 400); assert.match(dup.json.message, /이미 쓰는 이름/); assert.equal((await me.put('/site/profile/name', { name: '가'.repeat(33) })).status, 400); assert.equal((await me.put('/site/profile/name', { name: '하​늘' })).status, 400); assert.equal((await me.put('/site/profile/name', { name: '별' }, 'https://evil.example')).status, 403); assert.equal(s.store.displayNames()[MEMBER], undefined); } finally { await s.close(); } }); test('관리 화면: 관리자는 목록 · 남의 표시 이름, 등록 · 역할 · 해제는 최고 관리자만, 일반 멤버는 못 들어온다', async () => { const s = await stack(); try { const member = s.as(MEMBER); const admin = s.as(ADMIN); const sup = s.as(SUPER); assert.equal((await member.get('/site/admin/users')).status, 403); assert.equal((await member.get('/site/admin/overview')).status, 403); assert.equal((await member.put('/site/admin/names', { id: OTHER, name: 'x' })).status, 403); const list = await admin.get('/site/admin/users'); assert.equal(list.status, 200); assert.deepEqual(list.json.users.map((u: any) => [u.id, u.role]), [[SUPER, 'superadmin'], [ADMIN, 'admin']]); const ov = await admin.get('/site/admin/overview'); assert.equal(ov.json.role, 'admin'); assert.equal(ov.json.latest.version, '1.6.0'); assert.equal(ov.json.device.registered, false); const named = await admin.put('/site/admin/names', { id: MEMBER, name: '지호' }); assert.equal(named.status, 200); assert.equal(named.json.names[MEMBER], '지호'); assert.equal((await member.get('/auth/me')).json.user.name, '지호', '로그인해 있는 세션 이름도 바뀐다'); assert.equal((await admin.post('/site/admin/users', { id: MEMBER, role: 'user' })).status, 403); const added = await sup.post('/site/admin/users', { id: MEMBER, role: 'user' }); assert.equal(added.status, 200); assert.equal(added.json.users.find((u: any) => u.id === MEMBER).name, '지호', '등록할 때도 표시 이름으로'); assert.equal((await sup.patch(`/site/admin/users/${MEMBER}`, { role: 'admin' })).json.users.find((u: any) => u.id === MEMBER).role, 'admin'); assert.equal((await sup.patch(`/site/admin/users/${SUPER}`, { role: 'user' })).status, 400); assert.equal((await sup.del(`/site/admin/users/${MEMBER}`)).status, 200); assert.equal((await member.get('/site/admin/users')).status, 403); } finally { await s.close(); } });