Files
0pt/tests/validation/0pt-schemas.test.ts
T
Simon AxelssonandClaude Sonnet 5 dc6aaa0e10
CI (SIAX Cloud) / quality (push) Successful in 51s
Security Scan (SIAX Cloud) / security (push) Successful in 26s
CI (SIAX Cloud) / security (push) Successful in 28s
CI (SIAX Cloud) / integration (push) Successful in 1m1s
CI (SIAX Cloud) / integration (pull_request) Successful in 30s
CI (SIAX Cloud) / security (pull_request) Successful in 24s
CI (SIAX Cloud) / quality (pull_request) Successful in 48s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 22s
fix(data-os): P014 — close tenant-isolation + idempotency gaps, execute real restore drill
Tenant isolation (security-critical): every app/api/0pt/** route reading
tenant_id from client-controlled query/body never cross-checked it against
the token middleware.ts already verified (x-0pt-verified-tenant). Adds a
shared assertTenantMatchesVerified guard and wires it into all 20+ affected
routes. Also closes two full-leak-by-omission bugs found in this pass
(documents/capture and lakehouse/mappings GETs returned every tenant's rows
when tenant_id was simply omitted) and two "no tenant check at all" reads
(rate-policies/[id], lakehouse/mappings/[id]) reachable by ID alone.

Duplicate-delivery idempotency (correctness-critical): POST /api/orders now
honors Idempotency-Key (sql/006, replay-or-409-on-conflict, Postgres +
in-memory); the Python orchestrator's POST /orders/{id}/run — the actual
delivery trigger — is now idempotent under an asyncio lock, closing a real
concurrent-request race, not just the sequential-retry case.

Engine-duplication resolution: adaptive_router.py's docstring wrongly
invited a bypass ("see b0ts for scraping") contradicting the product's own
compliance stance; corrected, and given real (additive, not escalating)
block-signal tracking so a rate-limited/blocked source is excluded from the
plan instead of hammered. Also fixes a previously-undiscovered production
bug: the Python engine's only call into the TS quality bridge
(X-Internal-Token) was unconditionally 401'd by middleware.ts, which never
checked that credential — the advisory quality bridge was unreachable in any
environment enforcing auth.

Restore drill: executed for real against throwaway Docker Postgres
containers (not simulated). Found and fixed two real blockers along the way:
sql/004 referenced opt_datasets, which no migration ever created (chain was
never actually runnable end to end); lib/db.ts hardcoded ssl:"require",
making any non-TLS Postgres (incl. compose.dev.yaml's own local-dev
container) unreachable. After both fixes: full migration chain applies
cleanly, pg_dump/pg_restore round-trip is byte-identical (row counts +
content checksums), and real application code (lib/orders-store.ts,
SourceRegistryService's Postgres repository) reads the restored data back
correctly, including cross-tenant isolation still holding.

82 new tests (55 TS + 27 Python), all passing; full existing suite (291 TS +
55 Python) still green; typecheck clean on every changed/added file.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-03 12:00:43 +02:00

152 lines
4.6 KiB
TypeScript

/**
* Unit tests for src/validation/0pt-schemas.ts -- the Wave 2 request-body
* validation for 0pt's mutating capability routes. Confirms a valid body
* passes and a malformed body produces a structured 400.
*/
import { describe, it, expect } from 'vitest';
import {
parseBody,
documentCaptureSchema,
documentExtractionSchema,
sourceQuerySchema,
createSourceRatePolicySchema,
mapDatasetToIcebergTableSchema,
requestIcebergWriteSchema,
} from '@/src/validation/0pt-schemas';
describe('parseBody + documentCaptureSchema', () => {
const valid = {
mailbox: {
tenant_id: 't1',
client_id: 'c1',
client_secret: 's1',
mailbox_address: 'ap@x.io',
},
tenant_id: 't1',
project_id: 'p1',
};
it('accepts a valid capture body', () => {
const r = parseBody(documentCaptureSchema, valid);
expect(r.ok).toBe(true);
});
it('rejects a body missing tenant_id with a 400 + issues', () => {
const { tenant_id: _omit, ...bad } = valid;
const r = parseBody(documentCaptureSchema, bad);
expect(r.ok).toBe(false);
if (!r.ok) {
expect(r.status).toBe(400);
expect(r.body.error).toBe('validation_error');
expect(r.body.issues.some((i) => i.path === 'tenant_id')).toBe(true);
}
});
it('rejects a wrong-typed top field', () => {
const r = parseBody(documentCaptureSchema, { ...valid, top: 'ten' });
expect(r.ok).toBe(false);
});
});
describe('documentExtractionSchema', () => {
it('accepts minimal valid input', () => {
expect(parseBody(documentExtractionSchema, { tenant_id: 't', project_id: 'p' }).ok).toBe(true);
});
it('rejects empty body', () => {
expect(parseBody(documentExtractionSchema, {}).ok).toBe(false);
});
});
describe('sourceQuerySchema', () => {
it('accepts a valid query', () => {
const r = parseBody(sourceQuerySchema, {
query: 'SELECT 1',
tenant_id: 't',
project_id: 'p',
connection_config: { host: 'db' },
});
expect(r.ok).toBe(true);
});
it('rejects a blank query', () => {
const r = parseBody(sourceQuerySchema, {
query: ' ',
tenant_id: 't',
project_id: 'p',
connection_config: {},
});
expect(r.ok).toBe(false);
});
it('rejects a missing connection_config', () => {
const r = parseBody(sourceQuerySchema, { query: 'SELECT 1', tenant_id: 't', project_id: 'p' });
expect(r.ok).toBe(false);
});
});
describe('createSourceRatePolicySchema', () => {
const valid = {
source_rate_policy_id: 'rp1',
tenant_id: 't',
project_id: 'p',
source_id: 's',
rate_limit_per_minute: 60,
crawl_budget: 1000,
};
it('accepts a valid policy', () => {
expect(parseBody(createSourceRatePolicySchema, valid).ok).toBe(true);
});
it('rejects a bad access enum', () => {
expect(parseBody(createSourceRatePolicySchema, { ...valid, access: 'ftp' }).ok).toBe(false);
});
it('rejects a non-numeric rate_limit_per_minute', () => {
expect(parseBody(createSourceRatePolicySchema, { ...valid, rate_limit_per_minute: 'fast' }).ok).toBe(false);
});
});
describe('mapDatasetToIcebergTableSchema', () => {
const valid = {
tenant_id: 't',
project_id: 'p',
dataset_id: 'd',
dataset_iceberg_mapping_id: 'm',
namespace: 'ns',
table_name: 'tbl',
storage_uri: 's3://x',
catalog_ref: 'cat',
actor_id: 'a',
correlation_id: 'corr',
};
it('accepts a valid mapping', () => {
expect(parseBody(mapDatasetToIcebergTableSchema, valid).ok).toBe(true);
});
it('rejects a mapping missing storage_uri', () => {
const { storage_uri: _omit, ...bad } = valid;
expect(parseBody(mapDatasetToIcebergTableSchema, bad).ok).toBe(false);
});
});
describe('requestIcebergWriteSchema', () => {
const valid = {
// P014: tenant_id is now required -- the route cross-checks it against
// the caller's verified tenant and threads it into
// IcebergWriteService.requestIcebergWrite so a foreign mapping_id can no
// longer be written to (see getMapping's doc comment).
tenant_id: 't1',
actor_id: 'a',
correlation_id: 'corr',
schema_version: 'v1',
manifest_uri: 's3://m',
};
it('accepts a valid write request', () => {
expect(parseBody(requestIcebergWriteSchema, valid).ok).toBe(true);
});
it('rejects a write request missing manifest_uri', () => {
const { manifest_uri: _omit, ...bad } = valid;
expect(parseBody(requestIcebergWriteSchema, bad).ok).toBe(false);
});
it('rejects a write request missing tenant_id', () => {
const { tenant_id: _omit, ...bad } = valid;
expect(parseBody(requestIcebergWriteSchema, bad).ok).toBe(false);
});
});