daf24a8f81
POST /api/admin/house-spins creates or updates Banner Spins on a house account (ADMIN_EMAIL or NETWORK_LINKS_OWNER) for house ads such as the Network Ad Space rows, through the same bannerspin.js calls the member Links screen uses (create, update, addBanner, updateBanner). Admin auth only (portal session or the admin key); no member session is read or minted. Every call, denied ones too, goes to the new admin action log (adminaudit.js: MySQL table admin_audit, JSONL without a database) with who, what and when; GET /api/admin/house-spins/log reads it. qa/housespins.test.mjs (in run.sh netsrc) covers auth, house-account only, member checks, idempotent repeat, prune, serving + click attribution, the log and no session minted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
8.2 KiB
JavaScript
79 lines
8.2 KiB
JavaScript
// House Banner Spins admin route (2026-11-16). Boots server.js on its own port with a throwaway data dir (JSON store, no DB) and
|
|
// checks POST /api/admin/house-spins: admin only (no auth -> 401, logged as denied), house account only, creates a Banner Spin with
|
|
// its banners, source and fallback through the member code path, a second identical call changes nothing, an update adds / prunes
|
|
// by image, the image and click serve on a link domain (click -> the banner's target, src + s1 kept in the click row), every call
|
|
// is in the admin action log with who / what / when, and no member session is ever minted for the account.
|
|
// node qa/housespins.test.mjs (from the site dir; PORT default 8977)
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { spawn } from 'node:child_process';
|
|
|
|
const PORT = Number(process.env.PORT || 8977); const BASE = 'http://127.0.0.1:' + PORT;
|
|
let pass = 0, fail = 0; const ok = (c, what, x) => { if (c) { pass++; console.log(' ok ' + what); } else { fail++; console.log(' FAIL ' + what + (x !== undefined ? ' -> ' + JSON.stringify(x).slice(0, 500) : '')); } };
|
|
const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'hs-qa-')); const ADMIN = 'admin@hs.test', MEM = 'mem@hs.test', KEY = 'k'.repeat(20), LINK = 'lnk-hs.test';
|
|
fs.writeFileSync(path.join(DATA, 'site.json'), JSON.stringify({ linkDomains: LINK }));
|
|
const env = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, ADMIN_PASSWORD: KEY, ADMIN_EMAIL: ADMIN });
|
|
for (const k of ['NODE_ENV', 'DATABASE_URL', 'DOCSTORE_URL', 'ENGINE_KEY', 'LS_SYNC_SECRET', 'OUTBOUND', 'LS_TESTERS', 'CURTAIN', 'HUB_ENABLED', 'HUB_URL', 'NETWORK_LINKS_OWNER', 'NETWORK_SSO_SECRET']) delete env[k];
|
|
const site = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..');
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
let srv = null;
|
|
async function boot() { srv = spawn(process.execPath, ['server.js'], { cwd: site, env, stdio: ['ignore', 'ignore', 'ignore'] }); for (let i = 0; i < 60; i++) { try { if ((await fetch(BASE + '/api/config')).ok) break; } catch (e) {} await sleep(500); } await sleep(3800); }
|
|
async function stop() { if (srv) srv.kill(); srv = null; await sleep(600); }
|
|
const read = f => { try { return JSON.parse(fs.readFileSync(path.join(DATA, f), 'utf8')); } catch (e) { return null; } };
|
|
const post = async (body, auth, actor) => { const r = await fetch(BASE + '/api/admin/house-spins', { method: 'POST', headers: Object.assign({ 'Content-Type': 'application/json' }, auth ? { Authorization: 'Bearer ' + auth } : {}, actor ? { 'X-Admin-Actor': actor } : {}), body: JSON.stringify(body) }); return { status: r.status, j: await r.json().catch(() => ({})) }; };
|
|
const go = async (p, ua) => { const r = await fetch(BASE + p, { redirect: 'manual', headers: { Host: LINK, 'User-Agent': ua || 'Mozilla/5.0 (Windows NT 10.0) Chrome/130 hs-qa', Referer: 'https://www.networkadspace.com/page' } }); return { status: r.status, loc: r.headers.get('location') || '', type: r.headers.get('content-type') || '' }; };
|
|
const sessionsFor = e => { const s = read('sessions.json'); const list = s ? (Array.isArray(s) ? s : Object.values(s.sessions || s)) : []; return list.filter(x => x && String(x.email || '').toLowerCase() === e).length; };
|
|
try {
|
|
await boot();
|
|
let src = null; for (let i = 0; i < 20 && !src; i++) { src = ((read('sources.json') || {}).items || []).find(x => x.owner === ADMIN && x.template === 'viraladblitz'); if (!src) await sleep(500); }
|
|
ok(!!src, 'the admin account has its network sources (one to tag with)');
|
|
const T = 'https://linkspin.app/r/nwvab?src=nas&s1=martbost';
|
|
const IMG = n => 'https://viraladblitz.com/art/kit/vab-' + n + '-panel.png';
|
|
const spin = (imgs, extra) => Object.assign({ name: 'Network Ad Space: ViralAdBlitz 336x280', size: '336x280', fallback: T, sourceId: src && src.id, banners: imgs.map(n => ({ label: n, image: IMG(n), target: T, weight: 10 })) }, extra || {});
|
|
const sess0 = sessionsFor(ADMIN);
|
|
|
|
let r = await post({ email: ADMIN, spins: [spin(['free'])] }, null, 'qa');
|
|
ok(r.status === 401, 'no admin auth: 401', r);
|
|
r = await post({ email: ADMIN, spins: [spin(['free'])] }, 'wrong-key');
|
|
ok(r.status === 401, 'a wrong admin key: 401', r);
|
|
r = await post({ email: MEM, spins: [spin(['free'])] }, KEY);
|
|
ok(r.status === 400 && /house account/.test(r.j.error || ''), 'a member account (not a house account): refused', r);
|
|
|
|
r = await post({ email: ADMIN, host: LINK, spins: [spin(['free', 'spread', 'boost'])] }, KEY, 'qa-run');
|
|
const s1 = r.j.spins && r.j.spins[0];
|
|
ok(r.status === 200 && r.j.ok && s1 && s1.created && s1.added === 3 && s1.sourceId === (src && src.id) && s1.fallback === T, 'creates the Banner Spin with 3 banners, the source and the fallback', r.j);
|
|
ok(s1 && s1.img === 'https://' + LINK + '/i/' + s1.code + '.gif' && s1.click === 'https://' + LINK + '/c/' + s1.code, 'answers the image and click URLs on the chosen link domain', s1);
|
|
const stored = (read('bannerspin.json') || { spins: [] }).spins.find(x => s1 && x.id === s1.id);
|
|
ok(stored && stored.owner === ADMIN && stored.banners.length === 3 && stored.banners.every(b => b.target === T && b.active), 'stored on the admin account like a member-made Banner Spin', stored);
|
|
|
|
r = await post({ email: ADMIN, host: LINK, spins: [spin(['free', 'spread', 'boost'])] }, KEY, 'qa-run');
|
|
ok(r.status === 200 && r.j.spins[0].id === s1.id && !r.j.spins[0].created && r.j.spins[0].added === 0 && r.j.spins[0].updated === 0, 'the same call again changes nothing (reuses it by name + size)', r.j.spins);
|
|
r = await post({ email: ADMIN, spins: [spin(['free', 'boost'], { id: s1.id, prune: true })] }, KEY, 'qa-run');
|
|
ok(r.status === 200 && r.j.spins[0].removed === 1 && r.j.spins[0].banners === 2, 'by id with prune: a banner no longer listed is removed', r.j.spins);
|
|
r = await post({ email: ADMIN, spins: [{ id: 99999, banners: [] }] }, KEY);
|
|
ok(r.status === 200 && r.j.ok === false && /No Banner Spin/.test(r.j.spins[0].errors[0] || ''), 'an unknown id reports an error and changes nothing', r.j);
|
|
r = await post({ email: ADMIN, spins: [spin(['free'], { name: 'Bad size', size: '999x999' })] }, KEY);
|
|
ok(r.status === 200 && r.j.ok === false && /sizes/.test(r.j.spins[0].errors[0] || ''), 'the member checks apply (a bad size is refused)', r.j);
|
|
|
|
let g = await go('/i/' + s1.code + '.gif');
|
|
ok(g.status === 302 && /^https:\/\/viraladblitz\.com\/art\/kit\/vab-(free|boost)-panel\.png$/.test(g.loc), 'the image URL serves one of the kit banners', g);
|
|
g = await go('/c/' + s1.code + '?src=nas&s1=martbost');
|
|
ok(g.status === 302 && g.loc.startsWith(T), 'the click goes to the tracked network link', g);
|
|
await sleep(300);
|
|
const click = (read('bannerspin.json') || { clicks: [] }).clicks.slice(-1)[0];
|
|
ok(click && click.s === s1.id && click.sr === src.id && click.q && click.q.src === 'nas' && click.q.s1 === 'martbost' && click.m !== 'none', 'the click row: this Banner Spin, the source, src + s1 kept, matched to the view', click);
|
|
|
|
const log = await (await fetch(BASE + '/api/admin/house-spins/log?limit=50', { headers: { Authorization: 'Bearer ' + KEY } })).json();
|
|
const E = log.entries || [];
|
|
ok(E.length === 8, 'every call is in the admin action log (8)', E.length);
|
|
ok(E.filter(e => !e.ok && /^denied/.test(e.who)).length === 2 && E.some(e => e.who === 'denied (qa)'), 'denied calls logged as denied, with the actor label', E.map(e => e.who));
|
|
ok(E.filter(e => e.who === 'admin key (qa-run)').length === 3 && E.every(e => e.action === 'house-spins' && e.ts > Date.now() - 600000), 'who (admin key + actor), what and when on each row', E.map(e => [e.who, e.action]));
|
|
ok(E.some(e => e.target === ADMIN && e.ok && e.detail && e.detail.spins && e.detail.spins[0].code === s1.code), 'the detail names the account and the Banner Spin', E[0]);
|
|
ok(!JSON.stringify(E).includes(KEY), 'the admin key is never in the log');
|
|
ok((await fetch(BASE + '/api/admin/house-spins/log')).status === 401, 'reading the log needs admin auth');
|
|
ok(sessionsFor(ADMIN) === sess0, 'no member session was minted for the account', [sess0, sessionsFor(ADMIN)]);
|
|
} catch (e) { fail++; console.error(e); }
|
|
finally { await stop(); try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} }
|
|
console.log('\n' + pass + ' passed, ' + fail + ' failed'); process.exit(fail ? 1 : 0);
|