211 lines
7.2 KiB
TypeScript
211 lines
7.2 KiB
TypeScript
/**
|
|
* ERR-01..10 — status/error contract (catalogue §11.5).
|
|
*
|
|
* `ApiError` statuses must surface as the matching tRPC/HTTP code; a raw `Error`
|
|
* must stay INTERNAL_SERVER_ERROR without leaking SQL; zod failures must be
|
|
* BAD_REQUEST naming the offending path.
|
|
*
|
|
* Rows pinned `(current)`:
|
|
* - ERR-08: `admin.order.getOrderDetails` throws a raw `Error('Order not found')`
|
|
* -> INTERNAL_SERVER_ERROR (500), not 404 (catalogue §13.3).
|
|
* - ERR-10: the logger is server-side only; the reachable evidence is that the
|
|
* response carries the procedure path and no SQL/secrets.
|
|
*/
|
|
|
|
import {
|
|
apiClient,
|
|
captureError,
|
|
errorCode,
|
|
errorHttpStatus,
|
|
errorMessage,
|
|
expectTrpcError,
|
|
rawQuery,
|
|
uniqueSuffix,
|
|
} from '../helpers/api'
|
|
import { staffClient, superadminClient } from '../helpers/sessions'
|
|
import { registerThrowawayUser } from '../helpers/fixtures'
|
|
import { TEST_REGION_ID } from '../helpers/env'
|
|
|
|
jest.setTimeout(180_000)
|
|
|
|
const SQL_LEAK = /SQLITE_|D1_ERROR|UNIQUE constraint failed|DrizzleError|Failed query|insert into|select .* from/i
|
|
|
|
function batch0(body: unknown): any {
|
|
return Array.isArray(body) ? (body as any[])[0] : (body as any)
|
|
}
|
|
|
|
function square(lat: number, lng: number) {
|
|
return {
|
|
type: 'FeatureCollection' as const,
|
|
features: [
|
|
{
|
|
type: 'Feature' as const,
|
|
properties: {},
|
|
geometry: {
|
|
type: 'Polygon',
|
|
coordinates: [
|
|
[
|
|
[lng - 0.01, lat - 0.01],
|
|
[lng + 0.01, lat - 0.01],
|
|
[lng + 0.01, lat + 0.01],
|
|
[lng - 0.01, lat + 0.01],
|
|
[lng - 0.01, lat - 0.01],
|
|
],
|
|
],
|
|
},
|
|
},
|
|
],
|
|
}
|
|
}
|
|
|
|
describe('error contract — ApiError statuses (ERR-01..06)', () => {
|
|
it('ERR-01: ApiError 400 becomes BAD_REQUEST / HTTP 400', async () => {
|
|
const admin = await staffClient()
|
|
const error = await expectTrpcError(
|
|
() => admin.admin.product.updateProductPrices.mutate({ updates: [] }),
|
|
'BAD_REQUEST',
|
|
'No updates'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(400)
|
|
})
|
|
|
|
it('ERR-02: ApiError 401 becomes UNAUTHORIZED / HTTP 401', async () => {
|
|
const error = await expectTrpcError(
|
|
() =>
|
|
apiClient().admin.staffUser.login.mutate({
|
|
name: 'e2e_staff',
|
|
password: `wrong-${uniqueSuffix()}`,
|
|
}),
|
|
'UNAUTHORIZED',
|
|
'Invalid credentials'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(401)
|
|
})
|
|
|
|
it('ERR-03: ApiError 403 becomes FORBIDDEN / HTTP 403', async () => {
|
|
const admin = await staffClient()
|
|
const user = await registerThrowawayUser()
|
|
await admin.admin.user.updateUserSuspension.mutate({ userId: user.userId, isSuspended: true })
|
|
try {
|
|
const error = await expectTrpcError(
|
|
() =>
|
|
apiClient(user.token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId: 1, quantity: 1, slotId: 1 }],
|
|
addressId: 1,
|
|
regionId: TEST_REGION_ID,
|
|
} as any),
|
|
'FORBIDDEN',
|
|
'Unable to place order'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(403)
|
|
} finally {
|
|
await admin.admin.user.updateUserSuspension.mutate({ userId: user.userId, isSuspended: false })
|
|
}
|
|
})
|
|
|
|
it('ERR-04: ApiError 404 becomes NOT_FOUND / HTTP 404', async () => {
|
|
const admin = await staffClient()
|
|
const error = await expectTrpcError(
|
|
() => admin.admin.product.getProductById.query({ id: 999_999_999 }),
|
|
'NOT_FOUND',
|
|
'Product not found'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(404)
|
|
})
|
|
|
|
it('ERR-05: ApiError 409 becomes CONFLICT / HTTP 409', async () => {
|
|
const sa = await superadminClient()
|
|
const error = await expectTrpcError(
|
|
() =>
|
|
sa.superadmin.regions.create.mutate({
|
|
name: `E2E ERR DupPriority ${uniqueSuffix()}`,
|
|
center: { lat: 15.95, lng: 77.95 },
|
|
bounds: [
|
|
{ description: 'a', geojson: square(15.95, 77.95), priority: '7' },
|
|
{ description: 'b', geojson: square(15.96, 77.96), priority: '7' },
|
|
],
|
|
} as any),
|
|
'CONFLICT'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(409)
|
|
})
|
|
|
|
it('ERR-06: ApiError 422 becomes UNPROCESSABLE_CONTENT / HTTP 422', async () => {
|
|
const user = await registerThrowawayUser()
|
|
const error = await expectTrpcError(
|
|
() =>
|
|
apiClient(user.token).user.address.createAddress.mutate({
|
|
regionId: TEST_REGION_ID,
|
|
name: 'Out of area',
|
|
phone: '9876543210',
|
|
addressLine1: `Out ${uniqueSuffix()}`,
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
latitude: 0,
|
|
longitude: 0,
|
|
} as any),
|
|
'UNPROCESSABLE_CONTENT',
|
|
'outside this region'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(422)
|
|
})
|
|
})
|
|
|
|
describe('error contract — raw errors and zod (ERR-07..09)', () => {
|
|
it('ERR-07: an unknown error is INTERNAL_SERVER_ERROR with no SQL details', async () => {
|
|
const admin = await staffClient()
|
|
const error = await expectTrpcError(
|
|
() =>
|
|
admin.admin.coupon.create.mutate({
|
|
couponCode: `ERRBOTH${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`,
|
|
skuIds: [],
|
|
isApplyForAll: true,
|
|
flatDiscount: 10,
|
|
discountPercent: 10,
|
|
} as any),
|
|
'INTERNAL_SERVER_ERROR'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(500)
|
|
expect(errorMessage(error)).not.toMatch(SQL_LEAK)
|
|
expect(JSON.stringify(error)).not.toMatch(SQL_LEAK)
|
|
})
|
|
|
|
it('ERR-08 (current): a raw `Error` stays a 500 instead of a 404', async () => {
|
|
const admin = await staffClient()
|
|
const error = await expectTrpcError(
|
|
() => admin.admin.order.getOrderDetails.query({ orderId: 999_999_999 }),
|
|
'INTERNAL_SERVER_ERROR',
|
|
'Order not found'
|
|
)
|
|
expect(errorHttpStatus(error)).toBe(500)
|
|
})
|
|
|
|
it('ERR-09: a zod failure is BAD_REQUEST and names the offending path', async () => {
|
|
const staff = await (await import('../helpers/sessions')).staffSession()
|
|
const res = await rawQuery('admin.slots.getSlotById', { id: 'abc' }, { token: staff.token })
|
|
const error = batch0(res.body)?.error
|
|
expect(error?.data?.code).toBe('BAD_REQUEST')
|
|
expect(error?.data?.httpStatus).toBe(400)
|
|
expect(error?.message).toMatch(/id/)
|
|
expect(error?.message).toMatch(/expected number/i)
|
|
})
|
|
})
|
|
|
|
describe('error contract — logging (ERR-10)', () => {
|
|
it('ERR-10 (current): the response carries the procedure path and no secrets', async () => {
|
|
// The logger itself writes to the server console and is not readable over
|
|
// HTTP; the reachable contract is that the error payload identifies the
|
|
// failing procedure (`path`) without exposing SQL, tokens or stacks-as-secrets.
|
|
const admin = await staffClient()
|
|
const error = await captureError(
|
|
() => admin.admin.order.getOrderDetails.query({ orderId: 999_999_998 })
|
|
)
|
|
const data = (error as any)?.data ?? (error as any)?.shape?.data ?? {}
|
|
expect(String(data.path ?? '')).toContain('admin.order.getOrderDetails')
|
|
expect(errorCode(error)).toBe('INTERNAL_SERVER_ERROR')
|
|
expect(errorMessage(error)).not.toMatch(SQL_LEAK)
|
|
expect(JSON.stringify(data)).not.toMatch(SQL_LEAK)
|
|
expect(JSON.stringify(data)).not.toMatch(/Authorization|Bearer |JWT_SECRET/i)
|
|
})
|
|
})
|