wishforge-ide/tests/auth-expiry.test.ts
eust-w 6d2dcebe65 feat(ide): clone the project into IDE workspaces and push back through Git (FR-H2)
While someone has a workspace open, the gateway asks the API for a 15-minute
write token for the project's platform repository on behalf of that person
(membership and role checked each time) and delivers it to the workspace
backend. The backend writes it to a Git config file outside the repository
(a memory volume in Kubernetes) that the clone includes, and on the first
delivery clones the target branch into a folder named after the project. The
workspace cannot mint credentials itself, so access ends with the last
session.

- People commit and push to the target branch like with any Git server; the
  platform only accepts fast-forwards, and the next wish starts from that
  commit (covered end to end in the local loop test).
- Commits are authored by whoever opened the latest session, from the name and
  email in their sign-in; their own git config overrides it.
- Only the gateway can reach the delivery endpoint: it needs Theia's
  connection token, and the gateway refuses the path for browsers.
- The provisioner pins the workspace to its repository and lets the pod reach
  the Git service; the Kubernetes baseline admits that traffic. External
  repositories are not cloned.
- The local shared Theia clones each project into its own folder.
- The Theia image installs Git, and its stage is named theia: the default
  build target of that Dockerfile is the gateway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:52:37 +08:00

59 lines
2.5 KiB
TypeScript

import { beforeEach, expect, it, vi } from 'vitest';
const claims = vi.hoisted(() => ({ payload: {} as Record<string, unknown> }));
vi.mock('jose', async importOriginal => ({
...(await importOriginal<typeof import('jose')>()),
createRemoteJWKSet: () => ({}),
jwtVerify: async () => claims,
}));
import { Authenticator } from '../apps/api/src/auth.js';
import { MemoryStore } from './helpers/memory-store.js';
const env = {
NODE_ENV: 'production',
OIDC_ISSUER: 'https://issuer.test/realms/wishforge',
OIDC_AUDIENCE: 'wishforge-api',
WF_WEB_CLIENT_ID: 'console',
};
let db: MemoryStore;
beforeEach(() => {
db = new MemoryStore();
db.seed('alpha');
claims.payload = { sub: 'owner', tenant_id: 'alpha', azp: 'console', exp: Math.floor(Date.now() / 1000) + 300 };
});
it('carries the cryptographically verified identity expiry into the authenticated actor', async () => {
expect(await new Authenticator(db, env).authenticate('Bearer fixture')).toMatchObject({
userId: 'owner',
kind: 'human',
authExpiresAt: Number(claims.payload.exp) * 1000,
});
});
it('rejects absent, nonfinite and expired expiration even after a verifier returns identity claims', async () => {
const auth = new Authenticator(db, env);
for (const value of [undefined, Infinity, 0, Math.floor(Date.now() / 1000) - 1]) {
claims.payload.exp = value;
await expect(auth.authenticate('Bearer fixture')).rejects.toMatchObject({ code: 'UNAUTHENTICATED' });
}
});
it('rejects production plaintext issuers and credential-bearing issuer configuration', () => {
expect(() => new Authenticator(db, { ...env, OIDC_ISSUER: 'http://issuer.test/realm' })).toThrow('HTTPS');
expect(() => new Authenticator(db, { ...env, OIDC_ISSUER: 'https://user:secret@issuer.test/realm' })).toThrow(
'credentials',
);
});
it('takes a usable name and verified email from the sign-in for commit authorship', async () => {
const auth = new Authenticator(db, env);
Object.assign(claims.payload, { name: 'Ada Lovelace', email: 'ada@example.com', email_verified: true });
expect(await auth.authenticate('Bearer fixture')).toMatchObject({
displayName: 'Ada Lovelace',
email: 'ada@example.com',
});
Object.assign(claims.payload, {
name: 'Eve <eve@example.com>',
preferred_username: 'eve',
email: 'eve@example.com',
email_verified: false,
});
const forged = await auth.authenticate('Bearer fixture');
expect(forged.displayName).toBe('eve');
expect(forged.email).toBeUndefined();
});