Read existing Client catalogs through verified workspace context
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { createWorkspaceResolver } = require('../server/workspace.cjs');
|
||||
const { createConnectedCatalog } = require('../server/connected.cjs');
|
||||
const clientId = '11111111-1111-4111-8111-111111111111';
|
||||
function workspace(name = 'a', readConnectedCatalog) {
|
||||
return { clientId: name === 'a' ? clientId : '22222222-2222-4222-8222-222222222222', tenant: name, displayName: 'Fixture ' + name, namespace: name.repeat(64), readConnectedCatalog };
|
||||
}
|
||||
function catalog(products = []) { return { source: 'supabase', fetchedAt: '2026-10-19T00:00:00Z', products }; }
|
||||
|
||||
test('connected catalog preserves zero/unknown prices, image URLs and source timestamps without persistent writes', async () => {
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture' });
|
||||
const store = await get(workspace('a', async () => catalog([
|
||||
{ id: 'one', title: 'Source <SKU>', rawPrice: 0, rawRegularPrice: 270, currency: 'BDT', image: 'https://images.fixture/product.png' },
|
||||
{ id: 'two', title: 'Missing price', rawPrice: null, currency: null }
|
||||
])));
|
||||
assert.equal(store.readOnly, true); assert.equal(store.id, 'connected:' + clientId);
|
||||
assert.equal(store.products[0].rawPrice, 0); assert.equal(store.products[0].image, 'https://images.fixture/product.png');
|
||||
assert.equal(store.products[1].rawPrice, null); assert.equal(store.products[1].currency, null);
|
||||
assert.equal(store.products[1].link, ''); assert.equal(store.fetchedAt, '2026-10-19T00:00:00Z');
|
||||
});
|
||||
|
||||
test('connected cache shares one source read for the same client and separates other workspaces', async () => {
|
||||
let reads = 0;
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture' });
|
||||
const read = async () => { reads++; return catalog([{ id: 'one', title: 'Product', rawPrice: 380 }]); };
|
||||
const same = workspace('a', read);
|
||||
await Promise.all([get(same), get(same), get(same)]);
|
||||
assert.equal(reads, 1);
|
||||
await get(workspace('b', read)); assert.equal(reads, 2);
|
||||
});
|
||||
|
||||
test('failed connected reads never become empty success and can be retried after recovery', async () => {
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture', ttlMs: 0 });
|
||||
let broken = true;
|
||||
const same = workspace('a', async () => { if (broken) throw Error('fixture outage'); return catalog(); });
|
||||
await assert.rejects(get(same), /fixture outage/);
|
||||
broken = false;
|
||||
assert.equal((await get(same)).products.length, 0);
|
||||
});
|
||||
|
||||
test('simultaneous cold catalog reads across clients are bounded', async () => {
|
||||
const get = createConnectedCatalog({ publicClientOrigin: 'https://client.fixture', maxConcurrentReads: 1 });
|
||||
let release, entered;
|
||||
const started = new Promise(resolve => { entered = resolve; });
|
||||
const wait = new Promise(resolve => { release = resolve; });
|
||||
const first = get(workspace('a', async () => { entered(); await wait; return catalog(); }));
|
||||
await started;
|
||||
await assert.rejects(get(workspace('b', async () => catalog())), { code: 'CONNECTED_CATALOG_BUSY' });
|
||||
release(); await first;
|
||||
assert.equal((await get(workspace('b', async () => catalog()))).products.length, 0);
|
||||
});
|
||||
|
||||
test('Client catalog read forwards only server identity and rejects another canonical client response', async () => {
|
||||
const calls = [];
|
||||
let mismatch = false;
|
||||
const resolve = createWorkspaceResolver({ authMeUrl: 'https://auth.fixture/me', clientMeUrl: 'https://client.fixture/me', clientCatalogUrl: 'https://client.fixture/studio/catalog',
|
||||
fetchImpl: async (url, options) => {
|
||||
calls.push({ url, headers: options.headers });
|
||||
const actor = { id: 'actor', email: 'actor@example.test', role: 'client' };
|
||||
if (url.includes('auth.fixture')) return Response.json({ user: { ...actor, isActive: true } });
|
||||
if (url.endsWith('/studio/catalog')) return Response.json({ success: true, clientId: mismatch ? 'other-client' : clientId, tenant: 'canonical', ...catalog() });
|
||||
return Response.json({ success: true, clientId, tenant: 'canonical', actor, perms: { products: true } });
|
||||
}
|
||||
});
|
||||
const ws = await resolve({ url: '/api/get-synced-stores?tenant=victim', headers: { cookie: 'aura_access=fixture-token; unrelated=must-not-forward', 'x-client-id': 'victim' } });
|
||||
await ws.readConnectedCatalog();
|
||||
assert.equal(calls.at(-1).headers.Cookie, 'aura_access=fixture-token');
|
||||
assert.equal(calls.at(-1).headers['x-aura-tenant'], 'canonical');
|
||||
assert.equal(calls.at(-1).headers['x-client-id'], undefined);
|
||||
mismatch = true;
|
||||
await assert.rejects(ws.readConnectedCatalog(), { code: 'CATALOG_WORKSPACE_MISMATCH' });
|
||||
});
|
||||
Reference in New Issue
Block a user