Add tenant-scoped Studio backend primitives and static asset allowlist
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { amount, publicLink, shopifyProducts, wooProducts, schemaProducts } = require('../server/products.cjs');
|
||||
const context = { url: 'https://fixture-store.example/', name: 'Fixture Store', id: 'fixture-store', category: 'general' };
|
||||
|
||||
test('price parsing preserves free and decimal prices without inventing missing values', () => {
|
||||
assert.equal(amount(0), 0);
|
||||
assert.equal(amount('১,২৩৪.৫০'), 1234.5);
|
||||
assert.equal(amount(1e-7), 1e-7);
|
||||
for (const value of [undefined, null, '', 'Call for price', true, -2, Infinity, '1,2', '12USD']) assert.equal(amount(value), null);
|
||||
});
|
||||
|
||||
test('Shopify preserves each variant and its SKU, actual compare price and currency', () => {
|
||||
const rows = shopifyProducts({ products: [{ id: 7, title: 'Serum', handle: 'serum', variants: [
|
||||
{ id: 71, title: '5ml', sku: 'SERUM-5', price: '220.00', compare_at_price: '270.00' },
|
||||
{ id: 72, title: '30ml', sku: 'SERUM-30', price: '0.00', compare_at_price: null }
|
||||
] }] }, context, 'BDT');
|
||||
assert.equal(rows.length, 2);
|
||||
assert.equal(rows[0].rawPrice, 220);
|
||||
assert.equal(rows[0].rawRegularPrice, 270);
|
||||
assert.equal(rows[0].sku, 'SERUM-5');
|
||||
assert.equal(rows[1].rawPrice, 0);
|
||||
assert.equal(rows[1].regularPrice, '');
|
||||
assert.notEqual(rows[0].id, rows[1].id);
|
||||
});
|
||||
|
||||
test('missing Shopify prices remain unknown, and unspecified currency is not guessed as BDT', () => {
|
||||
const row = shopifyProducts({ products: [{ title: 'Unpriced item', variants: [{}] }] }, context)[0];
|
||||
assert.equal(row.rawPrice, null);
|
||||
assert.equal(row.currency, null);
|
||||
assert.equal(row.price, 'দাম পাওয়া যায়নি');
|
||||
assert.equal(row.rawRegularPrice, null);
|
||||
});
|
||||
|
||||
test('Woo Store API minor units preserve source prices and reject an unknown scale', () => {
|
||||
const input = { id: 8, name: 'Item', prices: { price: '12345', regular_price: '15000', currency_minor_unit: 2, currency_code: 'USD' } };
|
||||
const row = wooProducts([input], context)[0];
|
||||
assert.equal(row.rawPrice, 123.45);
|
||||
assert.equal(row.rawRegularPrice, 150);
|
||||
assert.equal(row.currency, 'USD');
|
||||
delete input.prices.currency_minor_unit;
|
||||
assert.equal(wooProducts([input], context)[0].rawPrice, null);
|
||||
});
|
||||
|
||||
test('schema products keep missing price unknown and do not turn aggregate lowPrice into an exact variant price', () => {
|
||||
const rows = schemaProducts({ '@graph': [
|
||||
{ '@type': 'Product', name: 'Unknown', sku: 'U' },
|
||||
{ '@type': 'Product', name: 'From price', sku: 'F', offers: { '@type': 'AggregateOffer', lowPrice: 100, highPrice: 900, priceCurrency: 'BDT' } },
|
||||
{ '@type': 'Product', name: 'Free sample', sku: 'Z', offers: { price: 0, priceCurrency: 'BDT' } }
|
||||
] }, context);
|
||||
assert.deepEqual(rows.map(r => r.rawPrice), [null, null, 0]);
|
||||
assert.ok(rows.every(r => r.rawRegularPrice === null));
|
||||
});
|
||||
|
||||
test('unsafe image/link schemes are excluded and stable variant identifiers repeat', () => {
|
||||
for (const value of ['javascript:alert(1)', 'data:text/html,unsafe', 'https://user:pass@example.com/']) assert.equal(publicLink(value, context.url), '');
|
||||
assert.equal(publicLink('/image.jpg', context.url), 'https://fixture-store.example/image.jpg');
|
||||
const payload = { products: [{ id: 5, title: 'Product', variants: [{ id: 6, price: '1.25' }] }] };
|
||||
assert.equal(shopifyProducts(payload, context)[0].id, shopifyProducts(payload, context)[0].id);
|
||||
});
|
||||
|
||||
test('products without platform IDs do not overwrite each other', () => {
|
||||
const rows = shopifyProducts({ products: [
|
||||
{ title: 'First', variants: [{ price: '10' }] },
|
||||
{ title: 'Second', variants: [{ price: '20' }] }
|
||||
] }, context);
|
||||
assert.notEqual(rows[0].id, rows[1].id);
|
||||
});
|
||||
|
||||
test('a schema strikethrough amount alone is not represented as the selling price', () => {
|
||||
const row = schemaProducts({ '@type': 'Product', name: 'Item', offers: {
|
||||
priceSpecification: { priceType: 'https://schema.org/StrikethroughPrice', price: 270, priceCurrency: 'BDT' }
|
||||
} }, context)[0];
|
||||
assert.equal(row.rawPrice, null);
|
||||
assert.equal(row.rawRegularPrice, 270);
|
||||
});
|
||||
|
||||
test('Woo variable parent range is not treated as an exact selected variant price', () => {
|
||||
const row = wooProducts([{ id: 9, type: 'variable', name: 'Shirt', prices: {
|
||||
price: '10000', regular_price: '15000', currency_minor_unit: 2, currency_code: 'BDT',
|
||||
price_range: { min_amount: '10000', max_amount: '20000' }
|
||||
} }], context)[0];
|
||||
assert.equal(row.rawPrice, null);
|
||||
assert.equal(row.rawRegularPrice, null);
|
||||
assert.equal(row.requiresVariantSelection, true);
|
||||
assert.deepEqual(row.priceRange, { minimum: 100, maximum: 200 });
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs/promises');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { createWorkspaceResolver, requireProductWrite } = require('../server/workspace.cjs');
|
||||
const { createStore } = require('../server/store.cjs');
|
||||
|
||||
function resolver({ role = 'client', tenant = 'verified-tenant', products = true, active = true, status = 200, captures = [] } = {}) {
|
||||
return createWorkspaceResolver({
|
||||
authMeUrl: 'http://auth.test/api/auth/me', clientMeUrl: 'http://client.test/api/me',
|
||||
fetchImpl: async (url, options) => {
|
||||
captures.push({ url, options });
|
||||
if (url.includes('auth.test')) return new Response(JSON.stringify({ user: { id: 'fixture-user', role, isActive: active } }), { status });
|
||||
return Response.json({ success: true, tenant, role: 'manager', perms: { products } });
|
||||
}
|
||||
});
|
||||
}
|
||||
const request = (headers = {}, url = '/api/get-synced-stores') => ({ headers: { cookie: 'aura_access=fixture-token', ...headers }, url });
|
||||
|
||||
test('uses canonical server tenant, not client-supplied IDs or requested tenant', async () => {
|
||||
const captures = [];
|
||||
const ws = await resolver({ captures })(request({ 'x-aura-tenant': 'another-tenant', 'x-client-id': 'victim' }, '/api/get-synced-stores?clientId=victim'));
|
||||
assert.equal(ws.tenant, 'verified-tenant');
|
||||
assert.match(ws.namespace, /^[a-f0-9]{64}$/);
|
||||
assert.equal(captures[1].options.headers['x-client-id'], undefined);
|
||||
assert.equal(captures[0].options.redirect, 'error');
|
||||
});
|
||||
|
||||
test('missing, ambiguous and inactive login fails before reading workspace', async () => {
|
||||
const captures = [], resolve = resolver({ captures });
|
||||
await assert.rejects(resolve({ headers: {}, url: '/' }), { status: 401 });
|
||||
await assert.rejects(resolve(request({ cookie: 'aura_access=a; aura_access=b' })), { status: 400 });
|
||||
assert.equal(captures.length, 0);
|
||||
const inactiveCalls = [];
|
||||
await assert.rejects(resolver({ active: false, captures: inactiveCalls })(request()), { status: 401 });
|
||||
assert.equal(inactiveCalls.length, 1);
|
||||
});
|
||||
|
||||
test('platform owner must select workspace explicitly, without a merchant fallback', async () => {
|
||||
await assert.rejects(resolver({ role: 'owner' })(request()), { code: 'WORKSPACE_REQUIRED' });
|
||||
const ws = await resolver({ role: 'owner', tenant: 'selected-store' })(request({}, '/api/get-synced-stores?tenant=selected-store'));
|
||||
assert.equal(ws.tenant, 'selected-store');
|
||||
});
|
||||
|
||||
test('identity service failures fail closed and catalog permissions come from Client', async () => {
|
||||
await assert.rejects(resolver({ status: 500 })(request()), { status: 503 });
|
||||
const ws = await resolver({ products: false })(request());
|
||||
assert.throws(() => requireProductWrite(ws), { status: 403 });
|
||||
requireProductWrite({ ...ws, canWriteProducts: true });
|
||||
});
|
||||
|
||||
test('owner and staff resolved to the same tenant share data; other tenant is isolated', async t => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'studio-store-'));
|
||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||
const stores = createStore(root);
|
||||
const owner = await resolver({ role: 'owner', tenant: 'shop-a' })(request({ 'x-aura-tenant': 'shop-a' }));
|
||||
const staff = await resolver({ tenant: 'shop-a' })(request());
|
||||
const other = await resolver({ tenant: 'shop-b' })(request());
|
||||
await stores.upsert(owner, { id: 'same-store-id', products: [{ rawPrice: 0 }] });
|
||||
assert.equal((await stores.read(staff))[0].products[0].rawPrice, 0);
|
||||
assert.deepEqual(await stores.read(other), []);
|
||||
await stores.remove(other, 'same-store-id');
|
||||
assert.equal((await stores.read(owner)).length, 1);
|
||||
});
|
||||
|
||||
test('concurrent imports do not overwrite each other and survive a new store instance', async t => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'studio-store-'));
|
||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||
const stores = createStore(root), ws = await resolver()(request());
|
||||
await Promise.all(Array.from({ length: 12 }, (_, i) => stores.upsert(ws, { id: 'store-' + i, products: [] })));
|
||||
assert.equal((await createStore(root).read(ws)).length, 12);
|
||||
});
|
||||
|
||||
test('corrupt saved data is not silently erased during the next import', async t => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'studio-store-'));
|
||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||
const stores = createStore(root), ws = await resolver()(request());
|
||||
await stores.read(ws);
|
||||
const file = path.join(root, ws.namespace, 'stores.json');
|
||||
await fs.writeFile(file, '{broken data');
|
||||
await assert.rejects(stores.upsert(ws, { id: 'next-store', products: [] }));
|
||||
assert.equal(await fs.readFile(file, 'utf8'), '{broken data');
|
||||
});
|
||||
|
||||
test('untrusted namespaces cannot become file paths', async t => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'studio-store-'));
|
||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||
await assert.rejects(createStore(root).read({ namespace: '../other-client' }), /namespace/);
|
||||
});
|
||||
Reference in New Issue
Block a user