'use strict'; const test = require('node:test'), assert = require('node:assert/strict'), crypto = require('node:crypto'), fs = require('node:fs'), os = require('node:os'), path = require('node:path'); const { createApp } = require('../server'); test('HTTP权限、登录、CSRF、并发重试、客户页面和备份访问', async t => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-http-test-')); const app = createApp({ mode: 'demo', port: 0, dataDir: dir }); await new Promise(r => app.server.listen(0, '127.0.0.1', r)); t.after(async () => { await new Promise(r => { app.server.closeAllConnections(); app.server.close(r); }); app.ledger.close(); fs.rmSync(dir, { recursive: true, force: true }); }); const base = 'http://127.0.0.1:' + app.server.address().port; let cookie = ''; const call = async (route, data, key = crypto.randomUUID(), extra = {}) => { const r = await fetch(base + route, { method: data ? 'POST' : 'GET', headers: { ...(cookie ? { Cookie: cookie } : {}), ...(data ? { 'Content-Type': 'application/json', Origin: base, 'X-Requested-With': 'maimai-ledger', 'Idempotency-Key': key, } : {}), ...extra, }, ...(data ? { body: JSON.stringify(data) } : {}), }); return r; }; assert.equal((await call('/api/state')).status, 401); assert.equal( (await call('/api/demo-login', { role: 'owner' }, undefined, { Origin: 'https://evil.example' })).status, 403, ); const login = await call('/api/demo-login', { role: 'owner' }); cookie = login.headers.get('set-cookie').split(';')[0]; assert.match(login.headers.get('set-cookie'), /HttpOnly/); const s = await (await call('/api/state')).json(); assert.equal(s.orders[0].status, 'delivered'); const payload = { date: s.today, customer: '合成客户', internal_note: '测试内部备注', salesperson: '合成开单人', payment_account: '助理微信', lines: [ { name: 'HTTP测试商品', unit: 'g', quantity: '1.2', pocket: '现货', payer: 'owner', cost: '1000', sale: '1200' }, ], fee_items: [], }; const key = crypto.randomUUID(); const results = await Promise.all([call('/api/orders/issue', payload, key), call('/api/orders/issue', payload, key)]); assert.deepEqual( results.map(r => r.status), [200, 200], ); const a = await results[0].json(), b = await results[1].json(), id = a.id; assert.equal(a.id, b.id); const detail = await (await call('/api/orders/' + id)).json(); assert.equal(detail.received, 120000); assert.equal(detail.payments.length, 1); assert.equal(detail.status, 'delivered'); assert.equal(detail.profit, 20000); const printable = await (await call('/api/orders/' + id + '/public')).json(); assert.ok(!('profit' in printable)); assert.ok(!('cost' in printable.lines[0])); const html = await (await call('/print/' + id)).text(); assert.ok(html.includes('纸单号')); assert.ok(!html.includes('系统单号')); assert.ok(!html.includes('1000.00')); assert.equal( (await call('/api/orders/' + id + '/receipt', { date: s.today, amount: '1', account: '助理微信', verified: true })) .status, 400, ); const backup = await (await call('/api/backup', {})).json(); assert.equal((await call('/api/backup/' + backup.name)).status, 200); const login2 = await call('/api/demo-login', { role: 'assistant' }); cookie = login2.headers.get('set-cookie').split(';')[0]; assert.equal((await call('/api/backup', {})).status, 403); assert.equal((await call('/api/printing', { store: '不允许' })).status, 403); assert.equal((await call('/api/backup/' + backup.name)).status, 403); await call('/api/logout', {}); assert.equal((await call('/api/orders/' + id)).status, 401); }); test('初始化及密码均哈希保存,非本机演示启动被阻止', async t => { assert.throws(() => createApp({ mode: 'demo', host: '0.0.0.0' }), /只能绑定本机/); const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-auth-test-')); const app = createApp({ mode: 'trial', port: 0, dataDir: dir }); await new Promise(r => app.server.listen(0, '127.0.0.1', r)); t.after(async () => { await new Promise(r => { app.server.closeAllConnections(); app.server.close(r); }); app.ledger.close(); fs.rmSync(dir, { recursive: true, force: true }); }); const base = 'http://127.0.0.1:' + app.server.address().port; const call = (p, b) => fetch(base + p, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'maimai-ledger', Origin: base }, body: JSON.stringify(b), }); const passwords = { ownerPassword: 'synthetic-owner-123', assistantPassword: 'synthetic-helper-123' }; assert.equal((await call('/api/setup', passwords)).status, 200); assert.equal((await call('/api/setup', passwords)).status, 403); assert.equal((await call('/api/login', { name: '麦麦', password: passwords.ownerPassword })).status, 200); assert.equal((await call('/api/login', { name: '麦麦', password: 'bad' })).status, 401); assert.ok(!JSON.stringify(app.ledger.rows('SELECT * FROM users')).includes(passwords.ownerPassword)); });