crosscode: Events 3.0 builds auth-client; tests on real Postgres
- scripts/products/events.sh: packages/auth-client is installed and built (after contracts) when Events uses it, as 3.0.0 does; without it a 3.0 deploy would fail at the first sign-in through Accounts. bats test. - test:accounts runs on real Postgres for both apps with EVENTS_TEST_DATABASE_URL and ACCOUNTS_TEST_DATABASE_URL (14/14); the Accounts test harness takes a real Prisma client. - e2e/excel.spec.ts resolves excelts from the backend (hoisted in the monorepo), so the e2e suite loads again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -83,13 +83,14 @@ export type Received = { path: string; headers: http.IncomingHttpHeaders; body:
|
|||||||
|
|
||||||
export type Harness = Awaited<ReturnType<typeof startHarness>>;
|
export type Harness = Awaited<ReturnType<typeof startHarness>>;
|
||||||
|
|
||||||
export async function startHarness(opts: { now?: () => Date } = {}) {
|
/** `db`: a real Prisma client instead of the in-memory one (an empty, migrated database). */
|
||||||
|
export async function startHarness(opts: { now?: () => Date; db?: unknown } = {}) {
|
||||||
// Accounts.
|
// Accounts.
|
||||||
const server = http.createServer();
|
const server = http.createServer();
|
||||||
const port = await listen(server);
|
const port = await listen(server);
|
||||||
const publicUrl = `http://127.0.0.1:${port}`;
|
const publicUrl = `http://127.0.0.1:${port}`;
|
||||||
const config = { ...loadConfig({ ACCOUNTS_URL: publicUrl, NODE_ENV: 'test', JOBS_ENABLED: 'false', LINKS_ALLOW_PRIVATE: 'true', HUB_URL: 'http://127.0.0.1:1' }) };
|
const config = { ...loadConfig({ ACCOUNTS_URL: publicUrl, NODE_ENV: 'test', JOBS_ENABLED: 'false', LINKS_ALLOW_PRIVATE: 'true', HUB_URL: 'http://127.0.0.1:1' }) };
|
||||||
const db = memoryDb();
|
const db = (opts.db ?? memoryDb()) as MemoryDb;
|
||||||
const messenger = new MemoryMessenger();
|
const messenger = new MemoryMessenger();
|
||||||
const ctx: Ctx = createCtx(config, db as never, { messenger, now: opts.now });
|
const ctx: Ctx = createCtx(config, db as never, { messenger, now: opts.now });
|
||||||
const provider = await createProvider(ctx);
|
const provider = await createProvider(ctx);
|
||||||
|
|||||||
@@ -23,7 +23,10 @@ const require = createRequire(import.meta.url);
|
|||||||
process.env.NODE_ENV = 'test';
|
process.env.NODE_ENV = 'test';
|
||||||
process.env.JWT_SECRET = 'an-events-test-secret-long-enough-for-the-encryption-key';
|
process.env.JWT_SECRET = 'an-events-test-secret-long-enough-for-the-encryption-key';
|
||||||
|
|
||||||
// Events' database: the in-memory stand-in, with Prisma's `{ increment }` for tokenVersion.
|
// With EVENTS_TEST_DATABASE_URL and ACCOUNTS_TEST_DATABASE_URL (empty, migrated databases), both
|
||||||
|
// apps use real Postgres; otherwise in-memory stand-ins (Events' with Prisma's `{ increment }`).
|
||||||
|
const PG = !!(process.env.EVENTS_TEST_DATABASE_URL && process.env.ACCOUNTS_TEST_DATABASE_URL);
|
||||||
|
if (PG) process.env.DATABASE_URL = process.env.EVENTS_TEST_DATABASE_URL;
|
||||||
const raw = memoryDb();
|
const raw = memoryDb();
|
||||||
const eventsDb = new Proxy(raw, {
|
const eventsDb = new Proxy(raw, {
|
||||||
get(target, prop: string) {
|
get(target, prop: string) {
|
||||||
@@ -45,7 +48,9 @@ const eventsDb = new Proxy(raw, {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
const dbPath = require.resolve('../../src/config/db.js');
|
const dbPath = require.resolve('../../src/config/db.js');
|
||||||
require.cache[dbPath] = { id: dbPath, filename: dbPath, loaded: true, exports: eventsDb } as never;
|
if (!PG) require.cache[dbPath] = { id: dbPath, filename: dbPath, loaded: true, exports: eventsDb } as never;
|
||||||
|
const realEventsDb = PG ? require(dbPath) : null;
|
||||||
|
const edb = (realEventsDb ?? eventsDb) as typeof eventsDb;
|
||||||
|
|
||||||
const { encrypt } = require('../../src/utils/encryption.js');
|
const { encrypt } = require('../../src/utils/encryption.js');
|
||||||
const { accountsRouter } = require('../../src/accounts/signin.ts');
|
const { accountsRouter } = require('../../src/accounts/signin.ts');
|
||||||
@@ -63,7 +68,7 @@ const totpSecret = generateSecret();
|
|||||||
const ids = { thandi: crypto.randomUUID(), sam: crypto.randomUUID(), lerato: crypto.randomUUID(), guest: crypto.randomUUID(), sipho: crypto.randomUUID() };
|
const ids = { thandi: crypto.randomUUID(), sam: crypto.randomUUID(), lerato: crypto.randomUUID(), guest: crypto.randomUUID(), sipho: crypto.randomUUID() };
|
||||||
|
|
||||||
async function eventsUser(data: Record<string, unknown>) {
|
async function eventsUser(data: Record<string, unknown>) {
|
||||||
return eventsDb.user.create({
|
return edb.user.create({
|
||||||
data: {
|
data: {
|
||||||
accountId: null, role: 'user', isActive: true, tokenVersion: 0, phoneNumber: null, failedAttempts: 0, lockUntil: null, twoFactorEnabled: false, twoFactorMethod: null,
|
accountId: null, role: 'user', isActive: true, tokenVersion: 0, phoneNumber: null, failedAttempts: 0, lockUntil: null, twoFactorEnabled: false, twoFactorMethod: null,
|
||||||
totpSecret: null, totpLastStep: null, newsEmail: false, newsWhatsApp: false, newsAskedAt: new Date(), noticeVersionAccepted: 0, lastLoginAt: new Date('2026-10-01'),
|
totpSecret: null, totpLastStep: null, newsEmail: false, newsWhatsApp: false, newsAskedAt: new Date(), noticeVersionAccepted: 0, lastLoginAt: new Date('2026-10-01'),
|
||||||
@@ -73,7 +78,13 @@ async function eventsUser(data: Record<string, unknown>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
h = await startHarness();
|
if (PG) {
|
||||||
|
const { PrismaClient } = await import('../../../../accounts/backend/src/generated/prisma/client.ts');
|
||||||
|
const { PrismaPg } = await import('@prisma/adapter-pg');
|
||||||
|
h = await startHarness({ db: new PrismaClient({ adapter: new PrismaPg({ connectionString: process.env.ACCOUNTS_TEST_DATABASE_URL! }) }) });
|
||||||
|
} else {
|
||||||
|
h = await startHarness();
|
||||||
|
}
|
||||||
setAuthClientLoader(() => import('@crosscode/auth-client'));
|
setAuthClientLoader(() => import('@crosscode/auth-client'));
|
||||||
|
|
||||||
// Events on a real port: the sign-in routes, the guard, and one page that needs signing in.
|
// Events on a real port: the sign-in routes, the guard, and one page that needs signing in.
|
||||||
@@ -91,8 +102,8 @@ before(async () => {
|
|||||||
// Connect (as Linked products would): the same secret on both sides.
|
// Connect (as Linked products would): the same secret on both sides.
|
||||||
const secret = newLinkSecret();
|
const secret = newLinkSecret();
|
||||||
await h.db.productLink.create({ data: { installId: EVENTS_ID, product: 'events', url: eventsUrl, publicKey: ed25519PublicToPem(generateEd25519().publicKey), secret: h.ctx.secrets.encrypt(secret), connectedAt: new Date() } });
|
await h.db.productLink.create({ data: { installId: EVENTS_ID, product: 'events', url: eventsUrl, publicKey: ed25519PublicToPem(generateEd25519().publicKey), secret: h.ctx.secrets.encrypt(secret), connectedAt: new Date() } });
|
||||||
await eventsDb.productLink.create({ data: { installId: ACCOUNTS_ID, product: 'accounts', url: h.publicUrl, publicKey: 'x', secret: encrypt(secret), connectedAt: new Date(), updatedAt: new Date() } });
|
await edb.productLink.create({ data: { installId: ACCOUNTS_ID, product: 'accounts', url: h.publicUrl, publicKey: 'x', secret: encrypt(secret), connectedAt: new Date(), updatedAt: new Date() } });
|
||||||
await eventsDb.appSetting.create({ data: { key: 'hub_install_id', value: EVENTS_ID } });
|
await edb.appSetting.create({ data: { key: 'hub_install_id', value: EVENTS_ID } });
|
||||||
|
|
||||||
// Events' people before the move.
|
// Events' people before the move.
|
||||||
await eventsUser({ id: ids.thandi, name: 'Thandi Mokoena', email: 'thandi@hope.test', password: await bcrypt.hash('Old-events-pass1', 4), newsEmail: true });
|
await eventsUser({ id: ids.thandi, name: 'Thandi Mokoena', email: 'thandi@hope.test', password: await bcrypt.hash('Old-events-pass1', 4), newsEmail: true });
|
||||||
@@ -150,12 +161,12 @@ describe('moving Events\' sign-in to Accounts', () => {
|
|||||||
assert.equal(report.linked.length, 1);
|
assert.equal(report.linked.length, 1);
|
||||||
mode.forgetMode();
|
mode.forgetMode();
|
||||||
assert.equal(await mode.authMode(), 'accounts');
|
assert.equal(await mode.authMode(), 'accounts');
|
||||||
const thandi = await eventsDb.user.findUnique({ where: { id: ids.thandi } }) as { accountId: string };
|
const thandi = await edb.user.findUnique({ where: { id: ids.thandi } }) as { accountId: string };
|
||||||
assert.equal(thandi.accountId, ids.thandi, 'sub = Events id');
|
assert.equal(thandi.accountId, ids.thandi, 'sub = Events id');
|
||||||
const lerato = await eventsDb.user.findUnique({ where: { id: ids.lerato } }) as { accountId: string };
|
const lerato = await edb.user.findUnique({ where: { id: ids.lerato } }) as { accountId: string };
|
||||||
const account = await h.db.account.findUnique({ where: { email: 'lerato@hope.test' } }) as { id: string };
|
const account = await h.db.account.findUnique({ where: { email: 'lerato@hope.test' } }) as { id: string };
|
||||||
assert.equal(lerato.accountId, account.id, 'linked to the account she had');
|
assert.equal(lerato.accountId, account.id, 'linked to the account she had');
|
||||||
assert.equal((await eventsDb.user.findUnique({ where: { id: ids.guest } }) as { accountId: string | null }).accountId, null);
|
assert.equal((await edb.user.findUnique({ where: { id: ids.guest } }) as { accountId: string | null }).accountId, null);
|
||||||
assert.equal((await h.db.account.findUnique({ where: { id: ids.sam } }) as { role: string }).role, 'admin');
|
assert.equal((await h.db.account.findUnique({ where: { id: ids.sam } }) as { role: string }).role, 'admin');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -228,7 +239,7 @@ describe('signing out, and Accounts\' calls', () => {
|
|||||||
await h.db.account.update({ where: { id: ids.thandi }, data: { name: 'Thandi M. Mokoena' } });
|
await h.db.account.update({ where: { id: ids.thandi }, data: { name: 'Thandi M. Mokoena' } });
|
||||||
await profileChanged(h.ctx, ids.thandi);
|
await profileChanged(h.ctx, ids.thandi);
|
||||||
await runDue(h.ctx);
|
await runDue(h.ctx);
|
||||||
assert.equal((await eventsDb.user.findUnique({ where: { id: ids.thandi } }) as { name: string }).name, 'Thandi M. Mokoena');
|
assert.equal((await edb.user.findUnique({ where: { id: ids.thandi } }) as { name: string }).name, 'Thandi M. Mokoena');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('people added here after the move (demo data) are moved too, and can sign in', async () => {
|
test('people added here after the move (demo data) are moved too, and can sign in', async () => {
|
||||||
@@ -236,7 +247,7 @@ describe('signing out, and Accounts\' calls', () => {
|
|||||||
await eventsUser({ id, name: 'Demo Person', email: 'demo.person@hope.test', password: await bcrypt.hash('Demo-pass-123', 4) });
|
await eventsUser({ id, name: 'Demo Person', email: 'demo.person@hope.test', password: await bcrypt.hash('Demo-pass-123', 4) });
|
||||||
const r = await move.moveNewPeople(null);
|
const r = await move.moveNewPeople(null);
|
||||||
assert.equal(r.moved, 1);
|
assert.equal(r.moved, 1);
|
||||||
assert.equal((await eventsDb.user.findUnique({ where: { id } }) as { accountId: string }).accountId, id);
|
assert.equal((await edb.user.findUnique({ where: { id } }) as { accountId: string }).accountId, id);
|
||||||
const signed = await signInThroughEvents(new Browser(), 'demo.person@hope.test', 'Demo-pass-123', '/');
|
const signed = await signInThroughEvents(new Browser(), 'demo.person@hope.test', 'Demo-pass-123', '/');
|
||||||
assert.ok('token' in signed && signed.token);
|
assert.ok('token' in signed && signed.token);
|
||||||
});
|
});
|
||||||
@@ -245,7 +256,7 @@ describe('signing out, and Accounts\' calls', () => {
|
|||||||
const a = await h.makeAccount({ email: 'newcomer@hope.test', password: 'Newcomer-pass-1', name: 'Newcomer' });
|
const a = await h.makeAccount({ email: 'newcomer@hope.test', password: 'Newcomer-pass-1', name: 'Newcomer' });
|
||||||
const r = await signInThroughEvents(new Browser(), 'newcomer@hope.test', 'Newcomer-pass-1', '/events');
|
const r = await signInThroughEvents(new Browser(), 'newcomer@hope.test', 'Newcomer-pass-1', '/events');
|
||||||
assert.ok('token' in r && r.token, JSON.stringify(r));
|
assert.ok('token' in r && r.token, JSON.stringify(r));
|
||||||
assert.equal((await eventsDb.user.findUnique({ where: { id: r.user.id } }) as { accountId: string; role: string }).accountId, a.id);
|
assert.equal((await edb.user.findUnique({ where: { id: r.user.id } }) as { accountId: string; role: string }).accountId, a.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Undo goes back to Events\' own sign-in', async () => {
|
test('Undo goes back to Events\' own sign-in', async () => {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { test, expect, seed, API } from "./fixtures";
|
|||||||
// Excel exports (ExcelTS since 1.51.6): a styled report and the attendance register come back as
|
// Excel exports (ExcelTS since 1.51.6): a styled report and the attendance register come back as
|
||||||
// real workbooks that open and hold what was asked for.
|
// real workbooks that open and hold what was asked for.
|
||||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||||
const { Workbook } = require(path.join(__dirname, "..", "backend", "node_modules", "@cj-tech-master", "excelts"));
|
const { Workbook } = require(require.resolve("@cj-tech-master/excelts", { paths: [path.join(__dirname, "..", "backend")] }));
|
||||||
|
|
||||||
const XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
const XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||||
|
|
||||||
|
|||||||
@@ -31,11 +31,12 @@ events_backup() {
|
|||||||
.catch(e => { console.error(" Backup failed: " + e.message); process.exit(1); });'
|
.catch(e => { console.error(" Backup failed: " + e.message); process.exit(1); });'
|
||||||
}
|
}
|
||||||
|
|
||||||
# The shared packages this checkout's Events uses (from 2.4.0: contracts and seo), built before it.
|
# The shared packages this checkout's Events uses (from 2.4.0: contracts and seo; from 3.0.0:
|
||||||
|
# auth-client, built after contracts, which it uses), built before it.
|
||||||
# Only the ones in the checkout, so going back to an older tag still installs and builds.
|
# Only the ones in the checkout, so going back to an older tag still installs and builds.
|
||||||
events_shared_packages() {
|
events_shared_packages() {
|
||||||
local p
|
local p
|
||||||
for p in packages/contracts packages/seo; do
|
for p in packages/contracts packages/auth-client packages/seo; do
|
||||||
if [ -f "$1/$p/package.json" ] && grep -q "\"@crosscode/${p#packages/}\"" "$1/apps/events/backend/package.json" "$1/apps/events/frontend/package.json" 2>/dev/null; then
|
if [ -f "$1/$p/package.json" ] && grep -q "\"@crosscode/${p#packages/}\"" "$1/apps/events/backend/package.json" "$1/apps/events/frontend/package.json" 2>/dev/null; then
|
||||||
printf '%s\n' "$p"
|
printf '%s\n' "$p"
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -15,8 +15,9 @@ setup() {
|
|||||||
|
|
||||||
checkout() {
|
checkout() {
|
||||||
local dir="$TMP/$1"; shift
|
local dir="$TMP/$1"; shift
|
||||||
mkdir -p "$dir/apps/events/backend" "$dir/apps/events/frontend" "$dir/packages/contracts" "$dir/packages/seo"
|
mkdir -p "$dir/apps/events/backend" "$dir/apps/events/frontend" "$dir/packages/contracts" "$dir/packages/seo" "$dir/packages/auth-client"
|
||||||
echo '{"name":"@crosscode/contracts"}' > "$dir/packages/contracts/package.json"
|
echo '{"name":"@crosscode/contracts"}' > "$dir/packages/contracts/package.json"
|
||||||
|
echo '{"name":"@crosscode/auth-client"}' > "$dir/packages/auth-client/package.json"
|
||||||
echo '{"name":"@crosscode/seo"}' > "$dir/packages/seo/package.json"
|
echo '{"name":"@crosscode/seo"}' > "$dir/packages/seo/package.json"
|
||||||
printf '{"dependencies":{%s}}' "$1" > "$dir/apps/events/backend/package.json"
|
printf '{"dependencies":{%s}}' "$1" > "$dir/apps/events/backend/package.json"
|
||||||
printf '{"dependencies":{%s}}' "$2" > "$dir/apps/events/frontend/package.json"
|
printf '{"dependencies":{%s}}' "$2" > "$dir/apps/events/frontend/package.json"
|
||||||
@@ -35,6 +36,17 @@ checkout() {
|
|||||||
[ "$(cut -d'|' -f2 "$CALLS" | tr '\n' ' ')" = "new/packages/contracts new/packages/seo new/apps/events/backend new/apps/events/frontend " ]
|
[ "$(cut -d'|' -f2 "$CALLS" | tr '\n' ' ')" = "new/packages/contracts new/packages/seo new/apps/events/backend new/apps/events/frontend " ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "3.0.0: auth-client is installed and built after contracts, before Events" {
|
||||||
|
checkout new '"@crosscode/auth-client":"*","@crosscode/contracts":"*"' '"@crosscode/contracts":"*","@crosscode/seo":"*"'
|
||||||
|
run events_shared_packages "$TMP/new"
|
||||||
|
[ "$output" = $'packages/contracts
|
||||||
|
packages/auth-client
|
||||||
|
packages/seo' ]
|
||||||
|
events_build "$TMP/new" hope
|
||||||
|
[ "$(cut -d'|' -f2 "$CALLS" | tr '
|
||||||
|
' ' ')" = "new/packages/contracts new/packages/auth-client new/packages/seo new/apps/events/backend new/apps/events/frontend " ]
|
||||||
|
}
|
||||||
|
|
||||||
@test "2.3.x (no shared packages used): installed and built as before" {
|
@test "2.3.x (no shared packages used): installed and built as before" {
|
||||||
checkout old '' ''
|
checkout old '' ''
|
||||||
run events_shared_packages "$TMP/old"
|
run events_shared_packages "$TMP/old"
|
||||||
|
|||||||
Reference in New Issue
Block a user