Files
ukmesh/scripts/lib/restore-owner-session.mjs

33 lines
1.7 KiB
JavaScript

// Executed only against the disposable restored owner database, using the
// candidate image and the same non-superuser connection as its owner portal.
import { randomBytes, randomUUID } from 'node:crypto';
import assert from 'node:assert/strict';
import pg from 'pg';
import {
closeOwnerAuthDb, createOwnerSession, getOwnerNodeIdsForUsername,
isOwnerSessionActive, revokeOwnerSession,
} from './dist/owner/ownerAuth.js';
const client = new pg.Client({ connectionString: process.env.OWNER_DATABASE_URL, options: '-c search_path=pg_catalog,core,public,pg_temp' });
const account = 'restore-fixture-' + randomUUID();
const node = randomBytes(32).toString('hex').toUpperCase();
await client.connect();
try {
const role = await client.query('SELECT current_user AS role');
assert.equal(role.rows[0].role, 'meshcore_owner');
await client.query('INSERT INTO core.owner_accounts(mqtt_username) VALUES ($1)', [account]);
await client.query(`INSERT INTO core.owner_account_nodes(mqtt_username,node_id,verified_at,verification_method)
VALUES ($1,$2,now(),'operator-database')`, [account, node]);
const { sessionId } = await createOwnerSession(account, 300000);
assert.equal(await isOwnerSessionActive(sessionId, account), true);
assert.deepEqual(await getOwnerNodeIdsForUsername(account), [node]);
await revokeOwnerSession(sessionId);
assert.equal(await isOwnerSessionActive(sessionId, account), false);
await client.query('DELETE FROM owner_sessions WHERE session_id=$1', [sessionId]);
await client.query('DELETE FROM core.owner_account_nodes WHERE mqtt_username=$1', [account]);
await client.query('DELETE FROM core.owner_accounts WHERE mqtt_username=$1', [account]);
} finally {
await client.end();
await closeOwnerAuthDb();
}