/** * ASTAFF-01..22 — `admin.staffUser` (catalogue §7.1). * * Login is `publicProcedure`; `me` / `sendAdminNotification` are staff-only. * The remaining reads/writes (`getStaff`, `getRoles`, `createStaffUser`, * `savePushToken`) are `protectedProcedure`, so a user token passes the guard * and then dereferences `ctx.staffUser!` — the known §13 gap pinned here. */ import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api' import { anonClient, staffClient, staffSession, superadminClient, userClient, } from '../helpers/sessions' import { PASSWORD } from '../helpers/fixtures' import { staffCredentials } from '../helpers/env' jest.setTimeout(180_000) interface CreatedStaff { id: number name: string password: string roleId: number } async function adminRoleId(): Promise { const admin = await staffClient() const roles: any = await admin.admin.staffUser.getRoles.query() const role = roles.roles.find((r: any) => r.name === 'admin') ?? roles.roles[0] if (!role?.id) throw new Error('[api-tests] no staff roles are available') return Number(role.id) } /** Create a uniquely-named staff row through the admin API. */ async function createStaff( overrides: { name?: string; password?: string; roleId?: number } = {} ): Promise { const admin = await staffClient() const name = overrides.name ?? `e2e_staff_${uniqueSuffix()}` const password = overrides.password ?? PASSWORD const roleId = overrides.roleId ?? (await adminRoleId()) const res: any = await admin.admin.staffUser.createStaffUser.mutate({ name, password, roleId }) return { id: Number(res.user.id), name, password, roleId } } /** Best-effort cleanup for a staff row this suite created (never seeded ones). */ async function removeStaff(id: number): Promise { try { const superadmin = await superadminClient() await superadmin.superadmin.staff.remove.mutate({ id }) } catch { // Left behind on purpose — the suite must not fail over cleanup. } } const notificationCount = (message: string): number => Number(message.match(/(\d+)/)?.[1] ?? -1) describe('admin.staffUser.login (ASTAFF-01..04)', () => { it('ASTAFF-01: staff login returns a token and the staff identity', async () => { const { name, password } = staffCredentials() const res: any = await apiClient().admin.staffUser.login.mutate({ name, password }) expect(res.message).toEqual('Login successful') expect(res.token).toEqual(expect.any(String)) expect(res.token.length).toBeGreaterThan(20) expect(res.staff).toEqual( expect.objectContaining({ id: expect.any(Number), name, regionId: expect.any(Number) }) ) expect(res.staff.id).toBeGreaterThan(0) expect(res.staff.regionId).toBeGreaterThan(0) }) it('ASTAFF-02: an unknown name is 401 invalid credentials', async () => { await expectTrpcError( () => apiClient().admin.staffUser.login.mutate({ name: `nobody-${uniqueSuffix()}`, password: PASSWORD, }), 'UNAUTHORIZED', 'invalid credentials' ) }) it('ASTAFF-03: a wrong password is 401', async () => { const { name } = staffCredentials() await expectTrpcError( () => apiClient().admin.staffUser.login.mutate({ name, password: 'definitely-wrong' }), 'UNAUTHORIZED', 'invalid credentials' ) }) it('ASTAFF-04: missing name/password is BAD_REQUEST', async () => { const login = apiClient().admin.staffUser.login await expectTrpcError(() => login.mutate({ name: 'only-name' } as any), 'BAD_REQUEST') await expectTrpcError(() => login.mutate({ password: 'only-password' } as any), 'BAD_REQUEST') await expectTrpcError(() => login.mutate({} as any), 'BAD_REQUEST') }) }) describe('admin.staffUser.me (ASTAFF-05..07)', () => { it('ASTAFF-05: me returns id/name/regionId/regionName', async () => { const session = await staffSession() const me: any = await (await staffClient()).admin.staffUser.me.query() expect(me.id).toBe(session.staffId) expect(me.name).toBe(session.name) expect(me.regionId).toBe(session.regionId) expect(me.regionName).toEqual(expect.any(String)) expect(me.regionName.length).toBeGreaterThan(0) }) it('ASTAFF-06: me without a token is 401', async () => { await expectTrpcError(() => anonClient().admin.staffUser.me.query(), 'UNAUTHORIZED') }) it('ASTAFF-07: me with a user token is rejected (staff-only guard)', async () => { const user = await userClient() await expectTrpcError(() => user.admin.staffUser.me.query(), 'UNAUTHORIZED') }) }) describe('admin.staffUser.getStaff / getRoles (ASTAFF-08..10)', () => { it('ASTAFF-08: getStaff returns staff with role and permissions', async () => { const admin = await staffClient() const res: any = await admin.admin.staffUser.getStaff.query() expect(Array.isArray(res.staff)).toBe(true) expect(res.staff.length).toBeGreaterThan(0) const session = await staffSession() const self = res.staff.find((s: any) => Number(s.id) === session.staffId) expect(self).toEqual(expect.objectContaining({ id: session.staffId, name: session.name })) expect(self.role).toEqual( expect.objectContaining({ id: expect.any(Number), name: expect.any(String) }) ) expect(Array.isArray(self.permissions)).toBe(true) for (const permission of self.permissions) { expect(permission).toEqual( expect.objectContaining({ id: expect.any(Number), name: expect.any(String) }) ) } }) it('ASTAFF-09 (current): a user token can read the full staff list', async () => { // Known §13 gap: `getStaff` is `protectedProcedure` (user OR staff) and does // not touch `ctx.staffUser`, so a regular user token receives every staff // row, role and permission. Pinned, not intended. const user = await userClient() const res: any = await user.admin.staffUser.getStaff.query() expect(Array.isArray(res.staff)).toBe(true) expect(res.staff.length).toBeGreaterThan(0) expect(res.staff[0]).toEqual(expect.objectContaining({ id: expect.any(Number) })) }) it('ASTAFF-10: getRoles returns the role list', async () => { const admin = await staffClient() const res: any = await admin.admin.staffUser.getRoles.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((r: any) => r.name)).toEqual(expect.arrayContaining(['admin'])) }) }) describe('admin.staffUser.createStaffUser (ASTAFF-11..15)', () => { it('ASTAFF-11: a uniquely-named staff is created and can log in', async () => { const created = await createStaff() try { const login: any = await apiClient().admin.staffUser.login.mutate({ name: created.name, password: created.password, }) expect(login.token).toEqual(expect.any(String)) expect(Number(login.staff.id)).toBe(created.id) expect(login.staff.name).toBe(created.name) } finally { await removeStaff(created.id) } }) it('ASTAFF-12: an existing name is 409 CONFLICT', async () => { const created = await createStaff() try { const admin = await staffClient() await expectTrpcError( () => admin.admin.staffUser.createStaffUser.mutate({ name: created.name, password: PASSWORD, roleId: created.roleId, }), 'CONFLICT', 'already exists' ) } finally { await removeStaff(created.id) } }) it('ASTAFF-13: a password shorter than 6 characters is BAD_REQUEST', async () => { const admin = await staffClient() await expectTrpcError( () => admin.admin.staffUser.createStaffUser.mutate({ name: `e2e_short_${uniqueSuffix()}`, password: '12345', roleId: 1, }), 'BAD_REQUEST' ) }) it('ASTAFF-14: an unknown role id is 400 BAD_REQUEST', async () => { const admin = await staffClient() await expectTrpcError( () => admin.admin.staffUser.createStaffUser.mutate({ name: `e2e_badrole_${uniqueSuffix()}`, password: PASSWORD, roleId: 999_999, }), 'BAD_REQUEST', 'invalid role' ) }) it('ASTAFF-15: the created staff inherits the calling staff region', async () => { const session = await staffSession() const created = await createStaff() try { const login: any = await apiClient().admin.staffUser.login.mutate({ name: created.name, password: created.password, }) expect(Number(login.staff.regionId)).toBe(session.regionId) const me: any = await apiClient(login.token).admin.staffUser.me.query() expect(me.regionId).toBe(session.regionId) expect(me.regionName).toEqual(expect.any(String)) } finally { await removeStaff(created.id) } }) }) describe('admin.staffUser.savePushToken (ASTAFF-16..17)', () => { it('ASTAFF-17 (current): a user token is 401 (no staff identity on the context)', async () => { // `savePushToken` is `protectedProcedure`, so the user token passes the // guard; the resolver then rejects the missing `ctx.staffUser` with a 401. const user = await userClient() await expectTrpcError( () => user.admin.staffUser.savePushToken.mutate({ token: `e2e-${uniqueSuffix()}` }), 'UNAUTHORIZED' ) await expectTrpcError( () => anonClient().admin.staffUser.savePushToken.mutate({ token: 'anon' }), 'UNAUTHORIZED' ) }) }) describe('admin.staffUser.sendAdminNotification (ASTAFF-18..22)', () => { it('ASTAFF-22: a user token is rejected by the staff-only guard', async () => { const user = await userClient() await expectTrpcError( () => user.admin.staffUser.sendAdminNotification.mutate({ title: 'x', text: 'y' }), 'UNAUTHORIZED' ) }) })