/** * Admin user management: listing, details, incidents, notification audience, * suspension and the staff list. * * Per-user endpoints are exercised against a freshly registered user rather than * the shared test account: `getUserDetails` builds an unchunked `IN (...)` over * the user's order ids and fails once that history is large (~110 orders), which * the shared account has accumulated from repeated runs. (Documented finding.) * * sendNotification targets a user with no push token, so nothing is delivered. */ import { apiClient, uniqueSuffix } from '../helpers/api' import { registerThrowawayUser } from '../helpers/fixtures' import { staffSession } from '../helpers/sessions' describe('admin user management', () => { let admin: ReturnType let staffId = 0 let subjectUserId = 0 beforeAll(async () => { const staff = await staffSession() staffId = staff.staffId admin = apiClient(staff.token) const subject = await registerThrowawayUser() subjectUserId = subject.userId }) it('getEssentials returns the dashboard counters', async () => { const res = await admin.admin.user.getEssentials.query() expect(typeof res.unresolvedComplaints).toBe('number') }) it('getAllUsers returns users', async () => { const res = await admin.admin.user.getAllUsers.query({ limit: 5 }) expect(Array.isArray(res.users)).toBe(true) }) it('getUserDetails returns the user', async () => { const res = await admin.admin.user.getUserDetails.query({ userId: subjectUserId }) const user = (res as { user?: { id: number }; id?: number }).user ?? res expect((user as { id: number }).id).toBe(subjectUserId) }) it('getStaff lists the staff (including the logged-in one)', async () => { const res = (await admin.admin.staffUser.getStaff.query()) as | { id: number }[] | { staff?: { id: number }[] } const list = Array.isArray(res) ? res : (res.staff ?? []) expect(list.some((s) => s.id === staffId)).toBe(true) }) it('adds a user incident that getUserIncidents returns', async () => { const comment = `API test incident ${uniqueSuffix()}` await admin.admin.user.addUserIncident.mutate({ userId: subjectUserId, adminComment: comment, }) const res = await admin.admin.user.getUserIncidents.query({ userId: subjectUserId }) expect(Array.isArray(res.incidents)).toBe(true) expect( res.incidents.some((i: { adminComment: string | null }) => i.adminComment === comment) ).toBe(true) }) it('getUsersForNotification returns a users array', async () => { const res = await admin.admin.user.getUsersForNotification.query({ search: '' }) expect(Array.isArray(res.users)).toBe(true) }) it('suspends and unsuspends the user', async () => { const suspendedFlag = async () => { const res = (await admin.admin.user.getUserDetails.query({ userId: subjectUserId, })) as { isSuspended?: boolean; user?: { isSuspended?: boolean } } return res.isSuspended ?? res.user?.isSuspended } await admin.admin.user.updateUserSuspension.mutate({ userId: subjectUserId, isSuspended: true, }) try { expect(await suspendedFlag()).toBe(true) } finally { await admin.admin.user.updateUserSuspension.mutate({ userId: subjectUserId, isSuspended: false, }) } expect(await suspendedFlag()).toBe(false) }) it('sendNotification tolerates a recipient with no push token', async () => { const res = await admin.admin.user.sendNotification.mutate({ userIds: [subjectUserId], title: 'API test', text: 'No-op recipient', }) expect(res).toBeTruthy() }) })