81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
/**
|
|
* Authorization boundaries across the two token kinds, plus cross-user isolation.
|
|
*
|
|
* tRPC `protectedProcedure` only requires *some* identity (`ctx.user` OR
|
|
* `ctx.staffUser`), so procedures that don't additionally check `ctx.staffUser`
|
|
* are reachable with a user token. Case 4 documents that gap.
|
|
*/
|
|
|
|
import { apiClient, captureError } from '../helpers/api'
|
|
import {
|
|
createFutureSlot,
|
|
createStoreWithSku,
|
|
ensureUserAddress,
|
|
registerThrowawayUser,
|
|
} from '../helpers/fixtures'
|
|
import { staffSession, userSession } from '../helpers/sessions'
|
|
|
|
describe('authorization boundaries', () => {
|
|
let ownerToken = ''
|
|
let ownerAddressId = 0
|
|
|
|
beforeAll(async () => {
|
|
const store = await createStoreWithSku()
|
|
await createFutureSlot([store.skuId])
|
|
const address = await ensureUserAddress()
|
|
const user = await userSession()
|
|
|
|
ownerToken = user.token
|
|
ownerAddressId = address.addressId
|
|
})
|
|
|
|
it('rejects anonymous requests', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient().user.order.getOrders.query({ page: 1, pageSize: 5 })
|
|
)
|
|
|
|
expect(error.message).toMatch(/UNAUTHORIZED|unauthorized/i)
|
|
})
|
|
|
|
it('a staff token cannot act as a user (no ctx.user)', async () => {
|
|
const staff = await staffSession()
|
|
|
|
const error = await captureError(() =>
|
|
apiClient(staff.token).user.order.getOrders.query({ page: 1, pageSize: 5 })
|
|
)
|
|
|
|
expect(error).toBeInstanceOf(Error)
|
|
})
|
|
|
|
it('a user token cannot use staff-guarded admin writes', async () => {
|
|
const deliveryTime = new Date(Date.now() + 3 * 3600_000).toISOString()
|
|
const freezeTime = new Date(Date.now() + 2 * 3600_000).toISOString()
|
|
|
|
const error = await captureError(() =>
|
|
apiClient(ownerToken).admin.slots.createSlot.mutate({
|
|
deliveryTime,
|
|
freezeTime,
|
|
isActive: true,
|
|
skuIds: [],
|
|
})
|
|
)
|
|
|
|
expect(error.message).toMatch(/UNAUTHORIZED|Access denied|unauthorized/i)
|
|
})
|
|
|
|
it('DOCUMENTS: a user token CAN read admin.product.getProducts (no staff guard)', async () => {
|
|
// admin.product.getProducts is a bare protectedProcedure — no ctx.staffUser
|
|
// check — so any authenticated user can read the full admin product list.
|
|
const products = await apiClient(ownerToken).admin.product.getProducts.query()
|
|
|
|
expect(Array.isArray(products.products)).toBe(true)
|
|
})
|
|
|
|
it("one user cannot see another user's addresses", async () => {
|
|
const other = await registerThrowawayUser()
|
|
|
|
const addresses = await apiClient(other.token).user.address.getUserAddresses.query()
|
|
|
|
expect(addresses.data.some((a) => a.id === ownerAddressId)).toBe(false)
|
|
})
|
|
})
|