diff --git a/src/routes.js b/src/routes.js new file mode 100644 index 0000000..ba4f359 --- /dev/null +++ b/src/routes.js @@ -0,0 +1,252 @@ +import { now, getSetting, setSetting } from './db.js'; +import { encrypt, encryptJson, decryptJson, verifyUnsubToken } from './crypto.js'; +import { html, json, redirect, readBody, HttpError } from './http.js'; +import { startGoogleLogin, finishGoogleLogin, destroySession } from './auth.js'; +import { loginPage, dashboardPage, settingsPage, unsubPage } from './views.js'; +import { enqueue, latestJobs, cancelJob, usageSummary } from './services/jobs.js'; +import { validateGitUrl, removeRepo } from './services/git.js'; +import { fetchRecap, clearRecapCache, validateSelect } from './services/recap.js'; +import { allowedToSend, EMAIL_RE } from './services/mailer.js'; +import { cpuTemp } from './services/thermal.js'; +import { + getProject, listProjects, createProject, saveSecretConfig, publicConfig, globalSmtp, + prospectStats, sentSince, repoDir, +} from './projects.js'; + +const lines = (s) => String(s || '').split('\n').map((x) => x.trim()).filter(Boolean).slice(0, 20); +const int = (v, min, max, def) => { const n = Math.floor(Number(v)); return Number.isFinite(n) ? Math.max(min, Math.min(max, n)) : def; }; +const pid = (params) => Number(params.id); + +export function registerRoutes(router, app) { + const { db, cfg } = app; + + // ---------- Public ---------- + router.get('/login', ({ res, url }) => html(res, loginPage(url.searchParams.get('e'))), { public: true }); + router.get('/auth/google', ({ res }) => startGoogleLogin(db, cfg, res), { public: true }); + router.get('/auth/google/callback', async ({ res, url }) => { + try { await finishGoogleLogin(db, cfg, url, res); } + catch (e) { redirect(res, `/login?e=${encodeURIComponent(e.message)}`); } + }, { public: true }); + + // Désinscription (RGPD). GET = page de confirmation (évite les désinscriptions par les scanners de liens), + // POST = désinscription effective, compatible "one-click" (RFC 8058). + router.get('/u/:email/:token', ({ res, params }) => { + const valid = verifyUnsubToken(cfg.secretKey, params.email, params.token); + html(res, unsubPage({ email: params.email, valid, done: false }), valid ? 200 : 400); + }, { public: true }); + router.post('/u/:email/:token', ({ res, params }) => { + const email = params.email.toLowerCase(); + const valid = verifyUnsubToken(cfg.secretKey, email, params.token); + if (valid) { + db.prepare("INSERT OR IGNORE INTO blacklist(email, reason, created_at) VALUES(?, 'unsubscribe', ?)").run(email, now()); + db.prepare("UPDATE prospects SET status = 'refused' WHERE email = ? AND status IN ('new','sent')").run(email); + } + html(res, unsubPage({ email, valid, done: valid }), valid ? 200 : 400); + }, { public: true }); + + // ---------- Privé ---------- + router.post('/logout', ({ req, res }) => { destroySession(db, req, res); redirect(res, '/login'); }); + + router.get('/', ({ res, session }) => { + const projects = listProjects(app).map((p) => { + const full = getProject(app, p.id, { secrets: true }); + return { project: p, smtp: publicConfig(full.smtp, 'pass'), dbCfg: publicConfig(full.dbCfg, 'password') }; + }); + html(res, dashboardPage({ email: session.email, projects, forgejoHost: getSetting(db, 'forgejo_host') })); + }); + + router.get('/settings', ({ res, session, url }) => { + const s = { + notify_email: getSetting(db, 'notify_email', ''), + forgejo_host: getSetting(db, 'forgejo_host', ''), + forgejo_token_set: !!getSetting(db, 'forgejo_token_enc'), + smtp: publicConfig(globalSmtp(app), 'pass'), + global_daily_cap: getSetting(db, 'global_daily_cap', '100'), + send_delay_min: getSetting(db, 'send_delay_min', '20'), + send_delay_max: getSetting(db, 'send_delay_max', '60'), + prospect_model: getSetting(db, 'prospect_model', 'haiku'), + blacklist_count: db.prepare('SELECT COUNT(*) n FROM blacklist').get().n, + }; + html(res, settingsPage({ email: session.email, s, saved: url.searchParams.has('ok') })); + }); + + router.post('/settings', async ({ req, res }) => { + const b = await readBody(req); + if (b.notify_email && !EMAIL_RE.test(b.notify_email)) throw new HttpError(400, 'Email de réception invalide'); + setSetting(db, 'notify_email', (b.notify_email || '').trim()); + setSetting(db, 'forgejo_host', (b.forgejo_host || '').trim().replace(/^https?:\/\//, '').replace(/\/.*$/, '')); + if (b.forgejo_token) setSetting(db, 'forgejo_token_enc', encrypt(cfg.secretKey, b.forgejo_token.trim())); + const cap = int(b.global_daily_cap, 1, 300, 100); + const dmin = int(b.send_delay_min, 5, 3600, 20); + const dmax = Math.max(dmin, int(b.send_delay_max, 5, 3600, 60)); + setSetting(db, 'global_daily_cap', String(cap)); + setSetting(db, 'send_delay_min', String(dmin)); + setSetting(db, 'send_delay_max', String(dmax)); + setSetting(db, 'prospect_model', b.prospect_model === 'sonnet' ? 'sonnet' : 'haiku'); + + const prev = globalSmtp(app) || {}; + const smtp = { host: b.smtp_host, port: b.smtp_port, user: b.smtp_user, pass: b.smtp_pass || prev.pass, fromEmail: b.smtp_fromEmail }; + setSetting(db, 'smtp_enc', smtp.host ? encryptJson(cfg.secretKey, smtp) : null); + redirect(res, '/settings?ok'); + }); + + // ---------- API JSON ---------- + router.get('/api/system', ({ res }) => { + const queue = db.prepare("SELECT COUNT(*) n FROM jobs WHERE status IN ('queued','running')").get().n; + json(res, { temp: cpuTemp(), queue, usage: usageSummary(db), thermalMax: cfg.thermalMax }); + }); + + router.post('/api/projects', async ({ req, res }) => { + const b = await readBody(req); + const name = String(b.name || '').trim().slice(0, 80); + if (!name) throw new HttpError(400, 'Nom requis'); + const host = getSetting(db, 'forgejo_host'); + if (!host) throw new HttpError(400, "Configure l'hôte Forgejo dans Réglages"); + let gitUrl; + try { gitUrl = validateGitUrl(String(b.gitUrl || '').trim(), host); } + catch (e) { throw new HttpError(400, e.message); } + if (db.prepare('SELECT 1 FROM projects WHERE git_url = ?').get(gitUrl)) throw new HttpError(409, 'Ce dépôt est déjà un projet'); + const id = createProject(app, { name, gitUrl }); + enqueue(db, { projectId: id, type: 'analyze' }); + json(res, { id }); + }); + + router.post('/api/projects/:id/delete', ({ res, params }) => { + const p = getProject(app, pid(params)); + db.prepare('DELETE FROM projects WHERE id = ?').run(p.id); + removeRepo(repoDir(cfg, p.id)); + clearRecapCache(p.id); + json(res, { ok: true }); + }); + + router.get('/api/projects/:id/status', ({ res, params }) => { + const p = getProject(app, pid(params)); + const jobs = latestJobs(db, p.id); + const tpl = db.prepare('SELECT subject, body, status, updated_at FROM mail_templates WHERE project_id = ?').get(p.id) || null; + const stats = prospectStats(db, p.id); + const day = now() - 86400; + const sendMax = allowedToSend({ + requested: 100, available: stats.new || 0, + projectCap: p.dailyMailCap, sentTodayProject: sentSince(db, day, p.id), + globalCap: Number(getSetting(db, 'global_daily_cap', '100')), sentTodayGlobal: sentSince(db, day), + }); + json(res, { + jobs, template: tpl, stats, sendMax, + targetValidated: p.targetValidated, hasProfile: !!p.profile, hasSmtp: p.hasSmtp, + }); + }); + + router.post('/api/projects/:id/target', async ({ req, res, params }) => { + const p = getProject(app, pid(params)); + const b = await readBody(req); + const target = { + description: String(b.description || '').slice(0, 300), + zone: String(b.zone || '').slice(0, 100), + taille: String(b.taille || '').slice(0, 100), + decideur: String(b.decideur || '').slice(0, 100), + secteurs: lines(b.secteurs), + requetes: lines(b.requetes), + }; + if (!target.description || !target.secteurs.length) throw new HttpError(400, 'Description et au moins un secteur requis'); + db.prepare('UPDATE projects SET target_json = ?, target_validated = 1 WHERE id = ?').run(JSON.stringify(target), p.id); + json(res, { ok: true }); + }); + + const jobRoute = (type, paramsOf = () => ({}), guard = () => {}) => async ({ req, res, params }) => { + const p = getProject(app, pid(params)); + const b = req.headers['content-type']?.includes('json') ? await readBody(req) : {}; + guard(p, b); + const r = enqueue(db, { projectId: p.id, type, params: paramsOf(b) }); + json(res, r); + }; + + router.post('/api/projects/:id/analyze', jobRoute('analyze')); + router.post('/api/projects/:id/template', jobRoute('template', () => ({}), (p) => { + if (!p.profile) throw new HttpError(400, "Analyse le projet d'abord"); + })); + router.post('/api/projects/:id/prospect', jobRoute('prospect', (b) => ({ count: int(b.count, 1, 50, 10) }), (p) => { + if (!p.targetValidated) throw new HttpError(400, 'Valide la cible avant de prospecter'); + })); + router.post('/api/projects/:id/evolution', jobRoute('evolution', (b) => ({ + count: int(b.count, 1, 20, 5), + level: ['rapide', 'normal', 'approfondi'].includes(b.level) ? b.level : 'normal', + }))); + router.post('/api/projects/:id/send', jobRoute('send', (b) => ({ count: int(b.count, 1, 100, 10) }), (p, b) => { + if (!b.confirm) throw new HttpError(400, 'Confirmation requise'); + const running = db.prepare("SELECT 1 FROM jobs WHERE project_id = ? AND type = 'prospect' AND status IN ('queued','running')").get(p.id); + if (running) throw new HttpError(409, 'La recherche de prospects est encore en cours'); + })); + + router.post('/api/projects/:id/template/save', async ({ req, res, params }) => { + const p = getProject(app, pid(params)); + const b = await readBody(req); + const subject = String(b.subject || '').trim().slice(0, 200); + const body = String(b.body || '').trim().slice(0, 5000); + if (!subject || !body) throw new HttpError(400, 'Objet et corps requis'); + const status = b.mode === 'validate' ? 'validated' : 'draft'; + db.prepare(`INSERT INTO mail_templates(project_id, subject, body, status, updated_at) VALUES(?, ?, ?, ?, ?) + ON CONFLICT(project_id) DO UPDATE SET subject = excluded.subject, body = excluded.body, status = excluded.status, updated_at = excluded.updated_at`) + .run(p.id, subject, body, status, now()); + json(res, { ok: true, status }); + }); + + router.post('/api/projects/:id/smtp', async ({ req, res, params }) => { + const p = getProject(app, pid(params)); + const b = await readBody(req); + if (b.fromEmail && !EMAIL_RE.test(b.fromEmail)) throw new HttpError(400, 'Email expéditeur invalide'); + saveSecretConfig(app, p.id, 'smtp_enc', { + host: String(b.host || '').trim(), port: int(b.port, 1, 65535, 587), user: String(b.user || '').trim(), + pass: b.pass || '', fromEmail: String(b.fromEmail || '').trim(), fromName: String(b.fromName || '').trim().slice(0, 80), + }, 'pass'); + db.prepare('UPDATE projects SET daily_mail_cap = ? WHERE id = ?').run(int(b.dailyCap, 1, 200, 30), p.id); + json(res, { ok: true }); + }); + + router.post('/api/projects/:id/db', async ({ req, res, params }) => { + const p = getProject(app, pid(params)); + const b = await readBody(req); + for (const k of ['sql_users', 'sql_clients', 'sql_revenue']) { + try { validateSelect(b[k]); } catch (e) { throw new HttpError(400, `${k} : ${e.message}`); } + } + saveSecretConfig(app, p.id, 'db_enc', { + type: b.type === 'pg' ? 'pg' : 'mysql', host: String(b.host || '').trim(), port: b.port ? int(b.port, 1, 65535, null) : null, + database: String(b.database || '').trim(), user: String(b.user || '').trim(), password: b.password || '', + ssl: b.ssl === 'on' || b.ssl === true, + sql_users: b.sql_users || '', sql_clients: b.sql_clients || '', sql_revenue: b.sql_revenue || '', + }, 'password'); + clearRecapCache(p.id); + json(res, { ok: true }); + }); + + router.get('/api/projects/:id/recap', async ({ res, params, url }) => { + const p = getProject(app, pid(params), { secrets: true }); + const stats = prospectStats(db, p.id); + const cost = db.prepare('SELECT COALESCE(SUM(cost_usd),0) c FROM jobs WHERE project_id = ?').get(p.id).c; + let business = null, error = null; + try { business = await fetchRecap(p.id, p.dbCfg, { force: url.searchParams.has('force') }); } + catch (e) { error = e.message; } + json(res, { business, error, stats, aiCostUsd: cost }); + }); + + router.get('/api/projects/:id/prospects', ({ res, params }) => { + const p = getProject(app, pid(params)); + const rows = db.prepare(`SELECT id, company, email, website, city, sector, score, status, sent_at FROM prospects + WHERE project_id = ? ORDER BY CASE status WHEN 'new' THEN 0 ELSE 1 END, score DESC, id DESC LIMIT 500`).all(p.id); + json(res, rows); + }); + + const STATUSES = ['new', 'sent', 'replied', 'client', 'refused', 'bounced']; + router.post('/api/prospects/:id/status', async ({ req, res, params }) => { + const b = await readBody(req); + if (!STATUSES.includes(b.status)) throw new HttpError(400, 'Statut invalide'); + const r = db.prepare('UPDATE prospects SET status = ? WHERE id = ?').run(b.status, Number(params.id)); + if (!r.changes) throw new HttpError(404, 'Prospect introuvable'); + if (b.status === 'refused') { + const { email } = db.prepare('SELECT email FROM prospects WHERE id = ?').get(Number(params.id)); + db.prepare("INSERT OR IGNORE INTO blacklist(email, reason, created_at) VALUES(?, 'refused', ?)").run(email, now()); + } + json(res, { ok: true }); + }); + + router.post('/api/jobs/:id/cancel', ({ res, params }) => { cancelJob(db, Number(params.id)); json(res, { ok: true }); }); +} diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..77ef6a8 --- /dev/null +++ b/src/server.js @@ -0,0 +1,90 @@ +import { createServer } from 'node:http'; +import { readFileSync } from 'node:fs'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { loadConfig } from './config.js'; +import { openDb } from './db.js'; +import { Router, json, redirect, html, HttpError } from './http.js'; +import { getSession, checkOrigin, purgeExpired } from './auth.js'; +import { registerRoutes } from './routes.js'; +import { Worker } from './services/jobs.js'; +import { makeHandlers } from './handlers.js'; +import { layout } from './views.js'; + +const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..'); + +// Fichiers statiques chargés une fois en mémoire (2 fichiers, quelques Ko) +const STATIC = { + '/static/app.css': ['text/css; charset=utf-8', readFileSync(join(ROOT, 'public/app.css'))], + '/static/app.js': ['text/javascript; charset=utf-8', readFileSync(join(ROOT, 'public/app.js'))], +}; + +const SECURITY_HEADERS = { + 'X-Content-Type-Options': 'nosniff', + 'X-Frame-Options': 'DENY', + 'Referrer-Policy': 'same-origin', + 'Content-Security-Policy': "default-src 'self'; style-src 'self'; script-src 'self'; img-src 'self' data:; form-action 'self' https://accounts.google.com; frame-ancestors 'none'", + 'Strict-Transport-Security': 'max-age=31536000', +}; + +export function createApp(cfg, { db = openDb(cfg.dataDir), worker = true, createTransport } = {}) { + const app = { db, cfg, createTransport }; + const router = new Router(); + registerRoutes(router, app); + + const server = createServer(async (req, res) => { + for (const [k, v] of Object.entries(SECURITY_HEADERS)) res.setHeader(k, v); + const url = new URL(req.url, cfg.appUrl); + try { + const st = STATIC[url.pathname]; + if (st && req.method === 'GET') { + res.writeHead(200, { 'Content-Type': st[0], 'Cache-Control': 'public, max-age=3600' }); + return res.end(st[1]); + } + const route = router.match(req.method, url.pathname); + if (!route) throw new HttpError(404, 'Page introuvable'); + const session = route.public ? null : getSession(db, req); + if (!route.public) { + if (!session) { + if (url.pathname.startsWith('/api/')) throw new HttpError(401, 'Non connecté'); + return redirect(res, '/login'); + } + checkOrigin(req, cfg); + } + await route.handler({ req, res, url, params: route.params, session, app }); + } catch (e) { + const status = e.status || 500; + if (status >= 500) console.error(`[${req.method} ${url.pathname}]`, e); + if (res.headersSent) return res.end(); + const msg = status >= 500 ? 'Erreur interne' : e.message; + if (url.pathname.startsWith('/api/') || req.headers.accept?.includes('json')) json(res, { error: msg }, status); + else html(res, layout({ title: 'Erreur', body: `

${status}

${msg.replace(/Retour

` }), status); + } + }); + // Pi : on limite ce qui traîne en mémoire + server.keepAliveTimeout = 5000; + server.headersTimeout = 10_000; + server.requestTimeout = 30_000; + + let w = null; + if (worker) { + w = new Worker(db, makeHandlers(app), { thermalMax: cfg.thermalMax }); + w.start(); + } + const purge = setInterval(() => purgeExpired(db), 3600_000); + purge.unref(); + + app.close = () => new Promise((r) => { w?.stop(); clearInterval(purge); server.close(() => { db.close(); r(); }); }); + app.server = server; + return app; +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) { + const cfg = loadConfig(); + const app = createApp(cfg); + app.server.listen(cfg.port, cfg.host, () => console.log(`AiTools sur http://${cfg.host}:${cfg.port} (${cfg.appUrl})`)); + const shutdown = () => app.close().then(() => process.exit(0)); + process.on('SIGTERM', shutdown); + process.on('SIGINT', shutdown); +} + diff --git a/test/http.test.js b/test/http.test.js new file mode 100644 index 0000000..d6c3cef --- /dev/null +++ b/test/http.test.js @@ -0,0 +1,146 @@ +import { test, before, after } from 'node:test'; +import assert from 'node:assert/strict'; +import { randomBytes } from 'node:crypto'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { openDb, now } from '../src/db.js'; +import { createApp } from '../src/server.js'; +import { unsubToken } from '../src/crypto.js'; + +const APP_URL = 'https://ai.tony-averty.dev'; +let app, base, cookie, tmp; + +before(async () => { + tmp = mkdtempSync(join(tmpdir(), 'aitools-http-')); + const cfg = { + appUrl: APP_URL, google: { clientId: 'cid', clientSecret: 'cs' }, allowedEmails: ['me@gmail.com'], + secretKey: randomBytes(32), dataDir: tmp, claudeBin: 'claude', thermalMax: 75, + }; + app = createApp(cfg, { db: openDb(':memory:'), worker: false }); + await new Promise((r) => app.server.listen(0, '127.0.0.1', r)); + base = `http://127.0.0.1:${app.server.address().port}`; + app.db.prepare('INSERT INTO sessions(id, email, created_at, expires_at) VALUES(?, ?, ?, ?)').run('sess1', 'me@gmail.com', now(), now() + 3600); + cookie = 'sid=sess1'; +}); +after(async () => { await app.close(); rmSync(tmp, { recursive: true, force: true }); }); + +const get = (p, auth = true) => fetch(base + p, { redirect: 'manual', headers: auth ? { cookie, accept: 'application/json' } : {} }); +const post = (p, body, { auth = true, origin = APP_URL, form = false } = {}) => fetch(base + p, { + method: 'POST', redirect: 'manual', + headers: { + ...(auth ? { cookie } : {}), ...(origin ? { origin } : {}), + 'content-type': form ? 'application/x-www-form-urlencoded' : 'application/json', accept: 'application/json', + }, + body: form ? new URLSearchParams(body).toString() : JSON.stringify(body ?? {}), +}); + +test('non connecté : redirection / 401 et pages publiques', async () => { + let r = await get('/', false); + assert.equal(r.status, 303); + assert.equal(r.headers.get('location'), '/login'); + r = await get('/api/system', false); + assert.equal(r.status, 401); + r = await get('/login', false); + assert.equal(r.status, 200); + assert.match(await r.text(), /Se connecter avec Google/); + assert.equal(r.headers.get('x-frame-options'), 'DENY'); + r = await get('/auth/google', false); + assert.equal(r.status, 303); + const loc = new URL(r.headers.get('location')); + assert.equal(loc.host, 'accounts.google.com'); + assert.equal(loc.searchParams.get('code_challenge_method'), 'S256'); + assert.equal(loc.searchParams.get('redirect_uri'), `${APP_URL}/auth/google/callback`); + r = await get('/static/app.js', false); + assert.equal(r.status, 200); +}); + +test('callback OAuth avec state inconnu → retour login avec erreur', async () => { + const r = await get('/auth/google/callback?code=x&state=forge', false); + assert.equal(r.status, 303); + assert.match(decodeURIComponent(r.headers.get('location')), /State OAuth invalide/); +}); + +test('anti-CSRF : POST sans bonne origine refusé', async () => { + let r = await post('/api/projects', { name: 'x' }, { origin: 'https://evil.com' }); + assert.equal(r.status, 403); + r = await post('/api/projects', { name: 'x' }, { origin: null }); + assert.equal(r.status, 403); +}); + +test('dashboard, réglages, création de projet et statut', async () => { + let r = await get('/'); + assert.equal(r.status, 200); + assert.match(await r.text(), /Aucun projet/); + + r = await post('/api/projects', { name: 'MEP', gitUrl: 'https://git.tony-averty.dev/Tony/MEP.git' }); + assert.equal(r.status, 400); + assert.match((await r.json()).error, /Forgejo/); + + r = await post('/settings', { notify_email: 'moi@x.fr', forgejo_host: 'https://git.tony-averty.dev/', forgejo_token: 'tok123', global_daily_cap: '9999', send_delay_min: '1', send_delay_max: '2', prospect_model: 'opus' }, { form: true }); + assert.equal(r.status, 303); + const s = (k) => app.db.prepare('SELECT value FROM settings WHERE key = ?').get(k)?.value; + assert.equal(s('forgejo_host'), 'git.tony-averty.dev'); + assert.equal(s('global_daily_cap'), '300', 'plafond borné'); + assert.equal(s('send_delay_min'), '5'); + assert.equal(s('prospect_model'), 'haiku', 'opus refusé pour la prospection'); + assert.ok(!s('forgejo_token_enc').includes('tok123'), 'token chiffré'); + + r = await post('/api/projects', { name: 'MEP', gitUrl: 'https://github.com/Tony/MEP.git' }); + assert.equal(r.status, 400); + assert.match((await r.json()).error, /doit être sur git\.tony-averty\.dev/); + r = await post('/api/projects', { name: 'MEP', gitUrl: 'https://git.tony-averty.dev/Tony/MEP.git' }); + assert.equal(r.status, 200); + const { id } = await r.json(); + r = await post('/api/projects', { name: 'MEP2', gitUrl: 'https://git.tony-averty.dev/Tony/MEP.git' }); + assert.equal(r.status, 409); + + r = await get(`/api/projects/${id}/status`); + const st = await r.json(); + assert.equal(st.jobs.analyze.status, 'queued'); + assert.equal(st.sendMax, 0); + + r = await post(`/api/projects/${id}/prospect`, { count: 5 }); + assert.equal(r.status, 400, 'cible non validée'); + r = await post(`/api/projects/${id}/target`, { description: 'commerces', secteurs: 'boulangerie\nfleuriste', zone: 'Rennes' }); + assert.equal(r.status, 200); + r = await post(`/api/projects/${id}/prospect`, { count: 999 }); + assert.equal(r.status, 200); + const job = app.db.prepare("SELECT params_json FROM jobs WHERE type = 'prospect'").get(); + assert.equal(JSON.parse(job.params_json).count, 50, 'borné à 50'); + + r = await post(`/api/projects/${id}/send`, { count: 5 }); + assert.equal(r.status, 400, 'confirmation obligatoire'); + r = await post(`/api/projects/${id}/send`, { count: 5, confirm: true }); + assert.equal(r.status, 409, 'recherche encore en cours'); + + r = await post(`/api/projects/${id}/db`, { type: 'mysql', sql_users: 'DELETE FROM users' }); + assert.equal(r.status, 400); + r = await post(`/api/projects/${id}/smtp`, { host: 'smtp.x', user: 'u', pass: 'secretpass', fromEmail: 'a@x.fr', dailyCap: 5000 }); + assert.equal(r.status, 200); + const row = app.db.prepare('SELECT smtp_enc, daily_mail_cap FROM projects WHERE id = ?').get(id); + assert.ok(!row.smtp_enc.includes('secretpass')); + assert.equal(row.daily_mail_cap, 200); + + r = await get('/'); + const page = await r.text(); + assert.ok(!page.includes('secretpass'), 'le mot de passe ne ressort jamais dans la page'); + assert.match(page, /readonly disabled/); + + r = await get(`/api/projects/${id}/recap`); + assert.match((await r.json()).error, /non configuré/); +}); + +test('désinscription : GET confirme, POST blackliste, token forgé refusé', async () => { + const email = 'contact@fournil.fr'; + const tok = unsubToken(app.cfg.secretKey, email); + let r = await get(`/u/${encodeURIComponent(email)}/${tok}`, false); + assert.equal(r.status, 200); + assert.match(await r.text(), /Confirmer/); + assert.equal(app.db.prepare('SELECT COUNT(*) n FROM blacklist').get().n, 0); + r = await post(`/u/${encodeURIComponent(email)}/${tok}`, 'List-Unsubscribe=One-Click', { auth: false, origin: null, form: true }); + assert.equal(r.status, 200); + assert.equal(app.db.prepare('SELECT COUNT(*) n FROM blacklist WHERE email = ?').get(email).n, 1); + r = await post(`/u/${encodeURIComponent(email)}/faux`, {}, { auth: false, origin: null }); + assert.equal(r.status, 400); +});