261 lines
8.7 KiB
TypeScript
261 lines
8.7 KiB
TypeScript
/**
|
|
* Authorization boundaries across the two token kinds.
|
|
*
|
|
* `protectedProcedure` only requires *some* identity (ctx.user OR ctx.staffUser),
|
|
* so every admin procedure that does not additionally check `ctx.staffUser` is
|
|
* reachable with a plain user token. The "pins: a user token CAN ..." tests
|
|
* document that gap; the staff-guarded ones assert the rejection.
|
|
*
|
|
* See tests/BUGS.md.
|
|
*/
|
|
|
|
import { apiClient, captureError, uniqueSuffix } from '../helpers/api'
|
|
import {
|
|
createFutureSlot,
|
|
createStoreWithSku,
|
|
registerThrowawayUser,
|
|
registerUserWithAddress,
|
|
} from '../helpers/fixtures'
|
|
import { staffSession } from '../helpers/sessions'
|
|
|
|
describe('authorization matrix', () => {
|
|
let admin: ReturnType<typeof apiClient>
|
|
let userToken = ''
|
|
let otherToken = ''
|
|
let addressId = 0
|
|
let skuId = 0
|
|
let slotId = 0
|
|
let orderId = 0
|
|
|
|
beforeAll(async () => {
|
|
const staff = await staffSession()
|
|
admin = apiClient(staff.token)
|
|
|
|
const store = await createStoreWithSku()
|
|
const slot = await createFutureSlot([store.skuId])
|
|
const owner = await registerUserWithAddress()
|
|
const other = await registerUserWithAddress()
|
|
|
|
userToken = owner.token
|
|
otherToken = other.token
|
|
addressId = owner.addressId
|
|
skuId = store.skuId
|
|
slotId = slot.slotId
|
|
|
|
const placed = await apiClient(userToken).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
paymentMethod: 'cod',
|
|
})
|
|
orderId = placed.data[0].id
|
|
})
|
|
|
|
describe('admin procedures guarded only by protectedProcedure', () => {
|
|
it('pins: a user token CAN cancel an order via admin.order.cancelOrder', async () => {
|
|
const res = await apiClient(userToken).admin.order.cancelOrder.mutate({
|
|
orderId,
|
|
reason: 'authorization matrix pin',
|
|
})
|
|
|
|
expect(res.success).toBe(true)
|
|
|
|
const detail = await apiClient(userToken).user.order.getOrderById.query({
|
|
orderId: String(orderId),
|
|
})
|
|
expect(String(detail.orderStatus).toLowerCase()).toMatch(/cancel/)
|
|
})
|
|
|
|
it('pins: a user token CAN suspend a user via admin.user.updateUserSuspension', async () => {
|
|
const victim = await registerThrowawayUser()
|
|
|
|
try {
|
|
const res = await apiClient(userToken).admin.user.updateUserSuspension.mutate({
|
|
userId: victim.userId,
|
|
isSuspended: true,
|
|
})
|
|
expect(res.success).toBe(true)
|
|
} finally {
|
|
await admin.admin.user.updateUserSuspension.mutate({
|
|
userId: victim.userId,
|
|
isSuspended: false,
|
|
})
|
|
}
|
|
})
|
|
|
|
it('pins: a user token CAN send a notification via admin.user.sendNotification', async () => {
|
|
const target = await registerThrowawayUser()
|
|
|
|
const res = await apiClient(userToken).admin.user.sendNotification.mutate({
|
|
userIds: [target.userId],
|
|
title: 'API test',
|
|
text: 'authorization matrix pin',
|
|
})
|
|
expect(res).toBeTruthy()
|
|
})
|
|
|
|
it('pins: a user token CAN toggle stock via admin.product.toggleOutOfStock', async () => {
|
|
const target = await createStoreWithSku()
|
|
|
|
try {
|
|
const res = await apiClient(userToken).admin.product.toggleOutOfStock.mutate({
|
|
id: target.productId,
|
|
})
|
|
expect(res.isOutOfStock).toBe(true)
|
|
} finally {
|
|
await admin.admin.product.toggleOutOfStock.mutate({ id: target.productId })
|
|
}
|
|
})
|
|
|
|
it('pins: a user token CAN create a staff account (admin.staffUser.createStaffUser)', async () => {
|
|
const roles = await apiClient(userToken).admin.staffUser.getRoles.query()
|
|
const roleId = roles.roles[0]?.id
|
|
expect(roleId).toBeGreaterThan(0)
|
|
|
|
const name = `matrix-staff-${uniqueSuffix()}`
|
|
|
|
const res = await apiClient(userToken).admin.staffUser.createStaffUser.mutate({
|
|
name,
|
|
password: 'Passw0rd!',
|
|
roleId,
|
|
})
|
|
expect(res).toBeTruthy()
|
|
|
|
// It really is a usable staff account.
|
|
const login = await apiClient().admin.staffUser.login.mutate({
|
|
name,
|
|
password: 'Passw0rd!',
|
|
})
|
|
expect(login.token).toBeTruthy()
|
|
})
|
|
})
|
|
|
|
describe('staff-guarded admin procedures', () => {
|
|
it('rejects a user token on admin.slots.createSlot', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient(userToken).admin.slots.createSlot.mutate({
|
|
deliveryTime: new Date(Date.now() + 3 * 3600_000).toISOString(),
|
|
freezeTime: new Date(Date.now() + 2 * 3600_000).toISOString(),
|
|
isActive: true,
|
|
skuIds: [],
|
|
})
|
|
)
|
|
expect(error.message).toMatch(/UNAUTHORIZED|Access denied/i)
|
|
})
|
|
|
|
it('rejects a user token on admin.coupon.create', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient(userToken).admin.coupon.create.mutate({
|
|
couponCode: `AUTH${uniqueSuffix()}`.toUpperCase(),
|
|
flatDiscount: 5,
|
|
isApplyForAll: true,
|
|
})
|
|
)
|
|
expect(error.message).toMatch(/UNAUTHORIZED|unauthorized/i)
|
|
})
|
|
|
|
it('rejects a user token on admin.region.updateRegionConstants', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient(userToken).admin.region.updateRegionConstants.mutate({
|
|
constants: [{ key: 'welcomeMessage', value: 'nope' }],
|
|
})
|
|
)
|
|
expect(error.message).toMatch(/UNAUTHORIZED|unauthorized/i)
|
|
})
|
|
|
|
it('rejects a staff token writing a global constant via admin.region.updateRegionConstants', async () => {
|
|
const staff = await staffSession()
|
|
const error = await captureError(() =>
|
|
apiClient(staff.token).admin.region.updateRegionConstants.mutate({
|
|
constants: [{ key: 'versionNum', value: '9.9.9' }],
|
|
})
|
|
)
|
|
expect(error.message).toMatch(/global constant/i)
|
|
})
|
|
})
|
|
|
|
describe('public admin.vendorSnippets procedures', () => {
|
|
it('serves getUpcomingSlots to anonymous callers', async () => {
|
|
const res = await apiClient().admin.vendorSnippets.getUpcomingSlots.query()
|
|
expect(res.success).toBe(true)
|
|
expect(Array.isArray(res.data)).toBe(true)
|
|
})
|
|
|
|
it('reaches updateOrderItemPackaging anonymously (domain error, not auth)', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient().admin.vendorSnippets.updateOrderItemPackaging.mutate({
|
|
orderItemId: 99_999_999,
|
|
is_packaged: true,
|
|
})
|
|
)
|
|
expect(error.message).not.toMatch(/UNAUTHORIZED|Access denied/i)
|
|
})
|
|
|
|
it('reaches getOrdersBySnippet anonymously (domain error, not auth)', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient().admin.vendorSnippets.getOrdersBySnippet.query({
|
|
snippetCode: `NO-SUCH-${uniqueSuffix()}`,
|
|
})
|
|
)
|
|
expect(error.message).not.toMatch(/UNAUTHORIZED|Access denied/i)
|
|
})
|
|
})
|
|
|
|
describe('staff token on user endpoints', () => {
|
|
it('cannot placeOrder', async () => {
|
|
const staff = await staffSession()
|
|
const error = await captureError(() =>
|
|
apiClient(staff.token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
paymentMethod: 'cod',
|
|
})
|
|
)
|
|
expect(error).toBeInstanceOf(Error)
|
|
})
|
|
|
|
it('cannot read user coupons', async () => {
|
|
const staff = await staffSession()
|
|
const error = await captureError(() =>
|
|
apiClient(staff.token).user.coupon.getEligible.query()
|
|
)
|
|
expect(error).toBeInstanceOf(Error)
|
|
})
|
|
})
|
|
|
|
describe('cross-user order access', () => {
|
|
it('user B cannot read user A order (getOrderById)', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient(otherToken).user.order.getOrderById.query({ orderId: String(orderId) })
|
|
)
|
|
expect(error.message).toMatch(/order not found/i)
|
|
})
|
|
|
|
it('user B cannot add notes to user A order', async () => {
|
|
const error = await captureError(() =>
|
|
apiClient(otherToken).user.order.updateUserNotes.mutate({
|
|
id: orderId,
|
|
userNotes: 'nope',
|
|
})
|
|
)
|
|
expect(error.message).toMatch(/order not found/i)
|
|
})
|
|
|
|
it('pins: user B cancelling user A order is swallowed into a generic message', async () => {
|
|
const fresh = await registerUserWithAddress()
|
|
const placed = await apiClient(fresh.token).user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId: fresh.addressId,
|
|
paymentMethod: 'cod',
|
|
})
|
|
|
|
const error = await captureError(() =>
|
|
apiClient(otherToken).user.order.cancelOrder.mutate({
|
|
id: placed.data[0].id,
|
|
reason: 'not mine',
|
|
})
|
|
)
|
|
// The specific 404 is masked by the try/catch in cancelOrder.
|
|
expect(error.message).toMatch(/failed to cancel order/i)
|
|
})
|
|
})
|
|
})
|