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 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.
|
||||
const server = http.createServer();
|
||||
const port = await listen(server);
|
||||
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 db = memoryDb();
|
||||
const db = (opts.db ?? memoryDb()) as MemoryDb;
|
||||
const messenger = new MemoryMessenger();
|
||||
const ctx: Ctx = createCtx(config, db as never, { messenger, now: opts.now });
|
||||
const provider = await createProvider(ctx);
|
||||
|
||||
@@ -23,7 +23,10 @@ const require = createRequire(import.meta.url);
|
||||
process.env.NODE_ENV = 'test';
|
||||
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 eventsDb = new Proxy(raw, {
|
||||
get(target, prop: string) {
|
||||
@@ -45,7 +48,9 @@ const eventsDb = new Proxy(raw, {
|
||||
},
|
||||
});
|
||||
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 { 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() };
|
||||
|
||||
async function eventsUser(data: Record<string, unknown>) {
|
||||
return eventsDb.user.create({
|
||||
return edb.user.create({
|
||||
data: {
|
||||
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'),
|
||||
@@ -73,7 +78,13 @@ async function eventsUser(data: Record<string, unknown>) {
|
||||
}
|
||||
|
||||
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'));
|
||||
|
||||
// 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.
|
||||
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 eventsDb.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.productLink.create({ data: { installId: ACCOUNTS_ID, product: 'accounts', url: h.publicUrl, publicKey: 'x', secret: encrypt(secret), connectedAt: new Date(), updatedAt: new Date() } });
|
||||
await edb.appSetting.create({ data: { key: 'hub_install_id', value: EVENTS_ID } });
|
||||
|
||||
// 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 });
|
||||
@@ -150,12 +161,12 @@ describe('moving Events\' sign-in to Accounts', () => {
|
||||
assert.equal(report.linked.length, 1);
|
||||
mode.forgetMode();
|
||||
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');
|
||||
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 };
|
||||
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');
|
||||
});
|
||||
});
|
||||
@@ -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 profileChanged(h.ctx, ids.thandi);
|
||||
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 () => {
|
||||
@@ -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) });
|
||||
const r = await move.moveNewPeople(null);
|
||||
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', '/');
|
||||
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 r = await signInThroughEvents(new Browser(), 'newcomer@hope.test', 'Newcomer-pass-1', '/events');
|
||||
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 () => {
|
||||
|
||||
@@ -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
|
||||
// real workbooks that open and hold what was asked for.
|
||||
// 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";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user