/** * Guards around placeOrder. Documents the backend's actual behaviour, including * the known gap that a non-existent slot only fails via the FK. */ import { apiClient } from '../helpers/api' import { createFutureSlot, createStoreWithSku, ensureUserAddress } from '../helpers/fixtures' import { userSession } from '../helpers/sessions' /** Run a call that must reject and hand back the error. */ async function captureError(fn: () => Promise): Promise { let error: Error | null = null try { await fn() } catch (caught) { error = caught as Error } if (!error) { throw new Error('[api-tests] expected the call to reject, but it resolved') } return error } describe('placeOrder guards', () => { let userToken = '' let skuId = 0 let slotId = 0 let addressId = 0 beforeAll(async () => { const store = await createStoreWithSku() const slot = await createFutureSlot([store.skuId]) const address = await ensureUserAddress() const user = await userSession() userToken = user.token skuId = store.skuId slotId = slot.slotId addressId = address.addressId }) it('rejects an unauthenticated request', async () => { const error = await captureError(() => apiClient().user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity: 1, slotId }], addressId, paymentMethod: 'cod', }) ) expect(error.message).toMatch(/UNAUTHORIZED|unauthorized|Authentication/i) }) it('rejects an address that is not the user\'s', async () => { const error = await captureError(() => apiClient(userToken).user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity: 1, slotId }], addressId: 99_999_999, paymentMethod: 'cod', }) ) expect(error.message).toMatch(/Invalid address/i) }) it('rejects an unknown skuId', async () => { const error = await captureError(() => apiClient(userToken).user.order.placeOrder.mutate({ selectedItems: [{ skuId: 99_999_999, quantity: 1, slotId }], addressId, paymentMethod: 'cod', }) ) expect(error.message).toMatch(/not found/i) }) it('accepts a non-existent slotId (known gap: no slot validity check)', async () => { // placeOrder only checks slot *capacity*; there is no FK/validity check on // the slot, so a bogus id is stored verbatim. This test documents that // behaviour — if the backend adds validation, update this case. const bogusSlotId = 99_999_999 const res = await apiClient(userToken).user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity: 1, slotId: bogusSlotId }], addressId, paymentMethod: 'cod', }) expect(res.success).toBe(true) expect(res.data[0].slotId).toBe(bogusSlotId) }) })