/** * UPL-01..28 — Upload/S3 suite (catalogue §9). * * Live HTTP/tRPC suite against the running backend. Each `it()` maps to one * catalogue row. The presigned-URL ledger (`upload_url_status`) is write-only, * so the pending→claimed transition is only observable indirectly: a key is * claimable while pending and rejected once claimed. Rows that pin current (not * intended) behaviour are prefixed `(current)` with the observed contract. * * Object reads go through the public assets domain. That domain is CDN-cached, * so a "deleted" object is asserted with a cache-busting query param (which * bypasses the CDN and hits R2 directly); a plain GET may still return a cached * body after a delete. */ import { createHash } from 'node:crypto' import { apiClient, expectTrpcError, uniqueSuffix, type ApiClient } from '../helpers/api' import { staffClient } from '../helpers/sessions' import { createProduct, createStore, registerThrowawayUser } from '../helpers/fixtures' import { getEssentialConsts } from '../helpers/cache' jest.setTimeout(180_000) const pathOf = (url: string) => url.split('?')[0] const keyOf = (url: string) => decodeURIComponent(pathOf(url).split('/').filter(Boolean).slice(-2).join('/')) const sha256 = (buf: Uint8Array) => createHash('sha256').update(buf).digest('hex') async function genUploads( client: ApiClient, contextString: string, mimeTypes: string[] ): Promise { const res: any = await client.common.generateUploadUrls.mutate({ contextString, mimeTypes, } as any) return res.uploadUrls } async function put(url: string, body: string | Buffer, contentType?: string): Promise { return fetch(url, { method: 'PUT', ...(contentType ? { headers: { 'content-type': contentType } } : {}), body: body as any, }) } /** GET the public asset URL. `bust` bypasses the CDN (used after a delete). */ async function publicGet(assetsDomain: string, key: string, bust = false): Promise { const suffix = bust ? `?bust=${uniqueSuffix()}` : '' return fetch(`${assetsDomain}${key}${suffix}`, bust ? { cache: 'no-store' } : {}) } function skuInput(overrides: Record = {}): any { const name = overrides.name ?? '1pc' return { name, price: overrides.price ?? 199, marketPrice: overrides.marketPrice === undefined ? 249 : overrides.marketPrice, images: overrides.images ?? [], isFlashAvailable: overrides.isFlashAvailable ?? false, flashPrice: overrides.flashPrice ?? null, isOutOfStock: overrides.isOutOfStock ?? false, isOffer: false, isComboOnly: false, isDeleted: false, isSuspended: overrides.isSuspended ?? false, features: overrides.features ?? [{ featureName: 'quantity', featureValue: name }], ...(overrides.id !== undefined ? { id: overrides.id } : {}), } } async function createWithImages( admin: ApiClient, storeId: number, name: string, images: string[] ): Promise { return admin.admin.product.createProduct.mutate({ name, shortDescription: 'upload probe', storeId, productType: 'item', skus: [skuInput({ images })], } as any) } /* -------------------------------------------------------------------------- */ /* Presign + transfer (UPL-01..10) */ /* -------------------------------------------------------------------------- */ describe('uploads: presign + transfer (UPL-01..10)', () => { it('UPL-01: a presigned product image round-trips bytes (public GET bytes + sha256)', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const [url] = await genUploads(admin, 'product_info', ['image/png']) const key = keyOf(url) const body = Buffer.from(`upl-01-${uniqueSuffix()}`) const putRes = await put(url, body, 'image/png') expect(putRes.status).toBe(200) const get = await publicGet(assetsDomain, key) expect(get.status).toBe(200) const got = Buffer.from(await get.arrayBuffer()) expect(got.equals(body)).toBe(true) expect(sha256(got)).toBe(sha256(body)) }) it('UPL-02: two folders generated in separate calls produce distinct keys/objects', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const [reviewUrl] = await genUploads(admin, 'review', ['image/png']) const [productUrl] = await genUploads(admin, 'product_info', ['image/png']) expect(reviewUrl).toContain('/review-images/') expect(productUrl).toContain('/product-images/') const reviewKey = keyOf(reviewUrl) const productKey = keyOf(productUrl) expect(reviewKey).not.toBe(productKey) await put(reviewUrl, 'review-body') await put(productUrl, 'product-body') expect(await (await publicGet(assetsDomain, reviewKey)).text()).toBe('review-body') expect(await (await publicGet(assetsDomain, productKey)).text()).toBe('product-body') }) it('UPL-03: png and jpeg in one call each get their own extension', async () => { const urls = await genUploads(await staffClient(), 'review', ['image/png', 'image/jpeg']) expect(urls).toHaveLength(2) expect(pathOf(urls[0]).endsWith('.png')).toBe(true) expect(pathOf(urls[1]).endsWith('.jpg')).toBe(true) }) it('UPL-04: an unknown mime type falls back to .jpg', async () => { const urls = await genUploads(await staffClient(), 'review', ['application/octet-stream']) expect(urls).toHaveLength(1) expect(pathOf(urls[0]).endsWith('.jpg')).toBe(true) }) it('UPL-05: an empty mimeTypes list returns no URLs', async () => { const res: any = await (await staffClient()).common.generateUploadUrls.mutate({ contextString: 'review', mimeTypes: [], }) expect(res.uploadUrls).toEqual([]) }) it('UPL-06: the presigned URL carries its signature and PUTs without R2 credentials', async () => { const admin = await staffClient() const [url] = await genUploads(admin, 'review', ['image/png']) expect(url).toContain('X-Amz-Signature=') expect(url).toContain('X-Amz-Credential=') // No Authorization header / R2 credentials: the query signature authorises it. const signedPut = await fetch(url, { method: 'PUT', body: 'signed-body' }) expect(signedPut.status).toBe(200) // The same object URL without the signed query is not writable. const unsignedPut = await fetch(pathOf(url), { method: 'PUT', body: 'unsigned-body' }) expect(unsignedPut.ok).toBe(false) expect(unsignedPut.status).toBeGreaterThanOrEqual(400) }) it('UPL-07 (current): Content-Type is unsigned, so a different type still PUTs', async () => { // `generateUploadUrl` signs only the query, never the Content-Type header, so // the presigned PUT accepts any content type — the catalogue's "current". const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const [url] = await genUploads(admin, 'review', ['image/png']) const key = keyOf(url) const putRes = await fetch(url, { method: 'PUT', headers: { 'content-type': 'text/plain' }, body: 'mislabeled-body', }) expect(putRes.status).toBe(200) expect(await (await publicGet(assetsDomain, key)).text()).toBe('mislabeled-body') }) it('UPL-08 (current): the presigned URL advertises a 180s expiry', async () => { // The catalogue asks for "after 180s PUT fails"; waiting 180s is not done in // the suite. The pinned, observable contract is the signed expiry value. const [url] = await genUploads(await staffClient(), 'review', ['image/png']) const expires = new URL(url).searchParams.get('X-Amz-Expires') expect(expires).toBe('180') }) it('UPL-09: an object that was never uploaded is a 404', async () => { const { assetsDomain } = await getEssentialConsts() const [url] = await genUploads(await staffClient(), 'review', ['image/png']) const key = keyOf(url) expect((await publicGet(assetsDomain, key)).status).toBe(404) }) it('UPL-10 (current): re-uploading the same key overwrites the object', async () => { // A presigned URL is valid for the whole expiry window, so a second PUT to the // same key overwrites the first (no server-side uniqueness guard). const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const [url] = await genUploads(admin, 'review', ['image/png']) const key = keyOf(url) await put(url, 'first-body') await put(url, 'second-body') expect(await (await publicGet(assetsDomain, key)).text()).toBe('second-body') }) }) /* -------------------------------------------------------------------------- */ /* Claim ledger + record writes (UPL-11..23) */ /* -------------------------------------------------------------------------- */ describe('uploads: claim ledger + record writes (UPL-11..18, 21..23)', () => { it('UPL-11 (current): a presign records a pending key that is claimable once', async () => { // `upload_url_status` is write-only: "a pending row exists per presign" is // observable only through a successful first claim. const admin = await staffClient() const store = await createStore() const [url] = await genUploads(admin, 'product_info', ['image/png']) const res: any = await createWithImages( admin, store.storeId, `E2E UPL pending ${uniqueSuffix()}`, [url] ) expect(res.message).toBe('Product created successfully') expect(Number(res.product.skus[0].id)).toBeGreaterThan(0) }) it('UPL-12: the first claim of a presigned key succeeds', async () => { const admin = await staffClient() const store = await createStore() const [url] = await genUploads(admin, 'product_info', ['image/png']) const res: any = await createWithImages( admin, store.storeId, `E2E UPL claim ${uniqueSuffix()}`, [url] ) expect(res.message).toBe('Product created successfully') }) it('UPL-13 (current): an unknown key is rejected with the claim-guard message', async () => { // The catalogue expects "Failed to claim upload URL"; the reachable contract // is the pre-write guard: ApiError 400 "were not uploaded through the app". const admin = await staffClient() const store = await createStore() await expectTrpcError( () => createWithImages(admin, store.storeId, `E2E UPL unknown ${uniqueSuffix()}`, [ `product-images/never-${uniqueSuffix()}.png`, ]), 'BAD_REQUEST', 'not uploaded through the app' ) }) it('UPL-14: re-using an already-claimed key is rejected', async () => { const admin = await staffClient() const store = await createStore() const [url] = await genUploads(admin, 'product_info', ['image/png']) await createWithImages(admin, store.storeId, `E2E UPL owner ${uniqueSuffix()}`, [url]) await expectTrpcError( () => createWithImages(admin, store.storeId, `E2E UPL reused ${uniqueSuffix()}`, [url]), 'BAD_REQUEST', 'not uploaded through the app' ) }) it('UPL-15: createProduct stores a claimed image as its asset URL', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const store = await createStore() const [url] = await genUploads(admin, 'product_info', ['image/png']) const key = keyOf(url) const res: any = await createWithImages( admin, store.storeId, `E2E UPL stored ${uniqueSuffix()}`, [url] ) const read: any = await admin.admin.product.getProductById.query({ id: Number(res.product.id), }) expect(read.product.skus[0].imageKeys).toEqual([key]) expect(read.product.skus[0].images).toEqual([`${assetsDomain}${key}`]) }) it('UPL-16: a fake image is rejected before any product row is written', async () => { const admin = await staffClient() const store = await createStore() const name = `E2E UPL fake ${uniqueSuffix()}` await expectTrpcError( () => createWithImages(admin, store.storeId, name, [ `product-images/fake-${uniqueSuffix()}.png`, ]), 'BAD_REQUEST', 'not uploaded through the app' ) const list: any = await admin.admin.product.getProducts.query() expect(list.products.some((p: any) => p.name === name)).toBe(false) }) it('UPL-17: updateProduct stores a newly claimed image', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const store = await createStore() const created: any = await admin.admin.product.createProduct.mutate({ name: `E2E UPL update ${uniqueSuffix()}`, shortDescription: 'upload probe', storeId: store.storeId, productType: 'item', skus: [skuInput()], } as any) const productId = Number(created.product.id) const skuId = Number(created.product.skus[0].id) const [url] = await genUploads(admin, 'product_info', ['image/png']) const key = keyOf(url) const res: any = await admin.admin.product.updateProduct.mutate({ id: productId, name: created.product.name, storeId: store.storeId, skus: [skuInput({ id: skuId, images: [url] })], } as any) expect(res.message).toBe('Product updated successfully') const read: any = await admin.admin.product.getProductById.query({ id: productId }) expect(read.product.skus[0].imageKeys).toEqual([key]) expect(read.product.skus[0].images).toEqual([`${assetsDomain}${key}`]) }) it("UPL-18: updateProduct rejects another product's already-claimed image", async () => { const admin = await staffClient() const store = await createStore() const [url] = await genUploads(admin, 'product_info', ['image/png']) // Claim it on a first product. await createWithImages(admin, store.storeId, `E2E UPL ownerB ${uniqueSuffix()}`, [url]) const target: any = await admin.admin.product.createProduct.mutate({ name: `E2E UPL target ${uniqueSuffix()}`, shortDescription: 'upload probe', storeId: store.storeId, productType: 'item', skus: [skuInput()], } as any) const productId = Number(target.product.id) const skuId = Number(target.product.skus[0].id) await expectTrpcError( () => admin.admin.product.updateProduct.mutate({ id: productId, name: target.product.name, storeId: store.storeId, skus: [skuInput({ id: skuId, images: [url] })], } as any), 'BAD_REQUEST', 'not uploaded through the app' ) const read: any = await admin.admin.product.getProductById.query({ id: productId }) expect(read.product.skus[0].imageKeys).toEqual([]) }) it('UPL-19: deleteImageUtil removes the object (public GET 404)', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const store = await createStore() const [url] = await genUploads(admin, 'product_info', ['image/png']) const key = keyOf(url) await put(url, 'delete-me') const created: any = await createWithImages( admin, store.storeId, `E2E UPL delete ${uniqueSuffix()}`, [url] ) expect((await publicGet(assetsDomain, key)).status).toBe(200) await admin.admin.product.updateProduct.mutate({ id: Number(created.product.id), name: created.product.name, storeId: store.storeId, skus: [skuInput({ id: Number(created.product.skus[0].id), images: [] })], deletedImageKeys: [key], } as any) // Bypass the CDN so the R2 object state is read. expect((await publicGet(assetsDomain, key, true)).status).toBe(404) }) it('UPL-20: replacing a tag image claims the new key and deletes the old object', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const [oldUrl] = await genUploads(admin, 'tags', ['image/png']) const oldKey = keyOf(oldUrl) await put(oldUrl, 'tag-old') const created: any = await admin.admin.product.createProductTag.mutate({ tagName: `E2E UPL tag ${uniqueSuffix()}`, imageUrl: oldUrl, uploadUrls: [oldUrl], } as any) expect(created.tag.imageUrl).toBe(`${assetsDomain}${oldKey}`) expect((await publicGet(assetsDomain, oldKey)).status).toBe(200) const [newUrl] = await genUploads(admin, 'tags', ['image/png']) const newKey = keyOf(newUrl) await put(newUrl, 'tag-new') const updated: any = await admin.admin.product.updateProductTag.mutate({ id: created.tag.id, imageUrl: newUrl, uploadUrls: [newUrl], } as any) expect(updated.tag.imageUrl).toBe(`${assetsDomain}${newKey}`) expect((await publicGet(assetsDomain, oldKey, true)).status).toBe(404) expect((await publicGet(assetsDomain, newKey)).status).toBe(200) }) it('UPL-21: respondToReview validates and claims review-response images', async () => { const admin = await staffClient() const store = await createStore() const product = await createProduct({ storeId: store.storeId }) const user = await registerThrowawayUser() const review: any = await apiClient(user.token).user.product.createReview.mutate({ productId: product.skuId, reviewBody: 'upl-21 review', ratings: 5, }) const [url] = await genUploads(admin, 'review_response', ['image/png']) const ok: any = await admin.admin.product.respondToReview.mutate({ reviewId: review.review.id, adminResponse: 'thanks', uploadUrls: [url], }) expect(ok.success).toBe(true) await expectTrpcError( () => admin.admin.product.respondToReview.mutate({ reviewId: review.review.id, adminResponse: 'again', uploadUrls: [url], }), 'BAD_REQUEST', 'not uploaded through the app' ) }) it('UPL-22: an invalid review image URL is lenient — the review is still created', async () => { const store = await createStore() const product = await createProduct({ storeId: store.storeId }) const user = await registerThrowawayUser() const res: any = await apiClient(user.token).user.product.createReview.mutate({ productId: product.skuId, reviewBody: 'lenient upload contract', ratings: 4, uploadUrls: [`review-images/unknown-${uniqueSuffix()}.png`], }) expect(res.success).toBe(true) expect(Number(res.review.id)).toBeGreaterThan(0) }) it('UPL-23: replacing a store image deletes the old object', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const [oldUrl] = await genUploads(admin, 'store', ['image/png']) const oldKey = keyOf(oldUrl) await put(oldUrl, 'store-old') const created: any = await admin.admin.store.createStore.mutate({ name: `E2E UPL store ${uniqueSuffix()}`, description: 'upload probe', imageUrl: oldUrl, } as any) const storeId = Number(created.store.id) expect((await publicGet(assetsDomain, oldKey)).status).toBe(200) const [newUrl] = await genUploads(admin, 'store', ['image/png']) const newKey = keyOf(newUrl) await put(newUrl, 'store-new') await admin.admin.store.updateStore.mutate({ id: storeId, name: `E2E UPL store updated ${uniqueSuffix()}`, imageUrl: newUrl, } as any) const read: any = await admin.admin.store.getStoreById.query({ id: storeId }) expect(read.store.imageUrl).toBe(`${assetsDomain}${newKey}`) expect((await publicGet(assetsDomain, oldKey, true)).status).toBe(404) }) }) /* -------------------------------------------------------------------------- */ /* Edges (UPL-24..28) */ /* -------------------------------------------------------------------------- */ describe('uploads: edges (UPL-24..28)', () => { it('UPL-24 (current): two identical mimes in one call each get a URL (collision not forced)', async () => { // Keys are `${folder}/${Date.now()}${ext}`. A same-millisecond collision would // violate upload_url_status' primary key and 500; the suite does not depend on // that timing. The pinned contract is one signed URL per requested mime. const urls = await genUploads(await staffClient(), 'review', ['image/png', 'image/png']) expect(urls).toHaveLength(2) expect(urls.every((u) => u.includes('X-Amz-Signature='))).toBe(true) }) it('UPL-25 (current): the presign-failure 500 path is not inducible; valid calls succeed', async () => { // Nothing in the public input can force `generateUploadUrl` to throw, so the // mapped ApiError 500 (and any partial upload_url_status row) is unreachable. // A malformed context is a schema 400, not the S3-failure 500. const urls = await genUploads(await staffClient(), 'review', ['image/png']) expect(urls).toHaveLength(1) expect(urls[0]).toContain('X-Amz-Signature=') const client = await staffClient() await expectTrpcError( () => genUploads(client, 'not-a-context', ['image/png']), 'BAD_REQUEST' ) }) it('UPL-26: the object holds the supplied bytes, not a validated image', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const [url] = await genUploads(admin, 'review', ['image/png']) const key = keyOf(url) const payload = `not-an-image-${uniqueSuffix()}` await put(url, payload, 'image/png') expect(await (await publicGet(assetsDomain, key)).text()).toBe(payload) }) it('UPL-27 (current): a traversal image reference is collapsed to a safe key', async () => { // `extractKeyFromPresignedUrl` keeps the last two path segments, so // `../../etc/passwd.png` collapses to `etc/passwd.png` with no `..` segment. const admin = await staffClient() const store: any = await admin.admin.store.createStore.mutate({ name: `E2E UPL traversal ${uniqueSuffix()}`, description: 'upload probe', imageUrl: 'https://example.com/store-images/../../etc/passwd.png', } as any) const read: any = await admin.admin.store.getStoreById.query({ id: Number(store.store.id) }) expect(read.store.imageUrl).not.toContain('..') expect(read.store.imageUrl.endsWith('/etc/passwd.png')).toBe(true) }) it('UPL-28: scaffoldAssetUrl turns a stored key into the public asset URL', async () => { const admin = await staffClient() const { assetsDomain } = await getEssentialConsts() const key = `tags/e2e-${uniqueSuffix()}.png` const created: any = await admin.admin.product.createProductTag.mutate({ tagName: `E2E UPL asset ${uniqueSuffix()}`, imageUrl: key, } as any) expect(created.tag.imageUrl).toBe(`${assetsDomain}${key}`) const read: any = await admin.admin.product.getProductTagById.query({ id: created.tag.id, }) expect(read.tag.imageUrl).toBe(`${assetsDomain}${key}`) }) })