feat(research): read verified frozen datasets (RP28)
This commit is contained in:
parent
04892a59fe
commit
4b9c642817
9 changed files with 263 additions and 18 deletions
|
|
@ -32,10 +32,13 @@ const payloadSql = 'SELECT payload FROM backtest_datasets WHERE hash=$1';
|
|||
const baseline = createDatasetV2(datasetInput());
|
||||
const path = (hash = baseline.manifest.hash) => `/api/v2/datasets/canonical/${hash}/export`;
|
||||
function fixture(load: () => Promise<unknown> = async () => baseline) {
|
||||
let researchEnabled = true;
|
||||
const payloadRead = mock(load);
|
||||
const query = mock(async (sql: string, values?: unknown[]) => {
|
||||
if (sql === 'SELECT id,enabled,policy FROM research_agents WHERE token_hash=$1') {
|
||||
return { rows: [{ id: agentId, enabled: true, policy: { allowedProjectIds: [] } }] };
|
||||
return {
|
||||
rows: [{ id: agentId, enabled: researchEnabled, policy: { allowedProjectIds: [] } }],
|
||||
};
|
||||
}
|
||||
if (sql !== payloadSql || values?.length !== 1) {
|
||||
throw new Error('Unexpected query: export must not write or read other tables');
|
||||
|
|
@ -69,7 +72,18 @@ function fixture(load: () => Promise<unknown> = async () => baseline) {
|
|||
headers: Record<string, string> = { Authorization: `Bearer ${tokens.admin}` },
|
||||
signal?: AbortSignal
|
||||
) => app.request(path(hash), { headers, signal });
|
||||
return { app, request, database, query, payloadRead, sourceRead, research };
|
||||
return {
|
||||
app,
|
||||
request,
|
||||
database,
|
||||
query,
|
||||
payloadRead,
|
||||
sourceRead,
|
||||
research,
|
||||
revokeResearch: () => {
|
||||
researchEnabled = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
function responseHeaders(response: Response) {
|
||||
expect(response.headers.get('Cache-Control')).toBe('no-store');
|
||||
|
|
@ -268,11 +282,11 @@ describe('validated canonical dataset export', () => {
|
|||
).rejects.toThrow('cancelled after query');
|
||||
});
|
||||
|
||||
test('existing read, admin, service and browser credentials may export; anonymous and real research credentials may not', async () => {
|
||||
test('read, admin, service, browser and research credentials export; anonymous and revoked credentials do not', async () => {
|
||||
const f = fixture();
|
||||
expect((await f.request(baseline.manifest.hash, {})).status).toBe(401);
|
||||
expect(f.query).not.toHaveBeenCalled();
|
||||
for (const token of [tokens.read, tokens.admin, tokens.service]) {
|
||||
for (const token of [tokens.read, tokens.admin, tokens.service, tokens.research]) {
|
||||
expect(
|
||||
(await f.request(baseline.manifest.hash, { Authorization: `Bearer ${token}` })).status
|
||||
).toBe(200);
|
||||
|
|
@ -289,20 +303,13 @@ describe('validated canonical dataset export', () => {
|
|||
expect((await f.request(baseline.manifest.hash, { Cookie: cookie! })).status).toBe(200);
|
||||
}
|
||||
const before = f.payloadRead.mock.calls.length;
|
||||
f.revokeResearch();
|
||||
const denied = await f.request(baseline.manifest.hash, {
|
||||
Authorization: `Bearer ${tokens.research}`,
|
||||
});
|
||||
expect(denied.status).toBe(403);
|
||||
expect(denied.status).toBe(401);
|
||||
expect(denied.headers.get('Content-Disposition')).toBeNull();
|
||||
expect(f.payloadRead).toHaveBeenCalledTimes(before);
|
||||
// Positive control: this token is recognized by the real resolver for an allowed route.
|
||||
expect(
|
||||
await f.research.authenticate(
|
||||
new Request('http://localhost/api/v2/experiments', {
|
||||
headers: { Authorization: `Bearer ${tokens.research}` },
|
||||
})
|
||||
)
|
||||
).toEqual({ principal: agentId });
|
||||
expect(
|
||||
f.query.mock.calls.filter(call => call[0].includes('FROM research_agents'))
|
||||
).toHaveLength(2);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue