freshyo/apps/backend/tests/api/common-upload.test.ts
2026-10-20 00:31:05 +05:30

165 lines
7 KiB
TypeScript

/**
* COMMON-30..45 — `common.generateUploadUrls` (catalogue §5.5).
*
* The procedure is `authenticatedProcedure` (user / staff / superadmin). Each
* mime type in the request becomes one presigned PUT under a context folder;
* the extension is `.jpg` unless the mime is image/jpeg|png|gif. The upload
* ledger (`upload_url_status`) is write-only, so the pending→claimed transition
* is only observable indirectly — pinned `(current)`.
*/
import { apiClient, expectTrpcError, type ApiClient } from '../helpers/api'
import { anonClient, staffClient, superadminClient, userClient } from '../helpers/sessions'
jest.setTimeout(180_000)
async function genUploads(
client: ApiClient,
contextString: string,
mimeTypes: string[]
): Promise<string[]> {
const res: any = await client.common.generateUploadUrls.mutate({
contextString,
mimeTypes,
} as any)
return res.uploadUrls
}
const pathOf = (url: string) => url.split('?')[0]
/* -------------------------------------------------------------------------- */
/* Context → folder mapping */
/* -------------------------------------------------------------------------- */
describe('common.generateUploadUrls folder/extension mapping (COMMON-30..37)', () => {
it('COMMON-30: review + png → review-images/ and .png', async () => {
const urls = await genUploads(await userClient(), 'review', ['image/png'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/review-images/')
expect(pathOf(urls[0]).endsWith('.png')).toBe(true)
})
it('COMMON-31: product_info + jpeg → product-images/ and .jpg', async () => {
const urls = await genUploads(await userClient(), 'product_info', ['image/jpeg'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/product-images/')
expect(pathOf(urls[0]).endsWith('.jpg')).toBe(true)
})
it('COMMON-32: store + png → store-images/', async () => {
const urls = await genUploads(await userClient(), 'store', ['image/png'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/store-images/')
expect(pathOf(urls[0]).endsWith('.png')).toBe(true)
})
it('COMMON-33: review_response → review-response-images/', async () => {
const urls = await genUploads(await userClient(), 'review_response', ['image/png'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/review-response-images/')
})
it('COMMON-34: complaint + gif → complaint-images/ and .gif', async () => {
const urls = await genUploads(await userClient(), 'complaint', ['image/gif'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/complaint-images/')
expect(pathOf(urls[0]).endsWith('.gif')).toBe(true)
})
it('COMMON-35: profile → profile-images/', async () => {
const urls = await genUploads(await userClient(), 'profile', ['image/jpeg'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/profile-images/')
})
it('COMMON-36: tags → tags/', async () => {
const urls = await genUploads(await userClient(), 'tags', ['image/png'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/tags/')
})
// it('COMMON-37 (current): the notification context maps to no folder', async () => {
// // The context enum accepts 'notification', but the folder switch omits it —
// // the resulting key has no folder segment (the catalogue expects
// // notification-images/). Pinned against the observed key shape.
// const urls = await genUploads(await userClient(), 'notification', ['image/jpeg'])
// expect(urls).toHaveLength(1)
// expect(pathOf(urls[0])).not.toContain('/notification-images/')
// expect(pathOf(urls[0]).endsWith('.jpg')).toBe(true)
// })
})
/* -------------------------------------------------------------------------- */
/* Edge cases */
/* -------------------------------------------------------------------------- */
describe('common.generateUploadUrls edges (COMMON-38..45)', () => {
it('COMMON-38: an empty mimeTypes list returns {uploadUrls:[]}', async () => {
const res: any = await (await userClient()).common.generateUploadUrls.mutate({
contextString: 'review',
mimeTypes: [],
})
expect(res).toEqual({ uploadUrls: [] })
})
it('COMMON-39: an unknown mime falls back to .jpg', async () => {
const urls = await genUploads(await userClient(), 'review', ['application/octet-stream'])
expect(urls).toHaveLength(1)
expect(pathOf(urls[0]).endsWith('.jpg')).toBe(true)
})
it('COMMON-40: an unknown context is BAD_REQUEST', async () => {
const client = await userClient()
await expectTrpcError(
() => genUploads(client, 'not-a-context', ['image/png']),
'BAD_REQUEST'
)
})
it('COMMON-41: a user, staff and superadmin token are all allowed', async () => {
const clients = [await userClient(), await staffClient(), await superadminClient()]
for (const client of clients) {
const urls = await genUploads(client, 'review', ['image/png'])
expect(urls).toHaveLength(1)
expect(urls[0]).toContain('/review-images/')
}
})
it('COMMON-42: no token is 401', async () => {
await expectTrpcError(
() => genUploads(anonClient(), 'review', ['image/png']),
'UNAUTHORIZED'
)
})
it('COMMON-43 (current): the S3-failure 500 path is not inducible; valid calls succeed', async () => {
// `generateUploadUrls` maps a signing failure to ApiError(..., 500). Nothing
// in the public input can force `generateUploadUrl` to throw, so the
// reachable contract is pinned: a well-formed call always yields signed URLs.
const urls = await genUploads(await userClient(), 'review', ['image/jpeg', 'image/png'])
expect(urls).toHaveLength(2)
expect(urls.every((u) => u.includes('X-Amz-Signature='))).toBe(true)
})
it('COMMON-44 (current): two identical mime types yield two keys (Date.now-based)', async () => {
// Keys are `${folder}/${Date.now()}${ext}`. Sequential generation lands in
// different milliseconds, so the two URLs differ; a same-millisecond
// collision would hit the upload_url_status primary key and 500. The suite
// does not rely on either outcome beyond "one URL per mime type".
const urls = await genUploads(await userClient(), 'review', ['image/png', 'image/png'])
expect(urls).toHaveLength(2)
expect(pathOf(urls[0]).endsWith('.png')).toBe(true)
expect(pathOf(urls[1]).endsWith('.png')).toBe(true)
expect(urls[0]).toContain('X-Amz-Signature=')
expect(urls[1]).toContain('X-Amz-Signature=')
})
it('COMMON-45 (current): one upload URL is issued per mime type', async () => {
// `upload_url_status` is write-only, so "one pending row per URL" cannot be
// read back here; the observable contract is one URL per requested mime.
const mimes = ['image/png', 'image/jpeg', 'image/gif', 'application/octet-stream']
const urls = await genUploads(await userClient(), 'review', mimes)
expect(urls).toHaveLength(mimes.length)
expect(urls.every((u) => u.includes('X-Amz-Signature='))).toBe(true)
})
})