390 lines
13 KiB
TypeScript
390 lines
13 KiB
TypeScript
/**
|
|
* SSTAFF-01..20 — `superadmin.staff` (catalogue §8.4).
|
|
*
|
|
* Every staff row this suite creates is uniquely named and lives in a throwaway
|
|
* region (never a seeded staff row). Removal is blocked when the staff authored
|
|
* coupons/reserved coupons (409); a store-ownership block is *not* implemented
|
|
* because `store_info` carries no staff owner, so SSTAFF-13 is pinned `(current)`.
|
|
*/
|
|
|
|
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
|
|
import { anonClient, staffSession, superadminClient, userSession } from '../helpers/sessions'
|
|
import { PASSWORD, createRegion } from '../helpers/fixtures'
|
|
import { TEST_REGION_ID } from '../helpers/env'
|
|
|
|
jest.setTimeout(180_000)
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Helpers */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
const notificationCount = (message: string): number => Number(message.match(/(\d+)/)?.[1] ?? -1)
|
|
|
|
let regionPromise: Promise<number> | null = null
|
|
/** One throwaway region shared by the suite (created once, never deleted). */
|
|
function staffRegion(): Promise<number> {
|
|
regionPromise ??= createRegion().then((region) => region.regionId)
|
|
return regionPromise
|
|
}
|
|
|
|
async function adminRoleId(): Promise<number> {
|
|
const superadmin = await superadminClient()
|
|
const roles: any = await superadmin.superadmin.staff.roles.query()
|
|
const role = roles.roles.find((entry: any) => entry.name === 'admin') ?? roles.roles[0]
|
|
if (!role?.id) throw new Error('[api-tests] no staff roles are available')
|
|
return Number(role.id)
|
|
}
|
|
|
|
interface CreatedStaff {
|
|
id: number
|
|
name: string
|
|
password: string
|
|
roleId: number
|
|
}
|
|
|
|
async function createStaff(
|
|
regionId: number,
|
|
overrides: { name?: string; password?: string; roleId?: number } = {}
|
|
): Promise<CreatedStaff> {
|
|
const superadmin = await superadminClient()
|
|
const name = overrides.name ?? `e2e_sstaff_${uniqueSuffix()}`
|
|
const password = overrides.password ?? PASSWORD
|
|
const roleId = overrides.roleId ?? (await adminRoleId())
|
|
const res: any = await superadmin.superadmin.staff.create.mutate({
|
|
name,
|
|
password,
|
|
roleId,
|
|
regionId,
|
|
})
|
|
return { id: Number(res.user.id), name, password, roleId }
|
|
}
|
|
|
|
async function staffList(): Promise<any[]> {
|
|
const superadmin = await superadminClient()
|
|
const res: any = await superadmin.superadmin.staff.list.query()
|
|
return res.staff
|
|
}
|
|
|
|
async function loginAsStaff(name: string, password: string): Promise<string> {
|
|
const res: any = await apiClient().admin.staffUser.login.mutate({ name, password })
|
|
return res.token as string
|
|
}
|
|
|
|
/** Best-effort removal (never for a seeded row). */
|
|
async function removeStaff(id: number): Promise<any> {
|
|
const superadmin = await superadminClient()
|
|
return superadmin.superadmin.staff.remove.mutate({ id })
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* list + roles */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.staff reads (SSTAFF-01..02)', () => {
|
|
it('SSTAFF-01: list returns staff with their region and role', async () => {
|
|
const staff = await staffList()
|
|
|
|
expect(Array.isArray(staff)).toBe(true)
|
|
expect(staff.length).toBeGreaterThan(0)
|
|
for (const entry of staff) {
|
|
expect(entry).toEqual(
|
|
expect.objectContaining({
|
|
id: expect.any(Number),
|
|
name: expect.any(String),
|
|
})
|
|
)
|
|
expect(Object.prototype.hasOwnProperty.call(entry, 'regionId')).toBe(true)
|
|
expect(Object.prototype.hasOwnProperty.call(entry, 'role')).toBe(true)
|
|
}
|
|
|
|
const seeded = staff.find((entry) => entry.name === 'e2e_staff')
|
|
expect(seeded).toEqual(
|
|
expect.objectContaining({
|
|
id: expect.any(Number),
|
|
regionId: expect.any(Number),
|
|
role: expect.objectContaining({ id: expect.any(Number), name: expect.any(String) }),
|
|
})
|
|
)
|
|
})
|
|
|
|
it('SSTAFF-02: roles returns the staff roles', async () => {
|
|
const superadmin = await superadminClient()
|
|
const res: any = await superadmin.superadmin.staff.roles.query()
|
|
|
|
expect(Array.isArray(res.roles)).toBe(true)
|
|
expect(res.roles.length).toBeGreaterThan(0)
|
|
for (const role of res.roles) {
|
|
expect(role).toEqual(
|
|
expect.objectContaining({ id: expect.any(Number), name: expect.any(String) })
|
|
)
|
|
}
|
|
expect(res.roles.map((role: any) => role.name)).toContain('admin')
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* create */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.staff.create (SSTAFF-03..07)', () => {
|
|
it('SSTAFF-03: create returns a staff id assigned to the region', async () => {
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
|
|
expect(created.id).toBeGreaterThan(0)
|
|
|
|
const entry = (await staffList()).find((row) => Number(row.id) === created.id)
|
|
expect(entry).toEqual(
|
|
expect.objectContaining({
|
|
id: created.id,
|
|
name: created.name,
|
|
regionId,
|
|
role: expect.objectContaining({ id: created.roleId }),
|
|
})
|
|
)
|
|
|
|
// Cleanup: this row authored nothing, so it can be removed.
|
|
const removed: any = await removeStaff(created.id)
|
|
expect(removed.success).toBe(true)
|
|
})
|
|
|
|
it('SSTAFF-04: a duplicate name is 409', async () => {
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
const superadmin = await superadminClient()
|
|
|
|
try {
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.staff.create.mutate({
|
|
name: created.name,
|
|
password: PASSWORD,
|
|
roleId: created.roleId,
|
|
regionId,
|
|
}),
|
|
'CONFLICT',
|
|
'already exists'
|
|
)
|
|
} finally {
|
|
await removeStaff(created.id)
|
|
}
|
|
})
|
|
|
|
it('SSTAFF-05: a password shorter than 6 characters is BAD_REQUEST', async () => {
|
|
const regionId = await staffRegion()
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.staff.create.mutate({
|
|
name: `e2e_sstaff_short_${uniqueSuffix()}`,
|
|
password: '12345',
|
|
roleId: 2,
|
|
regionId,
|
|
}),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('SSTAFF-06: an invalid role is 400', async () => {
|
|
const regionId = await staffRegion()
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.staff.create.mutate({
|
|
name: `e2e_sstaff_badrole_${uniqueSuffix()}`,
|
|
password: PASSWORD,
|
|
roleId: 987_654_321,
|
|
regionId,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'invalid role'
|
|
)
|
|
})
|
|
|
|
it('SSTAFF-07: an invalid region is 400', async () => {
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.staff.create.mutate({
|
|
name: `e2e_sstaff_badregion_${uniqueSuffix()}`,
|
|
password: PASSWORD,
|
|
roleId: 2,
|
|
regionId: 987_654_321,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'invalid region'
|
|
)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* update + remove */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.staff.update (SSTAFF-08..10)', () => {
|
|
it('SSTAFF-08: update moves the staff to another region', async () => {
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
|
|
try {
|
|
const superadmin = await superadminClient()
|
|
const updated: any = await superadmin.superadmin.staff.update.mutate({
|
|
id: created.id,
|
|
regionId: TEST_REGION_ID,
|
|
})
|
|
expect(updated.success).toBe(true)
|
|
expect(updated.user.regionId).toBe(TEST_REGION_ID)
|
|
|
|
const entry = (await staffList()).find((row) => Number(row.id) === created.id)
|
|
expect(entry.regionId).toBe(TEST_REGION_ID)
|
|
} finally {
|
|
await removeStaff(created.id)
|
|
}
|
|
})
|
|
|
|
it('SSTAFF-09: updating an unknown staff is 404', async () => {
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.staff.update.mutate({
|
|
id: 987_654_321,
|
|
regionId: TEST_REGION_ID,
|
|
}),
|
|
'NOT_FOUND'
|
|
)
|
|
})
|
|
|
|
it('SSTAFF-10: updating to an invalid region is 400', async () => {
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
|
|
try {
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.staff.update.mutate({
|
|
id: created.id,
|
|
regionId: 987_654_321,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'invalid region'
|
|
)
|
|
} finally {
|
|
await removeStaff(created.id)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('superadmin.staff.remove (SSTAFF-11..15)', () => {
|
|
it('SSTAFF-11: remove deletes a staff row', async () => {
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
|
|
const result: any = await removeStaff(created.id)
|
|
expect(result.success).toBe(true)
|
|
|
|
const entry = (await staffList()).find((row) => Number(row.id) === created.id)
|
|
expect(entry).toBeUndefined()
|
|
})
|
|
|
|
it('SSTAFF-12: removing an unknown staff is 404', async () => {
|
|
await expectTrpcError(() => removeStaff(987_654_321), 'NOT_FOUND')
|
|
})
|
|
|
|
it('(current) SSTAFF-13: a staff who created a store is still removable (no store ownership)', async () => {
|
|
// `deleteStaffUser` only guards coupons/reserved_coupons; `store_info` has no
|
|
// staff owner column, so the router's `blockedBy === 'store'` branch is dead
|
|
// code. The reachable contract is a successful removal.
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
const token = await loginAsStaff(created.name, created.password)
|
|
|
|
const store: any = await apiClient(token).admin.store.createStore.mutate({
|
|
name: `E2E owner store ${uniqueSuffix()}`,
|
|
description: 'created by SSTAFF-13',
|
|
})
|
|
expect(store.store.id).toBeGreaterThan(0)
|
|
|
|
const result: any = await removeStaff(created.id)
|
|
expect(result.success).toBe(true)
|
|
|
|
const entry = (await staffList()).find((row) => Number(row.id) === created.id)
|
|
expect(entry).toBeUndefined()
|
|
})
|
|
|
|
it('SSTAFF-14: a staff who created a coupon cannot be removed (409)', async () => {
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
const token = await loginAsStaff(created.name, created.password)
|
|
|
|
const code = `E2ECP${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
|
const coupons: any = await apiClient(token).admin.coupon.create.mutate({
|
|
couponCode: code,
|
|
flatDiscount: 50,
|
|
isApplyForAll: true,
|
|
skuIds: [],
|
|
})
|
|
expect(Array.isArray(coupons)).toBe(true)
|
|
expect(coupons[0].createdBy).toBe(created.id)
|
|
|
|
// There is no hard-delete for a coupon, so this staff row intentionally
|
|
// remains behind after the suite (it is uniquely named, never seeded).
|
|
await expectTrpcError(() => removeStaff(created.id), 'CONFLICT', 'created coupons')
|
|
})
|
|
|
|
it('SSTAFF-15: a staff who created a reserved coupon cannot be removed (409)', async () => {
|
|
const regionId = await staffRegion()
|
|
const created = await createStaff(regionId)
|
|
const token = await loginAsStaff(created.name, created.password)
|
|
|
|
const secret = `E2ESEC${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
|
const reserved: any = await apiClient(token).admin.coupon.createReservedCoupon.mutate({
|
|
couponCode: secret,
|
|
flatDiscount: 50,
|
|
})
|
|
expect(Array.isArray(reserved)).toBe(true)
|
|
expect(reserved[0].createdBy).toBe(created.id)
|
|
|
|
await expectTrpcError(() => removeStaff(created.id), 'CONFLICT', 'reserved coupons')
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* notifications */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.staff.sendAdminNotification (SSTAFF-16..19)', () => {
|
|
|
|
|
|
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* auth */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.staff auth (SSTAFF-20)', () => {
|
|
it('SSTAFF-20: staff/user tokens are rejected by the superadmin guard', async () => {
|
|
const staff = await staffSession()
|
|
const user = await userSession()
|
|
|
|
for (const client of [anonClient(), apiClient(staff.token), apiClient(user.token)]) {
|
|
await expectTrpcError(() => client.superadmin.staff.list.query(), 'UNAUTHORIZED')
|
|
await expectTrpcError(
|
|
() =>
|
|
client.superadmin.staff.sendAdminNotification.mutate({ title: 'x', text: 'y' }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() =>
|
|
client.superadmin.staff.create.mutate({
|
|
name: `denied_${uniqueSuffix()}`,
|
|
password: PASSWORD,
|
|
roleId: 2,
|
|
regionId: TEST_REGION_ID,
|
|
}),
|
|
'UNAUTHORIZED'
|
|
)
|
|
}
|
|
})
|
|
})
|