Files
openchamber/packages/web/server/lib/quota/routes.test.js
T
Bohdan Triapitsyn b09614fd68 refactor(quota): secure managed provider credentials (#2160)
- add shared owner-only credential storage for OpenCode Go, Ollama Cloud, and Cursor
- validate credentials before atomic writes using 0700 directories and 0600 files
- replace provider-specific credential routes with an allowlisted lifecycle API
- stop automatically reading Ollama's legacy cookie file
- stop reading or modifying Cursor's database during regular quota requests
- add explicit one-time Cursor credential import without mutating Cursor storage
- persist refreshed Cursor credentials only in OpenChamber-managed storage
- add Ollama Cloud and Cursor credential controls to provider settings
- preserve OpenCode Go tracking through the shared credential flow
- add VS Code credential management and Cursor quota parity
- reject authentication redirects, enforce request timeouts, and fail on unparseable usage pages
- mask stored secrets in API responses and extend quota security coverage
- update quota provider documentation
2026-07-12 16:21:38 +03:00

41 lines
1.7 KiB
JavaScript

import { afterAll, describe, expect, it, mock } from 'bun:test';
import express from 'express';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { registerQuotaRoutes } from './routes.js';
const previousDataDir = process.env.OPENCHAMBER_DATA_DIR;
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-go-routes-'));
process.env.OPENCHAMBER_DATA_DIR = temporaryDirectory;
afterAll(() => {
if (previousDataDir === undefined) delete process.env.OPENCHAMBER_DATA_DIR;
else process.env.OPENCHAMBER_DATA_DIR = previousDataDir;
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
});
describe('OpenCode Go credential routes', () => {
it('parses a JSON credential payload before validation', async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = mock(async () => new Response('rollingUsage:$R[1]={usagePercent:25,resetInSec:60}'));
const app = express();
registerQuotaRoutes(app, { getQuotaProviders: async () => ({}) });
const server = app.listen(0);
try {
const address = server.address();
if (!address || typeof address === 'string') throw new Error('Test server did not start');
const response = await originalFetch(`http://127.0.0.1:${address.port}/api/quota/credentials/opencode-go`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ workspaceId: 'wrk_test', authCookie: 'auth=secret' }),
});
expect(response.status).toBe(200);
expect(await response.json()).toEqual({ configured: true, workspaceId: 'wrk_test', secretMasked: '••••••••' });
} finally {
globalThis.fetch = originalFetch;
server.close();
}
});
});