133 lines
4.5 KiB
TypeScript
133 lines
4.5 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor, seedProduct } from './helpers/commerce';
|
|
|
|
const png = Buffer.concat([
|
|
Buffer.from('89504e470d0a1a0a', 'hex'),
|
|
Buffer.from('fake image body'),
|
|
]);
|
|
const webp = Buffer.concat([
|
|
Buffer.from('RIFF'),
|
|
Buffer.alloc(4),
|
|
Buffer.from('WEBPVP8 body'),
|
|
]);
|
|
|
|
describe('product images', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const upload = (
|
|
productId: string,
|
|
bytes = png,
|
|
contentType = 'image/png',
|
|
token = ctx.token,
|
|
) =>
|
|
ctx
|
|
.api()
|
|
.post(`/api/v1/products/${productId}/images`)
|
|
.auth(token, auth)
|
|
.attach('file', bytes, { filename: 'photo', contentType });
|
|
const storefront = (path: string) =>
|
|
ctx
|
|
.api()
|
|
.get(`/api/v1/storefront/${ctx.owner.organizationId}/products${path}`);
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('stores images and serves them publicly only for published products', async () => {
|
|
const { product } = await seedProduct(ctx);
|
|
const first = await upload(product.id).expect(201);
|
|
expect(first.body).toMatchObject({
|
|
mimeType: 'image/png',
|
|
size: png.length,
|
|
});
|
|
expect(first.body).not.toHaveProperty('content');
|
|
await upload(product.id, webp, 'image/webp').expect(201);
|
|
const path = `/${product.id}/images/${first.body.id}`;
|
|
await storefront(path).expect(404);
|
|
await ctx
|
|
.api()
|
|
.patch(`/api/v1/products/${product.id}/status`)
|
|
.auth(ctx.token, auth)
|
|
.send({ status: 'PUBLISHED' })
|
|
.expect(200);
|
|
const served = await storefront(path)
|
|
.buffer(true)
|
|
.parse((response, done) => {
|
|
const chunks: Buffer[] = [];
|
|
response.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
response.on('end', () => done(null, Buffer.concat(chunks)));
|
|
})
|
|
.expect(200);
|
|
expect(served.headers['content-type']).toBe('image/png');
|
|
expect(served.headers['cache-control']).toContain('public');
|
|
expect(Buffer.compare(served.body as Buffer, png)).toBe(0);
|
|
const detail = await storefront(`/${product.id}`).expect(200);
|
|
expect(detail.body.images).toHaveLength(2);
|
|
expect(detail.body.images[0]).toEqual({ id: first.body.id });
|
|
const list = await storefront('?limit=100').expect(200);
|
|
expect(
|
|
list.body.find((item: { id: string }) => item.id === product.id).images,
|
|
).toEqual([{ id: first.body.id }]);
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/storefront/${randomUUID()}/products${path}`)
|
|
.expect(404);
|
|
});
|
|
|
|
it('rejects disguised, oversized and over-quota uploads', async () => {
|
|
const { product } = await seedProduct(ctx);
|
|
for (const [bytes, type] of [
|
|
[Buffer.from('<svg onload=alert(1)>'), 'image/png'],
|
|
[png, 'image/jpeg'],
|
|
[Buffer.from('<svg/>'), 'image/svg+xml'],
|
|
] as const)
|
|
expect(
|
|
(await upload(product.id, bytes, type).expect(400)).body.code,
|
|
).toBe('PRODUCT_IMAGE_INVALID');
|
|
const big = Buffer.concat([png, Buffer.alloc(2 * 1024 * 1024)]);
|
|
expect((await upload(product.id, big)).status).toBeGreaterThanOrEqual(400);
|
|
for (let index = 0; index < 8; index++)
|
|
await upload(product.id).expect(201);
|
|
expect((await upload(product.id).expect(409)).body.code).toBe(
|
|
'PRODUCT_IMAGE_LIMIT',
|
|
);
|
|
});
|
|
|
|
it('requires catalog.manage and organization scope to add or remove', async () => {
|
|
const { product } = await seedProduct(ctx);
|
|
const image = await upload(product.id).expect(201);
|
|
const customer = await secondActor(ctx);
|
|
await upload(product.id, png, 'image/png', customer.token).expect(403);
|
|
const foreign = await secondActor(ctx, false, ['catalog.manage']);
|
|
await upload(product.id, png, 'image/png', foreign.token).expect(404);
|
|
await ctx
|
|
.api()
|
|
.delete(`/api/v1/products/${product.id}/images/${image.body.id}`)
|
|
.auth(foreign.token, auth)
|
|
.expect(404);
|
|
await ctx
|
|
.api()
|
|
.delete(`/api/v1/products/${product.id}/images/${image.body.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(204);
|
|
await ctx
|
|
.api()
|
|
.delete(`/api/v1/products/${product.id}/images/${image.body.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(404);
|
|
const admin = await ctx
|
|
.api()
|
|
.get(`/api/v1/products/${product.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(admin.body.images).toEqual([]);
|
|
});
|
|
});
|