Initial commit: multi-company financial management system
Root company app (src/, client/) — GL foundation, AR, and AP: - Segmented Chart of Accounts (Sage 300-style), double-entry PostingService with three-layer balance enforcement (app validation, CHECK constraints, deferred DB trigger), gap-free document numbering, RBAC with branch restrictions. - Accounts Receivable: Customers, Invoices, Credit Notes, Receipts with allocation, all posting through PostingService. - Accounts Payable: Suppliers, Supplier Invoices, Debit Notes, Payments with allocation, mirroring AR's posting pattern. - Tax Codes shared between AR and AP. - Company-app login is a token handoff from the Account Manager (RS256, single-use, company-scoped) rather than a local password. - Frontend reskinned to the approved mockup: navy sidebar, dashboard, tree-view Chart of Accounts, Customer/Supplier 360 pages. Account Manager (manager/, manager/client/) — central login gateway: - Password + TOTP 2FA + Google/Microsoft OAuth, company picker, and the RS256 handoff token issuance that the company app verifies. Covered by unit + integration tests against a real Postgres database (GL balance invariants, AR/AP posting correctness and allocation limits, the full handoff exchange including replay/cross-company/expiry/forgery rejection). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tq2DbEvzHzs13jDFhLaX44
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Requires the real, migrated, seeded Manager database (see SETUP.md) reachable via DATABASE_URL.
|
||||
* Run with: npm run test:integration
|
||||
*/
|
||||
import request from 'supertest';
|
||||
import { createApp } from '../../src/app';
|
||||
import { prisma } from '../../src/lib/prisma';
|
||||
|
||||
const app = createApp();
|
||||
|
||||
const OPERATOR_EMAIL = 'joosth2002@icloud.com';
|
||||
const OPERATOR_PASSWORD = 'ChangeMe123!';
|
||||
|
||||
describe('Manager auth + handoff (real DB)', () => {
|
||||
afterAll(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
|
||||
it('logs in with the seeded operator credentials', async () => {
|
||||
const response = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: OPERATOR_PASSWORD });
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.requiresTwoFactor).toBe(false);
|
||||
expect(response.body.accessToken).toBeDefined();
|
||||
expect(response.body.person.email).toBe(OPERATOR_EMAIL);
|
||||
});
|
||||
|
||||
it('rejects the wrong password', async () => {
|
||||
const response = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: 'wrong' });
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
it('lists the seeded demo company as a membership and issues a working handoff token', async () => {
|
||||
const login = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: OPERATOR_PASSWORD });
|
||||
const accessToken = login.body.accessToken as string;
|
||||
|
||||
const myCompanies = await request(app).get('/api/companies').set('Authorization', `Bearer ${accessToken}`);
|
||||
expect(myCompanies.status).toBe(200);
|
||||
expect(myCompanies.body.length).toBeGreaterThanOrEqual(1);
|
||||
const demo = myCompanies.body.find((c: { slug: string }) => c.slug === 'demo');
|
||||
expect(demo).toBeDefined();
|
||||
|
||||
const handoff = await request(app)
|
||||
.post(`/api/companies/${demo.companyId}/handoff`)
|
||||
.set('Authorization', `Bearer ${accessToken}`);
|
||||
expect(handoff.status).toBe(200);
|
||||
expect(handoff.body.token).toBeDefined();
|
||||
expect(handoff.body.redirectUrl).toBe('http://localhost:5173');
|
||||
});
|
||||
|
||||
it('refuses a handoff for a company the person has no membership in', async () => {
|
||||
const login = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: OPERATOR_PASSWORD });
|
||||
const accessToken = login.body.accessToken as string;
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/companies/999999/handoff')
|
||||
.set('Authorization', `Bearer ${accessToken}`);
|
||||
expect(response.status).toBe(403);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import crypto from 'crypto';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { signHandoffToken } from '../../src/lib/handoffToken';
|
||||
|
||||
const publicKeyPath = path.resolve(__dirname, '../../keys/handoff-public.pem');
|
||||
const privateKeyPath = path.resolve(__dirname, '../../keys/handoff-private.pem');
|
||||
const publicKey = fs.readFileSync(publicKeyPath, 'utf8');
|
||||
const privateKey = fs.readFileSync(privateKeyPath, 'utf8');
|
||||
|
||||
const claims = { sub: 'person@example.com', pid: 1, cid: 1, companySlug: 'demo' };
|
||||
|
||||
describe('signHandoffToken', () => {
|
||||
it('produces an RS256 token whose claims round-trip through the public key', () => {
|
||||
const { token, jti } = signHandoffToken(claims);
|
||||
|
||||
const decoded = jwt.verify(token, publicKey, { algorithms: ['RS256'] }) as typeof claims & { jti: string };
|
||||
expect(decoded.sub).toBe(claims.sub);
|
||||
expect(decoded.pid).toBe(claims.pid);
|
||||
expect(decoded.cid).toBe(claims.cid);
|
||||
expect(decoded.companySlug).toBe(claims.companySlug);
|
||||
expect(decoded.jti).toBe(jti);
|
||||
});
|
||||
|
||||
it('mints a distinct jti on every call (single-use tokens)', () => {
|
||||
const first = signHandoffToken(claims);
|
||||
const second = signHandoffToken(claims);
|
||||
expect(first.jti).not.toBe(second.jti);
|
||||
});
|
||||
|
||||
it('cannot be verified against a different keypair', () => {
|
||||
const { publicKey: wrongPublicKey } = crypto.generateKeyPairSync('rsa', {
|
||||
modulusLength: 2048,
|
||||
publicKeyEncoding: { type: 'spki', format: 'pem' },
|
||||
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
|
||||
});
|
||||
const { token } = signHandoffToken(claims);
|
||||
expect(() => jwt.verify(token, wrongPublicKey, { algorithms: ['RS256'] })).toThrow();
|
||||
});
|
||||
|
||||
it('rejects a tampered token', () => {
|
||||
const { token } = signHandoffToken(claims);
|
||||
const tampered = `${token.slice(0, -4)}abcd`;
|
||||
expect(() => jwt.verify(tampered, publicKey, { algorithms: ['RS256'] })).toThrow();
|
||||
});
|
||||
|
||||
it('rejects an expired token', () => {
|
||||
const expired = jwt.sign(claims, privateKey, { algorithm: 'RS256', expiresIn: -10, jwtid: 'expired-test' });
|
||||
expect(() => jwt.verify(expired, publicKey, { algorithms: ['RS256'] })).toThrow(/expired/i);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user