// @vitest-environment jsdom import {describe, expect, it} from 'vitest'; import {refreshSession} from './api.js'; import {changeUserRole, fetchRoleChanges, viewAs} from './admin.js'; import {fetchRoutes, json} from '../test/fetchRoutes.js'; describe('viewAs', () => { it('waits for a running refresh before it sends the refresh cookie', async () => { let finishRefresh; fetchRoutes({ 'POST /api/auth/refresh': () => new Promise((resolve) => { finishRefresh = () => resolve(json(204)); }), 'POST /api/auth/view-as': json(204), }); const refreshing = refreshSession(); const switching = viewAs('USER'); await Promise.resolve(); expect(fetch.mock.calls.map(([url]) => url).filter((url) => url.endsWith('/api/auth/view-as'))).toHaveLength(0); finishRefresh(); await refreshing; await switching; }); }); describe('role changes', () => { it('changeUserRole puts the new and the expected role', async () => { fetchRoutes({'PUT /api/admin/users/7/role': json(204)}); await changeUserRole(7, 'AUDITOR', 'USER'); const [url, init] = fetch.mock.calls[0]; expect(url).toMatch(/\/api\/admin\/users\/7\/role$/); expect(init.method).toBe('PUT'); expect(JSON.parse(init.body)).toEqual({role: 'AUDITOR', expectedRole: 'USER'}); }); it('fetchRoleChanges gets the log of the user', async () => { fetchRoutes({'GET /api/admin/users/7/role-changes': json(200, {results: []})}); const signal = new AbortController().signal; await fetchRoleChanges(7, {signal}); const [url, init] = fetch.mock.calls[0]; expect(url).toMatch(/\/api\/admin\/users\/7\/role-changes$/); expect(init.signal).toBe(signal); }); });