From 5f68ecb5f037e7fb935707607f7acd78f7e90736 Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 22:48:44 -0500 Subject: [PATCH 1/9] feat(api): let a deployment turn off the password breach check Every password set or changed asks the Have I Been Pwned API about it. PASSWORD_BREACH_CHECK_ENABLED (default true) turns that off, so the e2e suite and the api integration suites no longer depend on an outside server, and an air-gapped deployment need not wait on one either. Co-Authored-By: Claude Opus 5.5 --- .env.template | 3 +++ .../src/core/schemas/__tests__/env.schema.test.ts | 7 +++++++ apps/api/src/core/schemas/env.schema.ts | 1 + apps/api/src/users/__tests__/users.service.test.ts | 13 ++++++++++++- apps/api/src/users/users.service.ts | 7 ++++++- apps/api/vitest.config.ts | 3 +++ docker-compose.yaml | 1 + 7 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.env.template b/.env.template index 5b334cdd1..8cf0216ac 100644 --- a/.env.template +++ b/.env.template @@ -69,6 +69,9 @@ GATEWAY_API_KEY= # Optional server-wide GitHub token used to sync instrument repositories. Only needed for private # repos that were registered without their own access token; leave blank otherwise. GITHUB_TOKEN= +# Whether a password being set is checked against the Have I Been Pwned API, which needs outbound +# internet access (default: true). The end-to-end suite turns it off so it never depends on that server. +PASSWORD_BREACH_CHECK_ENABLED= # Whether 'debug' level logs should be enabled DEBUG=false # Whether 'verbose' level logs should be enabled diff --git a/apps/api/src/core/schemas/__tests__/env.schema.test.ts b/apps/api/src/core/schemas/__tests__/env.schema.test.ts index b7ad2bdc9..2ad1b3402 100644 --- a/apps/api/src/core/schemas/__tests__/env.schema.test.ts +++ b/apps/api/src/core/schemas/__tests__/env.schema.test.ts @@ -75,4 +75,11 @@ describe('$Env', () => { it('should not require storage variables while storage is disabled', () => { expect($Env.safeParse({ ...testEnv, STORAGE_ENABLED: 'false' }).success).toBe(true); }); + + it('should check passwords against known breaches unless it is turned off, so a deployment is protected by default', () => { + expect($Env.parse(testEnv).PASSWORD_BREACH_CHECK_ENABLED).toBe(true); + expect($Env.parse({ ...testEnv, PASSWORD_BREACH_CHECK_ENABLED: 'false' }).PASSWORD_BREACH_CHECK_ENABLED).toBe( + false + ); + }); }); diff --git a/apps/api/src/core/schemas/env.schema.ts b/apps/api/src/core/schemas/env.schema.ts index 67bc26067..393163eff 100644 --- a/apps/api/src/core/schemas/env.schema.ts +++ b/apps/api/src/core/schemas/env.schema.ts @@ -15,6 +15,7 @@ export const $Env = $BaseEnv GATEWAY_REFRESH_INTERVAL: $NumberLike.pipe(z.number().positive().int()), GATEWAY_SITE_ADDRESS: $UrlLike.optional(), GITHUB_TOKEN: z.string().min(1).optional(), + PASSWORD_BREACH_CHECK_ENABLED: $BooleanLike.default(true), STORAGE_ACCESS_KEY: z.string().min(1).optional(), STORAGE_BUCKET: z.string().min(1).optional(), STORAGE_ENABLED: $BooleanLike.optional(), diff --git a/apps/api/src/users/__tests__/users.service.test.ts b/apps/api/src/users/__tests__/users.service.test.ts index ef246e133..53612dfc9 100644 --- a/apps/api/src/users/__tests__/users.service.test.ts +++ b/apps/api/src/users/__tests__/users.service.test.ts @@ -1,4 +1,4 @@ -import { CryptoService, getModelToken } from '@douglasneuroinformatics/libnest'; +import { ConfigService, CryptoService, getModelToken } from '@douglasneuroinformatics/libnest'; import type { Model, RequestUser } from '@douglasneuroinformatics/libnest'; import { MockFactory } from '@douglasneuroinformatics/libnest/testing'; import type { MockedInstance } from '@douglasneuroinformatics/libnest/testing'; @@ -38,6 +38,7 @@ const baseUser = { describe('UsersService', () => { let usersService: UsersService; let userModel: MockedInstance>; + let configService: MockedInstance; let cryptoService: MockedInstance; let groupsService: MockedInstance; let permissionSetsService: MockedInstance; @@ -49,6 +50,7 @@ describe('UsersService', () => { UsersService, MockFactory.createForModelToken(getModelToken('User')), MockFactory.createForService(AuditLogger), + MockFactory.createForService(ConfigService), MockFactory.createForService(CryptoService), MockFactory.createForService(GroupsService), MockFactory.createForService(PermissionSetsService) @@ -56,6 +58,7 @@ describe('UsersService', () => { }).compile(); userModel = moduleRef.get(getModelToken('User')); usersService = moduleRef.get(UsersService); + configService = moduleRef.get(ConfigService); cryptoService = moduleRef.get(CryptoService); groupsService = moduleRef.get(GroupsService); permissionSetsService = moduleRef.get(PermissionSetsService); @@ -63,6 +66,7 @@ describe('UsersService', () => { userModel.exists.mockResolvedValue(false); userModel.create.mockResolvedValue({}); cryptoService.hashPassword.mockResolvedValue('hashed-password'); + configService.get.mockReturnValue(true); // By default the password is strong and has not appeared in a breach. (estimatePasswordStrength as Mock).mockReturnValue({ feedback: {}, score: 4, success: true }); (pwnedPassword as Mock).mockResolvedValue(0); @@ -290,6 +294,13 @@ describe('UsersService', () => { expect(userModel.create).toHaveBeenCalledOnce(); }); + it('should not contact Have I Been Pwned when the breach check is disabled, so an offline instance never waits on it', async () => { + configService.get.mockReturnValue(false); + await usersService.create({ ...baseUser }); + expect(pwnedPassword).not.toHaveBeenCalled(); + expect(userModel.create).toHaveBeenCalledOnce(); + }); + it('should persist mustResetPassword, so a generated password forces a reset at first sign-in', async () => { await usersService.create({ ...baseUser, mustResetPassword: true }); expect(userModel.create.mock.lastCall?.[0]).toMatchObject({ data: { mustResetPassword: true } }); diff --git a/apps/api/src/users/users.service.ts b/apps/api/src/users/users.service.ts index 082fa68bc..d703341bd 100644 --- a/apps/api/src/users/users.service.ts +++ b/apps/api/src/users/users.service.ts @@ -1,4 +1,4 @@ -import { CryptoService, InjectModel } from '@douglasneuroinformatics/libnest'; +import { ConfigService, CryptoService, InjectModel } from '@douglasneuroinformatics/libnest'; import type { Model, RequestUser } from '@douglasneuroinformatics/libnest'; import { estimatePasswordStrength } from '@douglasneuroinformatics/libpasswd'; import { @@ -35,6 +35,7 @@ export class UsersService { constructor( @InjectModel('User') private readonly userModel: Model<'User'>, private readonly auditLogger: AuditLogger, + private readonly configService: ConfigService, private readonly cryptoService: CryptoService, private readonly groupsService: GroupsService, private readonly permissionSetsService: PermissionSetsService @@ -353,6 +354,7 @@ export class UsersService { * `BadRequestException` if the password is too weak, matches the username, or has * appeared in a known data breach. The breach check fails open: if the Have I Been * Pwned API is unreachable (e.g. an air-gapped deployment), the password is allowed. + * `PASSWORD_BREACH_CHECK_ENABLED=false` skips the check, so nothing waits on that server. */ private async validatePassword(password: string, username: string): Promise { if (!estimatePasswordStrength(password).success) { @@ -361,6 +363,9 @@ export class UsersService { if (password.toLowerCase() === username.toLowerCase()) { throw this.passwordError('PASSWORD_MATCHES_USERNAME', 'Password must not be the same as the username'); } + if (!this.configService.get('PASSWORD_BREACH_CHECK_ENABLED')) { + return; + } let breachCount: number; try { breachCount = await pwnedPassword(password); diff --git a/apps/api/vitest.config.ts b/apps/api/vitest.config.ts index 18cfef3f7..a6a5cf41b 100644 --- a/apps/api/vitest.config.ts +++ b/apps/api/vitest.config.ts @@ -39,6 +39,9 @@ export default mergeConfig( plugins: [apiOnlySwcPlugin, libnestPlugin], root: import.meta.dirname, test: { + // The integration suites create users through the real UsersService, which would otherwise ask + // the Have I Been Pwned API about every password. + env: { PASSWORD_BREACH_CHECK_ENABLED: 'false' }, globals: true, name: 'api' } diff --git a/docker-compose.yaml b/docker-compose.yaml index d569ceb2a..fe5a0e0ed 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -55,6 +55,7 @@ services: - GATEWAY_ENABLED - GATEWAY_REFRESH_INTERVAL - GATEWAY_SITE_ADDRESS + - PASSWORD_BREACH_CHECK_ENABLED - SECRET_KEY - THROTTLER_ENABLED - LOGIN_REQUEST_THROTTLER_LIMIT From 9f2f446e2c1c252e694710c4e7028e0dda9a5f97 Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 22:48:46 -0500 Subject: [PATCH 2/9] test(testing): serve production builds to the end-to-end suite The gateway, web and playground dev servers held about 2.5 GB between them (the gateway alone 1.25 GB, running Vite in-process) and served every module unbundled. The suite now starts the gateway's production server and `vite preview` of the web and playground builds, which turbo builds before it. On two workers a run fell from 7.9 to 4.7 minutes and its typical footprint from 5.4 to 3.6 GB. Firefox ran only the @smoke tests and is dropped, along with its CI install. The API's gateway sync interval is now a constant the suite owns (2 s) and passes to the API, instead of the 10 s a deployment uses. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yaml | 4 ++-- apps/api/package.json | 2 +- apps/playground/package.json | 1 + apps/playground/vite.config.ts | 38 ++++++++++++++++++++-------------- apps/web/package.json | 1 + apps/web/vite.config.ts | 30 ++++++++++++++++----------- testing/playwright.config.ts | 28 ++++++++++++++----------- testing/src/support/env.ts | 8 +++++-- turbo.json | 9 +++++++- 9 files changed, 76 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c9d765669..cfd33cc1b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -46,10 +46,10 @@ jobs: run: pnpm test:coverage - name: Install Playwright # Cap the step so a hang fails fast instead of hitting the 6h job limit, skip the interactive - # apt prompt, and install only the browsers the e2e suite uses. + # apt prompt, and install only the browser the e2e suite uses. timeout-minutes: 10 env: DEBIAN_FRONTEND: noninteractive - run: pnpm --filter "@opendatacapture/testing" exec playwright install --with-deps chromium firefox + run: pnpm --filter "@opendatacapture/testing" exec playwright install --with-deps chromium - name: End-to-End Tests run: pnpm test:e2e diff --git a/apps/api/package.json b/apps/api/package.json index a1c624ce4..8f2b2581d 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -14,7 +14,7 @@ "build": "rm -rf dist && NODE_ENV=production libnest build -c libnest.config.ts", "db:generate": "prisma generate --no-hints", "dev": "NODE_ENV=development NODE_OPTIONS='--conditions=development' env-cmd -f ../../.env libnest dev -c libnest.config.ts", - "dev:test": "NODE_ENV=test env-cmd -f ../../.env env STORAGE_ENABLED=true libnest dev -c libnest.config.ts --no-watch", + "dev:test": "NODE_ENV=test env-cmd --no-override -f ../../.env env PASSWORD_BREACH_CHECK_ENABLED=false STORAGE_ENABLED=true libnest dev -c libnest.config.ts --no-watch", "start": "NODE_ENV=production env-cmd -f ../../.env node dist/app.js", "test": "env-cmd -f ../../.env vitest" }, diff --git a/apps/playground/package.json b/apps/playground/package.json index 4768359ed..ac63bbc68 100644 --- a/apps/playground/package.json +++ b/apps/playground/package.json @@ -8,6 +8,7 @@ "build": "tsc && pnpm exec vite build", "deploy": "rsync -r dist/* unrjos@cloud1.douglasneuroinformatics.ca:/home/unrjos/www/playground.opendatacapture.org", "dev": "NODE_ENV=development pnpm exec vite", + "preview": "pnpm exec vite preview", "test": "env-cmd -f ../../.env vitest" }, "dependencies": { diff --git a/apps/playground/vite.config.ts b/apps/playground/vite.config.ts index 0f49645cb..7099d1c71 100644 --- a/apps/playground/vite.config.ts +++ b/apps/playground/vite.config.ts @@ -5,7 +5,7 @@ import runtime from '@opendatacapture/vite-plugin-runtime'; import tailwindcss from '@tailwindcss/vite'; import react from '@vitejs/plugin-react-swc'; import { defineConfig } from 'vite'; -import type { Plugin } from 'vite'; +import type { Plugin, PreviewServer, ViteDevServer } from 'vite'; import type { PlaygroundConfig } from './src/preview/config.ts'; @@ -16,17 +16,30 @@ const { version } = JSON.parse(fs.readFileSync(path.resolve(import.meta.dirname, version: string; }; -/** Serves `/config.json` as the Caddyfile does in the published image, so both read the same variable. */ -const playgroundConfig = (): Plugin => ({ - configureServer(server) { +/** + * Serves `/config.json` as the Caddyfile does in the published image, so all three read the same + * variable: the dev server, and `vite preview` serving the build to the end-to-end suite. + */ +const playgroundConfig = (): Plugin => { + const serveConfig = (server: PreviewServer | ViteDevServer) => { server.middlewares.use('/config.json', (_request, response) => { const config: PlaygroundConfig = { previewOrigin: process.env.PLAYGROUND_PREVIEW_ORIGIN ?? '' }; response.setHeader('Content-Type', 'application/json'); response.end(JSON.stringify(config)); }); - }, - name: 'playground-config' -}); + }; + return { configurePreviewServer: serveConfig, configureServer: serveConfig, name: 'playground-config' }; +}; + +// The preview frame runs on the other loopback name (see `resolvePreviewOrigin`). Left to resolve +// `localhost` itself, Vite listens on ::1 alone under Node 24, and 127.0.0.1 refuses to connect. +// Browsers try both addresses for `localhost`, so this serves both names without opening the server +// to the network the way `host: true` would. `vite preview` serves the build to the end-to-end suite +// on the same address. +const server = { + host: '127.0.0.1', + port: parseInt(process.env.PLAYGROUND_DEV_SERVER_PORT ?? '3750', 10) +}; export default defineConfig(({ mode }) => ({ build: { @@ -64,17 +77,12 @@ export default defineConfig(({ mode }) => ({ }), tailwindcss() ], + // A preview on any other port would serve the suite nothing, so fail rather than move. + preview: { ...server, strictPort: true }, resolve: { alias: { '@': path.resolve(import.meta.dirname, 'src') } }, - server: { - // The preview frame runs on the other loopback name (see `resolvePreviewOrigin`). Left to - // resolve `localhost` itself, Vite listens on ::1 alone under Node 24, and 127.0.0.1 refuses to - // connect. Browsers try both addresses for `localhost`, so this serves both names without - // opening the server to the network the way `host: true` would. - host: '127.0.0.1', - port: parseInt(process.env.PLAYGROUND_DEV_SERVER_PORT ?? '3750', 10) - } + server })); diff --git a/apps/web/package.json b/apps/web/package.json index 794651e85..bcd15cace 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -8,6 +8,7 @@ "build": "tsc && NODE_ENV=production NODE_OPTIONS='--import=tsx' pnpm exec vite build", "dev": "NODE_ENV=development NODE_OPTIONS='--import=tsx' env-cmd -f ../../.env pnpm exec vite", "dev:test": "NODE_ENV=test NODE_OPTIONS='--import=tsx' env-cmd -f ../../.env pnpm exec vite --mode test", + "preview": "env-cmd -f ../../.env -- pnpm exec import-meta-env -x .env.public -p dist/index.html && NODE_OPTIONS='--import=tsx' env-cmd -f ../../.env pnpm exec vite preview", "format:translations": "find src/translations -name '*.json' -exec pnpm exec sort-json {} \\;", "start": "NODE_ENV=production env-cmd -f ../../.env -- pnpm exec import-meta-env -x .env.public -p dist/index.html && python -m http.server -d dist 3000", "test": "env-cmd -f ../../.env vitest" diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 6719ad35c..cbad143b3 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -22,6 +22,21 @@ export default defineConfig(async ({ command }) => { } } + // `vite preview` serves the production build to the end-to-end suite on the same origin, so it + // needs the same port and `/api` proxy as the dev server. + const server = { + port: webPort, + proxy: { + '/api/': { + rewrite: (path: string) => path.replace(/^\/api/, ''), + target: { + host: 'localhost', + port: apiPort + } + } + } + }; + return { build: { chunkSizeWarningLimit: 1000, @@ -60,22 +75,13 @@ export default defineConfig(async ({ command }) => { }), tailwindcss() ], + // A preview on any other port would serve the suite nothing, so fail rather than move. + preview: { ...server, strictPort: true }, resolve: { alias: { '@': path.resolve(import.meta.dirname, 'src') } }, - server: { - port: webPort, - proxy: { - '/api/': { - rewrite: (path: string) => path.replace(/^\/api/, ''), - target: { - host: 'localhost', - port: apiPort - } - } - } - } + server }; }); diff --git a/testing/playwright.config.ts b/testing/playwright.config.ts index c1fb7a024..1dc4e0148 100644 --- a/testing/playwright.config.ts +++ b/testing/playwright.config.ts @@ -2,7 +2,15 @@ import * as path from 'node:path'; import { defineConfig, devices } from '@playwright/test'; -import { apiPort, baseURL, gatewayPort, playgroundPort, webPort, workerCount } from './src/support/env'; +import { + apiPort, + baseURL, + gatewayPort, + gatewayRefreshInterval, + playgroundPort, + webPort, + workerCount +} from './src/support/env'; const webServerDefaults = { gracefulShutdown: { signal: 'SIGINT', timeout: 1000 }, @@ -27,40 +35,36 @@ export default defineConfig({ name: 'chromium', testMatch: '**/specs/**/*.spec.ts', use: { ...devices['Desktop Chrome'], baseURL, channel: 'chromium', headless: true } - }, - { - // Firefox is a cross-browser smoke check: only the `@smoke`-tagged critical flows. - dependencies: ['setup'], - grep: /@smoke/, - name: 'firefox', - testMatch: '**/specs/**/*.spec.ts', - use: { ...devices['Desktop Firefox'], baseURL, headless: true } } ], reporter: [['html', { open: 'never', outputFolder: path.resolve(import.meta.dirname, '.playwright/report') }]], testDir: path.resolve(import.meta.dirname, 'src'), + // The gateway, web and playground serve their production builds, which turbo builds before the + // suite: a dev server holds every module it has transformed in memory and serves each unbundled. webServer: [ { ...webServerDefaults, command: 'pnpm dev:test', cwd: path.resolve(import.meta.dirname, '../apps/api'), + env: { GATEWAY_REFRESH_INTERVAL: String(gatewayRefreshInterval) }, url: `http://localhost:${apiPort}/v1/setup` }, { ...webServerDefaults, - command: 'pnpm dev:test', + command: 'pnpm start', cwd: path.resolve(import.meta.dirname, '../apps/gateway'), + env: { GATEWAY_PROD_SERVER_PORT: String(gatewayPort) }, url: `http://localhost:${gatewayPort}/api/healthcheck` }, { ...webServerDefaults, - command: 'pnpm dev:test', + command: 'pnpm preview', cwd: path.resolve(import.meta.dirname, '../apps/web'), url: `http://localhost:${webPort}` }, { ...webServerDefaults, - command: 'pnpm dev', + command: 'pnpm preview', cwd: path.resolve(import.meta.dirname, '../apps/playground'), url: `http://localhost:${playgroundPort}` } diff --git a/testing/src/support/env.ts b/testing/src/support/env.ts index 5ef3dfb54..66e841ee6 100644 --- a/testing/src/support/env.ts +++ b/testing/src/support/env.ts @@ -40,8 +40,12 @@ export const webPort = requiredNumber('WEB_DEV_SERVER_PORT'); /** How many tests run at once, where `.env` says; `playwright.config.ts` holds the default. */ export const workerCount = optionalPositiveInteger('E2E_WORKERS'); -/** How often, in milliseconds, the API pulls completed assignments from the gateway. */ -export const gatewayRefreshInterval = requiredNumber('GATEWAY_REFRESH_INTERVAL'); +/** + * How often, in milliseconds, the API pulls completed assignments from the gateway. The suite owns + * this rather than reading `.env`: `playwright.config.ts` starts the API with it, so a sync is never + * the ten seconds a deployment waits. + */ +export const gatewayRefreshInterval = 2_000; // Tests run against the web origin; vite proxies `/api` through to the API server. export const baseURL = `http://localhost:${webPort}`; diff --git a/turbo.json b/turbo.json index faae7407a..898c12043 100644 --- a/turbo.json +++ b/turbo.json @@ -44,7 +44,14 @@ }, "@opendatacapture/testing#test:e2e": { "cache": false, - "dependsOn": ["^build", "//#generated", "@opendatacapture/gateway#db:push"] + "dependsOn": [ + "^build", + "//#generated", + "@opendatacapture/gateway#build", + "@opendatacapture/gateway#db:push", + "@opendatacapture/playground#build", + "@opendatacapture/web#build" + ] } } } From 4b29b9d846d98bfefdf66493fa743387f66fbace Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 23:24:13 -0500 Subject: [PATCH 3/9] test: prove in unit and API tests what the e2e suite re-proved Most end-to-end tests asserted a rule a unit or API test already pinned, or one that a component or a single route owns. Each was deleted only after its covering test was read, and where that test fell short the missing assertion was added first. Row scoping, which mocked models cannot show, moved to new suites in apps/api/test that run against the real database; tests that walked one flow were merged to share their setup, and preconditions are seeded over the API instead of the UI. The suite goes from 399 tests to 98 and, on two workers, from 4.7 to 1.3 minutes. Fixed sleeps are gone: the gateway tests poll the API for the synchronized state, the slow-import and malformed-submission tests are covered by unit tests, and the JSPsych example runs with its timing patched. Page-object members and ApiClient methods nothing uses any more are removed. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/assignments.service.test.ts | 11 + .../__tests__/sessions.service.test.ts | 20 + apps/api/test/helpers.ts | 23 + apps/api/test/suites/01-boot.suite.ts | 11 + .../test/suites/02-user-permissions.suite.ts | 77 ++ .../test/suites/04-permission-sets.suite.ts | 54 ++ .../api/test/suites/07-mail-delivery.suite.ts | 150 ++++ .../suites/07-record-assignment-link.suite.ts | 93 ++ .../suites/07-subject-custom-ids.suite.ts | 146 ++++ .../api/test/suites/08-authorization.suite.ts | 572 ++++++++++++ apps/api/test/suites/09-group-access.suite.ts | 139 +++ .../09-instrument-record-upload.suite.ts | 60 ++ apps/api/test/suites/09-log-sorting.suite.ts | 97 +++ .../suites/09-subject-record-filter.suite.ts | 58 ++ apps/gateway/src/__tests__/Root.test.tsx | 12 +- .../src/__tests__/entry-server.test.ts | 13 + .../src/routers/__tests__/api.router.test.ts | 3 +- .../src/routers/__tests__/cap.router.test.ts | 8 + .../Editor/__tests__/EditorTab.test.tsx | 55 ++ .../__tests__/DeleteInstrumentDialog.test.tsx | 40 + .../SaveButton/__tests__/SaveButton.test.tsx | 60 ++ .../__tests__/MainContent.test.tsx | 36 + .../Viewer/__tests__/Viewer.test.tsx | 54 ++ .../src/utils/__tests__/file.test.ts | 9 + .../BulkRemoteAssignmentWizard.test.tsx | 18 +- .../__tests__/InstrumentTable.test.tsx | 20 +- .../__tests__/LoginPageEditor.test.tsx | 9 + .../__tests__/StartSessionForm.test.tsx | 25 + .../__tests__/UpdateUserForm.test.tsx | 12 + .../__tests__/UserPermissionsEditor.test.tsx | 19 + .../__tests__/NavigationBlocker.test.tsx | 14 + .../src/hooks/__tests__/useNavItems.test.ts | 51 +- .../routes/_app/__tests__/contact.test.tsx | 21 +- .../src/routes/_app/__tests__/user.test.tsx | 6 + .../instrument-repos/__tests__/index.test.tsx | 14 + .../admin/users/__tests__/create.test.tsx | 31 +- .../$subjectId/table/__tests__/index.test.tsx | 8 + .../datahub/subjects/__tests__/index.test.tsx | 9 + .../__tests__/accessible-instruments.test.tsx | 11 +- .../instruments/render/__tests__/$id.test.tsx | 13 +- .../src/__tests__/build.test.ts | 10 + ...NHANCED_DEMOGRAPHICS_QUESTIONNAIRE.test.ts | 4 + .../DNP_HAPPINESS_QUESTIONNAIRE.test.ts | 4 + .../__tests__/FormContent.test.tsx | 53 ++ .../__tests__/InstrumentSummary.test.tsx | 43 + .../src/utils/__tests__/zodErrorMap.test.ts | 6 + testing/playwright.config.ts | 4 +- .../src/pages/_app/admin/audit/logs.page.ts | 6 - .../pages/_app/admin/branding/index.page.ts | 2 - .../_app/admin/branding/login-page.page.ts | 13 - .../_app/admin/instrument-repos/index.page.ts | 16 - testing/src/pages/_app/admin/mail.page.ts | 22 - .../permission-sets/$permissionSetId.page.ts | 6 - .../_app/admin/permission-sets/index.page.ts | 2 - testing/src/pages/_app/admin/settings.page.ts | 26 - .../pages/_app/admin/users/$userId.page.ts | 28 - testing/src/pages/_app/contact.page.ts | 4 - .../instruments/$instrumentId/graph.page.ts | 7 - .../_app/datahub/instruments/index.page.ts | 11 - .../subjects/$subjectId/assignments.page.ts | 2 - .../pages/_app/datahub/subjects/index.page.ts | 2 - .../pages/_app/group/email-templates.page.ts | 31 - testing/src/pages/_app/group/manage.page.ts | 2 - .../_app/group/remote-assignments.page.ts | 60 -- .../pages/_app/instruments/render/$id.page.ts | 13 - .../_app/session/remote-assignment.page.ts | 9 - .../pages/_app/session/start-session.page.ts | 8 - .../pages/_app/upload/$instrumentId.page.ts | 2 - testing/src/pages/_app/user.page.ts | 2 - testing/src/pages/auth/login.page.ts | 2 - testing/src/pages/playground/index.page.ts | 38 +- .../src/specs/accessible-instruments.spec.ts | 15 - testing/src/specs/admin-audit-logs.spec.ts | 87 +- testing/src/specs/admin-branding.spec.ts | 60 +- .../src/specs/admin-instrument-repos.spec.ts | 409 ++------- testing/src/specs/admin-instruments.spec.ts | 77 +- testing/src/specs/admin-management.spec.ts | 547 ++---------- testing/src/specs/admin-navigation.spec.ts | 56 -- testing/src/specs/admin-settings.spec.ts | 172 +--- testing/src/specs/api-docs.spec.ts | 15 - testing/src/specs/auth.spec.ts | 193 ++--- testing/src/specs/authorization.spec.ts | 817 +----------------- .../src/specs/bulk-remote-assignments.spec.ts | 169 +--- testing/src/specs/dashboard.spec.ts | 111 +-- testing/src/specs/datahub.spec.ts | 202 +---- testing/src/specs/disclaimer.spec.ts | 11 - testing/src/specs/error-page.spec.ts | 18 +- testing/src/specs/form-block.spec.ts | 4 - testing/src/specs/form-error-summary.spec.ts | 39 - testing/src/specs/form-reset.spec.ts | 84 -- testing/src/specs/gateway-assignment.spec.ts | 431 ++------- testing/src/specs/gateway-landing.spec.ts | 24 - testing/src/specs/group-archive.spec.ts | 86 +- .../src/specs/group-email-templates.spec.ts | 90 +- testing/src/specs/group-manage.spec.ts | 130 +-- testing/src/specs/group-switcher.spec.ts | 41 +- .../src/specs/instrument-completion.spec.ts | 488 +++-------- testing/src/specs/instrument-hub.spec.ts | 128 +-- .../specs/instrument-navigation-block.spec.ts | 40 +- testing/src/specs/list-return.spec.ts | 25 +- testing/src/specs/mail.spec.ts | 264 ------ testing/src/specs/password-reset.spec.ts | 111 +-- testing/src/specs/permission-sets.spec.ts | 198 +---- testing/src/specs/playground.spec.ts | 616 +++---------- .../src/specs/record-file-deletion.spec.ts | 42 - testing/src/specs/remote-assignment.spec.ts | 296 ++----- .../specs/remote-assignments-wizard.spec.ts | 190 +--- testing/src/specs/series-instrument.spec.ts | 25 - testing/src/specs/sessions.spec.ts | 95 -- testing/src/specs/start-session.spec.ts | 394 +-------- testing/src/specs/static-pages.spec.ts | 128 +-- testing/src/specs/subject-detail.spec.ts | 128 +-- testing/src/specs/table-sorting.spec.ts | 67 -- testing/src/specs/upload.spec.ts | 440 ++-------- testing/src/specs/user-account.spec.ts | 190 +--- testing/src/support/api-client.ts | 103 +-- testing/src/support/env.ts | 3 - 117 files changed, 3278 insertions(+), 7101 deletions(-) create mode 100644 apps/api/test/suites/07-mail-delivery.suite.ts create mode 100644 apps/api/test/suites/07-record-assignment-link.suite.ts create mode 100644 apps/api/test/suites/07-subject-custom-ids.suite.ts create mode 100644 apps/api/test/suites/08-authorization.suite.ts create mode 100644 apps/api/test/suites/09-group-access.suite.ts create mode 100644 apps/api/test/suites/09-instrument-record-upload.suite.ts create mode 100644 apps/api/test/suites/09-log-sorting.suite.ts create mode 100644 apps/api/test/suites/09-subject-record-filter.suite.ts create mode 100644 apps/playground/src/components/Editor/__tests__/EditorTab.test.tsx create mode 100644 apps/playground/src/components/Header/ActionsDropdown/__tests__/DeleteInstrumentDialog.test.tsx create mode 100644 apps/playground/src/components/Header/SaveButton/__tests__/SaveButton.test.tsx create mode 100644 apps/playground/src/components/MainContent/__tests__/MainContent.test.tsx create mode 100644 apps/playground/src/components/Viewer/__tests__/Viewer.test.tsx create mode 100644 apps/playground/src/utils/__tests__/file.test.ts delete mode 100644 testing/src/specs/admin-navigation.spec.ts delete mode 100644 testing/src/specs/api-docs.spec.ts delete mode 100644 testing/src/specs/form-error-summary.spec.ts delete mode 100644 testing/src/specs/form-reset.spec.ts delete mode 100644 testing/src/specs/record-file-deletion.spec.ts delete mode 100644 testing/src/specs/table-sorting.spec.ts diff --git a/apps/api/src/assignments/__tests__/assignments.service.test.ts b/apps/api/src/assignments/__tests__/assignments.service.test.ts index 2f6fa5953..fdbe45413 100644 --- a/apps/api/src/assignments/__tests__/assignments.service.test.ts +++ b/apps/api/src/assignments/__tests__/assignments.service.test.ts @@ -650,6 +650,17 @@ describe('AssignmentsService', () => { expect(gatewayService.deleteRemoteAssignment).not.toHaveBeenCalled(); }); + it("should scope the existence check to the caller's groups, so a manager outside the group cannot kill its link", async () => { + assignmentModel.exists.mockResolvedValueOnce(false); + await expect( + assignmentsService.updateById('assignment-1', { status: 'CANCELED' }, userAt('GROUP_MANAGER')) + ).rejects.toBeInstanceOf(NotFoundException); + expect(assignmentModel.exists.mock.lastCall?.[0]).toEqual({ + AND: [{ OR: [{ groupId: { in: [GROUP_ID] } }] }], + id: 'assignment-1' + }); + }); + it('should delete the gateway copy of an assignment the caller cancels, so its link stops working', async () => { assignmentModel.exists.mockResolvedValueOnce(true); assignmentModel.update.mockResolvedValueOnce({ groupId: GROUP_ID, id: 'assignment-1' }); diff --git a/apps/api/src/sessions/__tests__/sessions.service.test.ts b/apps/api/src/sessions/__tests__/sessions.service.test.ts index 55676707c..0ba18f063 100644 --- a/apps/api/src/sessions/__tests__/sessions.service.test.ts +++ b/apps/api/src/sessions/__tests__/sessions.service.test.ts @@ -70,6 +70,26 @@ describe('SessionsService', () => { expect(sessionModel.createMany.mock.lastCall?.[0]).toMatchObject({ data: [{ groupId: 'group-1' }] }); }); + // The batched path once handed over only the ids, so a new subject lost the demographics its + // first session named. + it('should hand every subject over with the demographics its session names', async () => { + const subjectData = { + dateOfBirth: new Date('1990-05-17T00:00:00.000Z'), + firstName: 'Ada', + id: 'subject-1', + lastName: 'Lovelace', + sex: 'FEMALE' as const + }; + + await sessionsService.createMany({ + entries: [{ date: new Date(), subjectData }], + groupId: null, + type: 'IN_PERSON' + }); + + expect(subjectsService.createMany).toHaveBeenCalledExactlyOnceWith([subjectData]); + }); + it('should associate the batch with the group in a single write', async () => { groupsService.findById.mockResolvedValueOnce({ id: 'group-1' } as any); diff --git a/apps/api/test/helpers.ts b/apps/api/test/helpers.ts index 29f15a76a..590dab547 100644 --- a/apps/api/test/helpers.ts +++ b/apps/api/test/helpers.ts @@ -58,3 +58,26 @@ export function defineSuite(name: string, fn: (this: TestSuiteThis) => Promise { + const { z } = await import(${JSON.stringify(import.meta.resolve('zod/v4'))}); + return { + __runtimeVersion: 1, + content: { score: { kind: 'number', label: 'Score', variant: 'input' } }, + details: { description: 'A form with one number field', license: 'Apache-2.0', title: ${JSON.stringify(name)} }, + internal: { edition: 1, name: ${JSON.stringify(name)} }, + kind: 'FORM', + language: 'en', + measures: ${measured ? "{ score: { kind: 'const', label: 'Score', ref: 'score' } }" : 'null'}, + tags: [], + validationSchema: z.object({ score: z.number() }) + }; + })()`; +} diff --git a/apps/api/test/suites/01-boot.suite.ts b/apps/api/test/suites/01-boot.suite.ts index 9426931fe..a8a1f78f4 100644 --- a/apps/api/test/suites/01-boot.suite.ts +++ b/apps/api/test/suites/01-boot.suite.ts @@ -19,6 +19,17 @@ export default defineSuite('boot', function () { }); }); + // The page itself is Redoc, a script from a CDN, so what the app owns is the page pointing it at + // the document every test below checks. + describe('GET /', () => { + it('should serve the API reference page, rendering the document at /spec.json', async () => { + const response = await this.app.inject({ method: 'GET', url: '/' }); + expect(response.statusCode).toBe(200); + expect(response.headers['content-type']).toMatch(/^text\/html/); + expect(response.body).toContain(''); + }); + }); + describe('GET /spec.json', () => { const getDocument = async (): Promise => { const response = await this.app.inject({ method: 'GET', url: '/spec.json' }); diff --git a/apps/api/test/suites/02-user-permissions.suite.ts b/apps/api/test/suites/02-user-permissions.suite.ts index e2c6167f6..d5008652d 100644 --- a/apps/api/test/suites/02-user-permissions.suite.ts +++ b/apps/api/test/suites/02-user-permissions.suite.ts @@ -47,6 +47,30 @@ export default defineSuite('user permissions', function () { return response.json(); }; + const createStandardUser = async (username: string, groupIds: string[]): Promise => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + basePermissionLevel: 'STANDARD', + firstName: 'Test', + groupIds, + lastName: 'User', + password: PASSWORD, + username + }, + url: '/v1/users' + }); + expect(response.statusCode).toBe(201); + return response.json(); + }; + + const findUser = async (id: string): Promise => { + const response = await this.app.inject({ headers: adminHeaders, method: 'GET', url: `/v1/users/${id}` }); + expect(response.statusCode).toBe(200); + return response.json(); + }; + beforeAll(async () => { const setup = await this.app.inject({ method: 'POST', @@ -102,6 +126,59 @@ export default defineSuite('user permissions', function () { subject: 'Subject' }); }); + + it('should refuse to grant a write on users, which only an administrator may make, and store nothing', async () => { + const member = await createStandardUser('user.write.grantee', [group.id]); + const response = await this.app.inject({ + headers: adminHeaders, + method: 'PUT', + payload: { kind: 'custom', permissions: [{ action: 'manage', groupId: group.id, subject: 'User' }] }, + url: `/v1/users/${member.id}/permissions` + }); + expect(response.statusCode).toBe(400); + expect((await findUser(member.id)).additionalPermissions).toEqual([]); + }); + }); + + describe('PATCH /v1/users/:id', () => { + it('should ignore permissions sent through a profile update, so only the permissions route can grant', async () => { + const member = await createStandardUser('profile.update.grantee', [group.id]); + const response = await this.app.inject({ + headers: adminHeaders, + method: 'PATCH', + payload: { additionalPermissions: [{ action: 'manage', groupId: null, subject: 'all' }] }, + url: `/v1/users/${member.id}` + }); + expect(response.statusCode).toBe(200); + expect((await findUser(member.id)).additionalPermissions).toEqual([]); + }); + + it('should drop a grant confined to a group the user is removed from, and keep an unscoped one', async () => { + const member = await createStandardUser('group.leaver', [group.id, otherGroup.id]); + const granted = await this.app.inject({ + headers: adminHeaders, + method: 'PUT', + payload: { + kind: 'custom', + permissions: [ + { action: 'read', groupId: group.id, subject: 'User' }, + { action: 'create', groupId: null, subject: 'Instrument' } + ] + }, + url: `/v1/users/${member.id}/permissions` + }); + expect(granted.statusCode).toBe(200); + const response = await this.app.inject({ + headers: adminHeaders, + method: 'PATCH', + payload: { groupIds: [otherGroup.id] }, + url: `/v1/users/${member.id}` + }); + expect(response.statusCode).toBe(200); + expect((await findUser(member.id)).additionalPermissions).toEqual([ + { action: 'create', groupId: null, subject: 'Instrument' } + ]); + }); }); // The permissions route refuses a `User` write, but one stored before it did is still in the diff --git a/apps/api/test/suites/04-permission-sets.suite.ts b/apps/api/test/suites/04-permission-sets.suite.ts index 979b4c118..bd4125837 100644 --- a/apps/api/test/suites/04-permission-sets.suite.ts +++ b/apps/api/test/suites/04-permission-sets.suite.ts @@ -176,6 +176,60 @@ export default defineSuite('permission sets', function () { expect(rules).toContainEqual({ action: 'read', subject: 'Assignment' }); }); + it("should confine a holder's-groups rule to the holder's groups, where an every-group rule reads every group", async () => { + const otherGroup = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { name: `Other Group ${randomUUID()}`, type: 'RESEARCH' }, + url: '/v1/groups' + }); + expect(otherGroup.statusCode).toBe(201); + const otherGroupId = otherGroup.json().id; + const holder = (await createUser()).json(); + const teammate = (await createUser({ basePermissionLevel: 'GROUP_MANAGER' })).json(); + const outsider = ( + await createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [otherGroupId] }) + ).json(); + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + name: `Set ${randomUUID()}`, + rules: [ + { action: 'read', scope: 'HOLDER_GROUPS', subject: 'User' }, + { action: 'read', scope: 'ALL_GROUPS', subject: 'Group' } + ] + }, + url: '/v1/permission-sets' + }); + expect(response.statusCode).toBe(201); + + // Read with a token minted after each change, since permissions are frozen into it at login. + const readAccess = async () => { + const headers = { authorization: `Bearer ${await login(holder.username)}` }; + const readIds = async (path: 'groups' | 'users') => { + const listing = await this.app.inject({ headers, method: 'GET', url: `/v1/${path}` }); + expect(listing.statusCode).toBe(200); + return listing.json<{ id: string }[]>().map(({ id }) => id); + }; + return { groupIds: await readIds('groups'), userIds: await readIds('users') }; + }; + const before = await readAccess(); + expect(before.userIds).not.toContain(teammate.id); + expect(before.groupIds).not.toContain(otherGroupId); + + const assigned = await setPermissions(holder.id, { + kind: 'permissionSet', + permissionSetId: response.json().id + }); + expect(assigned.statusCode).toBe(200); + + const after = await readAccess(); + expect(after.userIds).toContain(teammate.id); + expect(after.userIds).not.toContain(outsider.id); + expect(after.groupIds).toEqual(expect.arrayContaining([group.id, otherGroupId])); + }); + it('should switch between a set and custom grants, each write emptying the other', async () => { const permissionSet = await createPermissionSet(); const user = (await createUser()).json(); diff --git a/apps/api/test/suites/07-mail-delivery.suite.ts b/apps/api/test/suites/07-mail-delivery.suite.ts new file mode 100644 index 000000000..9537eef3f --- /dev/null +++ b/apps/api/test/suites/07-mail-delivery.suite.ts @@ -0,0 +1,150 @@ +import { randomUUID } from 'node:crypto'; +import { createServer } from 'node:net'; + +import { PRISMA_CLIENT_TOKEN } from '@douglasneuroinformatics/libnest'; +import type { EmailDeliveryResult } from '@opendatacapture/schemas/mail'; +import { afterAll, afterEach, beforeAll, expect, it, vi } from 'vitest'; + +import type { RuntimePrismaClient } from '@/core/prisma'; +import { GatewayService } from '@/gateway/gateway.service'; + +import { defineSuite } from '../helpers'; + +const PASSWORD = 'DataCapture2025_Test'; + +/** Logins come from an address of their own, so they spend none of the default address's throttle budget. */ +const LOGIN_ADDRESS = '192.0.2.70'; + +/** A loopback port nothing listens on, so a delivery attempt is refused at once rather than timing out. */ +async function findClosedPort(): Promise { + const server = createServer(); + await new Promise((resolve) => { + server.listen(0, '127.0.0.1', resolve); + }); + const address = server.address(); + await new Promise((resolve) => { + server.close(resolve); + }); + if (typeof address !== 'object' || address === null) { + throw new Error('Expected the probe server to listen on a TCP port'); + } + return address.port; +} + +/** + * The delivery results a caller relies on to fall back to manual delivery, over real HTTP: the + * controllers' unit tests mock the mail service, and the web app's tests mock these responses. + */ +export default defineSuite('mail delivery', function () { + let adminHeaders: { authorization: string }; + + const setMailConfig = async (enabled: boolean, port: number) => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'PATCH', + payload: { + config: { + enabled, + encryption: 'none', + host: '127.0.0.1', + password: 'mail-password', + port, + senderAddress: 'noreply@example.org', + username: 'mailer' + } + }, + url: '/v1/mail/settings' + }); + expect(response.statusCode).toBe(200); + }; + + let closedPort: number; + + beforeAll(async () => { + const login = await this.app.inject({ + method: 'POST', + payload: { password: PASSWORD, username: 'admin' }, + remoteAddress: LOGIN_ADDRESS, + url: '/v1/auth/login' + }); + expect(login.statusCode).toBe(200); + adminHeaders = { authorization: `Bearer ${login.json<{ accessToken: string }>().accessToken}` }; + closedPort = await findClosedPort(); + await setMailConfig(true, closedPort); + }); + + // Mail is instance-wide, and every later suite assumes it is off. + afterAll(async () => { + await setMailConfig(false, closedPort); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it('should answer a created user with the rendered welcome message when it cannot be delivered', async () => { + const username = `welcome-${randomUUID()}`; + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + basePermissionLevel: 'ADMIN', + email: `${username}@example.org`, + firstName: 'Welcome', + groupIds: [], + lastName: 'Probe', + password: PASSWORD, + username + }, + url: '/v1/users' + }); + + expect(response.statusCode).toBe(201); + const { welcomeEmail } = response.json<{ welcomeEmail: EmailDeliveryResult }>(); + expect(welcomeEmail).toMatchObject({ error: 'CONNECTION_REFUSED', status: 'FAILED' }); + expect(welcomeEmail.message).toContain(username); + }); + + // The bulk route sits beside `:id/email`, which would capture `bulk` as an assignment id if the + // two were ever matched in the wrong order. + it('should report a failed bulk assignment email as a delivery result rather than an error', async () => { + const prisma = this.app.get(PRISMA_CLIENT_TOKEN); + const group = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { name: `Mail Delivery ${randomUUID()}`, type: 'RESEARCH' }, + url: '/v1/groups' + }); + expect(group.statusCode).toBe(201); + const groupId = group.json<{ id: string }>().id; + const instrumentId = ( + await prisma.instrument.create({ + data: { bundle: '{}', groupIds: [groupId], id: `mail-delivery-${randomUUID()}` } + }) + ).id; + await prisma.group.update({ data: { accessibleInstrumentIds: [instrumentId] }, where: { id: groupId } }); + const subject = await prisma.subject.create({ data: { groupIds: [groupId], id: `mail-${randomUUID()}` } }); + vi.spyOn(this.app.get(GatewayService), 'createRemoteAssignment').mockResolvedValue(undefined as never); + const assignment = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { expiresAt: new Date(Date.now() + 86_400_000), groupId, instrumentId, subjectId: subject.id }, + url: '/v1/assignments' + }); + expect(assignment.statusCode).toBe(201); + + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + assignmentIds: [assignment.json<{ id: string }>().id], + language: 'en', + recipient: 'participant@example.org' + }, + url: '/v1/assignments/bulk/email' + }); + + expect(response.statusCode, response.body).toBe(201); + expect(response.json()).toMatchObject({ error: 'CONNECTION_REFUSED', status: 'FAILED' }); + }); +}); diff --git a/apps/api/test/suites/07-record-assignment-link.suite.ts b/apps/api/test/suites/07-record-assignment-link.suite.ts new file mode 100644 index 000000000..4f1628d2e --- /dev/null +++ b/apps/api/test/suites/07-record-assignment-link.suite.ts @@ -0,0 +1,93 @@ +import { randomUUID } from 'node:crypto'; + +import { PRISMA_CLIENT_TOKEN } from '@douglasneuroinformatics/libnest'; +import { afterEach, beforeAll, expect, it, vi } from 'vitest'; + +import type { RuntimePrismaClient } from '@/core/prisma'; +import { GatewayService } from '@/gateway/gateway.service'; +import { InstrumentsService } from '@/instruments/instruments.service'; + +import { defineSuite, scoreFormBundle } from '../helpers'; + +/** Logins come from an address of their own, so they spend none of the default address's throttle budget. */ +const LOGIN_ADDRESS = '192.0.2.71'; + +/** + * Only the gateway synchronizer may link a record to an assignment, so the create route drops the + * key, but a client that still sends one must not be refused for it. The strip is the schema's, and + * this is where the stripped body is shown to be what reaches the service. + */ +export default defineSuite('record assignment link', function () { + let prisma: RuntimePrismaClient; + let adminHeaders: { authorization: string }; + let groupId: string; + let instrumentId: string; + + beforeAll(async () => { + prisma = this.app.get(PRISMA_CLIENT_TOKEN); + const login = await this.app.inject({ + method: 'POST', + payload: { password: 'DataCapture2025_Test', username: 'admin' }, + remoteAddress: LOGIN_ADDRESS, + url: '/v1/auth/login' + }); + expect(login.statusCode).toBe(200); + adminHeaders = { authorization: `Bearer ${login.json<{ accessToken: string }>().accessToken}` }; + instrumentId = ( + await this.app.get(InstrumentsService).create({ bundle: scoreFormBundle('RECORD_ASSIGNMENT_LINK') }) + ).id; + // Created after the instrument, so the group is granted it. + const group = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { name: 'Record Assignment Link', type: 'RESEARCH' }, + url: '/v1/groups' + }); + expect(group.statusCode).toBe(201); + groupId = group.json<{ id: string }>().id; + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it('should accept a record that names an assignment, without linking the two', async () => { + const session = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { date: new Date(), groupId, subjectData: { id: `assigned-${randomUUID()}` }, type: 'IN_PERSON' }, + url: '/v1/sessions' + }); + expect(session.statusCode).toBe(201); + const { id: sessionId, subjectId } = session.json<{ id: string; subjectId: string }>(); + + const gatewayService = this.app.get(GatewayService); + vi.spyOn(gatewayService, 'createRemoteAssignment').mockResolvedValue(undefined as never); + const assignment = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { expiresAt: new Date(Date.now() + 86_400_000), groupId, instrumentId, subjectId }, + url: '/v1/assignments' + }); + expect(assignment.statusCode).toBe(201); + + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + assignmentId: assignment.json<{ id: string }>().id, + data: { score: 1 }, + date: new Date(), + groupId, + instrumentId, + sessionId, + subjectId + }, + url: '/v1/instrument-records' + }); + + expect(response.statusCode, response.body).toBe(201); + const { id } = response.json<{ id: string }>(); + expect(await prisma.instrumentRecord.findUniqueOrThrow({ where: { id } })).toMatchObject({ assignmentId: null }); + }); +}); diff --git a/apps/api/test/suites/07-subject-custom-ids.suite.ts b/apps/api/test/suites/07-subject-custom-ids.suite.ts new file mode 100644 index 000000000..f332bc516 --- /dev/null +++ b/apps/api/test/suites/07-subject-custom-ids.suite.ts @@ -0,0 +1,146 @@ +import { randomUUID } from 'node:crypto'; + +import type { $CreateSubjectData } from '@opendatacapture/schemas/subject'; +import { beforeAll, describe, expect, it } from 'vitest'; + +import { defineSuite } from '../helpers'; + +const PASSWORD = 'DataCapture2025_Test'; + +/** Logins come from an address of their own, so they spend none of the default address's throttle budget. */ +const LOGIN_ADDRESS = '192.0.2.72'; + +/** + * The start-session form fills its identifier combobox from these routes. Their queries lean on + * MongoDB semantics a mocked model cannot check (`isSet` on an absent field, a range over the id's + * scope prefix), so they are exercised here against a real database. + */ +export default defineSuite('subject custom id suggestions', function () { + let adminHeaders: { authorization: string }; + let outsiderHeaders: { authorization: string }; + + const login = async (username: string) => { + const response = await this.app.inject({ + method: 'POST', + payload: { password: PASSWORD, username }, + remoteAddress: LOGIN_ADDRESS, + url: '/v1/auth/login' + }); + expect(response.statusCode).toBe(200); + return { authorization: `Bearer ${response.json<{ accessToken: string }>().accessToken}` }; + }; + + const createGroup = async () => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { name: `Custom Ids ${randomUUID()}`, type: 'RESEARCH' }, + url: '/v1/groups' + }); + expect(response.statusCode).toBe(201); + return response.json<{ id: string }>().id; + }; + + /** Enrols a subject the way the app does, by starting a session for it. */ + const createSession = async (groupId: null | string, subjectData: $CreateSubjectData) => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { date: new Date(), groupId, subjectData, type: 'IN_PERSON' }, + url: '/v1/sessions' + }); + expect(response.statusCode).toBe(201); + }; + + const findCustomIds = async (url: string, headers = adminHeaders) => { + const response = await this.app.inject({ headers, method: 'GET', url }); + expect(response.statusCode).toBe(200); + return response.json(); + }; + + beforeAll(async () => { + adminHeaders = await login('admin'); + const username = `custom-ids-${randomUUID()}`; + const user = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + basePermissionLevel: 'GROUP_MANAGER', + firstName: 'Outside', + groupIds: [await createGroup()], + lastName: 'Manager', + password: PASSWORD, + username + }, + url: '/v1/users' + }); + expect(user.statusCode).toBe(201); + outsiderHeaders = await login(username); + }); + + describe('GET /v1/subjects/groups/:groupId/custom-ids', () => { + it('should list the group’s custom-id subjects but not those identified by personal information', async () => { + const groupId = await createGroup(); + const bareId = `bare-${randomUUID()}`; + const customId = `custom-${randomUUID()}`; + const personalInfoId = `personal-${randomUUID()}`; + // The start-session form sends a custom-id subject's date of birth and sex, but no name. + await createSession(groupId, { id: bareId }); + await createSession(groupId, { dateOfBirth: new Date('1990-01-01'), id: customId, sex: 'MALE' }); + await createSession(groupId, { + dateOfBirth: new Date('1990-01-01'), + firstName: 'Ada', + id: personalInfoId, + lastName: 'Lovelace', + sex: 'FEMALE' + }); + + const ids = await findCustomIds(`/v1/subjects/groups/${groupId}/custom-ids`); + + expect(ids.toSorted()).toStrictEqual([bareId, customId]); + }); + + it('should not list custom-id subjects from another group', async () => { + const groupId = await createGroup(); + await createSession(await createGroup(), { id: `other-${randomUUID()}` }); + + expect(await findCustomIds(`/v1/subjects/groups/${groupId}/custom-ids`)).toStrictEqual([]); + }); + + it('should list nothing to a group manager asking for a group they are not a member of', async () => { + const groupId = await createGroup(); + await createSession(groupId, { id: `private-${randomUUID()}` }); + + expect(await findCustomIds(`/v1/subjects/groups/${groupId}/custom-ids`, outsiderHeaders)).toStrictEqual([]); + }); + }); + + describe('GET /v1/subjects/default-group/custom-ids', () => { + it('should list custom ids scoped to the default group, but not those scoped to a named group', async () => { + const rootScopedId = `root$default-${randomUUID()}`; + const groupScopedId = `Clinic$named-${randomUUID()}`; + await createSession(null, { id: rootScopedId }); + await createSession(await createGroup(), { id: groupScopedId }); + + const ids = await findCustomIds('/v1/subjects/default-group/custom-ids'); + + expect(ids).toContain(rootScopedId); + expect(ids).not.toContain(groupScopedId); + }); + + // Starting a session in a group adds that group to the subject, but leaves its id alone. + it('should still list a default group custom id after its subject is seen in a group', async () => { + const rootScopedId = `root$regrouped-${randomUUID()}`; + await createSession(null, { id: rootScopedId }); + await createSession(await createGroup(), { id: rootScopedId }); + + expect(await findCustomIds('/v1/subjects/default-group/custom-ids')).toContain(rootScopedId); + }); + + it('should list no default group subjects to a group manager outside their groups', async () => { + await createSession(null, { id: `root$default-${randomUUID()}` }); + + expect(await findCustomIds('/v1/subjects/default-group/custom-ids', outsiderHeaders)).toStrictEqual([]); + }); + }); +}); diff --git a/apps/api/test/suites/08-authorization.suite.ts b/apps/api/test/suites/08-authorization.suite.ts new file mode 100644 index 000000000..b970cdfce --- /dev/null +++ b/apps/api/test/suites/08-authorization.suite.ts @@ -0,0 +1,572 @@ +import { randomUUID } from 'node:crypto'; + +import { PRISMA_CLIENT_TOKEN } from '@douglasneuroinformatics/libnest'; +import type { Group } from '@opendatacapture/schemas/group'; +import type { InstrumentRecord } from '@opendatacapture/schemas/instrument-records'; +import type { Session } from '@opendatacapture/schemas/session'; +import type { User } from '@opendatacapture/schemas/user'; +import { beforeAll, describe, expect, it } from 'vitest'; + +import type { RuntimePrismaClient } from '@/core/prisma'; + +import { defineSuite, scoreFormBundle } from '../helpers'; + +const PASSWORD = 'DataCapture2025_Test'; + +/** + * Every login here comes from this one address rather than inject's default. The throttler allows 50 + * logins a minute per client, and the suites before this one already spend much of that on the + * default address within the same minute. + */ +const LOGIN_ADDRESS = '192.0.2.80'; + +const INSTRUMENT_NAME = 'AUTHORIZATION_SCORE'; + +type AuthHeaders = { authorization: string }; + +type BasePermissionLevel = 'ADMIN' | 'GROUP_MANAGER' | 'STANDARD'; + +type RequestOptions = { method: 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT'; payload?: object; url: string }; + +/** A series over the suite's form, as a bundle; a series names its items and holds no schema of its own. */ +function seriesBundle(title: string) { + return `(async () => ({ + __runtimeVersion: 1, + content: [{ edition: 1, name: ${JSON.stringify(INSTRUMENT_NAME)} }, { edition: 1, name: ${JSON.stringify(INSTRUMENT_NAME)} }], + details: { description: 'A series', license: 'Apache-2.0', title: ${JSON.stringify(title)} }, + kind: 'SERIES', + language: 'en', + tags: [] + }))()`; +} + +/** + * Row-level scoping is not observable in the api's unit tests, whose models are mocked, so what each + * role may reach is asserted here against real rows. Runs after `02-user-permissions`, whose setup + * created the administrator. + */ +export default defineSuite('authorization', function () { + let prisma: RuntimePrismaClient; + let adminHeaders: AuthHeaders; + let instrumentId: string; + /** The group the managers and standard users below belong to. */ + let ownGroup: Group; + let foreignGroup: Group; + let manager: User; + let managerHeaders: AuthHeaders; + let standard: User; + let standardHeaders: AuthHeaders; + /** A user of the foreign group, whom no one in the own group may read. */ + let outsider: User; + + const login = async (username: string): Promise => { + const response = await this.app.inject({ + method: 'POST', + payload: { password: PASSWORD, username }, + remoteAddress: LOGIN_ADDRESS, + url: '/v1/auth/login' + }); + expect(response.statusCode).toBe(200); + return { authorization: `Bearer ${response.json<{ accessToken: string }>().accessToken}` }; + }; + + const request = (headers: AuthHeaders, options: RequestOptions) => this.app.inject({ headers, ...options }); + + /** The reduced token the playground uploads with, minted from an administrator's login token. */ + const mintInstrumentToken = async (): Promise => { + const response = await request(adminHeaders, { method: 'GET', url: '/v1/auth/create-instrument-token' }); + expect(response.statusCode).toBe(200); + return { authorization: `Bearer ${response.json<{ accessToken: string }>().accessToken}` }; + }; + + const createGroup = async (): Promise => { + const response = await request(adminHeaders, { + method: 'POST', + payload: { name: `Authorization ${randomUUID()}`, type: 'CLINICAL' }, + url: '/v1/groups' + }); + expect(response.statusCode).toBe(201); + const group = response.json(); + const granted = await request(adminHeaders, { + method: 'PATCH', + payload: { accessibleInstrumentIds: [instrumentId] }, + url: `/v1/groups/${group.id}` + }); + expect(granted.statusCode).toBe(200); + return group; + }; + + const createUser = async ( + basePermissionLevel: BasePermissionLevel, + groupIds: string[], + overrides: { mustResetPassword?: boolean } = {} + ): Promise => { + const response = await request(adminHeaders, { + method: 'POST', + payload: { + basePermissionLevel, + firstName: 'Authorization', + groupIds, + lastName: 'User', + password: PASSWORD, + username: `authorization-${randomUUID()}`, + ...overrides + }, + url: '/v1/users' + }); + expect(response.statusCode).toBe(201); + return response.json(); + }; + + const setPermissions = async (userId: string, permissions: object[]) => { + const response = await request(adminHeaders, { + method: 'PUT', + payload: { kind: 'custom', permissions }, + url: `/v1/users/${userId}/permissions` + }); + expect(response.statusCode).toBe(200); + }; + + /** Records for one subject per id, uploaded into the group as an administrator. */ + const uploadRecords = async (groupId: string, subjectIds: string[]) => { + const response = await request(adminHeaders, { + method: 'POST', + payload: { + groupId, + instrumentId, + records: subjectIds.map((subjectId) => ({ data: { score: 1 }, date: new Date(), subjectId })) + }, + url: '/v1/instrument-records/upload' + }); + expect(response.statusCode).toBe(201); + }; + + /** A record for a new subject in the group, written the way a completed instrument is. */ + const createRecord = async (groupId: string): Promise => { + const session = await request(adminHeaders, { + method: 'POST', + payload: { + date: new Date(), + groupId, + subjectData: { id: `authorization-${randomUUID()}` }, + type: 'IN_PERSON' + }, + url: '/v1/sessions' + }); + expect(session.statusCode).toBe(201); + const { id: sessionId, subjectId } = session.json(); + const record = await request(adminHeaders, { + method: 'POST', + payload: { data: { score: 1 }, date: new Date(), groupId, instrumentId, sessionId, subjectId }, + url: '/v1/instrument-records' + }); + expect(record.statusCode).toBe(201); + return record.json(); + }; + + /** A subject enrolled in the group, holding no records. */ + const createSubject = async (groupId: string): Promise => { + const session = await request(adminHeaders, { + method: 'POST', + payload: { date: new Date(), groupId, subjectData: { id: `authorization-${randomUUID()}` }, type: 'IN_PERSON' }, + url: '/v1/sessions' + }); + expect(session.statusCode).toBe(201); + return session.json().subjectId; + }; + + /** + * A series owned by the group, stored directly: `POST /v1/instruments/series` checks the new title + * against every stored instrument, and earlier suites store placeholder bundles it cannot read. + */ + const createSeries = async (groupId: string): Promise => { + const series = await prisma.instrument.create({ + data: { + bundle: seriesBundle(`Series ${randomUUID()}`), + groupIds: [groupId], + id: `authorization-series-${randomUUID()}`, + seriesGroupId: groupId + } + }); + return series.id; + }; + + const readUsernames = async (headers: AuthHeaders): Promise => { + const response = await request(headers, { method: 'GET', url: '/v1/users' }); + expect(response.statusCode).toBe(200); + return response.json().map(({ username }) => username); + }; + + beforeAll(async () => { + prisma = this.app.get(PRISMA_CLIENT_TOKEN); + adminHeaders = await login('admin'); + // Through the upload route the playground uses, with the token it uses; the token tests below + // re-post this bundle. + const created = await request(await mintInstrumentToken(), { + method: 'POST', + payload: { bundle: scoreFormBundle(INSTRUMENT_NAME, { measured: true }) }, + url: '/v1/instruments' + }); + expect(created.statusCode).toBe(201); + instrumentId = created.json<{ id: string }>().id; + ownGroup = await createGroup(); + foreignGroup = await createGroup(); + manager = await createUser('GROUP_MANAGER', [ownGroup.id]); + standard = await createUser('STANDARD', [ownGroup.id]); + outsider = await createUser('GROUP_MANAGER', [foreignGroup.id]); + managerHeaders = await login(manager.username); + standardHeaders = await login(standard.username); + }); + + // The sidebar hiding an admin link is cosmetic: a non-admin who navigates to an admin screen + // directly gets the real screen (#1470). What makes that harmless is the api refusing every request + // those screens fire, each gated on an action no non-admin base level grants. + describe('privileged requests from the admin screens', () => { + let target: { groupId: string; userId: string }; + + beforeAll(async () => { + const group = await createGroup(); + target = { groupId: group.id, userId: (await createUser('GROUP_MANAGER', [group.id])).id }; + }); + + const privilegedRequests = (): { options: RequestOptions; what: string }[] => [ + { + options: { + method: 'POST', + payload: { + basePermissionLevel: 'ADMIN', + firstName: 'Escalation', + groupIds: [], + lastName: 'Probe', + password: PASSWORD, + username: `escalation-${randomUUID()}` + }, + url: '/v1/users' + }, + what: 'create an administrator' + }, + { + options: { method: 'PATCH', payload: { basePermissionLevel: 'ADMIN' }, url: `/v1/users/${target.userId}` }, + what: "raise another user's permission level" + }, + { options: { method: 'PATCH', url: `/v1/users/${target.userId}/archive` }, what: 'archive a user' }, + { + // Gated on `manage all` rather than `update User`: an `update User` grant is one of the things + // this route hands out, so holding one must not be enough to reach it. + options: { + method: 'PUT', + payload: { kind: 'custom', permissions: [{ action: 'manage', groupId: null, subject: 'all' }] }, + url: `/v1/users/${target.userId}/permissions` + }, + what: 'grant another user a permission' + }, + { + // The value sent is the seeded one, so were this ever to succeed it would change nothing. + options: { method: 'PATCH', payload: { isExperimentalFeaturesEnabled: false }, url: '/v1/setup' }, + what: 'change instance-wide settings' + }, + { + // `POST /v1/groups` used to be declared `create Group`, which every group manager passes: the + // guard sees the subject type, and their `manage Group` rule's condition is invisible to it. + options: { + method: 'POST', + payload: { name: `Escalation ${randomUUID()}`, type: 'CLINICAL' }, + url: '/v1/groups' + }, + what: 'create a group' + }, + { options: { method: 'PATCH', url: `/v1/groups/${target.groupId}/archive` }, what: 'archive a group' }, + { options: { method: 'PATCH', url: `/v1/groups/${target.groupId}/unarchive` }, what: 'unarchive a group' }, + { + options: { + method: 'POST', + payload: { url: 'https://github.com/example/example' }, + url: '/v1/instrument-repos' + }, + what: 'import an instrument repository' + }, + { options: { method: 'GET', url: '/v1/audit/logs' }, what: 'read the audit log' }, + { + // The response omits the password, but the host, username and sender address are still the + // institution's outbound mail identity. + options: { method: 'GET', url: '/v1/mail/settings' }, + what: 'read the mail configuration' + }, + { + options: { + method: 'PATCH', + payload: { newUserEmailTemplate: { body: {}, subject: {} } }, + url: '/v1/mail/settings' + }, + what: 'rewrite the mail configuration or templates' + }, + { + // The one request that opens an outbound SMTP connection to a caller-supplied host using the + // stored credential. + options: { method: 'POST', payload: {}, url: '/v1/mail/test' }, + what: 'probe a mail server with the stored credential' + } + ]; + + it.each(['GROUP_MANAGER', 'STANDARD'] as const)( + 'should refuse every privileged request a %s user could fire from an admin screen', + async (role) => { + const headers = role === 'GROUP_MANAGER' ? managerHeaders : standardHeaders; + for (const { options, what } of privilegedRequests()) { + const response = await request(headers, options); + expect.soft(response.statusCode, `a ${role} user must not be able to ${what}`).toBe(403); + } + } + ); + }); + + // `GET /v1/instruments/info` is gated on `read Instrument`, which a standard user holds, but + // resolving `subjectId` reads instrument records, which they do not. Scoping that lookup to the + // caller must answer with an empty list rather than failing the request. + it.each(['GROUP_MANAGER', 'STANDARD'] as const)( + 'should answer the subject-filtered instrument list for a %s user rather than erroring', + async (role) => { + const response = await request(role === 'GROUP_MANAGER' ? managerHeaders : standardHeaders, { + method: 'GET', + url: '/v1/instruments/info?subjectId=any-subject' + }); + expect(response.statusCode).toBe(200); + expect(response.json()).toStrictEqual([]); + } + ); + + // The playground uploads a bundle with a token minted by `GET /auth/create-instrument-token`, and + // that token is the only non-interactive caller of `POST /instruments`, which is how #1392 severed + // the two: the route moved to `manage Instrument` while the token still carried `create`. + describe('granular instrument token', () => { + it('should be accepted by the instrument upload, which interprets the bundle before refusing a duplicate', async () => { + const response = await request(await mintInstrumentToken(), { + method: 'POST', + payload: { bundle: scoreFormBundle(INSTRUMENT_NAME, { measured: true }) }, + url: '/v1/instruments' + }); + expect(response.statusCode).toBe(409); + }); + + it('should not mint its successor, so it expires an hour after it was issued', async () => { + const response = await request(await mintInstrumentToken(), { + method: 'GET', + url: '/v1/auth/create-instrument-token' + }); + expect(response.statusCode).toBe(403); + }); + + // The token's `manage Instrument` satisfies this route's `delete Instrument` with no group + // condition, so a guard that admitted it would answer 404 for an id that exists in no group. + it('should be refused on a route its permissions satisfy other than the upload', async () => { + const response = await request(await mintInstrumentToken(), { + method: 'DELETE', + url: `/v1/instruments/${'0'.repeat(24)}` + }); + expect(response.statusCode).toBe(403); + }); + }); + + // A group manager holds `manage Group` for their own groups, and `@RouteAccess` sees only the + // subject type, so the guard lets this through and the row scoping in `GroupsService` is the whole + // check. Asserted by effect rather than by status code for that reason. + it('should not let a group manager rename a group they do not belong to', async () => { + const response = await request(managerHeaders, { + method: 'PATCH', + payload: { name: `Hijacked ${randomUUID()}` }, + url: `/v1/groups/${foreignGroup.id}` + }); + expect(response.statusCode).toBeGreaterThanOrEqual(400); + const group = await request(adminHeaders, { method: 'GET', url: `/v1/groups/${foreignGroup.id}` }); + expect(group.json().name).toBe(foreignGroup.name); + }); + + // `create Assignment` is satisfied at the guard by a group manager's rule for their own groups, so + // the service alone decides which group an assignment is filed under. + it('should not let a group manager file an assignment under a group they do not belong to', async () => { + const subjectId = await createSubject(foreignGroup.id); + const response = await request(managerHeaders, { + method: 'POST', + payload: { expiresAt: new Date(Date.now() + 86_400_000), groupId: foreignGroup.id, instrumentId, subjectId }, + url: '/v1/assignments' + }); + expect(response.statusCode).toBe(404); + const assignments = await request(adminHeaders, { method: 'GET', url: `/v1/assignments?subjectId=${subjectId}` }); + expect(assignments.json()).toStrictEqual([]); + }); + + // The web client exports only the current group and then keeps only the subjects its table lists, + // so no screen can show whether the api itself leaves another group's records out. Both records + // belong to one subject enrolled in both groups, so only each record's own group separates them. + // The body is msgpack, whose entries carry their record's group id as a plain string, so its bytes + // are searched. + it("should leave another group's records out of a group manager's export, even of a subject enrolled in both groups", async () => { + const exportingGroup = await createGroup(); + const headers = await login((await createUser('GROUP_MANAGER', [exportingGroup.id])).username); + const sharedSubjectId = `authorization-shared-${randomUUID()}`; + await uploadRecords(exportingGroup.id, [sharedSubjectId]); + await uploadRecords(foreignGroup.id, [sharedSubjectId]); + + const response = await request(headers, { method: 'GET', url: '/v1/instrument-records/export' }); + + expect(response.statusCode).toBe(200); + expect(response.rawPayload.includes(exportingGroup.id)).toBe(true); + expect(response.rawPayload.includes(foreignGroup.id)).toBe(false); + }); + + // The datahub's instrument hub reads these endpoints with no `groupId`, so each decides on its own + // what the caller may see. The manager here belongs to a group holding no records at all, so + // anything the summaries name came from another group. + it.each(['by-instrument', 'by-series', 'by-subject'])( + "should leave another group's records out of the %s datahub summary a group manager reads", + async (summary) => { + await uploadRecords(foreignGroup.id, [`authorization-hub-${randomUUID()}`]); + const emptyGroup = await createGroup(); + const headers = await login((await createUser('GROUP_MANAGER', [emptyGroup.id])).username); + + const response = await request(headers, { method: 'GET', url: `/v1/instrument-records/summary/${summary}` }); + + expect(response.statusCode).toBe(200); + expect(response.json()).toStrictEqual([]); + } + ); + + describe('series records', () => { + const readSeriesRecords = (seriesInstrumentId: string) => { + return request(managerHeaders, { + method: 'GET', + url: `/v1/instrument-records?seriesInstrumentId=${seriesInstrumentId}` + }); + }; + + // `read Instrument` carries no group condition, so the ability cannot confine this lookup on its + // own: before the caller's groups narrowed it, a series another group owns answered 200 with an + // empty list where an unknown id answers 404, so the status alone reported that the series is real. + it("should answer for another group's series exactly as for one that does not exist", async () => { + const foreign = await readSeriesRecords(await createSeries(foreignGroup.id)); + const absent = await readSeriesRecords('0'.repeat(24)); + expect(foreign.statusCode).toBe(absent.statusCode); + expect(foreign.statusCode).toBe(404); + }); + + // Or the narrowing above would have closed the leak by breaking the hub it exists to serve. + it("should answer for a series the caller's own group owns", async () => { + const response = await readSeriesRecords(await createSeries(ownGroup.id)); + expect(response.statusCode).toBe(200); + expect(response.json()).toStrictEqual([]); + }); + }); + + // `/admin/users` renders a populated table for a non-admin, which is only acceptable because the + // rows it can read are the ones they may already see elsewhere in the app. + describe('GET /v1/users', () => { + it('should scope the list a group manager reads to their own group', async () => { + const usernames = await readUsernames(managerHeaders); + expect(usernames).toContain(standard.username); + expect(usernames).not.toContain(outsider.username); + }); + + it('should limit the list a standard user reads to their own account', async () => { + expect(await readUsernames(standardHeaders)).toStrictEqual([standard.username]); + }); + + it('should confine a granted read to the group it names, where an unscoped one reads every group', async () => { + const grantee = await createUser('STANDARD', [ownGroup.id]); + + // Permissions are signed into the token at login, so every grant needs a fresh one. + await setPermissions(grantee.id, [{ action: 'read', groupId: ownGroup.id, subject: 'User' }]); + const scoped = await readUsernames(await login(grantee.username)); + expect(scoped).toContain(manager.username); + expect(scoped).not.toContain(outsider.username); + + await setPermissions(grantee.id, [{ action: 'read', groupId: null, subject: 'User' }]); + expect(await readUsernames(await login(grantee.username))).toContain(outsider.username); + }); + }); + + // A grant is the only way a non-admin reaches `PATCH /instrument-records/:id`, since no base level + // below admin holds `update` or `delete` on records. + describe('PATCH /v1/instrument-records/:id with an update grant', () => { + let editorHeaders: AuthHeaders; + + const patchRecord = (id: string, data: object) => { + return request(editorHeaders, { method: 'PATCH', payload: { data }, url: `/v1/instrument-records/${id}` }); + }; + + beforeAll(async () => { + const editor = await createUser('STANDARD', [ownGroup.id]); + await setPermissions(editor.id, [{ action: 'update', groupId: ownGroup.id, subject: 'InstrumentRecord' }]); + editorHeaders = await login(editor.username); + }); + + // The write used to be scoped by the caller's `delete` rules rather than the `update` rules the + // route is gated on, so a holder of `update` alone passed the guard and had every write refused. + it('should let the holder edit a record in the group the grant names, without holding delete', async () => { + const record = await createRecord(ownGroup.id); + + const response = await patchRecord(record.id, { score: 3 }); + + expect(response.statusCode).toBe(200); + const stored = await request(adminHeaders, { method: 'GET', url: `/v1/instrument-records/${record.id}` }); + expect(stored.json().data).toStrictEqual({ score: 3 }); + }); + + // The lookup the payload is merged into is scoped as well. Before it was, an invalid payload for + // another group's record answered 400 where an unknown id answers 404, which reported the id real. + it('should answer an edit to a record outside the grant exactly as for one that does not exist', async () => { + const record = await createRecord(foreignGroup.id); + const invalid = { score: 'not a number' }; + + const foreign = await patchRecord(record.id, invalid); + const absent = await patchRecord('0'.repeat(24), invalid); + + expect(foreign.statusCode).toBe(absent.statusCode); + expect(foreign.statusCode).toBe(404); + }); + }); + + // The import route answers with the rows it wrote, and used to find them by re-querying the + // instrument with the request's optional `groupId` as the only filter, so omitting it returned every + // group's records for that instrument to the lowest role, which holds no `read InstrumentRecord`. + it('should answer an upload with only the records it wrote, never those of another group', async () => { + await uploadRecords(foreignGroup.id, [`authorization-foreign-${randomUUID()}`]); + const ownSubjectId = `authorization-own-${randomUUID()}`; + + const response = await request(standardHeaders, { + method: 'POST', + payload: { instrumentId, records: [{ data: { score: 1 }, date: new Date(), subjectId: ownSubjectId }] }, + url: '/v1/instrument-records/upload' + }); + + expect(response.statusCode).toBe(201); + expect(response.json().map(({ subjectId }) => subjectId)).toStrictEqual([ownSubjectId]); + }); + + // The token a user who must reset their password is issued carries no permission but to read + // themselves, so a client that ignores the web app's redirect to the reset page still reaches nothing. + describe('a user who must reset their password', () => { + let user: User; + let headers: AuthHeaders; + + beforeAll(async () => { + user = await createUser('GROUP_MANAGER', [ownGroup.id], { mustResetPassword: true }); + headers = await login(user.username); + }); + + it.each(['/v1/subjects', '/v1/sessions', '/v1/groups', '/v1/assignments'])( + 'should be refused GET %s by the api, not only by the router', + async (url) => { + expect((await request(headers, { method: 'GET', url })).statusCode).toBe(403); + } + ); + + // `read User` is granted, because the reset itself is gated on it. `@RouteAccess` only checks the + // subject type, so this route answers 200; what confines it is the rule's condition, applied by + // `accessibleQuery`, which reduces the list to the one user asking. + it('should read only their own account from the user list', async () => { + const response = await request(headers, { method: 'GET', url: '/v1/users' }); + expect(response.statusCode).toBe(200); + expect(response.json().map(({ id }) => id)).toStrictEqual([user.id]); + }); + }); +}); diff --git a/apps/api/test/suites/09-group-access.suite.ts b/apps/api/test/suites/09-group-access.suite.ts new file mode 100644 index 000000000..413b1cbee --- /dev/null +++ b/apps/api/test/suites/09-group-access.suite.ts @@ -0,0 +1,139 @@ +import { randomUUID } from 'node:crypto'; + +import type { Group } from '@opendatacapture/schemas/group'; +import type { Session } from '@opendatacapture/schemas/session'; +import { beforeAll, expect, it } from 'vitest'; + +import { InstrumentsService } from '@/instruments/instruments.service'; + +import { defineSuite, scoreFormBundle } from '../helpers'; + +const PASSWORD = 'DataCapture2025_Test'; + +type AuthHeaders = { authorization: string }; + +export default defineSuite('group access', function () { + let adminHeaders: AuthHeaders; + + const login = async (username: string): Promise => { + const response = await this.app.inject({ + method: 'POST', + payload: { password: PASSWORD, username }, + url: '/v1/auth/login' + }); + expect(response.statusCode).toBe(200); + return { authorization: `Bearer ${response.json<{ accessToken: string }>().accessToken}` }; + }; + + const createGroup = async (): Promise => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { name: `Group Access ${randomUUID()}`, type: 'CLINICAL' }, + url: '/v1/groups' + }); + expect(response.statusCode).toBe(201); + return response.json(); + }; + + /** A group manager of `group`, by username; every one shares {@link PASSWORD}. */ + const createManager = async (group: Group): Promise => { + const username = `group-access-${randomUUID()}`; + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + basePermissionLevel: 'GROUP_MANAGER', + firstName: 'Group', + groupIds: [group.id], + lastName: 'Manager', + password: PASSWORD, + username + }, + url: '/v1/users' + }); + expect(response.statusCode).toBe(201); + return username; + }; + + const readGroup = (id: string, headers: AuthHeaders) => { + return this.app.inject({ headers, method: 'GET', url: `/v1/groups/${id}` }); + }; + + beforeAll(async () => { + adminHeaders = await login('admin'); + }); + + // Deliberately POST alone, without the PATCH a seeding helper follows it with, so creation must + // connect the instrument itself. An uploaded instrument has no `sourceRepoId` key at all, and the + // selection has to match that absence as well as an explicit null. + it('should give a newly created group access to every uploaded instrument', async () => { + const instrument = await this.app.get(InstrumentsService).create({ bundle: scoreFormBundle('GROUP_ACCESS_NEW') }); + + const group = await createGroup(); + + expect(group.accessibleInstrumentIds).toContain(instrument.id); + }); + + it('should keep the accessible instruments and settings a group manager saves for their group', async () => { + const instruments = await Promise.all( + ['GROUP_ACCESS_KEPT', 'GROUP_ACCESS_DROPPED'].map((name) => { + return this.app.get(InstrumentsService).create({ bundle: scoreFormBundle(name) }); + }) + ); + const group = await createGroup(); + const managerHeaders = await login(await createManager(group)); + const accessibleInstrumentIds = group.accessibleInstrumentIds.filter((id) => id !== instruments[1]!.id); + + const update = await this.app.inject({ + headers: managerHeaders, + method: 'PATCH', + payload: { accessibleInstrumentIds, settings: { subjectIdDisplayLength: 6 } }, + url: `/v1/groups/${group.id}` + }); + expect(update.statusCode).toBe(200); + + const saved = (await readGroup(group.id, managerHeaders)).json(); + expect(saved.accessibleInstrumentIds).toContain(instruments[0]!.id); + expect(saved.accessibleInstrumentIds).not.toContain(instruments[1]!.id); + expect(saved.settings.subjectIdDisplayLength).toBe(6); + }); + + // An archive reaches a member only through the token they are issued next, so each read signs in + // afresh. + it('should take an archived group and its subjects away from its members, and give them back on unarchive', async () => { + const group = await createGroup(); + const session = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { date: new Date(), groupId: group.id, subjectData: { id: randomUUID() }, type: 'IN_PERSON' }, + url: '/v1/sessions' + }); + expect(session.statusCode).toBe(201); + const { subjectId } = session.json(); + const username = await createManager(group); + + const readAsMember = async () => { + const headers = await login(username); + const [groupResponse, subjectResponse] = await Promise.all([ + readGroup(group.id, headers), + this.app.inject({ headers, method: 'GET', url: `/v1/subjects/${subjectId}` }) + ]); + return { group: groupResponse.statusCode, subject: subjectResponse.statusCode }; + }; + const setArchived = async (action: 'archive' | 'unarchive') => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'PATCH', + url: `/v1/groups/${group.id}/${action}` + }); + expect(response.statusCode).toBe(200); + }; + + expect(await readAsMember()).toStrictEqual({ group: 200, subject: 200 }); + await setArchived('archive'); + expect(await readAsMember()).toStrictEqual({ group: 404, subject: 404 }); + await setArchived('unarchive'); + expect(await readAsMember()).toStrictEqual({ group: 200, subject: 200 }); + }); +}); diff --git a/apps/api/test/suites/09-instrument-record-upload.suite.ts b/apps/api/test/suites/09-instrument-record-upload.suite.ts new file mode 100644 index 000000000..e9a3f9811 --- /dev/null +++ b/apps/api/test/suites/09-instrument-record-upload.suite.ts @@ -0,0 +1,60 @@ +import { randomUUID } from 'node:crypto'; + +import { beforeAll, expect, it } from 'vitest'; + +import { InstrumentsService } from '@/instruments/instruments.service'; + +import { defineSuite, scoreFormBundle } from '../helpers'; + +type UploadedRecord = { subjectId: string }; + +export default defineSuite('instrument record upload', function () { + let adminHeaders: { authorization: string }; + let groupId: string; + let instrumentId: string; + + const upload = async (subjectIds: string[]): Promise => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + groupId, + instrumentId, + records: subjectIds.map((subjectId) => ({ data: { score: 1 }, date: new Date(), subjectId })) + }, + url: '/v1/instrument-records/upload' + }); + expect(response.statusCode).toBe(201); + return response.json(); + }; + + beforeAll(async () => { + const login = await this.app.inject({ + method: 'POST', + payload: { password: 'DataCapture2025_Test', username: 'admin' }, + url: '/v1/auth/login' + }); + expect(login.statusCode).toBe(200); + adminHeaders = { authorization: `Bearer ${login.json<{ accessToken: string }>().accessToken}` }; + const group = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { name: 'Record Upload', type: 'RESEARCH' }, + url: '/v1/groups' + }); + expect(group.statusCode).toBe(201); + groupId = group.json<{ id: string }>().id; + instrumentId = (await this.app.get(InstrumentsService).create({ bundle: scoreFormBundle('RECORD_UPLOAD') })).id; + }); + + // The response was once every record in the group for the instrument, so a second upload handed + // the first one's records back to the caller. The earlier upload is what tells the two apart. + it('should answer a batch upload with exactly the records it created', async () => { + await upload([`upload-earlier-${randomUUID()}`]); + const batch = ['a', 'b', 'c'].map((suffix) => `upload-${suffix}-${randomUUID()}`); + + const records = await upload(batch); + + expect(records.map((record) => record.subjectId).sort()).toStrictEqual([...batch].sort()); + }); +}); diff --git a/apps/api/test/suites/09-log-sorting.suite.ts b/apps/api/test/suites/09-log-sorting.suite.ts new file mode 100644 index 000000000..2a765b545 --- /dev/null +++ b/apps/api/test/suites/09-log-sorting.suite.ts @@ -0,0 +1,97 @@ +import { PRISMA_CLIENT_TOKEN } from '@douglasneuroinformatics/libnest'; +import { $AuditLogsSortBy, AUDIT_LOGS_MAX_PAGE_SIZE } from '@opendatacapture/schemas/audit'; +import type { $AuditLogsPage } from '@opendatacapture/schemas/audit'; +import { $InstrumentRepoSyncLogsSortBy } from '@opendatacapture/schemas/instrument-repo'; +import type { InstrumentRepoSyncLogsPage } from '@opendatacapture/schemas/instrument-repo'; +import { beforeAll, describe, expect, it } from 'vitest'; + +import type { RuntimePrismaClient } from '@/core/prisma'; + +import { defineSuite } from '../helpers'; + +const SORT_ORDERS = ['asc', 'desc'] as const; + +// Several orderings reach Mongo as a relation or a composite-list count rather than a field, which +// only a real database can prove it accepts. +export default defineSuite('log sorting', function () { + let adminHeaders: { authorization: string }; + + beforeAll(async () => { + const login = await this.app.inject({ + method: 'POST', + payload: { password: 'DataCapture2025_Test', username: 'admin' }, + url: '/v1/auth/login' + }); + expect(login.statusCode).toBe(200); + adminHeaders = { authorization: `Bearer ${login.json<{ accessToken: string }>().accessToken}` }; + }); + + describe('GET /v1/audit/logs', () => { + const findAuditLogs = async (query: { [key: string]: string }) => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'GET', + query: { limit: String(AUDIT_LOGS_MAX_PAGE_SIZE), ...query }, + url: '/v1/audit/logs' + }); + expect(response.statusCode).toBe(200); + return response.json<$AuditLogsPage>().data; + }; + + it.each($AuditLogsSortBy.options.flatMap((sortBy) => SORT_ORDERS.map((sortOrder) => [sortBy, sortOrder])))( + 'should order the audit log by %s %s', + async (sortBy, sortOrder) => { + expect(await findAuditLogs({ sortBy, sortOrder })).not.toHaveLength(0); + } + ); + + it('should order the audit log by the username of each entry, which is a relation', async () => { + const usernames = (await findAuditLogs({ sortBy: 'user', sortOrder: 'asc' })).flatMap((log) => { + return log.user ? [log.user.username] : []; + }); + expect(usernames.length).toBeGreaterThan(1); + expect(usernames).toStrictEqual([...usernames].sort()); + }); + }); + + describe('GET /v1/instrument-repos/sync/logs', () => { + const failure = { instrumentDir: 'forms/broken', message: 'Failed to bundle', stage: 'BUNDLE' } as const; + + const findSyncLogs = async (query: { [key: string]: string }) => { + const response = await this.app.inject({ + headers: adminHeaders, + method: 'GET', + query, + url: '/v1/instrument-repos/sync/logs' + }); + expect(response.statusCode).toBe(200); + return response.json().data; + }; + + beforeAll(async () => { + const prisma = this.app.get(PRISMA_CLIENT_TOKEN); + await prisma.instrumentRepoSyncLog.createMany({ + data: [ + { action: 'SYNC', failures: [failure, failure], importedCount: 0, repoName: 'log-sorting/two-failures' }, + { action: 'IMPORT', failures: [], importedCount: 2, repoName: 'log-sorting/no-failures' }, + { action: 'SYNC', failures: [failure], importedCount: 1, repoName: 'log-sorting/one-failure' } + ] + }); + }); + + it.each( + $InstrumentRepoSyncLogsSortBy.options.flatMap((sortBy) => SORT_ORDERS.map((sortOrder) => [sortBy, sortOrder])) + )('should order the activity log by %s %s', async (sortBy, sortOrder) => { + expect(await findSyncLogs({ sortBy, sortOrder })).not.toHaveLength(0); + }); + + it('should order the activity log by how many instruments failed, which is the length of a list', async () => { + const logs = await findSyncLogs({ sortBy: 'failedCount', sortOrder: 'asc' }); + expect(logs.filter((log) => log.repoName.startsWith('log-sorting/')).map((log) => log.repoName)).toStrictEqual([ + 'log-sorting/no-failures', + 'log-sorting/one-failure', + 'log-sorting/two-failures' + ]); + }); + }); +}); diff --git a/apps/api/test/suites/09-subject-record-filter.suite.ts b/apps/api/test/suites/09-subject-record-filter.suite.ts new file mode 100644 index 000000000..d7765a0b7 --- /dev/null +++ b/apps/api/test/suites/09-subject-record-filter.suite.ts @@ -0,0 +1,58 @@ +import { randomUUID } from 'node:crypto'; + +import type { Group } from '@opendatacapture/schemas/group'; +import { expect, it } from 'vitest'; + +import { defineSuite } from '../helpers'; + +const PASSWORD = 'DataCapture2025_Test'; + +export default defineSuite('subject record filter', function () { + const login = async (username: string) => { + const response = await this.app.inject({ + method: 'POST', + payload: { password: PASSWORD, username }, + url: '/v1/auth/login' + }); + expect(response.statusCode).toBe(200); + return { authorization: `Bearer ${response.json<{ accessToken: string }>().accessToken}` }; + }; + + // `GET /v1/subjects` is gated on `read Subject`, which a standard user holds, but resolving + // `hasRecord` reads instrument records, which they do not. The honest answer is an empty list. + it('should answer the with-records filter with no subjects for a caller who may read no records', async () => { + const adminHeaders = await login('admin'); + const group = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { name: `Record Filter ${randomUUID()}`, type: 'RESEARCH' }, + url: '/v1/groups' + }); + expect(group.statusCode).toBe(201); + const username = `record-filter-${randomUUID()}`; + const user = await this.app.inject({ + headers: adminHeaders, + method: 'POST', + payload: { + basePermissionLevel: 'STANDARD', + firstName: 'Record', + groupIds: [group.json().id], + lastName: 'Filter', + password: PASSWORD, + username + }, + url: '/v1/users' + }); + expect(user.statusCode).toBe(201); + + const response = await this.app.inject({ + headers: await login(username), + method: 'GET', + query: { hasRecord: 'true' }, + url: '/v1/subjects' + }); + + expect(response.statusCode).toBe(200); + expect(response.json()).toStrictEqual([]); + }); +}); diff --git a/apps/gateway/src/__tests__/Root.test.tsx b/apps/gateway/src/__tests__/Root.test.tsx index dd4c0e602..a5209d374 100644 --- a/apps/gateway/src/__tests__/Root.test.tsx +++ b/apps/gateway/src/__tests__/Root.test.tsx @@ -1,9 +1,10 @@ // @vitest-environment happy-dom import { useNotificationsStore } from '@douglasneuroinformatics/libui/hooks'; +import { i18n } from '@douglasneuroinformatics/libui/i18n'; import type { InstrumentRendererProps, InstrumentSubmitHandler } from '@opendatacapture/react-core'; import type { InstrumentBundleContainer } from '@opendatacapture/schemas/instrument'; -import { act, cleanup, render, screen } from '@testing-library/react'; +import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'; import axios from 'axios'; import type { MockInstance } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; @@ -98,6 +99,7 @@ describe('Root', () => { afterEach(() => { cleanup(); vi.restoreAllMocks(); + i18n.changeLanguage('en'); }); it('should reveal the page once mounted on the client, so unstyled server markup is never shown', () => { @@ -110,6 +112,14 @@ describe('Root', () => { expect(screen.getByTestId('gateway-landing')).toBeTruthy(); }); + it('should translate the landing page when the patient picks another language, so one who cannot read the default can follow it', async () => { + render(); + expect(screen.getByRole('heading').textContent).toBe('Welcome'); + fireEvent.keyDown(screen.getByTestId('language-toggle').querySelector('button')!, { key: 'Enter' }); + fireEvent.click(await screen.findByRole('menuitem', { name: 'Français' })); + expect(screen.getByRole('heading').textContent).toBe('Bienvenue'); + }); + it('should keep the instrument locked until the subject solves the verification challenge', () => { renderAssignment(); expect(screen.getByTestId('instrument-renderer').getAttribute('data-disable-begin')).toBe('true'); diff --git a/apps/gateway/src/__tests__/entry-server.test.ts b/apps/gateway/src/__tests__/entry-server.test.ts index 63c2931a6..a5c57dcd6 100644 --- a/apps/gateway/src/__tests__/entry-server.test.ts +++ b/apps/gateway/src/__tests__/entry-server.test.ts @@ -7,6 +7,7 @@ describe('render', () => { const { html } = render({ activeLanguages: ['en', 'fr'], kind: 'landing', language: 'en' }); expect(html).toContain('data-testid="gateway-landing"'); expect(html).toContain('Welcome'); + expect(html).toContain('open the secure link you were sent'); }); it('should render in the language it is given rather than the one the previous request left on the shared translator', () => { @@ -25,4 +26,16 @@ describe('render', () => { }); expect(html).not.toContain('data-testid="gateway-landing"'); }); + + it('should render an assignment in the language it is given, so the first paint is not swapped after hydration', () => { + const { html } = render({ + activeLanguages: ['en', 'fr'], + id: 'assignment-1', + kind: 'assignment', + language: 'fr', + target: { bundle: 'export default {}', id: 'instrument-1', kind: 'FORM' }, + token: 'token' + }); + expect(html).toContain('Aperçu'); + }); }); diff --git a/apps/gateway/src/routers/__tests__/api.router.test.ts b/apps/gateway/src/routers/__tests__/api.router.test.ts index 00788060f..fda12a7c2 100644 --- a/apps/gateway/src/routers/__tests__/api.router.test.ts +++ b/apps/gateway/src/routers/__tests__/api.router.test.ts @@ -246,7 +246,8 @@ describe('apiRouter', () => { it('should start a series with a leading separator, so the first item is counted as submitted', async () => { markAssignmentVerified('series-first'); const { privateKey } = await createStoredAssignment('series-first', { encryptedData: null, symmetricKey: null }); - await request('PATCH', '/assignments/series-first', { data: { item: 1 }, kind: 'SERIES' }); + const response = await request('PATCH', '/assignments/series-first', { data: { item: 1 }, kind: 'SERIES' }); + expect(response.status).toBe(200); const { data } = prisma.remoteAssignmentModel.update.mock.lastCall![0]; const [emptyData, cipherText] = data.encryptedData.split('$'); const [emptyKey, symmetricKey] = data.symmetricKey.split('$'); diff --git a/apps/gateway/src/routers/__tests__/cap.router.test.ts b/apps/gateway/src/routers/__tests__/cap.router.test.ts index 91ae46e78..9a20fb502 100644 --- a/apps/gateway/src/routers/__tests__/cap.router.test.ts +++ b/apps/gateway/src/routers/__tests__/cap.router.test.ts @@ -105,6 +105,14 @@ describe('capRouter', () => { expect(cap.validateToken).not.toHaveBeenCalled(); }); + it('should refuse an id longer than any assignment id whatever token accompanies it, so the set cannot grow unbounded', async () => { + const id = 'x'.repeat(10_000); + const response = await post('/verify', { id, token: 'unsolved' }); + expect(response.status).toBe(400); + expect(cap.validateToken).not.toHaveBeenCalled(); + expect(isAssignmentVerified(id)).toBe(false); + }); + it('should refuse an invalid token without marking the assignment verified, so a bot cannot submit', async () => { cap.validateToken.mockResolvedValueOnce({ success: false }); const response = await post('/verify', { id: 'assignment-invalid', token: 'bad-token' }); diff --git a/apps/playground/src/components/Editor/__tests__/EditorTab.test.tsx b/apps/playground/src/components/Editor/__tests__/EditorTab.test.tsx new file mode 100644 index 000000000..dc478d8ee --- /dev/null +++ b/apps/playground/src/components/Editor/__tests__/EditorTab.test.tsx @@ -0,0 +1,55 @@ +// @vitest-environment happy-dom + +import { cleanup, fireEvent, render, screen } from '@testing-library/react'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +import { EditorTab } from '../EditorTab'; + +const store = vi.hoisted(() => ({ + state: { + closeFile: (_filename: string) => undefined, + selectedFilename: 'b.ts', + selectFile: (_filename: string) => undefined + } +})); + +vi.mock('@/store', () => ({ + useAppStore: (selector: (state: typeof store.state) => TSelected) => selector(store.state) +})); + +describe('EditorTab', () => { + beforeEach(() => { + store.state.closeFile = vi.fn(); + store.state.selectFile = vi.fn(); + }); + + afterEach(() => { + cleanup(); + }); + + it('should close its file without selecting it when its X is clicked, so closing a background tab keeps the active one', () => { + const { container } = render(); + fireEvent.click(container.querySelector('svg')!); + expect(store.state.closeFile).toHaveBeenCalledExactlyOnceWith('a.ts'); + expect(store.state.selectFile).not.toHaveBeenCalled(); + }); + + it('should select its file when clicked anywhere but its X', () => { + render(); + fireEvent.click(screen.getByText('a.ts')); + expect(store.state.selectFile).toHaveBeenCalledExactlyOnceWith('a.ts'); + expect(store.state.closeFile).not.toHaveBeenCalled(); + }); + + it('should mark only the tab of the selected file as current', () => { + render( + <> + + + + ); + const [first, second] = screen.getAllByRole('button'); + expect(first!.getAttribute('aria-current')).toBe('false'); + expect(second!.getAttribute('aria-current')).toBe('true'); + }); +}); diff --git a/apps/playground/src/components/Header/ActionsDropdown/__tests__/DeleteInstrumentDialog.test.tsx b/apps/playground/src/components/Header/ActionsDropdown/__tests__/DeleteInstrumentDialog.test.tsx new file mode 100644 index 000000000..3a48f9eac --- /dev/null +++ b/apps/playground/src/components/Header/ActionsDropdown/__tests__/DeleteInstrumentDialog.test.tsx @@ -0,0 +1,40 @@ +// @vitest-environment happy-dom + +import { i18n } from '@douglasneuroinformatics/libui/i18n'; +import { cleanup, fireEvent, render, screen } from '@testing-library/react'; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; + +import { DeleteInstrumentDialog } from '../DeleteInstrumentDialog'; + +const store = vi.hoisted(() => ({ + state: { + removeInstrument: (_id: string) => undefined, + selectedInstrument: { id: 'mine' } + } +})); + +vi.mock('@/store', () => ({ + useAppStore: (selector: (state: typeof store.state) => TSelected) => selector(store.state) +})); + +describe('DeleteInstrumentDialog', () => { + beforeAll(() => { + i18n.init({ translations: {} }); + }); + + beforeEach(() => { + store.state.removeInstrument = vi.fn(); + }); + + afterEach(() => { + cleanup(); + }); + + it('should remove the selected instrument once deletion is confirmed, which makes the store load the default template', () => { + const setIsOpen = vi.fn(); + render(); + fireEvent.click(screen.getByRole('button', { name: 'Delete' })); + expect(store.state.removeInstrument).toHaveBeenCalledExactlyOnceWith('mine'); + expect(setIsOpen).toHaveBeenCalledWith(false); + }); +}); diff --git a/apps/playground/src/components/Header/SaveButton/__tests__/SaveButton.test.tsx b/apps/playground/src/components/Header/SaveButton/__tests__/SaveButton.test.tsx new file mode 100644 index 000000000..e7c40fd84 --- /dev/null +++ b/apps/playground/src/components/Header/SaveButton/__tests__/SaveButton.test.tsx @@ -0,0 +1,60 @@ +// @vitest-environment happy-dom + +import { i18n } from '@douglasneuroinformatics/libui/i18n'; +import { act, cleanup, render, screen } from '@testing-library/react'; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; + +import type { EditorFile } from '@/models/editor-file.model'; +import type { InstrumentRepository } from '@/models/instrument-repository.model'; + +import { SaveButton } from '../SaveButton'; + +const savedFiles: EditorFile[] = [{ content: 'export default {};', name: 'index.ts' }]; + +const store = vi.hoisted(() => ({ + editorFiles: { current: [] as EditorFile[] }, + state: { + selectedInstrument: null as null | Pick, + updateSelectedInstrument: () => undefined + } +})); + +vi.mock('@/hooks/useFilesRef', () => ({ useFilesRef: () => store.editorFiles })); + +vi.mock('@/store', () => ({ + useAppStore: (selector: (state: typeof store.state) => TSelected) => selector(store.state) +})); + +const tick = () => { + act(() => { + vi.advanceTimersByTime(1000); + }); +}; + +describe('SaveButton', () => { + beforeAll(() => { + i18n.init({ translations: {} }); + }); + + beforeEach(() => { + vi.useFakeTimers(); + store.editorFiles.current = [{ content: 'export default { edited: true };', name: 'index.ts' }]; + store.state.selectedInstrument = { category: 'Saved', files: savedFiles }; + }); + + afterEach(() => { + cleanup(); + vi.useRealTimers(); + }); + + it('should disable Save once an edited saved instrument gives way to an example, so edits are never written into one', () => { + const { rerender } = render(); + tick(); + expect(screen.getByRole('button').disabled).toBe(false); + + store.state.selectedInstrument = { category: 'Examples', files: savedFiles }; + rerender(); + tick(); + expect(screen.getByRole('button').disabled).toBe(true); + }); +}); diff --git a/apps/playground/src/components/MainContent/__tests__/MainContent.test.tsx b/apps/playground/src/components/MainContent/__tests__/MainContent.test.tsx new file mode 100644 index 000000000..4e145babb --- /dev/null +++ b/apps/playground/src/components/MainContent/__tests__/MainContent.test.tsx @@ -0,0 +1,36 @@ +import { renderToStaticMarkup } from 'react-dom/server'; + +import { i18n } from '@douglasneuroinformatics/libui/i18n'; +import type { PanelProps } from 'react-resizable-panels'; +import { beforeAll, describe, expect, it, vi } from 'vitest'; + +import { MainContent } from '../MainContent'; + +const Panel = vi.hoisted(() => vi.fn((_props: PanelProps) => null)); + +vi.mock('@douglasneuroinformatics/libui/hooks', async (importOriginal) => ({ + ...(await importOriginal()), + useMediaQuery: () => true +})); + +vi.mock('@/components/Editor', () => ({ Editor: () => null })); + +vi.mock('@/components/Viewer', () => ({ Viewer: () => null })); + +vi.mock('@/components/Resizable', () => ({ + Resizable: { Handle: () => null, Panel, PanelGroup: ({ children }: PanelProps) => children } +})); + +describe('MainContent', () => { + beforeAll(() => { + i18n.init({ translations: {} }); + }); + + it('should size the desktop panes as percentages, since react-resizable-panels reads a bare number as pixels', () => { + renderToStaticMarkup(); + expect(Panel.mock.calls.map(([props]) => props)).toMatchObject([ + { defaultSize: '66%', id: 'editor-pane', minSize: '25%' }, + { defaultSize: '34%', id: 'preview-pane', minSize: '25%' } + ]); + }); +}); diff --git a/apps/playground/src/components/Viewer/__tests__/Viewer.test.tsx b/apps/playground/src/components/Viewer/__tests__/Viewer.test.tsx new file mode 100644 index 000000000..2db4fe4d1 --- /dev/null +++ b/apps/playground/src/components/Viewer/__tests__/Viewer.test.tsx @@ -0,0 +1,54 @@ +// @vitest-environment happy-dom + +import { Suspense } from 'react'; + +import { i18n } from '@douglasneuroinformatics/libui/i18n'; +import { act, cleanup, render, screen } from '@testing-library/react'; +import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; + +import { Viewer } from '../Viewer'; + +const store = vi.hoisted(() => ({ + state: { + indexFilename: 'index.ts', + settings: { refreshInterval: 2000 }, + setTranspilerState: () => undefined, + transpilerState: { status: 'initial' }, + viewer: { key: 0 } + } +})); + +// The server names the editor's own origin, which is what a deployment that points +// PLAYGROUND_PREVIEW_ORIGIN at the editor's host serves. +const config = vi.hoisted(() => Promise.resolve({ previewOrigin: window.location.origin })); + +vi.mock('@/preview/config', () => ({ fetchPlaygroundConfig: () => config })); + +vi.mock('@/hooks/useFilesRef', () => ({ useFilesRef: () => ({ current: [] }) })); + +vi.mock('@/store', () => ({ + useAppStore: (selector: (state: typeof store.state) => TSelected) => selector(store.state) +})); + +describe('Viewer', () => { + beforeAll(() => { + i18n.init({ translations: {} }); + }); + + afterEach(() => { + cleanup(); + }); + + it('should refuse to run the preview when the server names the editor origin, rather than evaluate instruments beside the editor', async () => { + await act(async () => { + render( + + + + ); + await config; + }); + expect(await screen.findByRole('heading', { name: 'Preview Unavailable' })).toBeDefined(); + expect(screen.queryByTestId('preview-frame')).toBeNull(); + }); +}); diff --git a/apps/playground/src/utils/__tests__/file.test.ts b/apps/playground/src/utils/__tests__/file.test.ts new file mode 100644 index 000000000..9ece7b671 --- /dev/null +++ b/apps/playground/src/utils/__tests__/file.test.ts @@ -0,0 +1,9 @@ +import { describe, expect, it } from 'vitest'; + +import { editorFileToInput } from '../file'; + +describe('editorFileToInput', () => { + it('should decode a base64 asset whose extension is upper case, so the bundler receives the image rather than its text', () => { + expect(editorFileToInput({ content: 'AAEC', name: 'stim.PNG' }).content).toEqual(new Uint8Array([0, 1, 2])); + }); +}); diff --git a/apps/web/src/components/BulkRemoteAssignmentWizard/__tests__/BulkRemoteAssignmentWizard.test.tsx b/apps/web/src/components/BulkRemoteAssignmentWizard/__tests__/BulkRemoteAssignmentWizard.test.tsx index 9c11ca8e1..6c4ae5d75 100644 --- a/apps/web/src/components/BulkRemoteAssignmentWizard/__tests__/BulkRemoteAssignmentWizard.test.tsx +++ b/apps/web/src/components/BulkRemoteAssignmentWizard/__tests__/BulkRemoteAssignmentWizard.test.tsx @@ -223,17 +223,19 @@ describe('BulkRemoteAssignmentWizard', () => { expect(wizard.setSourceMode).toHaveBeenCalledWith('FILE'); }); - it('should hand the source step the selection the wizard holds, so stepping back does not discard it', () => { - const wizard = mockWizard('SOURCE'); + it('should hand the source step the selection and pasted text the wizard holds, so stepping back does not discard them', () => { + const wizard = mockWizard('SOURCE', { pastedText: 'subjectId\npasted-1\n' }); renderWizard(); expect(propsOf(SourceStep)).toMatchObject({ groupName: 'Depression Clinic', isMailEnabled: false, onBack, onParsed: wizard.acceptParsed, + onPastedChange: wizard.setPastedText, onSelectedChange: wizard.setSelectedSubjectIds, onStepChange: wizard.goTo, onSubjectsSelected: wizard.selectSubjects, + pasted: 'subjectId\npasted-1\n', selectedIds: wizard.subjectIds, subjectIdDisplayLength: 9, subjects @@ -280,7 +282,7 @@ describe('BulkRemoteAssignmentWizard', () => { describe('timepoints step', () => { it('should hand the timepoints step the batch it is building and the preflight state', () => { - const wizard = mockWizard('TIMEPOINTS', { isPreflighting: true }); + const wizard = mockWizard('TIMEPOINTS', { isPreflighting: true, redirectUrl: 'https://example.com/done' }); renderWizard(); expect(propsOf(TimepointsStep)).toMatchObject({ defaultExpiresAt: '2026-11-01', @@ -288,7 +290,9 @@ describe('BulkRemoteAssignmentWizard', () => { isLoading: true, onChange: wizard.setTimepoints, onConfirm: wizard.confirmTimepoints, + onRedirectUrlChange: wizard.setRedirectUrl, onStepChange: wizard.goTo, + redirectUrl: 'https://example.com/done', subjectCount: 2, timepoints }); @@ -308,7 +312,12 @@ describe('BulkRemoteAssignmentWizard', () => { code: 'BULK_ASSIGNMENT_REFUSED', issues: [{ kind: 'SUBJECT_UNAVAILABLE', subjectIds: ['Depression_Clinic$001'] }] }; - const wizard = mockWizard('REVIEW', { failure, isSubmitting: true, transportError: true }); + const wizard = mockWizard('REVIEW', { + failure, + isSubmitting: true, + redirectUrl: 'https://example.com/done', + transportError: true + }); renderWizard(); expect(propsOf(ReviewStep)).toMatchObject({ describeSubject: wizard.describeSubject, @@ -316,6 +325,7 @@ describe('BulkRemoteAssignmentWizard', () => { isSubmitting: true, onStepChange: wizard.goTo, onSubmit: wizard.submit, + redirectUrl: 'https://example.com/done', subjectCount: 2, timepoints, transportError: true diff --git a/apps/web/src/components/InstrumentTable/__tests__/InstrumentTable.test.tsx b/apps/web/src/components/InstrumentTable/__tests__/InstrumentTable.test.tsx index fcf665c92..c1d559e25 100644 --- a/apps/web/src/components/InstrumentTable/__tests__/InstrumentTable.test.tsx +++ b/apps/web/src/components/InstrumentTable/__tests__/InstrumentTable.test.tsx @@ -1,9 +1,10 @@ import { i18n } from '@douglasneuroinformatics/libui/i18n'; -import { cleanup, fireEvent, render, screen } from '@testing-library/react'; +import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { InstrumentTable } from '@/components/InstrumentTable/InstrumentTable'; import type { InstrumentRow } from '@/components/InstrumentTable/InstrumentTable'; +import { savedTableViews } from '@/services/table-views'; import '@/services/i18n'; @@ -90,6 +91,8 @@ const openPreview = () => { describe('InstrumentTable', () => { beforeEach(() => { vi.clearAllMocks(); + // The table restores the sort it was left with, so one test's sort would carry into the next. + savedTableViews.clear(); mocks.instrumentInfo = []; mocks.preview.current = null; i18n.changeLanguage('en'); @@ -219,6 +222,21 @@ describe('InstrumentTable', () => { expect(onOpen).toHaveBeenCalledWith(expect.objectContaining({ id: 'hq-1' })); }); + // Sorting is the only way to find an instrument in a long catalog without searching for it, so the + // header has to reorder the rows rather than merely render a chevron. + it('should reorder the rows by title, ascending and then descending, as the Instrument header is clicked', () => { + renderTable([ + row({ id: 'beta', title: 'Beta' }), + row({ id: 'alpha', title: 'Alpha' }), + row({ id: 'gamma', title: 'Gamma' }) + ]); + const header = within(screen.getByTestId('data-table-head')).getByText('Instrument').closest('button')!; + fireEvent.click(header); + expect(listedTitles()).toStrictEqual(['Alpha', 'Beta', 'Gamma']); + fireEvent.click(header); + expect(listedTitles()).toStrictEqual(['Gamma', 'Beta', 'Alpha']); + }); + it('should match the search against the instrument title', () => { renderTable([row(), row({ id: 'stroop-1', title: 'Stroop Task' })]); fireEvent.change(screen.getByRole('searchbox'), { target: { value: 'stroop' } }); diff --git a/apps/web/src/components/LoginPageEditor/__tests__/LoginPageEditor.test.tsx b/apps/web/src/components/LoginPageEditor/__tests__/LoginPageEditor.test.tsx index 988028eaa..e1798767b 100644 --- a/apps/web/src/components/LoginPageEditor/__tests__/LoginPageEditor.test.tsx +++ b/apps/web/src/components/LoginPageEditor/__tests__/LoginPageEditor.test.tsx @@ -90,6 +90,15 @@ describe('LoginPageEditor', () => { ); }); + it('should refuse to save a custom image size left blank, but allow it once the image is hidden', () => { + mocks.branding = { logoSize: 'custom' }; + render(); + const saveButton = screen.getByRole('button', { name: 'Save' }); + expect(saveButton.disabled).toBe(true); + fireEvent.click(document.getElementById('showLogo')!); + expect(saveButton.disabled).toBe(false); + }); + it('should open the fullscreen preview from the preview column', () => { render(); fireEvent.click(screen.getByRole('button', { name: 'Fullscreen preview' })); diff --git a/apps/web/src/components/StartSessionForm/__tests__/StartSessionForm.test.tsx b/apps/web/src/components/StartSessionForm/__tests__/StartSessionForm.test.tsx index f6baf333d..27b424c79 100644 --- a/apps/web/src/components/StartSessionForm/__tests__/StartSessionForm.test.tsx +++ b/apps/web/src/components/StartSessionForm/__tests__/StartSessionForm.test.tsx @@ -277,6 +277,17 @@ describe('StartSessionForm', () => { expect(dateOfBirthInput().disabled).toBe(false); }); + // The popup closes on Enter, a click away or a tab, so a clinician who types a returning + // subject's id in full never has to pick it from the list. + it('should fill in the details when a typed identifier matches an existing subject, as picking it would', async () => { + existingSubject({ dateOfBirth: '1990-06-15', sex: 'MALE' }); + renderForm(['alpha']); + typeIdentifier('alpha'); + closeIdentifierPopup(); + await waitFor(() => expect(sexTrigger().disabled).toBe(true)); + expect(dateOfBirthInput().value).toBe('1990-06-15'); + }); + it('should not look up an identifier absent from the options, since no subject can have it', () => { renderForm(['alpha']); typeIdentifier('gamma'); @@ -391,6 +402,20 @@ describe('StartSessionForm', () => { expect(onSubmit).not.toHaveBeenCalled(); }); + it('should require every personal detail when the form is submitted empty', async () => { + const form = renderForm([], null, 'PERSONAL_INFO'); + submit(form); + await waitFor(() => { + expect(errorMessages()).toEqual([ + { field: 'subjectFirstName', message: 'This field is required' }, + { field: 'subjectLastName', message: 'This field is required' }, + { field: 'subjectDateOfBirth', message: 'This field is required' }, + { field: 'subjectSex', message: 'This field is required' } + ]); + }); + expect(onSubmit).not.toHaveBeenCalled(); + }); + it("should reject a date of birth younger than the group's minimum age", async () => { const form = renderForm([], groupWithSettings({ minimumAge: 18 }), 'PERSONAL_INFO'); fillPersonalInfo(form, { dateOfBirth: '2020-01-01', firstName: 'Jane' }); diff --git a/apps/web/src/components/UpdateUserForm/__tests__/UpdateUserForm.test.tsx b/apps/web/src/components/UpdateUserForm/__tests__/UpdateUserForm.test.tsx index e5db395cc..f0a17414a 100644 --- a/apps/web/src/components/UpdateUserForm/__tests__/UpdateUserForm.test.tsx +++ b/apps/web/src/components/UpdateUserForm/__tests__/UpdateUserForm.test.tsx @@ -85,6 +85,18 @@ describe('UpdateUserForm', () => { expect(submittedData()).toMatchObject({ mustResetPassword: false, password: STRONG_PASSWORD }); }); + // The browser fills these fields with the administrator's own saved credentials, so clearing one is + // the ordinary way out of that, and it used to reject the field for being empty. + it('should save without a new password once one typed in is cleared again', async () => { + const { form } = renderForm(); + type(form, 'password', STRONG_PASSWORD); + type(form, 'password', ''); + submit(); + await waitFor(() => expect(onSubmit).toHaveBeenCalled()); + expect(submittedData().password).toBeUndefined(); + expect(errorMessages()).toEqual([]); + }); + it('should fill both password fields with a generated passphrase', () => { const { form } = renderForm(); fireEvent.click(screen.getByLabelText('Generate Passphrase')); diff --git a/apps/web/src/components/UserPermissionsEditor/__tests__/UserPermissionsEditor.test.tsx b/apps/web/src/components/UserPermissionsEditor/__tests__/UserPermissionsEditor.test.tsx index 675fa6a3c..6c3d6ce3c 100644 --- a/apps/web/src/components/UserPermissionsEditor/__tests__/UserPermissionsEditor.test.tsx +++ b/apps/web/src/components/UserPermissionsEditor/__tests__/UserPermissionsEditor.test.tsx @@ -186,6 +186,25 @@ describe('UserPermissionsEditor', () => { expect(onDraftsChange.mock.lastCall?.[0]).toEqual([{ action: 'read', scope: 'group-2', subject: 'Subject' }]); }); + it('should offer as a scope only the groups the user belongs to, since a grant elsewhere would be refused', () => { + const groupsWithOther = [...groups, { id: 'group-3', name: 'Group Three' }]; + render(); + choose('action', 'read'); + choose('subject', 'Subject'); + fireEvent.keyDown(screen.getByTestId('scope-select-trigger'), { key: 'Enter' }); + expect(screen.getByTestId('scope-select-item-group-1')).toBeTruthy(); + expect(screen.getByTestId('scope-select-item-group-2')).toBeTruthy(); + expect(screen.queryByTestId('scope-select-item-group-3')).toBeNull(); + }); + + it('should offer no scope for a resource that cannot be confined to a group', () => { + render(); + choose('action', 'create'); + choose('subject', 'Instrument'); + expect(screen.queryByTestId('scope-select-trigger')).toBeNull(); + expect(onDraftsChange.mock.lastCall?.[0]).toMatchObject([{ action: 'create', subject: 'Instrument' }]); + }); + it('should leave the other drafts untouched when one row changes', () => { render(); fireEvent.click(screen.getByRole('button', { name: 'Add Permission' })); diff --git a/apps/web/src/components/__tests__/NavigationBlocker.test.tsx b/apps/web/src/components/__tests__/NavigationBlocker.test.tsx index c3936714f..183dccb43 100644 --- a/apps/web/src/components/__tests__/NavigationBlocker.test.tsx +++ b/apps/web/src/components/__tests__/NavigationBlocker.test.tsx @@ -70,6 +70,20 @@ describe('NavigationBlocker', () => { expect(window.location.pathname).toBe('/'); }); + it('should navigate without asking while inactive, so leaving a submitted instrument does not prompt', async () => { + await renderBlocker(false); + act(() => history.push('/elsewhere')); + expect(await screen.findByText(DESTINATION_TEXT)).toBeTruthy(); + expect(screen.queryByTestId('blocker-dialog')).toBeNull(); + }); + + it('should navigate without asking when the navigation ignores blockers, as ending a session does', async () => { + await renderBlocker(true); + act(() => history.push('/elsewhere', undefined, { ignoreBlocker: true })); + expect(await screen.findByText(DESTINATION_TEXT)).toBeTruthy(); + expect(screen.queryByTestId('blocker-dialog')).toBeNull(); + }); + it('should complete the navigation once the user confirms leaving', async () => { await renderBlocker(true); act(() => history.push('/elsewhere')); diff --git a/apps/web/src/hooks/__tests__/useNavItems.test.ts b/apps/web/src/hooks/__tests__/useNavItems.test.ts index 20a0e6151..434947d7d 100644 --- a/apps/web/src/hooks/__tests__/useNavItems.test.ts +++ b/apps/web/src/hooks/__tests__/useNavItems.test.ts @@ -1,5 +1,6 @@ -import { cleanup, renderHook } from '@testing-library/react'; -import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { i18n } from '@douglasneuroinformatics/libui/i18n'; +import { act, cleanup, renderHook } from '@testing-library/react'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { useNavItems } from '../useNavItems'; @@ -221,6 +222,32 @@ describe('useNavItems', () => { ]); }); + describe('in Spanish', () => { + beforeEach(() => { + act(() => i18n.changeLanguage('es')); + }); + + afterEach(() => { + act(() => i18n.changeLanguage('en')); + }); + + // The admin nav is written as inline `t()` strings rather than namespace keys, where a missing + // Spanish entry would render in English without any error. + it('should name the admin panel, its groups and its pages in Spanish', () => { + const panel = renderHook(() => useNavItems()) + .result.current.flat() + .find((item) => item.label === 'Panel de administración'); + expect(panel?.children?.map((child) => child.label)).toStrictEqual([ + 'Registros de auditoría', + 'Acceso', + 'Instrumentos', + 'Instancia' + ]); + const instance = panel?.children?.find((child) => child.label === 'Instancia'); + expect(instance?.children?.find((child) => child.url === '/admin/settings')?.label).toBe('Configuración'); + }); + }); + // The audit logs record activity on every entity, not only access changes, so they sit beside the // groups rather than inside one, one click from the panel. it('should link the audit logs directly from the admin panel rather than from inside a group', () => { @@ -238,6 +265,26 @@ describe('useNavItems', () => { ]); }); + // Both views of the instruments page are one route told apart by its search, so each link has to + // carry its view or both would open the same table. + it('should gather the instrument views and the repositories under instruments', () => { + const instruments = adminPanel()?.children?.find((child) => child.label === 'Instruments'); + expect(instruments?.children?.map(({ search, url }) => ({ search, url }))).toStrictEqual([ + { search: { view: 'forms' }, url: '/admin/instruments' }, + { search: { view: 'series' }, url: '/admin/instruments' }, + { search: undefined, url: '/admin/instrument-repos' } + ]); + }); + + it('should gather the instance-wide configuration pages under instance', () => { + const instance = adminPanel()?.children?.find((child) => child.label === 'Instance'); + expect(instance?.children?.map((child) => child.url)).toStrictEqual([ + '/admin/settings', + '/admin/branding', + '/admin/mail' + ]); + }); + it('should leave no admin page at the top level of the sidebar, so admin links never mix with clinical ones', () => { const topLevelUrls = renderHook(() => useNavItems()) .result.current.flat() diff --git a/apps/web/src/routes/_app/__tests__/contact.test.tsx b/apps/web/src/routes/_app/__tests__/contact.test.tsx index a1cb00943..d14a6e76c 100644 --- a/apps/web/src/routes/_app/__tests__/contact.test.tsx +++ b/apps/web/src/routes/_app/__tests__/contact.test.tsx @@ -1,6 +1,7 @@ import { i18n } from '@douglasneuroinformatics/libui/i18n'; +import { localizeZodErrors } from '@opendatacapture/react-core'; import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; import { Route } from '@/routes/_app/contact'; @@ -31,6 +32,11 @@ describe('Route', () => { vi.stubGlobal('open', open); }); + // The app installs its localized validation messages at boot, which a rendered route alone skips. + beforeAll(async () => { + await localizeZodErrors({ targets: ['app'], translator: i18n }); + }); + afterEach(() => { cleanup(); vi.unstubAllGlobals(); @@ -52,6 +58,19 @@ describe('Route', () => { }); }); + // The unselected reason raises an issue code that once fell through to zod's untranslated default. + it('should report both an unselected reason and a blank message as required', async () => { + renderPage(); + fireEvent.click(screen.getByRole('button', { name: 'Submit' })); + await waitFor(() => { + expect(screen.getAllByTestId('error-message-text').map((error) => error.textContent)).toEqual([ + 'This field is required', + 'This field is required' + ]); + }); + expect(open).not.toHaveBeenCalled(); + }); + it('should not open an email when the message is blank, so an empty email is never drafted', async () => { renderPage(); fireEvent.click(screen.getByTestId('contactReason-select-trigger')); diff --git a/apps/web/src/routes/_app/__tests__/user.test.tsx b/apps/web/src/routes/_app/__tests__/user.test.tsx index 54748a880..14cfbcf6b 100644 --- a/apps/web/src/routes/_app/__tests__/user.test.tsx +++ b/apps/web/src/routes/_app/__tests__/user.test.tsx @@ -157,6 +157,12 @@ describe('Route', () => { }); }); + it('should describe the password dialog, so it is announced with more than its title', () => { + renderPage(); + const descriptionId = openPasswordDialog().getAttribute('aria-describedby')!; + expect(document.getElementById(descriptionId)?.textContent).toContain('confirm it to save the change'); + }); + it('should reject a weak password without sending it', async () => { renderPage(); submitPasswords(openPasswordDialog(), 'abc', 'abc'); diff --git a/apps/web/src/routes/_app/admin/instrument-repos/__tests__/index.test.tsx b/apps/web/src/routes/_app/admin/instrument-repos/__tests__/index.test.tsx index bb132e4c0..2b98c3f99 100644 --- a/apps/web/src/routes/_app/admin/instrument-repos/__tests__/index.test.tsx +++ b/apps/web/src/routes/_app/admin/instrument-repos/__tests__/index.test.tsx @@ -528,6 +528,20 @@ describe('Route', () => { expect(screen.queryByText('Add Instrument Repository')).toBeNull(); }); + it('should keep the add dialog open with the URL entered when the import is refused, so it can be corrected', () => { + const url = 'https://github.com/owner/repository'; + const Component = Route.options.component!; + mocks.repos = [repoFixture()]; + const { rerender } = render(); + openAddDialog(); + submitAddForm({ url }); + mocks.isCreatePending = true; + rerender(); + mocks.isCreatePending = false; + rerender(); + expect(screen.getByLabelText('GitHub URL').value).toBe(url); + }); + it('should close the add dialog when it is dismissed', () => { renderPage(); openAddDialog(); diff --git a/apps/web/src/routes/_app/admin/users/__tests__/create.test.tsx b/apps/web/src/routes/_app/admin/users/__tests__/create.test.tsx index 8c662a594..f1c404ba0 100644 --- a/apps/web/src/routes/_app/admin/users/__tests__/create.test.tsx +++ b/apps/web/src/routes/_app/admin/users/__tests__/create.test.tsx @@ -196,6 +196,17 @@ describe('Route', () => { expect(mocks.mutateAsync.mock.lastCall?.[0].data.email).toBe('jdoe@example.org'); }); + it('should create the user without an email once one typed in is cleared again', async () => { + const form = renderPage(); + fillCredentials(form); + choosePermissionLevel(form, 'ADMIN'); + type(form, 'email', 'jdoe@example.org'); + type(form, 'email', ''); + submit(); + await waitFor(() => expect(mocks.mutateAsync).toHaveBeenCalled()); + expect(mocks.mutateAsync.mock.lastCall?.[0].data.email).toBeUndefined(); + }); + it('should report a generic failure when the create request fails for a reason other than the password', async () => { mocks.mutateAsync.mockRejectedValue(new Error('Network Error')); fillAndSubmit(); @@ -361,10 +372,16 @@ describe('Route', () => { it('should fill both password fields with a generated passphrase', () => { const form = renderPage(); fireEvent.click(screen.getByLabelText('Generate Passphrase')); - expect(field(form, 'password').value).not.toBe(''); + expect(field(form, 'password').value.split('-')).toHaveLength(5); expect(field(form, 'confirmPassword').value).toBe(field(form, 'password').value); }); + it('should reveal a generated passphrase, since a password nobody can read cannot be relayed', () => { + const form = renderPage(); + fireEvent.click(screen.getByLabelText('Generate Passphrase')); + expect(field(form, 'password').type).toBe('text'); + }); + it('should force a password reset for a user created with a generated passphrase', async () => { const form = renderPage(); type(form, 'username', 'jdoe'); @@ -374,7 +391,7 @@ describe('Route', () => { choosePermissionLevel(form, 'ADMIN'); submit(); await waitFor(() => expect(mocks.mutateAsync).toHaveBeenCalled()); - expect(mocks.mutateAsync.mock.lastCall?.[0].data.mustResetPassword).toBe(true); + expect(mocks.mutateAsync.mock.lastCall?.[0].data).toMatchObject({ mustResetPassword: true, username: 'jdoe' }); }); it('should reject a weak password before contacting the server', async () => { @@ -443,6 +460,16 @@ describe('Route', () => { }); }); + it('should create a disabled user below administrator with no group, since such an account only attributes uploaded data', async () => { + const form = renderPage(); + fillCredentials(form); + choosePermissionLevel(form, 'STANDARD'); + fireEvent.click(form.querySelector('#disabled-true')!); + submit(); + await waitFor(() => expect(mocks.mutateAsync).toHaveBeenCalled()); + expect(mocks.mutateAsync.mock.lastCall?.[0].data).toMatchObject({ disabled: true, groupIds: [] }); + }); + it('should submit the groups chosen for a user below administrator', async () => { mocks.groups = [{ id: 'group-1', name: 'Group One' }]; const form = renderPage(); diff --git a/apps/web/src/routes/_app/datahub/subjects/$subjectId/table/__tests__/index.test.tsx b/apps/web/src/routes/_app/datahub/subjects/$subjectId/table/__tests__/index.test.tsx index 787bbfb9b..501176df4 100644 --- a/apps/web/src/routes/_app/datahub/subjects/$subjectId/table/__tests__/index.test.tsx +++ b/apps/web/src/routes/_app/datahub/subjects/$subjectId/table/__tests__/index.test.tsx @@ -85,6 +85,14 @@ describe('Route', () => { expect(headers()).toEqual(['DATE_COLLECTED', 'COLLECTION_METHOD', 'SERIES']); }); + // `subject-table` once named the Table tab link as well, so a selector for the record table found + // the tab instead (#1475). + it('should carry the subject-table test id on the record table alone', () => { + renderTable([record()]); + expect(screen.getAllByTestId('subject-table')).toHaveLength(1); + expect(within(screen.getByTestId('subject-table')).getAllByTestId('data-table')).toHaveLength(1); + }); + it('should render a multiple-choice answer as comma-separated text', () => { renderTable([record({ totalScore: new Set(['a', 'b']) })]); expect(screen.getByTestId('subject-table-cell-totalScore').textContent).toBe('a, b'); diff --git a/apps/web/src/routes/_app/datahub/subjects/__tests__/index.test.tsx b/apps/web/src/routes/_app/datahub/subjects/__tests__/index.test.tsx index bea2cfdca..376c09964 100644 --- a/apps/web/src/routes/_app/datahub/subjects/__tests__/index.test.tsx +++ b/apps/web/src/routes/_app/datahub/subjects/__tests__/index.test.tsx @@ -244,6 +244,15 @@ describe('Route', () => { expect(listedSubjects()).toEqual(['bob']); }); + // The search bar is a form, and a native submit reloaded the page, which drops the in-memory + // access token and lands on the login page. + it('should cancel a submit of the search bar, so Enter keeps the results and the user signed in', () => { + renderDataHub(); + fireEvent.change(screen.getByRole('searchbox'), { target: { value: 'BOB' } }); + expect(fireEvent.submit(screen.getByTestId('data-table-search-bar'))).toBe(false); + expect(listedSubjects()).toEqual(['bob']); + }); + it('should never list a subject whose unscoped id is empty', () => { mocks.subjects = [subject('ROOT$'), BOB]; renderDataHub(); diff --git a/apps/web/src/routes/_app/instruments/__tests__/accessible-instruments.test.tsx b/apps/web/src/routes/_app/instruments/__tests__/accessible-instruments.test.tsx index 570001d8c..ed45ee09a 100644 --- a/apps/web/src/routes/_app/instruments/__tests__/accessible-instruments.test.tsx +++ b/apps/web/src/routes/_app/instruments/__tests__/accessible-instruments.test.tsx @@ -7,7 +7,7 @@ import { Route } from '@/routes/_app/instruments/accessible-instruments'; import '@/services/i18n'; -type InstrumentInfoStub = { details: { title: string }; id: string; kind: 'FORM' }; +type InstrumentInfoStub = { details: { title: string }; id: string; kind: 'FILE' | 'FORM' }; type MockState = { instrumentInfo: InstrumentInfoStub[] | undefined; @@ -79,6 +79,15 @@ describe('Route', () => { expect(screen.queryByRole('button', { name: 'Excluded' })).toBeNull(); }); + // Only the gateway cannot deliver a file instrument's uploads, so the in-person list must not + // inherit the remote assignment page's exclusion. + it('should offer a file instrument the group may administer, since an in-person session can take its uploads', () => { + mocks.instrumentInfo = [{ details: { title: 'File Upload' }, id: 'instrument-3', kind: 'FILE' }]; + mocks.store.currentGroup = { accessibleInstrumentIds: ['instrument-3'] }; + render(); + expect(screen.getByRole('button', { name: 'File Upload' })).toBeTruthy(); + }); + it('should show a loading fallback instead of the showcase while the instruments load', () => { mocks.instrumentInfo = undefined; render(); diff --git a/apps/web/src/routes/_app/instruments/render/__tests__/$id.test.tsx b/apps/web/src/routes/_app/instruments/render/__tests__/$id.test.tsx index a3ea65e47..cc74586dd 100644 --- a/apps/web/src/routes/_app/instruments/render/__tests__/$id.test.tsx +++ b/apps/web/src/routes/_app/instruments/render/__tests__/$id.test.tsx @@ -7,11 +7,17 @@ import type { Subject } from '@opendatacapture/schemas/subject'; import { cleanup, render, screen } from '@testing-library/react'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { NavigationBlocker } from '@/components/NavigationBlocker'; import { Route } from '@/routes/_app/instruments/render/$id'; import '@/services/i18n'; -type InstrumentRendererProps = { onSubmit: InstrumentSubmitHandler; subject?: Subject; target: unknown }; +type InstrumentRendererProps = { + NavigationBlocker?: unknown; + onSubmit: InstrumentSubmitHandler; + subject?: Subject; + target: unknown; +}; type AxiosRequestConfigStub = { onUploadProgress: (event: { loaded: number }) => void }; @@ -118,6 +124,11 @@ describe('Route', () => { expect(rendererProps()).toMatchObject({ subject, target: mocks.bundle }); }); + it('should hand the renderer the router-backed NavigationBlocker, so leaving a started instrument asks first', () => { + render(); + expect(rendererProps().NavigationBlocker).toBe(NavigationBlocker); + }); + it('should render without a subject when the session has none', () => { mocks.store.currentSession = createSession(null); render(); diff --git a/packages/instrument-bundler/src/__tests__/build.test.ts b/packages/instrument-bundler/src/__tests__/build.test.ts index 66b34c856..c878b1235 100644 --- a/packages/instrument-bundler/src/__tests__/build.test.ts +++ b/packages/instrument-bundler/src/__tests__/build.test.ts @@ -74,6 +74,16 @@ describe('build', () => { expect(js).toContain('/runtime/v1/react@19.x/jsx-runtime'); }); + it('should embed an image whose extension is upper case as a data URL, so a stimulus named like a camera file loads', async () => { + const bytes = new Uint8Array([137, 80, 78, 71]); + const inputs = [ + { content: `import stim from './stim.PNG';\nexport default { content: stim };`, name: 'index.ts' }, + { content: bytes, name: 'stim.PNG' } + ]; + const { js } = await build({ inputs }); + expect(js).toContain(`data:image/png;base64,${Buffer.from(bytes).toString('base64')}`); + }); + it('should refuse to guess when the source imports two versions of react', async () => { const inputs = [ { content: `import '/runtime/v1/react@18.x';\nimport '/runtime/v1/react@19.x';`, name: 'index.tsx' } diff --git a/packages/instrument-library/src/__tests__/DNP_ENHANCED_DEMOGRAPHICS_QUESTIONNAIRE.test.ts b/packages/instrument-library/src/__tests__/DNP_ENHANCED_DEMOGRAPHICS_QUESTIONNAIRE.test.ts index 8e988bfbe..97346085b 100644 --- a/packages/instrument-library/src/__tests__/DNP_ENHANCED_DEMOGRAPHICS_QUESTIONNAIRE.test.ts +++ b/packages/instrument-library/src/__tests__/DNP_ENHANCED_DEMOGRAPHICS_QUESTIONNAIRE.test.ts @@ -35,6 +35,10 @@ function getFieldLabel(name: 'ageAtImmigration' | 'maritalStatus') { } describe('DNP_ENHANCED_DEMOGRAPHICS_QUESTIONNAIRE', () => { + it('should offer a reset button, since it is the longest form in the library', () => { + expect(instrument.resetButton).toBe(true); + }); + describe('options', () => { it('should label each option in English for an English-speaking subject', () => { expect(getStringField('gender').options.en).toEqual({ female: 'Woman', male: 'Man', nonBinary: 'Non-Binary' }); diff --git a/packages/instrument-library/src/__tests__/DNP_HAPPINESS_QUESTIONNAIRE.test.ts b/packages/instrument-library/src/__tests__/DNP_HAPPINESS_QUESTIONNAIRE.test.ts index fc5b921a6..9b0179c4f 100644 --- a/packages/instrument-library/src/__tests__/DNP_HAPPINESS_QUESTIONNAIRE.test.ts +++ b/packages/instrument-library/src/__tests__/DNP_HAPPINESS_QUESTIONNAIRE.test.ts @@ -22,6 +22,10 @@ function getOverallLifeSatisfaction() { } describe('DNP_HAPPINESS_QUESTIONNAIRE', () => { + it('should not declare a reset button, so its form offers none', () => { + expect(instrument.resetButton).toBeUndefined(); + }); + describe('reasonNotSatisfied', () => { it('should stay hidden until the subject has answered whether they are satisfied', () => { expect(getDynamicField('reasonNotSatisfied').render({})).toBeNull(); diff --git a/packages/react-core/src/components/FormContent/__tests__/FormContent.test.tsx b/packages/react-core/src/components/FormContent/__tests__/FormContent.test.tsx index c1158f6df..cc10d048c 100644 --- a/packages/react-core/src/components/FormContent/__tests__/FormContent.test.tsx +++ b/packages/react-core/src/components/FormContent/__tests__/FormContent.test.tsx @@ -89,6 +89,59 @@ describe('FormContent', () => { expect(screen.getByRole('button', { name: 'Reset' })).toBeTruthy(); }); + it('should offer no reset button to an instrument that does not declare one', () => { + renderFormContent(); + expect(screen.queryByRole('button', { name: 'Reset' })).toBeNull(); + }); + + describe('with a reset button', () => { + const resettable: AnyUnilingualFormInstrument = { + ...instrument, + content: { + favoriteNumber: { kind: 'number', label: 'Favorite Number', variant: 'input' }, + maritalStatus: { + kind: 'string', + label: 'Marital Status', + options: { married: 'Married', single: 'Single' }, + variant: 'select' + } + }, + resetButton: true, + validationSchema: z.object({ favoriteNumber: z.number(), maritalStatus: z.enum(['married', 'single']) }) + }; + + const getMaritalStatus = () => document.querySelector('select[name="maritalStatus"]')!; + + const answer = (favoriteNumber: string, maritalStatus: string) => { + fireEvent.change(screen.getByLabelText('Favorite Number'), { target: { value: favoriteNumber } }); + fireEvent.change(getMaritalStatus(), { target: { value: maritalStatus } }); + }; + + it('should clear every answer, not only the validation errors, when the reset button is clicked', async () => { + renderFormContent({ instrument: resettable }); + answer('7', 'married'); + await waitFor(() => { + expect(getMaritalStatus().value).toBe('married'); + }); + fireEvent.click(screen.getByRole('button', { name: 'Reset' })); + await waitFor(() => { + expect(screen.getByLabelText('Favorite Number').value).toBe(''); + }); + expect(getMaritalStatus().value).toBe(''); + }); + + it('should submit the answers given after a reset', async () => { + const onSubmit = renderFormContent({ instrument: resettable }); + answer('7', 'married'); + fireEvent.click(screen.getByRole('button', { name: 'Reset' })); + answer('3', 'single'); + fireEvent.click(screen.getByRole('button', { name: 'Submit' })); + await waitFor(() => { + expect(onSubmit).toHaveBeenCalledWith({ data: { favoriteNumber: 3, maritalStatus: 'single' }, kind: 'FORM' }); + }); + }); + }); + it('should submit the validated answers tagged as form data', async () => { const onSubmit = renderFormContent(); fireEvent.change(screen.getByLabelText('Favorite Number'), { target: { value: '7' } }); diff --git a/packages/react-core/src/components/InstrumentSummary/__tests__/InstrumentSummary.test.tsx b/packages/react-core/src/components/InstrumentSummary/__tests__/InstrumentSummary.test.tsx index 4df30de36..48846a2c9 100644 --- a/packages/react-core/src/components/InstrumentSummary/__tests__/InstrumentSummary.test.tsx +++ b/packages/react-core/src/components/InstrumentSummary/__tests__/InstrumentSummary.test.tsx @@ -190,6 +190,49 @@ describe('InstrumentSummary', () => { expect(screen.getByText('Results')).toBeTruthy(); }); + it('should leave out a computed measure marked hidden when the instrument shows its measures by default', () => { + const instrument: AnyUnilingualFormInstrument = { + ...baseInstrument, + defaultMeasureVisibility: 'visible', + measures: { + hiddenMeasure: { kind: 'computed', label: 'Hidden Measure', value: () => 1, visibility: 'hidden' }, + shownMeasure: { kind: 'computed', label: 'Shown Measure', value: () => 2 } + } + }; + render(); + expect(screen.getByText('Shown Measure')).toBeTruthy(); + expect(screen.queryByText('Hidden Measure')).toBeNull(); + }); + + it.each([ + { data: { hasPet: false }, hasPet: false }, + { data: { hasPet: true, petName: 'Rex' }, hasPet: true } + ])( + 'should list the measure on a dynamic question only once the respondent was shown it (hasPet: $hasPet)', + ({ data, hasPet }) => { + const instrument: AnyUnilingualFormInstrument = { + ...baseInstrument, + content: { + hasPet: { kind: 'boolean', label: 'Has Pet', variant: 'radio' }, + petName: { + deps: ['hasPet'], + kind: 'dynamic', + render: (values) => (values.hasPet ? { kind: 'string', label: 'Pet Name', variant: 'input' } : null) + } + }, + defaultMeasureVisibility: 'visible', + measures: { + hasPet: { kind: 'const', label: 'Has Pet Measure', ref: 'hasPet' }, + petName: { kind: 'const', label: 'Pet Name Measure', ref: 'petName' } + }, + validationSchema: z.object({ hasPet: z.boolean(), petName: z.string().optional() }) + }; + render(); + expect(screen.getByText('Has Pet Measure')).toBeTruthy(); + expect(Boolean(screen.queryByText('Pet Name Measure'))).toBe(hasPet); + } + ); + it('should copy a measure without a value as NA, so the copied summary has no blank lines', async () => { const writeText = vi.fn().mockResolvedValue(undefined); vi.stubGlobal('navigator', { ...navigator, clipboard: { writeText } }); diff --git a/packages/react-core/src/utils/__tests__/zodErrorMap.test.ts b/packages/react-core/src/utils/__tests__/zodErrorMap.test.ts index cd03ca518..330120505 100644 --- a/packages/react-core/src/utils/__tests__/zodErrorMap.test.ts +++ b/packages/react-core/src/utils/__tests__/zodErrorMap.test.ts @@ -196,6 +196,12 @@ describe('createZodErrorMaps', () => { {}, REQUIRED ], + [ + 'an untouched select backed by an enum, which zod reports as an invalid value', + z4.object({ x: z4.enum(['A', 'B']) }), + {}, + REQUIRED + ], ['a decimal in an integer field', z4.number().int(), 1.5, 'Must be a whole number'], ['a number where text is expected', z4.string(), 1, 'Must be text'], ['text where a number is expected', z4.number(), 'x', 'Must be a number'], diff --git a/testing/playwright.config.ts b/testing/playwright.config.ts index 1dc4e0148..3cc4e9e8c 100644 --- a/testing/playwright.config.ts +++ b/testing/playwright.config.ts @@ -69,7 +69,7 @@ export default defineConfig({ url: `http://localhost:${playgroundPort}` } ], - // Playwright's own default is half the cores. That many workers log in faster than the API's login - // throttle allows partway through the suite, and leave a developer's machine little for anything else. + // Playwright's own default is half the cores. Each worker is a Chromium beside the four servers, so + // memory runs out long before the cores do: two workers already peak near 7 GB. workers: workerCount ?? (process.env.CI ? 1 : 2) }); diff --git a/testing/src/pages/_app/admin/audit/logs.page.ts b/testing/src/pages/_app/admin/audit/logs.page.ts index 7ec80ab99..bf380800d 100644 --- a/testing/src/pages/_app/admin/audit/logs.page.ts +++ b/testing/src/pages/_app/admin/audit/logs.page.ts @@ -5,21 +5,15 @@ import { AppPage } from '../../route.page'; export type AuditLogsFilter = 'action' | 'entity' | 'group' | 'user'; export class AuditLogsPage extends AppPage { - readonly clearFiltersButton: Locator; readonly dataTable: Locator; readonly downloadButton: Locator; readonly pageHeader: Locator; - readonly timeHeaderButton: Locator; constructor(page: Page) { super(page); this.pageHeader = page.getByTestId('page-header'); this.dataTable = page.getByTestId('data-table'); - this.clearFiltersButton = page.getByTestId('audit-logs-clear-filters'); this.downloadButton = page.getByRole('button', { name: 'Download' }); - this.timeHeaderButton = this.dataTable - .getByTestId('data-table-head') - .getByRole('button', { exact: true, name: 'Time' }); } filterButton(filter: AuditLogsFilter) { diff --git a/testing/src/pages/_app/admin/branding/index.page.ts b/testing/src/pages/_app/admin/branding/index.page.ts index 984d03071..2b1b2c032 100644 --- a/testing/src/pages/_app/admin/branding/index.page.ts +++ b/testing/src/pages/_app/admin/branding/index.page.ts @@ -3,12 +3,10 @@ import type { Locator, Page } from '@playwright/test'; import { AppPage } from '../../route.page'; export class BrandingPage extends AppPage { - readonly loginPageLink: Locator; readonly pageHeader: Locator; constructor(page: Page) { super(page); this.pageHeader = page.getByTestId('page-header'); - this.loginPageLink = page.getByRole('link', { name: 'Login Page' }); } } diff --git a/testing/src/pages/_app/admin/branding/login-page.page.ts b/testing/src/pages/_app/admin/branding/login-page.page.ts index c35f17c30..5243ef62b 100644 --- a/testing/src/pages/_app/admin/branding/login-page.page.ts +++ b/testing/src/pages/_app/admin/branding/login-page.page.ts @@ -4,14 +4,10 @@ import type { Locator, Page } from '@playwright/test'; import { AppPage } from '../../route.page'; export class BrandingLoginPagePage extends AppPage { - readonly customLogoHeightInput: Locator; - readonly customLogoWidthInput: Locator; readonly enableBrandingCheckbox: Locator; readonly instanceNameEnglishInput: Locator; readonly instanceNameFrenchInput: Locator; - readonly logoSizeSelect: Locator; readonly saveButton: Locator; - readonly showLogoCheckbox: Locator; constructor(page: Page) { super(page); @@ -20,10 +16,6 @@ export class BrandingLoginPagePage extends AppPage { this.instanceNameEnglishInput = page.locator('#instanceName-en'); this.instanceNameFrenchInput = page.locator('#instanceName-fr'); this.enableBrandingCheckbox = page.locator('#enableBranding'); - this.showLogoCheckbox = page.locator('#showLogo'); - this.logoSizeSelect = page.locator('#logoSize'); - this.customLogoWidthInput = page.getByLabel('Width', { exact: true }); - this.customLogoHeightInput = page.getByLabel('Height', { exact: true }); this.saveButton = page.getByRole('button', { name: 'Save' }); } @@ -31,9 +23,4 @@ export class BrandingLoginPagePage extends AppPage { await this.saveButton.click(); await expect(this.$ref.getByText('The login page has been updated.')).toBeVisible(); } - - async selectLogoSize(label: string) { - await this.logoSizeSelect.click(); - await this.$ref.getByRole('option', { exact: true, name: label }).click(); - } } diff --git a/testing/src/pages/_app/admin/instrument-repos/index.page.ts b/testing/src/pages/_app/admin/instrument-repos/index.page.ts index bfd19874d..683f20a78 100644 --- a/testing/src/pages/_app/admin/instrument-repos/index.page.ts +++ b/testing/src/pages/_app/admin/instrument-repos/index.page.ts @@ -6,14 +6,9 @@ export class InstrumentReposPage extends AppPage { readonly addRepositoryButton: Locator; /** Wraps whichever tab is showing; drawn invisible so the tables sit directly on the page. */ readonly card: Locator; - readonly clearLogButton: Locator; /** The repositories table, shown on the Repositories tab. */ readonly dataTable: Locator; - readonly githubUrlInput: Locator; - readonly importButton: Locator; readonly pageHeader: Locator; - readonly sectionTitle: Locator; - readonly syncAllButton: Locator; /** The activity log, shown on its own tab in place of the repositories table. */ readonly syncLogTable: Locator; @@ -22,23 +17,12 @@ export class InstrumentReposPage extends AppPage { this.pageHeader = page.getByTestId('page-header'); this.card = page.getByTestId('instrument-repos-card'); this.dataTable = page.getByTestId('data-table').first(); - this.sectionTitle = page.getByTestId('instrument-repos-section-title'); this.syncLogTable = page.getByTestId('instrument-repo-sync-log'); - this.syncAllButton = page.getByRole('button', { name: 'Sync All' }); - this.clearLogButton = page.getByRole('button', { name: 'Clear Log' }); this.addRepositoryButton = page.getByRole('button', { name: 'Add Repository' }); - this.githubUrlInput = page.getByLabel('GitHub URL'); // A plain HTML form (not the shared `Form` component), so its accessible name is the literal label. - this.importButton = page.getByRole('button', { name: 'Import' }); } async openActivityLog(): Promise { await this.$ref.getByTestId('instrument-repos-section-ACTIVITY').click(); } - - /** Opens a repository row's action menu and picks one of its entries. */ - async selectRowAction(rowIndex: number, label: string): Promise { - await this.dataTable.getByTestId('row-actions-trigger').nth(rowIndex).click(); - await this.$ref.getByRole('menuitem', { name: label }).click(); - } } diff --git a/testing/src/pages/_app/admin/mail.page.ts b/testing/src/pages/_app/admin/mail.page.ts index d5775f0b4..77898cb07 100644 --- a/testing/src/pages/_app/admin/mail.page.ts +++ b/testing/src/pages/_app/admin/mail.page.ts @@ -9,14 +9,8 @@ export class MailSettingsPage extends AppPage { readonly port: Locator; readonly saveConfig: Locator; readonly senderAddress: Locator; - readonly sendTest: Locator; readonly serverCard: Locator; - readonly templateBody: Locator; - readonly templateCard: Locator; - readonly templateLanguage: Locator; - readonly templateSave: Locator; readonly testConnection: Locator; - readonly testRecipient: Locator; readonly username: Locator; constructor(page: Page) { @@ -30,17 +24,6 @@ export class MailSettingsPage extends AppPage { this.senderAddress = page.getByTestId('mail-sender-address'); this.saveConfig = page.getByTestId('mail-save-config'); this.testConnection = page.getByTestId('mail-test-connection'); - this.testRecipient = page.getByTestId('mail-test-recipient'); - this.sendTest = page.getByTestId('mail-send-test'); - this.templateCard = page.getByTestId('mail-template-card'); - this.templateBody = page.getByTestId('new-user-template-body'); - this.templateLanguage = page.getByTestId('new-user-template-language'); - this.templateSave = page.getByTestId('mail-template-save'); - } - - /** The options of the welcome template's language picker, once it is open. */ - get openLanguageOptions(): Locator { - return this.$ref.getByRole('option'); } /** Idempotent: a configuration left enabled by an earlier spec already shows the form. */ @@ -56,11 +39,6 @@ export class MailSettingsPage extends AppPage { await this.serverCard.waitFor({ state: 'visible' }); } - /** The validation message rendered under a field, keyed by that field's control id. */ - fieldError(field: string): Locator { - return this.$ref.getByTestId(`mail-${field}-error`); - } - async fillServerConfig({ host, password, diff --git a/testing/src/pages/_app/admin/permission-sets/$permissionSetId.page.ts b/testing/src/pages/_app/admin/permission-sets/$permissionSetId.page.ts index 16ab9e096..2175c79ed 100644 --- a/testing/src/pages/_app/admin/permission-sets/$permissionSetId.page.ts +++ b/testing/src/pages/_app/admin/permission-sets/$permissionSetId.page.ts @@ -3,18 +3,12 @@ import type { Locator, Page } from '@playwright/test'; import { AppPage } from '../../route.page'; export class PermissionSetPage extends AppPage { - readonly deleteButton: Locator; readonly destructiveActionDialog: Locator; readonly ruleRows: Locator; constructor(page: Page) { super(page); this.ruleRows = page.getByTestId('permission-set-rule-row'); - this.deleteButton = page.getByTestId('delete-permission-set'); this.destructiveActionDialog = page.getByTestId('destructive-action-dialog'); } - - async confirmDelete() { - await this.destructiveActionDialog.getByRole('button', { name: 'Yes' }).click(); - } } diff --git a/testing/src/pages/_app/admin/permission-sets/index.page.ts b/testing/src/pages/_app/admin/permission-sets/index.page.ts index 80c805f50..b775685bf 100644 --- a/testing/src/pages/_app/admin/permission-sets/index.page.ts +++ b/testing/src/pages/_app/admin/permission-sets/index.page.ts @@ -3,7 +3,6 @@ import type { Locator, Page } from '@playwright/test'; import { AppPage } from '../../route.page'; export class PermissionSetsPage extends AppPage { - readonly createLink: Locator; readonly destructiveActionDialog: Locator; readonly table: Locator; @@ -11,7 +10,6 @@ export class PermissionSetsPage extends AppPage { super(page); // libui's DataTable sets its own testid and takes none from the route. this.table = page.getByTestId('data-table'); - this.createLink = page.getByTestId('create-permission-set'); this.destructiveActionDialog = page.getByTestId('destructive-action-dialog'); } diff --git a/testing/src/pages/_app/admin/settings.page.ts b/testing/src/pages/_app/admin/settings.page.ts index 384e077eb..e108eaf81 100644 --- a/testing/src/pages/_app/admin/settings.page.ts +++ b/testing/src/pages/_app/admin/settings.page.ts @@ -1,14 +1,10 @@ -import type { Language } from '@opendatacapture/schemas/core'; -import type { TableColorMode } from '@opendatacapture/schemas/setup'; import type { Locator, Page } from '@playwright/test'; import { AppPage } from '../route.page'; export class AdminSettingsPage extends AppPage { readonly defaultAssignmentDurationInput: Locator; - readonly groupSwitcherPositionSelect: Locator; readonly pageHeader: Locator; - readonly tableColorModeSelect: Locator; readonly uploaderToggle: Locator; constructor(page: Page) { @@ -16,28 +12,6 @@ export class AdminSettingsPage extends AppPage { this.pageHeader = page.getByTestId('page-header'); this.uploaderToggle = page.getByRole('switch', { name: 'Enable Uploader' }); this.defaultAssignmentDurationInput = page.getByTestId('default-assignment-duration-input'); - this.groupSwitcherPositionSelect = page.getByTestId('group-switcher-position-select'); - this.tableColorModeSelect = page.getByTestId('table-color-mode-select'); - } - - /** The labels beside the active-language checkboxes, in the order they are listed. */ - get activeLanguageLabels(): Locator { - return this.$ref.getByTestId('active-languages').locator('label'); - } - - activeLanguageCheckbox(language: Language): Locator { - return this.$ref.getByTestId(`active-language-${language}`); - } - - /** `name` is the language's autonym, which is how the toggle labels its options. */ - async selectLanguage(name: string) { - await this.sidebar.getByTestId('language-toggle').getByRole('button').click(); - await this.$ref.getByRole('menuitem', { name }).click(); - } - - async selectTableColorMode(mode: TableColorMode) { - await this.tableColorModeSelect.click(); - await this.$ref.getByTestId(`table-color-mode-option-${mode}`).click(); } async setDefaultAssignmentDuration(days: number) { diff --git a/testing/src/pages/_app/admin/users/$userId.page.ts b/testing/src/pages/_app/admin/users/$userId.page.ts index 9ac70e2e6..fd1874811 100644 --- a/testing/src/pages/_app/admin/users/$userId.page.ts +++ b/testing/src/pages/_app/admin/users/$userId.page.ts @@ -6,30 +6,16 @@ import { AppPage } from '../../route.page'; export class AdminUserPage extends AppPage { readonly addPermissionRow: Locator; - readonly adminNotice: Locator; - readonly backLink: Locator; - readonly chosenPermissionSetRules: Locator; - readonly discardCustomGrantsWarning: Locator; - readonly manageAllWarning: Locator; readonly permissionRows: Locator; - readonly permissionSourceTabs: Locator; - readonly permissionsTable: Locator; readonly profileForm: Locator; readonly submitError: Locator; constructor(page: Page) { super(page); - this.backLink = page.getByTestId('admin-user-back'); this.profileForm = page.getByTestId('update-user-form'); this.submitError = page.getByTestId('admin-user-edit-error'); - this.permissionsTable = page.getByTestId('user-permissions-table'); this.permissionRows = page.getByTestId('user-permission-row'); this.addPermissionRow = page.getByTestId('add-permission-row').last(); - this.adminNotice = page.getByTestId('user-permissions-admin-notice'); - this.manageAllWarning = page.getByTestId('manage-all-warning'); - this.permissionSourceTabs = page.getByTestId('user-permissions-source'); - this.chosenPermissionSetRules = page.getByTestId('permission-set-chosen').getByTestId('permission-set-rule-row'); - this.discardCustomGrantsWarning = page.getByTestId('user-permissions-discard-warning'); } /** @@ -48,16 +34,6 @@ export class AdminUserPage extends AppPage { await this.addPermissionRow.getByRole('button', { name: 'Add Permission' }).click(); } - /** The items render in a portal outside the card, so they are found on the page. */ - async choosePermissionSet(permissionSetId: string) { - await this.$ref.getByTestId('permission-set-select-trigger').click(); - await this.$ref.getByTestId(`permission-set-select-item-${permissionSetId}`).click(); - } - - async choosePermissionSource(source: 'custom' | 'permissionSet') { - await this.$ref.getByTestId(`user-permissions-source-${source}`).click(); - } - async removePermission(index: number) { await this.permissionRows.nth(index).getByTestId('user-permission-remove').click(); } @@ -72,8 +48,4 @@ export class AdminUserPage extends AppPage { await this.addPermissionRow.getByTestId(`${field}-select-trigger`).click(); await this.$ref.getByTestId(`${field}-select-item-${value}`).click(); } - - async submitPermission() { - await this.saveProfile(); - } } diff --git a/testing/src/pages/_app/contact.page.ts b/testing/src/pages/_app/contact.page.ts index 82ea1157e..36810dd9f 100644 --- a/testing/src/pages/_app/contact.page.ts +++ b/testing/src/pages/_app/contact.page.ts @@ -37,10 +37,6 @@ export class ContactPage extends AppPage { }); } - async getCapturedMailtoLink(): Promise { - return this.$ref.evaluate(() => window.__capturedMailto); - } - async selectReason(reason: string): Promise { await this.reasonField.click(); await this.$ref.getByRole('option', { name: reason }).click(); diff --git a/testing/src/pages/_app/datahub/instruments/$instrumentId/graph.page.ts b/testing/src/pages/_app/datahub/instruments/$instrumentId/graph.page.ts index 49cc2fd34..d34614b31 100644 --- a/testing/src/pages/_app/datahub/instruments/$instrumentId/graph.page.ts +++ b/testing/src/pages/_app/datahub/instruments/$instrumentId/graph.page.ts @@ -5,13 +5,11 @@ import { AppPage } from '../../../route.page'; export class InstrumentHubGraphPage extends AppPage { readonly chart: Locator; readonly chartToggle: Locator; - readonly colourByTrigger: Locator; readonly measureTrigger: Locator; constructor(page: Page) { super(page); this.chart = page.getByTestId('instrument-hub-chart'); this.chartToggle = page.getByTestId('instrument-hub-chart-toggle'); - this.colourByTrigger = page.getByTestId('instrument-hub-colour-by-trigger'); this.measureTrigger = page.getByTestId('instrument-hub-measure-trigger'); } @@ -29,11 +27,6 @@ export class InstrumentHubGraphPage extends AppPage { await this.chartToggle.getByRole('tab', { name: chart }).click(); } - async selectColourBy(label: string) { - await this.colourByTrigger.click(); - await this.$ref.getByRole('option', { name: label }).click(); - } - async selectMeasure(label: string) { await this.measureTrigger.click(); await this.$ref.getByRole('option', { name: label }).click(); diff --git a/testing/src/pages/_app/datahub/instruments/index.page.ts b/testing/src/pages/_app/datahub/instruments/index.page.ts index 4f3fcb6fc..0b127b7a2 100644 --- a/testing/src/pages/_app/datahub/instruments/index.page.ts +++ b/testing/src/pages/_app/datahub/instruments/index.page.ts @@ -23,15 +23,4 @@ export class InstrumentHubPage extends AppPage { row(title: string): Locator { return this.rows.filter({ hasText: title }); } - - /** Clicks a column header to cycle its sort. Removal is disabled, so this toggles asc ⇄ desc. */ - async sortBy(label: string) { - await this.$ref.getByTestId('data-table-head').getByRole('button', { name: label }).click(); - } - - /** The visible title of every listed instrument, top to bottom — the first cell of each row. */ - async titles(): Promise { - const rows = await this.rows.all(); - return Promise.all(rows.map(async (row) => (await row.locator('> div').first().innerText()).trim())); - } } diff --git a/testing/src/pages/_app/datahub/subjects/$subjectId/assignments.page.ts b/testing/src/pages/_app/datahub/subjects/$subjectId/assignments.page.ts index cb8237e48..cdbbf2e7f 100644 --- a/testing/src/pages/_app/datahub/subjects/$subjectId/assignments.page.ts +++ b/testing/src/pages/_app/datahub/subjects/$subjectId/assignments.page.ts @@ -3,14 +3,12 @@ import type { Locator, Page } from '@playwright/test'; import { AppPage } from '../../../route.page'; export class SubjectAssignmentsPage extends AppPage { - readonly assignmentLink: Locator; readonly assignmentRows: Locator; readonly cancelButton: Locator; constructor(page: Page) { super(page); this.assignmentRows = page.getByTestId('data-table-row'); - this.assignmentLink = page.getByRole('link', { name: 'Link to Assignment' }); this.cancelButton = page.getByRole('button', { name: 'Cancel' }); } } diff --git a/testing/src/pages/_app/datahub/subjects/index.page.ts b/testing/src/pages/_app/datahub/subjects/index.page.ts index 4020ac840..84c4e5b70 100644 --- a/testing/src/pages/_app/datahub/subjects/index.page.ts +++ b/testing/src/pages/_app/datahub/subjects/index.page.ts @@ -8,7 +8,6 @@ export class DatahubPage extends AppPage { readonly filtersTrigger: Locator; readonly minRecordsFilter: Locator; readonly pageHeader: Locator; - readonly rowActionsTrigger: Locator; readonly rows: Locator; readonly searchBar: Locator; readonly searchInput: Locator; @@ -20,7 +19,6 @@ export class DatahubPage extends AppPage { this.minRecordsFilter = page.getByTestId('datahub-filter-min-records'); this.collectedPresetFilter = page.getByTestId('datahub-filter-collected-preset'); this.pageHeader = page.getByTestId('page-header'); - this.rowActionsTrigger = page.getByTestId('row-actions-trigger').first(); this.rows = page.getByTestId('data-table-body').getByTestId('data-table-row'); this.searchBar = page.getByTestId('data-table-search-bar'); this.searchInput = this.searchBar.getByRole('searchbox'); diff --git a/testing/src/pages/_app/group/email-templates.page.ts b/testing/src/pages/_app/group/email-templates.page.ts index 29a8bc2cf..e27a282b4 100644 --- a/testing/src/pages/_app/group/email-templates.page.ts +++ b/testing/src/pages/_app/group/email-templates.page.ts @@ -6,7 +6,6 @@ export class GroupEmailTemplatesPage extends AppPage { readonly builtInDefaultBadge: Locator; readonly createAnyway: Locator; readonly createBody: Locator; - readonly createLanguage: Locator; readonly createSubject: Locator; readonly createSubmit: Locator; readonly deleteConfirm: Locator; @@ -19,7 +18,6 @@ export class GroupEmailTemplatesPage extends AppPage { this.root = page.getByTestId('group-email-templates'); this.name = page.getByTestId('template-name'); this.createSubject = page.getByTestId('template-create-subject'); - this.createLanguage = page.getByTestId('template-create-language'); this.createBody = page.getByTestId('template-create-body'); this.insertUrl = page.getByTestId('template-create-insert-url'); this.createSubmit = page.getByTestId('template-create-submit'); @@ -28,23 +26,6 @@ export class GroupEmailTemplatesPage extends AppPage { this.builtInDefaultBadge = page.getByTestId('template-active-builtin'); } - /** The create form's inline validation message, shown when the body is missing a placeholder. */ - get createError(): Locator { - return this.$ref.getByTestId('template-create-error'); - } - - /** The options of whichever language picker is currently open. */ - get openLanguageOptions(): Locator { - return this.$ref.getByRole('option'); - } - - /** Create a template authored in one language, accepting the missing-translations warning. */ - async createTemplate(fields: { body: string; name: string; subject: string }) { - await this.fillCreateForm(fields); - await this.createAnyway.waitFor({ state: 'visible' }); - await this.createAnyway.click(); - } - /** * The delete control on the row for `name`. Scoped to that row because earlier specs leave * their own templates in the list, so the first delete button is not this test's, and because @@ -54,18 +35,6 @@ export class GroupEmailTemplatesPage extends AppPage { return this.rowFor(name).getByTestId(/^template-delete-/); } - /** - * Fill and submit the create form in the currently selected language. Authoring a single - * language raises the missing-translations warning, so callers confirm it via - * {@link createAnyway}. - */ - async fillCreateForm({ body, name, subject }: { body: string; name: string; subject: string }) { - await this.name.fill(name); - await this.createSubject.fill(subject); - await this.createBody.fill(body); - await this.createSubmit.click(); - } - /** The list row whose label is `name`. */ rowFor(name: string): Locator { return this.root.getByTestId('template-row').filter({ hasText: name }); diff --git a/testing/src/pages/_app/group/manage.page.ts b/testing/src/pages/_app/group/manage.page.ts index 759559e20..b0738d5b3 100644 --- a/testing/src/pages/_app/group/manage.page.ts +++ b/testing/src/pages/_app/group/manage.page.ts @@ -7,7 +7,6 @@ export class GroupManagePage extends AppPage { readonly instrumentCreatedAtTags: Locator; /** Every row's preview button, in row order, for asserting they form a column. */ readonly instrumentPreviewButtons: Locator; - readonly subjectIdDisplayLengthInput: Locator; // The shared `Form` component's own submit button always has `aria-label="Submit"`, regardless of // its visible text, so this is the correct accessible name even though no custom label is set here. readonly submitButton: Locator; @@ -16,7 +15,6 @@ export class GroupManagePage extends AppPage { super(page); this.instrumentCreatedAtTags = this.$ref.locator('[data-testid^="instrument-created-at-"]'); this.instrumentPreviewButtons = this.$ref.locator('[data-testid^="instrument-preview-"]'); - this.subjectIdDisplayLengthInput = page.getByLabel('Preferred Subject ID Display Length'); this.submitButton = page.getByRole('button', { name: 'Submit' }); } diff --git a/testing/src/pages/_app/group/remote-assignments.page.ts b/testing/src/pages/_app/group/remote-assignments.page.ts index beffaad3c..68e83e0bd 100644 --- a/testing/src/pages/_app/group/remote-assignments.page.ts +++ b/testing/src/pages/_app/group/remote-assignments.page.ts @@ -5,34 +5,15 @@ import { AppPage } from '../route.page'; type WizardStepName = 'INSTRUMENTS' | 'REVIEW' | 'SUBJECTS'; export class RemoteAssignmentsPage extends AppPage { - readonly addTimepoint: Locator; readonly breadcrumbs: Locator; /** The scrolling frame around the column mapping grid, capped so a wide file cannot lengthen the step. */ readonly columnMapping: Locator; - /** The confirm button of the mapping step, which the refusals have to sit above. */ - readonly confirmMapping: Locator; - readonly confirmTimepoints: Locator; /** The page column the wizard must fit inside; `Layout` renders exactly one `main`. */ readonly container: Locator; - readonly emailAll: Locator; - readonly emailListApply: Locator; - readonly emailListDropzone: Locator; - readonly emailListMatched: Locator; - readonly emailListToggle: Locator; - readonly emailTable: Locator; - readonly errorList: Locator; readonly fileInput: Locator; - readonly instrumentSelect: Locator; readonly mapStep: Locator; - readonly parsePasted: Locator; - readonly pasteInput: Locator; - readonly redirectUrlError: Locator; readonly redirectUrlInput: Locator; - /** The review step's note of where the batch sends its subjects once they finish. */ - readonly reviewRedirectUrl: Locator; readonly selectAllSubjects: Locator; - readonly skipUnplaceable: Locator; - readonly submitBatch: Locator; readonly timepointsStep: Locator; readonly useSelectedSubjects: Locator; readonly wizard: Locator; @@ -40,27 +21,10 @@ export class RemoteAssignmentsPage extends AppPage { constructor(page: Page) { super(page); this.breadcrumbs = page.getByTestId('bulk-breadcrumbs'); - this.confirmMapping = page.getByTestId('bulk-confirm-mapping'); - this.emailAll = page.getByTestId('bulk-email-all'); - this.emailListApply = page.getByTestId('email-list-apply'); // Same libui trap as `fileInput` below: FileDropzone hardcodes its own testid and forwards // none, so `email-list-dropzone` never reaches the DOM. In the subject-picker tab this panel // holds the only dropzone on the page. - this.emailListDropzone = page.getByTestId('dropzone').locator('input[type="file"]'); - this.emailListMatched = page.getByTestId('email-list-matched'); - this.emailListToggle = page.getByTestId('email-list-upload-toggle'); - this.emailTable = page.getByTestId('bulk-email-table'); - this.errorList = page.getByTestId('bulk-error-list'); - this.parsePasted = page.getByTestId('bulk-parse-pasted'); - this.pasteInput = page.getByTestId('bulk-paste-input'); - this.redirectUrlError = page.getByTestId('bulk-redirect-url-error'); this.redirectUrlInput = page.getByTestId('bulk-redirect-url-input'); - this.reviewRedirectUrl = page.getByTestId('bulk-review-redirect-url'); - this.skipUnplaceable = page.getByTestId('bulk-skip-unplaceable'); - this.addTimepoint = page.getByTestId('bulk-add-timepoint'); - this.confirmTimepoints = page.getByTestId('bulk-confirm-timepoints'); - this.instrumentSelect = page.getByTestId('bulk-instrument-select'); - this.submitBatch = page.getByTestId('bulk-submit'); this.columnMapping = page.getByTestId('bulk-column-mapping'); this.container = page.locator('main'); // libui's FileDropzone hardcodes its own testid and forwards none, so the `bulk-file-dropzone` @@ -73,38 +37,14 @@ export class RemoteAssignmentsPage extends AppPage { this.wizard = page.getByTestId('bulk-remote-assignment-wizard'); } - /** Add the first offered instrument as a timepoint and continue to review. */ - async addFirstInstrument() { - await this.addFirstTimepoint(); - await this.confirmTimepoints.click(); - } - - /** Add the first offered instrument as a timepoint, staying on the step. */ - async addFirstTimepoint() { - await this.instrumentSelect.click(); - await this.$ref.getByRole('option').first().click(); - await this.addTimepoint.click(); - } - breadcrumb(step: WizardStepName): Locator { return this.$ref.getByTestId(`bulk-breadcrumb-${step}`); } - /** Paste a batch and move to the mapping step with it. */ - async pasteSubjects(text: string) { - await this.sourceMode('PASTE'); - await this.pasteInput.fill(text); - await this.parsePasted.click(); - } - async sourceMode(mode: 'FILE' | 'PASTE' | 'SELECT') { await this.$ref.getByTestId(`bulk-source-mode-${mode}`).click(); } - async uploadEmailList(file: Parameters[0]) { - await this.emailListDropzone.setInputFiles(file); - } - async uploadFile(file: Parameters[0]) { await this.fileInput.setInputFiles(file); } diff --git a/testing/src/pages/_app/instruments/render/$id.page.ts b/testing/src/pages/_app/instruments/render/$id.page.ts index e6bea0bd2..3e3bbe814 100644 --- a/testing/src/pages/_app/instruments/render/$id.page.ts +++ b/testing/src/pages/_app/instruments/render/$id.page.ts @@ -18,13 +18,8 @@ export class RenderInstrumentPage extends AppPage { readonly happinessSatisfiedRadio: Locator; /** Asks the clinician to confirm leaving an instrument whose responses have not been submitted. */ readonly leaveDialog: Locator; - /** Rendered only for an instrument declaring `resetButton: true`; libui gives it this aria-label. */ - readonly resetButton: Locator; /** Shown once every item of a series has been administered, in place of a scalar summary. */ readonly seriesCompletionHeading: Locator; - /** The bar shown above each item of a series, with its "Instrument N of M" label. */ - readonly seriesProgress: Locator; - readonly seriesProgressBar: Locator; readonly submitButton: Locator; readonly summaryHeading: Locator; @@ -34,10 +29,7 @@ export class RenderInstrumentPage extends AppPage { this.consentPreamble = page.getByTestId('consent-preamble'); this.happinessSatisfiedRadio = this.$ref.getByRole('radio', { name: 'Yes' }); this.leaveDialog = page.getByTestId('blocker-dialog'); - this.resetButton = page.getByRole('button', { name: 'Reset' }); this.seriesCompletionHeading = page.getByRole('heading', { name: /Thank You/i }); - this.seriesProgress = page.getByTestId('series-progress'); - this.seriesProgressBar = this.seriesProgress.getByRole('progressbar'); this.submitButton = page.getByRole('button', { name: 'Submit' }); this.summaryHeading = page.getByRole('heading', { name: /Summary of Results/i }); this.errorMessages = page.getByTestId('error-message-text'); @@ -45,11 +37,6 @@ export class RenderInstrumentPage extends AppPage { this.firstInvalidField = page.locator('[data-field-group]:has([data-testid="error-message-text"])').first(); } - /** Accepts the General Consent Form by selecting the affirmative radio option. */ - async acceptConsent(): Promise { - await this.$ref.getByRole('radio', { name: /I have read/ }).click(); - } - /** * Answers the Happiness Questionnaire with "No" overall and leaves the reason empty, which its * validation schema rejects. diff --git a/testing/src/pages/_app/session/remote-assignment.page.ts b/testing/src/pages/_app/session/remote-assignment.page.ts index 58485023f..7aecd7683 100644 --- a/testing/src/pages/_app/session/remote-assignment.page.ts +++ b/testing/src/pages/_app/session/remote-assignment.page.ts @@ -12,8 +12,6 @@ export class RemoteAssignmentPage extends AppPage { /** The dialog's optional field for where the subject is sent once they finish. */ readonly redirectUrlInput: Locator; readonly urlInput: Locator; - /** The message libui shows under a field that failed validation. */ - readonly validationError: Locator; constructor(page: Page) { super(page); @@ -21,7 +19,6 @@ export class RemoteAssignmentPage extends AppPage { this.instrumentShowcase = page.getByTestId('instrument-showcase'); this.createDialog = page.getByRole('dialog'); this.redirectUrlInput = this.createDialog.getByLabel('Redirect URL (Optional)'); - this.validationError = this.createDialog.getByTestId('error-message-text'); this.urlInput = page.locator('input[readonly]'); // The copy-to-clipboard button carries no accessible name or testid; it is the only button // rendered immediately next to the readonly URL input. @@ -31,12 +28,6 @@ export class RemoteAssignmentPage extends AppPage { this.closeResultButton = page.getByRole('button', { name: 'Close' }).first(); } - async clickFirstInstrumentCard() { - const firstCard = this.instrumentShowcase.locator('li').first(); - await firstCard.waitFor({ state: 'visible' }); - await firstCard.click(); - } - /** Cards carry a content-hash testid, so match on the visible title instead. */ instrumentCard(title: string): Locator { return this.instrumentShowcase.locator('[data-testid^="instrument-card-"]').filter({ hasText: title }).first(); diff --git a/testing/src/pages/_app/session/start-session.page.ts b/testing/src/pages/_app/session/start-session.page.ts index 4cc3e7e5a..05e7590f9 100644 --- a/testing/src/pages/_app/session/start-session.page.ts +++ b/testing/src/pages/_app/session/start-session.page.ts @@ -4,7 +4,6 @@ import { AppPage } from '../route.page'; export class StartSessionPage extends AppPage { readonly dateOfBirthField: Locator; - readonly endSessionButton: Locator; readonly errorMessages: Locator; readonly pageHeader: Locator; readonly selectField: Locator; @@ -30,7 +29,6 @@ export class StartSessionPage extends AppPage { this.sexTrigger = this.sessionForm.getByTestId('subjectSex-select-trigger'); this.subjectIdErrors = this.sessionForm.locator('[data-field-group="subjectId"]').getByTestId('error-message-text'); // The end session nav item opens a dialog rather than navigating, so it carries no route. - this.endSessionButton = page.getByTestId('nav-button-#'); } /** Clicks outside the identifier combobox, which closes its popup and commits what was typed. */ @@ -39,12 +37,6 @@ export class StartSessionPage extends AppPage { await this.pageHeader.click(); } - async endSession() { - await this.endSessionButton.click(); - await this.$ref.getByRole('button', { name: 'Yes' }).click(); - await this.sessionForm.waitFor({ state: 'visible' }); - } - async fillCustomIdentifier(customIdentifier: string, sex: string) { await this.typeSubjectId(customIdentifier); await this.fillSessionDetails(sex); diff --git a/testing/src/pages/_app/upload/$instrumentId.page.ts b/testing/src/pages/_app/upload/$instrumentId.page.ts index 6772cca6b..e84bc0eba 100644 --- a/testing/src/pages/_app/upload/$instrumentId.page.ts +++ b/testing/src/pages/_app/upload/$instrumentId.page.ts @@ -8,7 +8,6 @@ export class UploadInstrumentPage extends AppPage { readonly pageHeader: Locator; readonly submitButton: Locator; readonly templateButton: Locator; - readonly templateErrorHeading: Locator; readonly tryAgainButton: Locator; constructor(page: Page) { @@ -19,7 +18,6 @@ export class UploadInstrumentPage extends AppPage { this.errorHeading = page.getByRole('heading', { name: 'An error has happened within the request' }); this.tryAgainButton = page.getByRole('button', { name: 'Try Again' }); this.templateButton = page.getByRole('button', { name: 'Template' }); - this.templateErrorHeading = page.getByRole('heading', { name: 'Error Occurred Downloading Sample Template' }); } async uploadFile(file: Parameters[0]) { diff --git a/testing/src/pages/_app/user.page.ts b/testing/src/pages/_app/user.page.ts index 88f76aed6..791c9ea49 100644 --- a/testing/src/pages/_app/user.page.ts +++ b/testing/src/pages/_app/user.page.ts @@ -5,7 +5,6 @@ import { AppPage } from './route.page'; export class UserPage extends AppPage { readonly changePasswordButton: Locator; readonly emailField: Locator; - readonly firstNameField: Locator; readonly pageHeader: Locator; readonly passwordDialog: Locator; readonly phoneNumberField: Locator; @@ -22,7 +21,6 @@ export class UserPage extends AppPage { this.passwordDialog = page.getByRole('dialog', { name: 'Change Password' }); this.profileForm = page.getByTestId('profile-form'); this.emailField = this.profileForm.getByLabel('Email'); - this.firstNameField = this.profileForm.getByLabel('First Name'); this.phoneNumberField = this.profileForm.getByLabel('Phone Number'); } diff --git a/testing/src/pages/auth/login.page.ts b/testing/src/pages/auth/login.page.ts index 68733dc22..f55a121ec 100644 --- a/testing/src/pages/auth/login.page.ts +++ b/testing/src/pages/auth/login.page.ts @@ -7,7 +7,6 @@ import { RootPage } from '../__root.page'; export class LoginPage extends RootPage { readonly _requiresAuth = false; readonly demoDialog: Locator; - readonly demoDialogBranding: Locator; readonly demoTableHeaderRow: Locator; /** * One row per demo account. A click anywhere in the row logs straight in as it; the row itself is @@ -19,7 +18,6 @@ export class LoginPage extends RootPage { constructor(page: Page) { super(page); this.demoDialog = page.getByTestId('demo-dialog'); - this.demoDialogBranding = this.demoDialog.getByTestId('demo-dialog-branding'); this.demoTableHeaderRow = this.demoDialog.getByRole('row').filter({ has: page.getByRole('columnheader') }); this.demoUserRows = this.demoDialog.getByRole('row').filter({ has: page.getByRole('cell') }); this.loginForm = page.getByTestId('login-form'); diff --git a/testing/src/pages/playground/index.page.ts b/testing/src/pages/playground/index.page.ts index 8ac721ed5..b6fc9b2a6 100644 --- a/testing/src/pages/playground/index.page.ts +++ b/testing/src/pages/playground/index.page.ts @@ -1,3 +1,4 @@ +import { expect } from '@playwright/test'; import type { FrameLocator, Locator, Page } from '@playwright/test'; import { playgroundURL } from '../../support/env'; @@ -8,43 +9,20 @@ import { playgroundURL } from '../../support/env'; */ export class PlaygroundPage { readonly $ref: Page; - readonly editorPane: Locator; /** The instrument list's search box, inside the popover {@link instrumentSelector} opens. */ readonly instrumentSearch: Locator; /** The header's button that opens the instrument list. */ readonly instrumentSelector: Locator; - /** The handle between the editor and preview panes, which resizes them. */ - readonly paneSeparator: Locator; /** The instrument preview, which runs on another origin than the editor. */ readonly preview: FrameLocator; readonly previewFrame: Locator; - readonly previewPane: Locator; constructor(page: Page) { this.$ref = page; - this.editorPane = page.getByTestId('editor-pane'); this.instrumentSelector = page.getByRole('combobox', { name: 'Load an instrument...' }); this.instrumentSearch = page.getByPlaceholder('Search...'); - this.paneSeparator = page.getByRole('separator'); this.previewFrame = page.getByTestId('preview-frame'); this.preview = page.frameLocator('[data-testid="preview-frame"]'); - this.previewPane = page.getByTestId('preview-pane'); - } - - /** Drags the pane separator to `x`, in page coordinates. */ - async dragPaneSeparatorTo(x: number): Promise { - const separator = (await this.paneSeparator.boundingBox())!; - const y = separator.y + separator.height / 2; - await this.$ref.mouse.move(separator.x + separator.width / 2, y); - await this.$ref.mouse.down(); - await this.$ref.mouse.move(x, y, { steps: 10 }); - await this.$ref.mouse.up(); - } - - /** The editor's share of the width the editor and preview panes divide between them, from 0 to 1. */ - async editorShare(): Promise { - const [editor, preview] = await Promise.all([this.editorPane.boundingBox(), this.previewPane.boundingBox()]); - return editor!.width / (editor!.width + preview!.width); } /** Opens the editor, or a share link built against {@link playgroundURL}. */ @@ -68,4 +46,18 @@ export class PlaygroundPage { await this.searchInstruments(label); await this.instrumentOption(label).click(); } + + /** + * Sets how often, in milliseconds, the viewer checks the editor's files for a change to compile, + * through the header's User Settings dialog. + */ + async setRefreshInterval(milliseconds: number): Promise { + await this.$ref.locator('button:has(svg.lucide-ellipsis-vertical)').click(); + await this.$ref.getByRole('menuitem', { name: 'User Settings' }).click(); + const dialog = this.$ref.getByRole('dialog'); + await dialog.getByLabel('Refresh Interval').fill(String(milliseconds)); + // libui labels every form's submit button "Submit" for assistive technology, whatever it reads. + await dialog.getByRole('button', { name: 'Submit' }).click(); + await expect(dialog).toBeHidden(); + } } diff --git a/testing/src/specs/accessible-instruments.spec.ts b/testing/src/specs/accessible-instruments.spec.ts index e88d768e4..574e739b7 100644 --- a/testing/src/specs/accessible-instruments.spec.ts +++ b/testing/src/specs/accessible-instruments.spec.ts @@ -18,18 +18,3 @@ test.describe('accessible instruments on a phone', () => { expect(kindFilterBox.y).toBeGreaterThanOrEqual(searchBarBox.y + searchBarBox.height); }); }); - -test.describe('accessible instruments', () => { - test('should filter the instrument showcase to cards matching the search query', async ({ getPageModel }) => { - const accessibleInstrumentsPage = await getPageModel('/instruments/accessible-instruments'); - await expect(accessibleInstrumentsPage.instrumentShowcase).toBeVisible(); - - const nonMatchingCard = accessibleInstrumentsPage.instrumentCard('General Consent Form'); - await expect(nonMatchingCard).toBeVisible(); - - await accessibleInstrumentsPage.search('Happiness'); - - await expect(accessibleInstrumentsPage.instrumentCard('Happiness Questionnaire')).toBeVisible(); - await expect(nonMatchingCard).toBeHidden(); - }); -}); diff --git a/testing/src/specs/admin-audit-logs.spec.ts b/testing/src/specs/admin-audit-logs.spec.ts index f93ce7dce..c87382c6c 100644 --- a/testing/src/specs/admin-audit-logs.spec.ts +++ b/testing/src/specs/admin-audit-logs.spec.ts @@ -5,101 +5,30 @@ import { expect, test } from '../support/fixtures'; test.describe('admin audit logs', () => { test.use({ actingRole: 'ADMIN' }); - test('should show the audit logs table', async ({ getPageModel }) => { - const auditLogsPage = await getPageModel('/admin/audit/logs'); - - await expect(auditLogsPage.pageHeader).toContainText('Audit Logs'); - await expect(auditLogsPage.dataTable).toBeVisible(); - }); - // Only `Login` (via `auth.service.ts`) and assignment actions (via `assignments.service.ts`) // actually call `AuditLogger.log`; creating a group, user, instrument, subject etc. is not audited // despite the Entity filter offering those options -- see #1473. Login is therefore the // only auditable action this spec can reliably trigger and verify. - test('should record a login and find it through the action filter', async ({ apiRequestContext, getPageModel }) => { + test('should record a login, find it through the action filter and download the logs', async ({ + apiRequestContext, + getPageModel + }) => { // `getPageModel('ADMIN')` reuses a token cached once per worker (see `roleAccount` in fixtures.ts), // so it would not itself produce a fresh entry here. Logging in directly does. await ApiClient.login(apiRequestContext, { password: ADMIN.password, username: ADMIN.username }); const auditLogsPage = await getPageModel('/admin/audit/logs'); + await expect(auditLogsPage.pageHeader).toContainText('Audit Logs'); await auditLogsPage.filterBy('action', 'Login'); + await expect(auditLogsPage.filterButton('action')).toContainText('Login'); + await expect(auditLogsPage.$ref).toHaveURL(/action=LOGIN/); const firstRow = auditLogsPage.dataTable.getByTestId('data-table-row').first(); - await expect(firstRow).toContainText('Login'); await expect(firstRow).toContainText('User'); await expect(firstRow.getByTestId('audit-log-action')).toHaveText('Login'); - }); - - test('should show the chosen option on its filter button and put it in the URL', async ({ getPageModel }) => { - const auditLogsPage = await getPageModel('/admin/audit/logs'); - await auditLogsPage.filterBy('entity', 'Session'); - - await expect(auditLogsPage.filterButton('entity')).toContainText('Session'); - await expect(auditLogsPage.$ref).toHaveURL(/entity=SESSION/); - }); - - test('should clear every filter at once', async ({ getPageModel }) => { - const auditLogsPage = await getPageModel('/admin/audit/logs'); - await expect(auditLogsPage.clearFiltersButton).toBeHidden(); - - await auditLogsPage.filterBy('action', 'Login'); - await auditLogsPage.filterBy('entity', 'User'); - await auditLogsPage.clearFiltersButton.click(); - - await expect(auditLogsPage.clearFiltersButton).toBeHidden(); - await expect(auditLogsPage.$ref).not.toHaveURL(/action=|entity=/); - await expect(auditLogsPage.filterButton('action')).toHaveText('Action'); - }); - - test('should download the audit logs as JSON', async ({ getPageModel }) => { - const auditLogsPage = await getPageModel('/admin/audit/logs'); const downloadPromise = auditLogsPage.$ref.waitForEvent('download'); await auditLogsPage.downloadButton.click(); - const download = await downloadPromise; - - expect(download.suggestedFilename()).toMatch(/^ODC_Audit_Logs_\d+\.json$/); - }); -}); - -test.describe('admin audit logs sorting', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should request the oldest logs first on the first click of the time header', async ({ getPageModel }) => { - const auditLogsPage = await getPageModel('/admin/audit/logs'); - - const ascendingRequest = auditLogsPage.$ref.waitForRequest( - (request) => request.url().includes('/v1/audit/logs') && request.url().includes('sortOrder=asc') - ); - await auditLogsPage.timeHeaderButton.click(); - - expect((await ascendingRequest).url()).toContain('sortOrder=asc'); - }); -}); - -test.describe('admin audit logs fetching', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should fetch the logs once per navigation, not again when the table mounts', async ({ getPageModel, page }) => { - const auditLogsRequests: string[] = []; - page.on('request', (request) => { - if (request.method() === 'GET' && new URL(request.url()).pathname.endsWith('/v1/audit/logs')) { - auditLogsRequests.push(request.url()); - } - }); - - const auditLogsPage = await getPageModel('/admin/audit/logs'); - const firstRow = auditLogsPage.dataTable.getByTestId('data-table-row').first(); - await expect(firstRow).toBeVisible(); - // eslint-disable-next-line playwright/no-networkidle -- proving no duplicate request fires needs a quiet period, which is what network idle is - await page.waitForLoadState('networkidle'); - expect(auditLogsRequests).toHaveLength(1); - - await auditLogsPage.filterBy('action', 'Login'); - await expect(auditLogsPage.$ref).toHaveURL(/action=LOGIN/); - await expect(firstRow).toContainText('Login'); - // eslint-disable-next-line playwright/no-networkidle -- proving no duplicate request fires needs a quiet period, which is what network idle is - await page.waitForLoadState('networkidle'); - expect(auditLogsRequests).toHaveLength(2); + expect((await downloadPromise).suggestedFilename()).toMatch(/^ODC_Audit_Logs_\d+\.json$/); }); }); diff --git a/testing/src/specs/admin-branding.spec.ts b/testing/src/specs/admin-branding.spec.ts index a41284842..55c3bb3f9 100644 --- a/testing/src/specs/admin-branding.spec.ts +++ b/testing/src/specs/admin-branding.spec.ts @@ -2,44 +2,12 @@ import { LoginPage } from '../pages/auth/login.page'; import { PHONE_VIEWPORT } from '../support/constants'; import { expect, test } from '../support/fixtures'; -// The login page branding is one instance-wide document and every save sends all of its fields, so -// two tests saving it concurrently would overwrite each other's values. -test.describe.configure({ mode: 'serial' }); - test.describe('admin branding', () => { test.use({ actingRole: 'ADMIN' }); - test('should link from the branding index to the login page editor', async ({ getPageModel }) => { - const brandingPage = await getPageModel('/admin/branding'); - - await expect(brandingPage.pageHeader).toContainText('Branding'); - await expect(brandingPage.loginPageLink).toBeVisible(); - - await brandingPage.loginPageLink.click(); - await expect(brandingPage.$ref).toHaveURL('/admin/branding/login-page'); - }); - - test('should save a login page branding change and keep it after leaving and returning', async ({ - getPageModel, - uniqueId - }) => { - const instanceName = `E2E Instance ${uniqueId}`; - - const editorPage = await getPageModel('/admin/branding/login-page'); - await editorPage.instanceNameEnglishInput.fill(instanceName); - await editorPage.saveButton.click(); - - await expect(editorPage.$ref.getByText('The login page has been updated.')).toBeVisible(); - - const reloadedPage = await getPageModel('/admin/branding/login-page'); - await expect(reloadedPage.instanceNameEnglishInput).toHaveValue(instanceName); - }); -}); - -test.describe('login page instance name', () => { - test.use({ actingRole: 'ADMIN', viewport: PHONE_VIEWPORT }); - - test('should show an instance name set only in French to an English reader on a narrow screen', async ({ + // The login page branding is one instance-wide document, so the test restores it at the end. The + // name is set in French alone, so the login page has to fall back to it for an English reader. + test('should save a login page branding change, keep it, and show it on a narrow login page', async ({ getPageModel, page, uniqueId @@ -52,6 +20,10 @@ test.describe('login page instance name', () => { await editorPage.instanceNameFrenchInput.fill(frenchOnlyName); await editorPage.saveAndConfirm(); + const reloadedPage = await getPageModel('/admin/branding/login-page'); + await expect(reloadedPage.instanceNameFrenchInput).toHaveValue(frenchOnlyName); + + await page.setViewportSize(PHONE_VIEWPORT); const loginPage = new LoginPage(page); await loginPage.goto('/auth/login'); await loginPage.loginForm.waitFor(); @@ -64,21 +36,3 @@ test.describe('login page instance name', () => { await restoredEditorPage.saveAndConfirm(); }); }); - -test.describe('login image custom size', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should allow saving after hiding a login image that has an incomplete custom size', async ({ - getPageModel - }) => { - const editorPage = await getPageModel('/admin/branding/login-page'); - await editorPage.showLogoCheckbox.check(); - await editorPage.selectLogoSize('Custom'); - await editorPage.customLogoWidthInput.fill(''); - await editorPage.customLogoHeightInput.fill(''); - await expect(editorPage.saveButton).toBeDisabled(); - - await editorPage.showLogoCheckbox.uncheck(); - await expect(editorPage.saveButton).toBeEnabled(); - }); -}); diff --git a/testing/src/specs/admin-instrument-repos.spec.ts b/testing/src/specs/admin-instrument-repos.spec.ts index 5a84da0df..006ae15b0 100644 --- a/testing/src/specs/admin-instrument-repos.spec.ts +++ b/testing/src/specs/admin-instrument-repos.spec.ts @@ -1,3 +1,5 @@ +import type { Locator } from '@playwright/test'; + import { expect, test } from '../support/fixtures'; // Importing a repository clones real instruments from GitHub server-side (see @@ -15,105 +17,9 @@ test.describe('admin instrument repos', () => { await expect(reposPage.dataTable).toBeVisible(); await expect(reposPage.addRepositoryButton).toBeVisible(); }); - - test('should reject a malformed GitHub URL without making a network request', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.addRepositoryButton.click(); - await reposPage.githubUrlInput.fill('not-a-valid-url'); - await reposPage.importButton.click(); - - await expect(reposPage.$ref.getByText('Please enter a valid GitHub repository URL.')).toBeVisible(); - // Client-side rejection, so the dialog stays open on the same page rather than submitting. - await expect(reposPage.githubUrlInput).toBeVisible(); - }); -}); - -test.describe('admin instrument repos slow import', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should wait out an import slower than the default request timeout and report success', async ({ - getPageModel, - page - }) => { - test.setTimeout(60_000); - // Answered by the test rather than the API, which must not reach out to GitHub under NODE_ENV=test. - await page.route('**/v1/instrument-repos', async (route) => { - if (route.request().method() !== 'POST') { - return route.continue(); - } - await new Promise((resolve) => { - setTimeout(resolve, 12_000); - }); - return route.fulfill({ json: {}, status: 201 }); - }); - const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.addRepositoryButton.click(); - await reposPage.githubUrlInput.fill('https://github.com/example/slow-instruments'); - await reposPage.importButton.click(); - - await expect(reposPage.$ref.getByText('Repository imported successfully')).toBeVisible({ timeout: 30_000 }); - await expect(reposPage.$ref.getByText('Failed to import repository')).toBeHidden(); - }); -}); - -test.describe('admin instrument repos refused import', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should report a refused import and keep the dialog open with the URL, instead of the error page', async ({ - getPageModel, - page - }) => { - const url = 'https://github.com/example/missing-instruments'; - await page.route('**/v1/instrument-repos', (route) => { - if (route.request().method() !== 'POST') { - return route.continue(); - } - return route.fulfill({ - json: { error: 'Bad Request', message: 'Repository not found', statusCode: 400 }, - status: 400 - }); - }); - const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.addRepositoryButton.click(); - await reposPage.githubUrlInput.fill(url); - await reposPage.importButton.click(); - - await expect(reposPage.$ref.getByText('Repository not found')).toBeVisible(); - await expect(reposPage.githubUrlInput).toHaveValue(url); - await expect(reposPage.$ref.getByTestId('error-page')).toBeHidden(); - }); -}); - -test.describe('admin instrument repos empty repository', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should name the directories it looked in when a repository provides no instruments', async ({ - getPageModel, - page - }) => { - const message = - 'Found no instruments in example/empty. An instrument is a directory holding an index.ts, index.tsx, index.js or index.jsx file, within lib/file, lib/forms, lib/interactive, lib/series at the root of the repository.'; - await page.route('**/v1/instrument-repos', (route) => { - if (route.request().method() !== 'POST') { - return route.continue(); - } - return route.fulfill({ json: { error: 'Bad Gateway', message, statusCode: 502 }, status: 502 }); - }); - const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.addRepositoryButton.click(); - await reposPage.githubUrlInput.fill('https://github.com/example/empty'); - await reposPage.importButton.click(); - - await expect(reposPage.$ref.getByText(message)).toBeVisible(); - await expect(reposPage.$ref.getByTestId('error-page')).toBeHidden(); - }); }); -test.describe('admin instrument repos activity log', () => { +test.describe('admin instrument repos layout', () => { test.use({ actingRole: 'ADMIN' }); // Answered by the test rather than the API, which must not reach out to GitHub under NODE_ENV=test. @@ -145,7 +51,7 @@ test.describe('admin instrument repos activity log', () => { importedCount: 2, message: null, repoId: repo.id, - repoName: repo.name, + repoName: 'LaboratoireDeRecherchePsychiatrique/instruments-cliniques-internes-du-laboratoire', updatedAt: '2026-10-28T00:00:00.000Z' }; @@ -153,82 +59,21 @@ test.describe('admin instrument repos activity log', () => { await page.route('**/v1/instrument-repos', (route) => { return route.request().method() === 'GET' ? route.fulfill({ json: [repo] }) : route.continue(); }); - // Regexes, not globs: the history is now requested with query parameters, and its two routes - // must not match each other. + // Regexes, not globs: the history is requested with query parameters, and its two routes must not + // match each other. await page.route(/\/v1\/instrument-repos\/sync\/logs\/latest/, (route) => route.fulfill({ json: [partialSync] })); await page.route(/\/v1\/instrument-repos\/sync\/logs(\?|$)/, (route) => { - return route.request().method() === 'DELETE' - ? route.fulfill({ json: { count: 1 } }) - : route.fulfill({ json: { data: [partialSync], pageCount: 1, total: 1 } }); + return route.fulfill({ json: { data: [partialSync], pageCount: 1, total: 1 } }); }); }); - test('should keep a long repository URL inside its column, even on the selected row', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - const url = reposPage.dataTable.getByTestId('instrument-repo-url'); - - await url.click(); - - const overflow = await url.evaluate((element) => ({ - clipped: element.scrollWidth > element.clientWidth, - spill: element.getBoundingClientRect().right - element.parentElement!.getBoundingClientRect().right - })); - expect(overflow.clipped).toBe(true); - expect(overflow.spill).toBeLessThanOrEqual(0); - }); - - // The count used to be read from each instrument's provenance, which names the import that created - // it, so an instrument the repository provides but did not create went uncounted here while the - // activity log still counted it as imported. - test('should count the instruments a repository provides, whichever import created them', async ({ - getPageModel, - page - }) => { - await page.route(/\/v1\/instruments\/info/, (route) => route.fulfill({ json: [] })); - const reposPage = await getPageModel('/admin/instrument-repos'); - - await expect(reposPage.dataTable.getByTestId('instrument-repo-instrument-count')).toHaveText('2'); - }); - - test('should say how many of the imported instruments were new, so the total is not read as all new', async ({ + // Layout is the one thing the unit tests cannot see (happy-dom has no layout engine), so this walks + // both tabs once and measures everything that has to fit at the admin pages' real width. + test('should fit both tables to the page, clipping long values to one line rather than spilling or scrolling', async ({ getPageModel }) => { const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.openActivityLog(); - - await expect(reposPage.syncLogTable.getByText('2 (1 new)')).toBeVisible(); - }); - - test('should count the failed instruments and explain them in the activity log', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - - await expect(reposPage.dataTable.getByTestId('instrument-repo-failed-count')).toHaveText('1'); - await reposPage.openActivityLog(); - await expect(reposPage.syncLogTable.getByTestId('sync-log-status')).toHaveText('Partial success'); - await reposPage.syncLogTable.getByTestId('sync-log-view-messages').click(); - const details = reposPage.$ref.getByTestId('sync-log-error-details'); - await expect(details).toContainText('CPP_SERIES'); - await expect(details).toContainText('Load'); - }); - - test('should show one table at a time, switching between them by tab', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - await expect(reposPage.dataTable.getByTestId('instrument-repo-url')).toBeVisible(); - await expect(reposPage.syncLogTable).toHaveCount(0); - - await expect(reposPage.sectionTitle).toHaveText('Manage Repositories'); - - await reposPage.openActivityLog(); - - await expect(reposPage.syncLogTable).toBeVisible(); - await expect(reposPage.sectionTitle).toHaveText('Review Activity'); - await expect(reposPage.$ref.getByTestId('instrument-repo-url')).toHaveCount(0); - }); - - test('should draw no card behind either tab', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - const expectInvisible = async () => { + const expectInvisibleCard = async () => { await expect(reposPage.card).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)'); await expect(reposPage.card).toHaveCSS('border-top-color', 'rgba(0, 0, 0, 0)'); // Tailwind v4 compiles `shadow-none` to transparent layers rather than the keyword `none`, @@ -236,36 +81,15 @@ test.describe('admin instrument repos activity log', () => { const shadow = await reposPage.card.evaluate((card) => getComputedStyle(card).boxShadow); expect(shadow.replace(/rgba\(0, 0, 0, 0\)|0px|,|\s/g, '')).toBe(''); }; - await expectInvisible(); - - await reposPage.openActivityLog(); - - await expectInvisible(); - }); - - test('should offer a sort on every column of both tables, asking the server to order the activity log', async ({ - getPageModel, - page - }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - const repoHead = reposPage.dataTable.getByTestId('data-table-head'); - for (const name of ['Repository Name', 'URL', 'Instruments', 'Failed', 'Last Synced']) { - await expect(repoHead.getByRole('button', { name })).toBeVisible(); - } - - await reposPage.openActivityLog(); - const logHead = reposPage.syncLogTable.getByTestId('data-table-head'); - for (const name of ['Date', 'Repository', 'Action', 'Status', 'Imported', 'Failed']) { - await expect(logHead.getByRole('button', { name })).toBeVisible(); - } - const request = page.waitForRequest((req) => req.url().includes('sortBy=status')); - await logHead.getByRole('button', { name: 'Status' }).click(); - await request; - }); - - test('should leave room for every header beside its sort icon, so none is clipped', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - const clippedLabels = (table: typeof reposPage.dataTable) => { + // libui renders a table `visibility: hidden`, at the default width of every column, until its + // ResizeObserver has measured the container and sized the columns to it. Measuring before then + // reads that provisional width, which is wider than the page. + const measuredBody = async (table: Locator) => { + const body = table.getByTestId('data-table-body'); + await expect(body).toBeVisible(); + return body; + }; + const clippedHeaders = (table: Locator) => { return table .getByTestId('data-table-head') .locator('button span') @@ -273,178 +97,45 @@ test.describe('admin instrument repos activity log', () => { return labels.filter((label) => label.scrollWidth > label.clientWidth).map((label) => label.textContent); }); }; - - await expect(reposPage.dataTable.getByTestId('instrument-repo-url')).toBeVisible(); - expect(await clippedLabels(reposPage.dataTable)).toEqual([]); - await reposPage.openActivityLog(); - await expect(reposPage.syncLogTable.getByTestId('data-table-head')).toBeVisible(); - expect(await clippedLabels(reposPage.syncLogTable)).toEqual([]); - }); - - test('should fit both tables to the page without a horizontal scroll bar', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - const overflows = (table: typeof reposPage.dataTable) => { - return table - .getByTestId('data-table-body') - .evaluate( - (body) => body.parentElement!.parentElement!.scrollWidth > body.parentElement!.parentElement!.clientWidth - ); + const scrollsSideways = (body: Locator) => { + return body.evaluate((element) => { + const container = element.parentElement!.parentElement!; + return container.scrollWidth > container.clientWidth; + }); }; - expect(await overflows(reposPage.dataTable)).toBe(false); - await reposPage.openActivityLog(); - expect(await overflows(reposPage.syncLogTable)).toBe(false); - }); - - test('should hold a one-row table open to five rows, so it does not collapse to a sliver', async ({ - getPageModel - }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - const body = reposPage.dataTable.getByTestId('data-table-body'); - - const height = await body.evaluate((element) => element.getBoundingClientRect().height); - const rowHeight = await body + const reposBody = await measuredBody(reposPage.dataTable); + await expectInvisibleCard(); + expect(await clippedHeaders(reposPage.dataTable)).toEqual([]); + expect(await scrollsSideways(reposBody)).toBe(false); + const rowHeight = await reposBody .getByTestId('data-table-row') .evaluate((element) => element.getBoundingClientRect().height); - expect(height).toBeGreaterThanOrEqual(rowHeight * 5); - }); - - test('should count zero failed instruments when the whole sync failed, rather than its reason', async ({ - getPageModel, - page - }) => { - const failedSync = { ...partialSync, failures: [], importedCount: 0, message: 'Repository not found' }; - await page.route(/\/v1\/instrument-repos\/sync\/logs\/latest/, (route) => route.fulfill({ json: [failedSync] })); - const reposPage = await getPageModel('/admin/instrument-repos'); - - await expect(reposPage.dataTable.getByTestId('instrument-repo-failed-count')).toHaveText('0'); - }); + expect(await reposBody.evaluate((element) => element.getBoundingClientRect().height)).toBeGreaterThanOrEqual( + rowHeight * 5 + ); + const url = reposPage.dataTable.getByTestId('instrument-repo-url'); + await url.click(); + const urlLayout = await url.evaluate((element) => ({ + clipped: element.scrollWidth > element.clientWidth, + spill: element.getBoundingClientRect().right - element.parentElement!.getBoundingClientRect().right + })); + expect(urlLayout.clipped).toBe(true); + expect(urlLayout.spill).toBeLessThanOrEqual(0); - test('should clip a long repository name to one line and name it in full on hover', async ({ - getPageModel, - page - }) => { - const repoName = 'LaboratoireDeRecherchePsychiatrique/instruments-cliniques-internes-du-laboratoire'; - await page.route(/\/v1\/instrument-repos\/sync\/logs(\?|$)/, (route) => { - return route.fulfill({ json: { data: [{ ...partialSync, repoName }], pageCount: 1, total: 1 } }); - }); - const reposPage = await getPageModel('/admin/instrument-repos'); await reposPage.openActivityLog(); - const cell = reposPage.syncLogTable.getByTestId('sync-log-repo-name'); - - await expect(cell).toHaveAttribute('title', repoName); - const layout = await cell.evaluate((element) => ({ + const logBody = await measuredBody(reposPage.syncLogTable); + await expectInvisibleCard(); + expect(await clippedHeaders(reposPage.syncLogTable)).toEqual([]); + expect(await scrollsSideways(logBody)).toBe(false); + const repoName = reposPage.syncLogTable.getByTestId('sync-log-repo-name'); + await expect(repoName).toHaveAttribute('title', partialSync.repoName); + const repoNameLayout = await repoName.evaluate((element) => ({ clipped: element.scrollWidth > element.clientWidth, lines: Math.round(element.getBoundingClientRect().height / parseFloat(getComputedStyle(element).lineHeight)) })); - expect(layout.clipped).toBe(true); - expect(layout.lines).toBe(1); - }); - - test('should keep the failure reason out of the activity log row, and show it in full on View', async ({ - getPageModel - }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - await reposPage.openActivityLog(); - - await expect(reposPage.syncLogTable).not.toContainText('PHQ_9'); - await reposPage.syncLogTable.getByTestId('sync-log-view-messages').click(); - - const details = reposPage.$ref.getByTestId('sync-log-error-details'); - await expect(details).toContainText("Series item 'PHQ_9' edition 2 does not exist on this instance"); - }); - - test('should say the activity log is empty in one line, rather than render an empty table', async ({ - getPageModel, - page - }) => { - await page.route(/\/v1\/instrument-repos\/sync\/logs(\?|$)/, (route) => { - return route.fulfill({ json: { data: [], pageCount: 0, total: 0 } }); - }); - const reposPage = await getPageModel('/admin/instrument-repos'); - await reposPage.openActivityLog(); - - await expect(reposPage.syncLogTable).toHaveText('No imports or syncs recorded yet.'); - await expect(reposPage.syncLogTable.getByTestId('data-table')).toHaveCount(0); - }); - - test('should ask the server for the next page, rather than hold the whole history', async ({ - getPageModel, - page - }) => { - await page.route(/\/v1\/instrument-repos\/sync\/logs(\?|$)/, (route) => { - const requested = new URL(route.request().url()).searchParams.get('page') ?? '1'; - return route.fulfill({ - json: { - data: [{ ...partialSync, id: `log-${requested}`, repoName: `Repo on page ${requested}` }], - pageCount: 2, - total: 11 - } - }); - }); - const reposPage = await getPageModel('/admin/instrument-repos'); - await reposPage.openActivityLog(); - await expect(reposPage.syncLogTable).toContainText('Repo on page 1'); - - await reposPage.syncLogTable.getByRole('button', { name: '2' }).click(); - - await expect(reposPage.syncLogTable).toContainText('Repo on page 2'); - }); - - test('should open the activity log of one repository from its row actions', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.selectRowAction(0, 'Logs'); - - const dialog = reposPage.$ref.getByRole('dialog'); - await expect(dialog).toContainText(`Activity log of "${repo.name}"`); - await expect(dialog).toHaveAttribute('aria-describedby', /.+/); - await expect(dialog.getByTestId('sync-log-status')).toHaveText('Partial success'); - }); - - test('should link the viewed repository to its source and offer an instrument search', async ({ getPageModel }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.selectRowAction(0, 'View'); - - await expect(reposPage.$ref.getByTestId('instrument-repo-source-link')).toHaveAttribute('href', repo.url); - await expect(reposPage.$ref.getByPlaceholder('Search instruments...')).toBeVisible(); - }); - - test('should sync every repository and warn when one of them had failed instruments', async ({ - getPageModel, - page - }) => { - let syncAllRequested = false; - await page.route('**/v1/instrument-repos/sync/all', (route) => { - syncAllRequested = true; - return route.fulfill({ json: [partialSync] }); - }); - const reposPage = await getPageModel('/admin/instrument-repos'); - - await reposPage.syncAllButton.click(); - - await expect(reposPage.$ref.getByText('1 of 1 repositories had problems. See the activity log.')).toBeVisible(); - expect(syncAllRequested).toBe(true); - }); - - test('should clear the activity log only after warning that it is deleted from the database', async ({ - getPageModel, - page - }) => { - const reposPage = await getPageModel('/admin/instrument-repos'); - const deleteRequest = page.waitForRequest( - (request) => request.method() === 'DELETE' && request.url().endsWith('/v1/instrument-repos/sync/logs') - ); - - await reposPage.openActivityLog(); - await reposPage.clearLogButton.click(); - const dialog = reposPage.$ref.getByRole('dialog'); - await expect(dialog).toContainText('permanently deleted from the database'); - await dialog.getByRole('button', { name: 'Yes' }).click(); - - await deleteRequest; - await expect(reposPage.$ref.getByText('Activity log cleared')).toBeVisible(); + expect(repoNameLayout.clipped).toBe(true); + expect(repoNameLayout.lines).toBe(1); }); }); diff --git a/testing/src/specs/admin-instruments.spec.ts b/testing/src/specs/admin-instruments.spec.ts index cfc1f91d6..03dd89c48 100644 --- a/testing/src/specs/admin-instruments.spec.ts +++ b/testing/src/specs/admin-instruments.spec.ts @@ -1,99 +1,42 @@ import { expect, test } from '../support/fixtures'; -const API = '/api/v1'; - test.describe('admin instruments', () => { test.use({ actingRole: 'ADMIN' }); - test('should preview an instrument when its row is double-clicked', async ({ getPageModel, page }) => { - const title = 'Happiness Questionnaire'; - const instrumentsPage = await getPageModel('/admin/instruments'); - await instrumentsPage.searchBar.fill(title); - await instrumentsPage.row(title).dblclick(); - await expect(page.getByRole('dialog')).toContainText(title); - }); - - test('should tag the kind of an instrument in the forms view', async ({ getPageModel }) => { + test('should tag the kind of an instrument and preview it when its row is double-clicked', async ({ + getPageModel, + page + }) => { const title = 'Happiness Questionnaire'; const instrumentsPage = await getPageModel('/admin/instruments'); await instrumentsPage.searchBar.fill(title); await expect(instrumentsPage.row(title).getByTestId('instrument-cell-kind')).toHaveText('Form'); - }); - test('should reach the series view from the Instruments submenu in the sidebar', async ({ getPageModel, page }) => { - await getPageModel('/admin/instruments'); - const sidebar = page.getByTestId('sidebar'); - // The admin links sit in the Admin Panel section, which starts collapsed even on an admin page. - await sidebar.getByRole('button', { exact: true, name: 'Admin Panel' }).click(); - await sidebar.getByRole('button', { exact: true, name: 'Instruments' }).click(); - await sidebar.getByRole('button', { exact: true, name: 'Series' }).click(); - await expect(page).toHaveURL(/\/admin\/instruments\?view=series/); - await expect(page.getByTestId('page-header')).toContainText('Series Instruments'); - }); - - test('should find a series by searching, listed under the name of the group that owns it', async ({ - api, - getPageModel, - uniqueId - }) => { - const group = await api.createGroup({ name: `SeriesOwner${uniqueId}` }); - const title = `Series ${uniqueId}`; - await api.createSeries(group.id, title); - - const seriesPage = await getPageModel('/admin/instruments'); - await seriesPage.openSeriesView(); - await seriesPage.searchBar.fill(title); + await instrumentsPage.row(title).dblclick(); - await expect(seriesPage.row(title)).toContainText(`SeriesOwner${uniqueId}`); + await expect(page.getByRole('dialog')).toContainText(title); }); - test('should archive a series, so no new assignment of it is accepted, and unarchive it again', async ({ - adminToken, + test('should find a series under the group that owns it, archive it and unarchive it again', async ({ api, - apiRequestContext, getPageModel, uniqueId }) => { const group = await api.createGroup({ name: `SeriesArchive${uniqueId}` }); const title = `Series ${uniqueId}`; - const seriesId = await api.createSeries(group.id, title); - const subjectId = await api.createSubject(group.id); + await api.createSeries(group.id, title); const seriesPage = await getPageModel('/admin/instruments'); await seriesPage.openSeriesView(); await seriesPage.searchBar.fill(title); + await expect(seriesPage.row(title)).toContainText(`SeriesArchive${uniqueId}`); + await seriesPage.chooseRowAction(title, 'Archive'); await expect(seriesPage.archiveDialog).toContainText(`SeriesArchive${uniqueId}`); await seriesPage.confirmArchive.click(); await expect(seriesPage.seriesStatus(title)).toHaveAttribute('data-archived', 'true'); - const assignment = await apiRequestContext.post(`${API}/assignments`, { - data: { expiresAt: new Date(Date.now() + 86_400_000), groupId: group.id, instrumentId: seriesId, subjectId }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(assignment.status(), await assignment.text()).toBe(422); - await seriesPage.chooseRowAction(title, 'Unarchive'); await expect(seriesPage.seriesStatus(title)).toHaveAttribute('data-archived', 'false'); }); - - test('should archive an active series on a double-click, through the confirmation, and unarchive an archived one', async ({ - api, - getPageModel, - uniqueId - }) => { - const group = await api.createGroup({ name: `SeriesDoubleClick${uniqueId}` }); - const title = `Series ${uniqueId}`; - await api.createSeries(group.id, title); - - const seriesPage = await getPageModel('/admin/instruments'); - await seriesPage.openSeriesView(); - await seriesPage.searchBar.fill(title); - await seriesPage.row(title).dblclick(); - await seriesPage.confirmArchive.click(); - await expect(seriesPage.seriesStatus(title)).toHaveAttribute('data-archived', 'true'); - - await seriesPage.row(title).dblclick(); - await expect(seriesPage.seriesStatus(title)).toHaveAttribute('data-archived', 'false'); - }); }); diff --git a/testing/src/specs/admin-management.spec.ts b/testing/src/specs/admin-management.spec.ts index 3802befbd..3c3936166 100644 --- a/testing/src/specs/admin-management.spec.ts +++ b/testing/src/specs/admin-management.spec.ts @@ -21,7 +21,11 @@ async function dismissWelcomeEmailFallback(page: Page, { requireSuccessToast = f test.describe('admin management', () => { test.use({ actingRole: 'ADMIN' }); - test('should create a group through the UI @smoke', async ({ authenticateAs, page, uniqueId }) => { + test('should create a group through the UI, then find it by search and open its manage sheet', async ({ + authenticateAs, + page, + uniqueId + }) => { const groupName = `E2E Group ${uniqueId}`; await authenticateAs('ADMIN'); @@ -32,184 +36,54 @@ test.describe('admin management', () => { await page.getByTestId('type-select-item-CLINICAL').click(); await page.getByRole('button', { name: 'Submit' }).click(); - // Success toast + redirect are the app's own success signals; the "group is now listed" case is - // covered by the search test below. await expect(page.getByRole('heading', { name: 'Success' })).toBeVisible(); await expect(page).toHaveURL('/admin/groups'); - }); - test('should list users for an admin', async ({ authenticateAs, page }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/users'); - - await expect(page.getByTestId('data-table')).toBeVisible(); - // The admin created during setup is always present. - await expect(page.getByTestId('data-table-body')).toContainText('admin'); - }); - - test('should show enabled and disabled accounts immediately left of archive status', async ({ - api, - authenticateAs, - page - }) => { - const group = await api.createGroup(); - const { user: enabled } = await api.createUser({ groupIds: [group.id] }); - const { user: disabled } = await api.createUser({ disabled: true, groupIds: [group.id] }); - await authenticateAs('ADMIN'); - await page.goto('/admin/users'); - await expect(page.getByTestId('data-table-head').getByRole('button', { name: 'Status' })).toBeVisible(); - const headers = await page.getByTestId('data-table-head').getByRole('button').allTextContents(); - expect(headers.filter(Boolean).indexOf('Enabled / Disabled')).toBe(headers.filter(Boolean).indexOf('Status') - 1); - const search = page.getByTestId('data-table-search-bar').getByRole('searchbox'); - await search.fill(enabled.username); - await expect(page.getByTestId('user-login-status')).toHaveText('Enabled'); - await expect(page.getByTestId('user-status-active')).toBeVisible(); - await search.fill(disabled.username); - await expect(page.getByTestId('user-login-status')).toHaveText('Disabled'); - await expect(page.getByTestId('user-status-active')).toBeVisible(); - }); - - test('should show the groups of a user in the users list and every one of them on hover', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const secondGroup = await api.createGroup({ name: `B Group ${uniqueId}` }); - const firstGroup = await api.createGroup({ name: `A Group ${uniqueId}` }); - const { user } = await api.createUser({ groupIds: [secondGroup.id, firstGroup.id] }); - await authenticateAs('ADMIN'); - await page.goto('/admin/users'); - await page.getByTestId('data-table-search-bar').getByRole('searchbox').fill(user.username); - const groupsCell = page.getByTestId('user-groups'); - await expect(groupsCell).toHaveText(`${firstGroup.name} +1`); - await groupsCell.hover(); - await expect(page.getByTestId('user-groups-list').first()).toHaveText([firstGroup.name, secondGroup.name].join('')); - }); - - test('should show a validation error when the group name is missing', async ({ authenticateAs, page }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/groups/create'); - - await page.getByRole('button', { name: 'Submit' }).click(); - - // Every missing field reports the same thing, including the ones backed by a select — those - // raise an issue code that used to fall through to zod's untranslated default. - const errorMessages = page.getByTestId('error-message-text'); - await expect(errorMessages.first()).toBeVisible(); - for (const message of await errorMessages.allTextContents()) { - expect(message).toBe('This field is required'); - } - await expect(page).toHaveURL('/admin/groups/create'); - }); - - test('should find a created group by search and open its manage sheet', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const group = await api.createGroup({ name: `E2E Group ${uniqueId}` }); - - await authenticateAs('ADMIN'); - await page.goto('/admin/groups'); - - await page.getByTestId('data-table-search-bar').getByRole('searchbox').fill(group.name); + await page.getByTestId('data-table-search-bar').getByRole('searchbox').fill(groupName); const row = page.getByTestId('data-table-row'); await expect(row).toHaveCount(1); - await expect(row).toContainText(group.name); + await expect(row).toContainText(groupName); await expect(row.getByTestId('group-cell-type')).toBeVisible(); await expect(row.getByTestId('group-status-active')).toBeVisible(); await row.getByTestId('row-actions-trigger').click(); await page.getByTestId('row-actions-dropdown').getByRole('menuitem', { name: 'Manage' }).click(); - await expect(page.getByRole('dialog').getByText(group.name)).toBeVisible(); - }); - - test('should show validation errors when submitting the user creation form empty', async ({ - authenticateAs, - page - }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - await page.getByRole('button', { name: 'Submit' }).click(); - - // Username, password, confirm password, first name and last name are all required. - await expect( - page.getByTestId('error-message-text').filter({ hasText: 'This field is required' }).first() - ).toBeVisible(); - await expect(page).toHaveURL('/admin/users/create'); + await expect(page.getByRole('dialog').getByText(groupName)).toBeVisible(); }); - test('should reject a password that does not meet the strength requirement', async ({ + test('should list users with their login status left of their archive status, and every group on hover', async ({ + api, authenticateAs, page, uniqueId }) => { + const secondGroup = await api.createGroup({ name: `B Group ${uniqueId}` }); + const firstGroup = await api.createGroup({ name: `A Group ${uniqueId}` }); + const { user: enabled } = await api.createUser({ groupIds: [secondGroup.id, firstGroup.id] }); + const { user: disabled } = await api.createUser({ disabled: true, groupIds: [firstGroup.id] }); await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - await page.getByLabel('Username').fill(`user${uniqueId}`); - await page.getByLabel('Password', { exact: true }).fill('weak'); - await page.getByLabel('Confirm Password').fill('weak'); - await page.getByLabel('First Name').fill('Test'); - await page.getByLabel('Last Name').fill('User'); - await page.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-ADMIN').click(); - await page.getByRole('button', { name: 'Submit' }).click(); - - await expect( - page.getByTestId('error-message-text').filter({ hasText: 'Insufficient password strength' }) - ).toBeVisible(); - await expect(page).toHaveURL('/admin/users/create'); - }); - - test('should reject a non-admin user created without a group', async ({ authenticateAs, page, uniqueId }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - await page.getByLabel('Username').fill(`user${uniqueId}`); - await page.getByLabel('Password', { exact: true }).fill(SEEDED_USER_PASSWORD); - await page.getByLabel('Confirm Password').fill(SEEDED_USER_PASSWORD); - await page.getByLabel('First Name').fill('Test'); - await page.getByLabel('Last Name').fill('User'); - // By testid rather than `getByRole('combobox').first()`: when mail is enabled a welcome-email - // language select renders above the form, and `.first()` then opens the wrong one. - await page.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-GROUP_MANAGER').click(); - await page.getByRole('button', { name: 'Submit' }).click(); - - await expect( - page.getByTestId('error-message-text').filter({ hasText: 'must belong to at least one group' }) - ).toBeVisible(); - await expect(page).toHaveURL('/admin/users/create'); - }); + await page.goto('/admin/users'); - test('should allow a disabled non-admin user with no group, since such an account only attributes uploaded data', async ({ - authenticateAs, - page, - uniqueId - }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); + // The admin created during setup is always present. + await expect(page.getByTestId('data-table-body')).toContainText('admin'); + const headers = (await page.getByTestId('data-table-head').getByRole('button').allTextContents()).filter(Boolean); + expect(headers.indexOf('Enabled / Disabled')).toBe(headers.indexOf('Status') - 1); - await page.getByLabel('Username').fill(`user${uniqueId}`); - await page.getByLabel('Password', { exact: true }).fill(SEEDED_USER_PASSWORD); - await page.getByLabel('Confirm Password').fill(SEEDED_USER_PASSWORD); - await page.getByLabel('First Name').fill('Test'); - await page.getByLabel('Last Name').fill('User'); - await page.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-GROUP_MANAGER').click(); - // The radio items carry stable ids (`-true`), unlike their labels, which libui translates. - await page.locator('#disabled-true').click(); - await page.getByRole('button', { name: 'Submit' }).click(); + const search = page.getByTestId('data-table-search-bar').getByRole('searchbox'); + await search.fill(enabled.username); + await expect(page.getByTestId('user-login-status')).toHaveText('Enabled'); + await expect(page.getByTestId('user-status-active')).toBeVisible(); + const groupsCell = page.getByTestId('user-groups'); + await expect(groupsCell).toHaveText(`${firstGroup.name} +1`); + await groupsCell.hover(); + await expect(page.getByTestId('user-groups-list').first()).toHaveText([firstGroup.name, secondGroup.name].join('')); - await dismissWelcomeEmailFallback(page); - await expect(page).toHaveURL('/admin/users'); + await search.fill(disabled.username); + await expect(page.getByTestId('user-login-status')).toHaveText('Disabled'); + await expect(page.getByTestId('user-status-active')).toBeVisible(); }); - test('should create a user through the UI with the username lowercased and show it in the users list @smoke', async ({ + test('should create a user through the UI with the username lowercased and show it in the users list', async ({ api, authenticateAs, page, @@ -271,211 +145,59 @@ test.describe('admin management', () => { expect((await api.findUserById(user.id)).archivedAt).toBeTruthy(); }); - test('should say why a save failed when the rejected field is scrolled out of view', async ({ + test('should save account changes and completed permission rows together without using plus to confirm them', async ({ api, - getPageModel - }) => { - // The API accepts a non-admin user with no group; the profile form, like the create form, does - // not. Groups is its last section, so the inline rejection sits below the fold of a page the - // admin submits from the top of -- the submit otherwise reads as a no-op (#1472). - const { user } = await api.createUser({ groupIds: [] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.profileForm.getByLabel('Email').fill(`${user.username}@example.com`); - await userPage.saveProfile(); - - await expect(userPage.submitError).toBeVisible(); - await expect(userPage.submitError).toContainText('must belong to at least one group'); - }); - - test('should create a user whose email was typed and then cleared', async ({ authenticateAs, page, uniqueId }) => { - const username = `user_${uniqueId}`; - - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - const createUserForm = page.getByTestId('create-user-form'); - await createUserForm.getByLabel('Username').fill(username); - await createUserForm.getByLabel('Password', { exact: true }).fill(SEEDED_USER_PASSWORD); - await createUserForm.getByLabel('Confirm Password').fill(SEEDED_USER_PASSWORD); - await createUserForm.getByLabel('First Name').fill('Test'); - await createUserForm.getByLabel('Last Name').fill('User'); - - const emailInput = createUserForm.getByLabel('Email'); - await emailInput.fill(`contact-${uniqueId}@example.org`); - await emailInput.clear(); - - // ADMIN so the form does not also demand a group. - await createUserForm.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-ADMIN').click(); - await createUserForm.getByRole('button', { name: 'Submit' }).click(); - - await dismissWelcomeEmailFallback(page); - await expect(page).toHaveURL('/admin/users'); - }); - - test('should show a username taken in another case as an error on the focused field, without creating the user', async ({ - api, - authenticateAs, + getPageModel, page, uniqueId }) => { - const { user } = await api.createUser({ username: `taken_${uniqueId}` }); - - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - await page.getByLabel('Username').fill(user.username.toUpperCase()); - await page.getByLabel('Password', { exact: true }).fill(SEEDED_USER_PASSWORD); - await page.getByLabel('Confirm Password').fill(SEEDED_USER_PASSWORD); - await page.getByLabel('First Name').fill('Test'); - await page.getByLabel('Last Name').fill('User'); - await page.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-ADMIN').click(); - await page.getByRole('button', { name: 'Submit' }).click(); - - await expect(page.getByTestId('error-message-text').filter({ hasText: 'Username already exists' })).toBeVisible(); - await expect(page.getByLabel('Username')).toBeFocused(); - await expect(page).toHaveURL('/admin/users/create'); - }); - - test('should refuse a username that differs from an existing one only by case over the API', async ({ - api, - uniqueId - }) => { - await api.createUser({ username: `taken_${uniqueId}` }); - await expect(api.createUser({ username: `TAKEN_${uniqueId.toUpperCase()}` })).rejects.toThrow(/already exists/); - }); - - test('should reject a phone number with too few digits over the API', async ({ api }) => { - await expect(api.createUser({ phoneNumber: '123' })).rejects.toThrow(/Phone number must contain at least 7 digits/); - }); - - test('should return to the users list from the back link on the user page', async ({ api, getPageModel, page }) => { - const { user } = await api.createUser(); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.backLink.click(); - - await expect(page).toHaveURL('/admin/users'); - }); - - test('should list the permissions a user already holds on the user page', async ({ api, getPageModel }) => { const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - await api.setUserPermissions(user.id, [{ action: 'read', groupId: null, subject: 'Subject' }]); - + const { user } = await api.createUser({ email: `contact-${uniqueId}@example.org`, groupIds: [group.id] }); const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await expect(userPage.permissionRows).toHaveCount(1); - await expect(userPage.permissionRows.first()).toContainText('Read'); - await expect(userPage.permissionRows.first()).toContainText('Subject'); - await expect(userPage.permissionRows.first().getByTestId('user-permission-scope')).toContainText('All Groups'); - }); - test('should save a user whose new password was typed and then cleared', async ({ api, getPageModel, uniqueId }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - const newPassword = userPage.profileForm.getByLabel('Set new password'); - - // The browser fills these fields with the administrator's own saved credentials, so clearing - // one is the ordinary way out of that -- and used to reject the field for being empty, and its - // confirmation for no longer matching. - await expect(newPassword).toHaveAttribute('autocomplete', 'new-password'); - await expect(userPage.profileForm.getByLabel('Confirm new password')).toHaveAttribute( - 'autocomplete', - 'new-password' + // Enabled then Disabled on one line under Status, with Enabled chosen for an enabled user. + const enabled = userPage.profileForm.getByLabel('Enabled', { exact: true }); + const disabled = userPage.profileForm.getByLabel('Disabled', { exact: true }); + await expect(enabled).toBeChecked(); + await expect(disabled).not.toBeChecked(); + const enabledBox = (await enabled.boundingBox())!; + const disabledBox = (await disabled.boundingBox())!; + expect(Math.abs(enabledBox.y - disabledBox.y)).toBeLessThan(2); + expect(disabledBox.x).toBeGreaterThan(enabledBox.x); + expect(enabledBox.y).toBeGreaterThan( + (await userPage.profileForm.getByText('Status', { exact: true }).boundingBox())!.y ); - await newPassword.fill(`${uniqueId}-then-cleared`); - await newPassword.clear(); - await userPage.saveProfile(); - await expect(userPage.$ref.getByRole('heading', { name: 'Success' }).last()).toBeVisible(); - }); - - test("should clear a user's email from the user page", async ({ api, getPageModel, uniqueId }) => { - const email = `contact-${uniqueId}@example.org`; - const group = await api.createGroup(); - const { user } = await api.createUser({ email, groupIds: [group.id] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - const emailInput = userPage.profileForm.getByLabel('Email'); - await expect(emailInput).toHaveValue(email); - await emailInput.clear(); - await userPage.saveProfile(); - await expect(userPage.$ref.getByRole('heading', { name: 'Success' }).last()).toBeVisible(); - - expect((await api.findUserById(user.id)).email).toBeNull(); - }); - - test('should save account and completed permission rows together without using plus to confirm them', async ({ - api, - getPageModel, - page, - uniqueId - }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - const email = `shared-save-${uniqueId}@example.org`; - + // One save button, below the permissions, submits both cards. await expect(userPage.profileForm.getByRole('button', { name: 'Submit' })).toBeHidden(); const save = page.getByTestId('save-user-changes'); - const permissionsCard = page.getByTestId('user-permissions-card'); - expect((await save.boundingBox())!.y).toBeGreaterThan((await permissionsCard.boundingBox())!.y); + expect((await save.boundingBox())!.y).toBeGreaterThan( + (await page.getByTestId('user-permissions-card').boundingBox())!.y + ); - await userPage.profileForm.getByLabel('Email').fill(email); + // Clearing the email is the account change, since a blank field must be saved as no email at all. + await userPage.profileForm.getByLabel('Email').clear(); await userPage.addPermission({ action: 'read', subject: 'User' }); await userPage.addPermissionRowAfterCurrent(); await userPage.addPermission({ action: 'create', subject: 'Instrument' }); const beforeSave = await api.findUserById(user.id); - expect(beforeSave.email).not.toBe(email); + expect(beforeSave.email).not.toBeNull(); expect(beforeSave.additionalPermissions).toEqual([]); await userPage.saveProfile(); await expect(userPage.permissionRows).toHaveCount(2); await expect(page.getByRole('heading', { exact: true, name: 'Success' })).toHaveCount(1); await page.reload(); - await expect(userPage.profileForm.getByLabel('Email')).toHaveValue(email); - expect((await api.findUserById(user.id)).additionalPermissions).toEqual([ + await expect(userPage.profileForm.getByLabel('Email')).toHaveValue(''); + const saved = await api.findUserById(user.id); + expect(saved.email).toBeNull(); + expect(saved.additionalPermissions).toEqual([ { action: 'read', groupId: group.id, subject: 'User' }, { action: 'create', groupId: null, subject: 'Instrument' } ]); }); - test('should save no permissions when account validation fails', async ({ api, getPageModel }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.addPermission({ action: 'read', subject: 'User' }); - await userPage.profileForm.getByLabel('Email').fill('invalid-email'); - await userPage.saveProfile(); - await expect(userPage.submitError).toBeVisible(); - expect((await api.findUserById(user.id)).additionalPermissions).toEqual([]); - }); - - test('should offer Enabled then Disabled on one line under Status, with Enabled chosen for an enabled user', async ({ - api, - getPageModel - }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - const enabled = userPage.profileForm.getByLabel('Enabled', { exact: true }); - const disabled = userPage.profileForm.getByLabel('Disabled', { exact: true }); - - await expect(enabled).toBeChecked(); - await expect(disabled).not.toBeChecked(); - const enabledBox = (await enabled.boundingBox())!; - const disabledBox = (await disabled.boundingBox())!; - expect(Math.abs(enabledBox.y - disabledBox.y)).toBeLessThan(2); - expect(disabledBox.x).toBeGreaterThan(enabledBox.x); - const statusLabel = userPage.profileForm.getByText('Status', { exact: true }); - expect(enabledBox.y).toBeGreaterThan((await statusLabel.boundingBox())!.y); - }); - - test('should refuse to save while a permission row is half filled, rather than silently dropping it', async ({ + test('should save nothing while a permission row is half filled or the account is invalid, rather than silently dropping either', async ({ api, getPageModel, page, @@ -484,7 +206,8 @@ test.describe('admin management', () => { const [group, otherGroup] = await Promise.all([api.createGroup(), api.createGroup()]); const { user } = await api.createUser({ groupIds: [group.id, otherGroup.id] }); const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.profileForm.getByLabel('Email').fill(`half-filled-${uniqueId}@example.org`); + const email = userPage.profileForm.getByLabel('Email'); + await email.fill(`half-filled-${uniqueId}@example.org`); // With two groups no scope is preselected, so this row cannot become a grant yet. await userPage.addPermission({ action: 'read', subject: 'Subject' }); @@ -498,6 +221,12 @@ test.describe('admin management', () => { expect(stored.additionalPermissions).toEqual([]); await userPage.selectPermissionOption('scope', otherGroup.id); + await email.fill('invalid-email'); + await userPage.saveProfile(); + await expect(userPage.submitError).toBeVisible(); + expect((await api.findUserById(user.id)).additionalPermissions).toEqual([]); + + await email.fill(`half-filled-${uniqueId}@example.org`); await userPage.saveProfile(); await expect(page.getByTestId('permission-drafts-incomplete')).toBeHidden(); await expect(userPage.permissionRows).toHaveCount(1); @@ -506,158 +235,32 @@ test.describe('admin management', () => { ]); }); - test('should discard unsaved permission additions and removals on reload', async ({ api, getPageModel, page }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - await api.setUserPermissions(user.id, [{ action: 'create', groupId: null, subject: 'Instrument' }]); - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.removePermission(0); - await userPage.addPermission({ action: 'read', subject: 'User' }); - await page.reload(); - await expect(userPage.permissionRows).toHaveCount(1); - await expect(userPage.permissionRows.first()).toContainText('Instrument'); - await expect(userPage.addPermissionRow.getByTestId('action-select-trigger')).toContainText('Choose'); - }); - - test('should grant a permission confined to the one group a user belongs to from the user page @smoke', async ({ + test('should replace a grant with one confined to the only group the user belongs to from the user page', async ({ api, getPageModel, uniqueId }) => { const group = await api.createGroup({ name: `Scoped Group ${uniqueId}` }); const { user } = await api.createUser({ basePermissionLevel: 'STANDARD', groupIds: [group.id] }); + await api.setUserPermissions(user.id, [{ action: 'create', groupId: null, subject: 'Instrument' }]); const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - // The scope is left untouched: with a single group it is preselected, so the group is the - // default rather than something the admin has to remember to choose. - await userPage.addPermission({ action: 'read', subject: 'User' }); - expect((await api.findUserById(user.id)).additionalPermissions).toStrictEqual([]); - await userPage.saveProfile(); - await expect(userPage.permissionRows).toHaveCount(1); - await expect(userPage.permissionRows.first().getByTestId('user-permission-scope')).toContainText(group.name); - expect((await api.findUserById(user.id)).additionalPermissions).toStrictEqual([ - { action: 'read', groupId: group.id, subject: 'User' } - ]); - }); - - test('should offer only the groups the user belongs to as a scope', async ({ api, getPageModel, uniqueId }) => { - const firstGroup = await api.createGroup({ name: `First Group ${uniqueId}` }); - const secondGroup = await api.createGroup({ name: `Second Group ${uniqueId}` }); - const otherGroup = await api.createGroup({ name: `Other Group ${uniqueId}` }); - const { user } = await api.createUser({ groupIds: [firstGroup.id, secondGroup.id] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.selectPermissionOption('action', 'read'); - await userPage.selectPermissionOption('subject', 'Subject'); - await userPage.addPermissionRow.getByTestId('scope-select-trigger').click(); - await expect(userPage.$ref.getByTestId(`scope-select-item-${firstGroup.id}`)).toBeVisible(); - await expect(userPage.$ref.getByTestId(`scope-select-item-${otherGroup.id}`)).toHaveCount(0); - await userPage.$ref.getByTestId(`scope-select-item-${secondGroup.id}`).click(); - await userPage.submitPermission(); - - await expect(userPage.permissionRows.first().getByTestId('user-permission-scope')).toContainText(secondGroup.name); - expect((await api.findUserById(user.id)).additionalPermissions).toStrictEqual([ - { action: 'read', groupId: secondGroup.id, subject: 'Subject' } - ]); - }); - - test('should offer no scope for a resource that cannot be confined to a group, and mark the grant as applying to all groups', async ({ - api, - getPageModel - }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.selectPermissionOption('action', 'create'); - await userPage.selectPermissionOption('subject', 'Instrument'); - await expect(userPage.addPermissionRow.getByTestId('scope-select-trigger')).toHaveCount(0); - await userPage.submitPermission(); - + await expect(userPage.permissionRows.first()).toContainText('Create'); + await expect(userPage.permissionRows.first()).toContainText('Instrument'); await expect(userPage.permissionRows.first().getByTestId('user-permission-scope')).toContainText('All Groups'); - expect((await api.findUserById(user.id)).additionalPermissions).toStrictEqual([ - { action: 'create', groupId: null, subject: 'Instrument' } - ]); - }); - - test('should remove a grant from the user page', async ({ api, getPageModel }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - await api.setUserPermissions(user.id, [ - { action: 'read', groupId: group.id, subject: 'Subject' }, - { action: 'create', groupId: null, subject: 'Instrument' } - ]); - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await expect(userPage.permissionRows).toHaveCount(2); await userPage.removePermission(0); - expect((await api.findUserById(user.id)).additionalPermissions).toHaveLength(2); + // The scope is left untouched: with a single group it is preselected, so the group is the + // default rather than something the admin has to remember to choose. + await userPage.addPermission({ action: 'read', subject: 'User' }); + expect((await api.findUserById(user.id)).additionalPermissions).toHaveLength(1); await userPage.saveProfile(); + await expect(userPage.permissionRows.first().getByTestId('user-permission-scope')).toContainText(group.name); await expect(userPage.permissionRows).toHaveCount(1); expect((await api.findUserById(user.id)).additionalPermissions).toStrictEqual([ - { action: 'create', groupId: null, subject: 'Instrument' } + { action: 'read', groupId: group.id, subject: 'User' } ]); }); - - // Every route that writes a user is admin-only, so a grant of any User action but read does nothing. - test('should offer only Read on User as a grant', async ({ api, getPageModel }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.selectPermissionOption('subject', 'User'); - await userPage.addPermissionRow.getByTestId('action-select-trigger').click(); - - await expect(userPage.$ref.getByTestId('action-select-item-read')).toBeVisible(); - for (const action of ['create', 'delete', 'manage', 'update']) { - await expect(userPage.$ref.getByTestId(`action-select-item-${action}`)).toHaveCount(0); - } - }); - - test('should warn that Manage (All) on All makes the user an administrator', async ({ api, getPageModel }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.selectPermissionOption('action', 'manage'); - await expect(userPage.manageAllWarning).toHaveCount(0); - await userPage.selectPermissionOption('subject', 'all'); - - await expect(userPage.manageAllWarning).toBeVisible(); - }); - - test('should replace the permissions editor with a notice for an administrator', async ({ api, getPageModel }) => { - const { user } = await api.createUser({ basePermissionLevel: 'ADMIN', groupIds: [] }); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await expect(userPage.adminNotice).toBeVisible(); - await expect(userPage.permissionsTable).toHaveCount(0); - await expect(userPage.addPermissionRow).toHaveCount(0); - }); -}); - -test.describe('admin group creation conflicts', () => { - test('should stay on the form and say why when the group name is already taken', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const groupName = `E2E Duplicate Group ${uniqueId}`; - await api.createGroup({ name: groupName }); - - await authenticateAs('ADMIN'); - await page.goto('/admin/groups/create'); - - await page.getByLabel('Group Name').fill(groupName); - await page.getByTestId('type-select-trigger').click(); - await page.getByTestId('type-select-item-CLINICAL').click(); - await page.getByRole('button', { name: 'Submit' }).click(); - - await expect(page.getByTestId('error-message-text')).toContainText('already exists'); - await expect(page).toHaveURL('/admin/groups/create'); - await expect(page.getByLabel('Group Name')).toHaveValue(groupName); - }); }); diff --git a/testing/src/specs/admin-navigation.spec.ts b/testing/src/specs/admin-navigation.spec.ts deleted file mode 100644 index d5ae584c6..000000000 --- a/testing/src/specs/admin-navigation.spec.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { expect, test } from '../support/fixtures'; - -const ADMIN_PANEL_GROUPS = { - Access: ['/admin/groups', '/admin/users', '/admin/permission-sets'], - Instance: ['/admin/settings', '/admin/branding', '/admin/mail'], - Instruments: ['/admin/instrument-repos'] -}; - -test.describe('admin navigation', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should file every admin page under a group in the Admin Panel, leaving none at the top level', async ({ - getPageModel - }) => { - const dashboardPage = await getPageModel('/dashboard'); - await expect(dashboardPage.sidebar.getByRole('button', { exact: true, name: 'Admin Panel' })).toBeVisible(); - await expect(dashboardPage.sidebar.locator('[data-nav-url^="/admin/"]')).toHaveCount(0); - - await dashboardPage.expandNavGroup('Admin Panel'); - for (const [group, urls] of Object.entries(ADMIN_PANEL_GROUPS)) { - await dashboardPage.expandNavGroup(group); - for (const url of urls) { - await expect(dashboardPage.sidebar.getByTestId(`nav-button-${url}`)).toBeVisible(); - } - } - }); - - test('should reach the audit logs straight from the Admin Panel, without opening a group', async ({ - getPageModel - }) => { - const dashboardPage = await getPageModel('/dashboard'); - await dashboardPage.expandNavGroup('Admin Panel'); - await dashboardPage.sidebar.getByTestId('nav-button-/admin/audit/logs').click(); - await expect(dashboardPage.$ref).toHaveURL('/admin/audit/logs'); - }); - - test('should reach the users list through Access in the Admin Panel', async ({ getPageModel }) => { - const dashboardPage = await getPageModel('/dashboard'); - await dashboardPage.expandNavGroup('Admin Panel'); - await dashboardPage.expandNavGroup('Access'); - await dashboardPage.sidebar.getByTestId('nav-button-/admin/users').click(); - await expect(dashboardPage.$ref).toHaveURL('/admin/users'); - await expect(dashboardPage.$ref.getByTestId('page-header')).toContainText('Manage Users'); - }); - - test('should keep the Admin Panel open on a page beneath one of its links', async ({ getPageModel }) => { - const brandingPage = await getPageModel('/admin/branding'); - const adminPanel = brandingPage.sidebar.getByRole('button', { exact: true, name: 'Admin Panel' }); - await expect(adminPanel).toHaveAttribute('aria-expanded', 'true'); - - await brandingPage.loginPageLink.click(); - await expect(brandingPage.$ref).toHaveURL('/admin/branding/login-page'); - await expect(adminPanel).toHaveAttribute('aria-expanded', 'true'); - await expect(brandingPage.sidebar.getByTestId('nav-button-/admin/branding')).toBeVisible(); - }); -}); diff --git a/testing/src/specs/admin-settings.spec.ts b/testing/src/specs/admin-settings.spec.ts index 36b593e93..cc0606c65 100644 --- a/testing/src/specs/admin-settings.spec.ts +++ b/testing/src/specs/admin-settings.spec.ts @@ -1,8 +1,6 @@ import { MAX_ASSIGNMENT_DURATION_DAYS } from '@opendatacapture/schemas/setup'; -import type { TableColorMode } from '@opendatacapture/schemas/setup'; import type { Page } from '@playwright/test'; -import { GroupEmailTemplatesPage } from '../pages/_app/group/email-templates.page'; import { expect, test } from '../support/fixtures'; /** Every setting on this page autosaves, so the PATCH is the only signal that a change landed. */ @@ -15,30 +13,24 @@ function waitForSetupPatch(page: Page) { test.describe('admin settings', () => { test.use({ actingRole: 'ADMIN' }); - test('should toggle the uploader feature and reflect it in the sidebar nav', async ({ getPageModel, page }) => { + // Both settings are one instance-wide document rather than a uniquely-named record, so the uploader + // is switched back off at the end. The duration is random so that it never matches the one already + // stored, which the settings page would not save. + test('should save instance settings, applying them at once and keeping them across a reload', async ({ + getPageModel, + page, + uniqueId + }) => { + const durationDays = 1 + (Number.parseInt(uniqueId, 16) % MAX_ASSIGNMENT_DURATION_DAYS); + const uploaderLink = page.getByTestId('nav-button-/upload'); + const settingsPage = await getPageModel('/admin/settings'); + await expect(settingsPage.pageHeader).toContainText('Application Settings'); - // `isExperimentalFeaturesEnabled` is a single instance-wide document (seeded `false`), not a - // uniquely-named record, so the toggle is restored at the end rather than left on. const enabled = waitForSetupPatch(page); await settingsPage.uploaderToggle.click(); expect((await enabled).ok()).toBe(true); - await expect(page.getByTestId('nav-button-/upload')).toBeVisible(); - - const disabled = waitForSetupPatch(page); - await settingsPage.uploaderToggle.click(); - expect((await disabled).ok()).toBe(true); - await expect(page.getByTestId('nav-button-/upload')).toHaveCount(0); - }); - - // Not `@smoke`: the setting is one instance-wide value, and outside CI the firefox copy runs alongside - // this one, so each would overwrite the other's value between its save and its reload. The value is - // random so that it never matches the one already stored, which the settings page would not save. - test('should persist the default assignment duration', async ({ getPageModel, page, uniqueId }) => { - const durationDays = 1 + (Number.parseInt(uniqueId, 16) % MAX_ASSIGNMENT_DURATION_DAYS); - - const settingsPage = await getPageModel('/admin/settings'); - await expect(settingsPage.pageHeader).toContainText('Application Settings'); + await expect(uploaderLink).toBeVisible(); const saved = waitForSetupPatch(page); await settingsPage.setDefaultAssignmentDuration(durationDays); @@ -46,139 +38,11 @@ test.describe('admin settings', () => { await page.reload(); await expect(settingsPage.defaultAssignmentDurationInput).toHaveValue(String(durationDays)); - }); - - // Not `@smoke`, for the reason above. `tableColorMode` is one instance-wide document that every table - // reads, so it is restored at the end and no other spec may assert how a tag is drawn. - test('should redraw status cells in every table color mode', async ({ getPageModel, page }) => { - const settingsPage = await getPageModel('/admin/settings'); - - const saveModeAndOpenUsers = async (mode: TableColorMode) => { - await page.goto('/admin/settings'); - const saved = waitForSetupPatch(page); - await settingsPage.selectTableColorMode(mode); - expect((await saved).ok()).toBe(true); - await page.goto('/admin/users'); - return page.getByTestId('user-status-active').first(); - }; - - const text = await saveModeAndOpenUsers('text'); - await expect(text.getByTestId('color-tag-dot')).toHaveCount(0); - await expect(text.getByTestId('color-tag-label')).toHaveCSS('color', 'rgb(21, 128, 61)'); - - const none = await saveModeAndOpenUsers('none'); - await expect(none.getByTestId('color-tag-dot')).toHaveCount(0); - await expect(none.getByTestId('color-tag-label')).not.toHaveCSS('color', 'rgb(21, 128, 61)'); - - const tags = await saveModeAndOpenUsers('tags'); - await expect(tags.getByTestId('color-tag-dot')).toHaveCSS('background-color', 'rgb(22, 163, 74)'); - }); - - test.describe('active languages', () => { - // `activeLanguages` is one instance-wide document seeded with English and French, so these tests - // restore it at the end and must not run concurrently with each other. - test.describe.configure({ mode: 'serial' }); - - test('should hide the language toggle once only one language is offered', async ({ getPageModel, page }) => { - const settingsPage = await getPageModel('/admin/settings'); - - await expect(settingsPage.activeLanguageCheckbox('en')).toBeVisible(); - await expect(page.getByTestId('sidebar').getByTestId('language-toggle')).toBeVisible(); - - const deactivated = waitForSetupPatch(page); - await settingsPage.activeLanguageCheckbox('fr').click(); - expect((await deactivated).ok()).toBe(true); + await expect(uploaderLink).toBeVisible(); - await expect(page.getByTestId('sidebar').getByTestId('language-toggle')).toHaveCount(0); - // The last remaining language cannot be turned off, so an instance always offers one. - await expect(settingsPage.activeLanguageCheckbox('en')).toBeDisabled(); - - const restored = waitForSetupPatch(page); - await settingsPage.activeLanguageCheckbox('fr').click(); - expect((await restored).ok()).toBe(true); - await expect(page.getByTestId('sidebar').getByTestId('language-toggle')).toBeVisible(); - }); - - test('should move the sidebar to an offered language when the language being read is deactivated', async ({ - getPageModel, - page - }) => { - const settingsPage = await getPageModel('/admin/settings'); - - // The sidebar is the casualty when this goes wrong: it renders the toggle, so it is the ancestor - // a correction made from the toggle cannot reach. `Iniciar una sesión` is a namespace string, - // translated on any branch. Spanish is restored to inactive at the end, as it is seeded. - const sidebar = page.getByTestId('sidebar'); - const activated = waitForSetupPatch(page); - await settingsPage.activeLanguageCheckbox('es').click(); - expect((await activated).ok()).toBe(true); - - await sidebar.getByTestId('language-toggle').getByRole('button').click(); - await page.getByRole('menuitem', { name: 'Español' }).click(); - await expect(sidebar).toContainText('Iniciar una sesión'); - - const deactivatedSpanish = waitForSetupPatch(page); - await settingsPage.activeLanguageCheckbox('es').click(); - expect((await deactivatedSpanish).ok()).toBe(true); - - await expect(sidebar).toContainText('Start Session'); - await expect(sidebar).not.toContainText('Iniciar una sesión'); - }); - - test('should render the admin nav in Spanish once Spanish is activated', async ({ getPageModel, page }) => { - const settingsPage = await getPageModel('/admin/settings'); - - // Spanish is off by default, so reaching it at all takes activating it first. The admin nav - // group and its submenu are inline `t()` strings rather than namespace keys, which is exactly - // where a missing Spanish entry hides: the page renders, in English. - const activated = waitForSetupPatch(page); - await settingsPage.activeLanguageCheckbox('es').click(); - expect((await activated).ok()).toBe(true); - - await settingsPage.selectLanguage('Español'); - const sidebar = page.getByTestId('sidebar'); - // The group headers are `NavGroup`s, not `NavButton`s, so they carry no testid and are matched - // by role; their submenu items are `NavButton`s and are matched by theirs. - await expect(sidebar.getByRole('button', { name: 'Panel de administración' })).toBeVisible(); - await expect(sidebar.getByRole('button', { exact: true, name: 'Instancia' })).toBeVisible(); - await expect(sidebar.getByTestId('nav-button-/admin/settings')).toHaveText('Configuración'); - - await settingsPage.selectLanguage('English'); - const removed = waitForSetupPatch(page); - await settingsPage.activeLanguageCheckbox('es').click(); - expect((await removed).ok()).toBe(true); - }); - - // Authoring a template in a language the instance does not offer writes copy nothing can send. - // It lives here rather than in `group-email-templates.spec.ts` because the tests above turn - // Spanish on and off: run in parallel with them, an exact list of options would flake. Being in - // this serial block is also why the page is reached with a group manager's token by hand. - test('should offer no inactive language in the group email template editor', async ({ authenticateAs, page }) => { - await authenticateAs('GROUP_MANAGER'); - await page.goto('/group/email-templates'); - - const templatesPage = new GroupEmailTemplatesPage(page); - await templatesPage.createLanguage.click(); - await expect(templatesPage.openLanguageOptions).toHaveText(['English', 'French']); - }); - }); - - // The checkboxes were listed in the canonical `LANGUAGES` order, which in English differs from - // alphabetical, so this one assertion tells a sorted list from an unsorted one. - test('should list the languages by their translated name', async ({ getPageModel }) => { - const settingsPage = await getPageModel('/admin/settings'); - - await expect(settingsPage.activeLanguageLabels).toHaveText(['English', 'French', 'Spanish']); - }); - - test('should apply the group switcher position preference immediately', async ({ getPageModel }) => { - const settingsPage = await getPageModel('/admin/settings'); - - await settingsPage.groupSwitcherPositionSelect.click(); - await settingsPage.$ref.getByRole('option', { name: 'Top Right Corner' }).click(); - - // This preference is saved to this browser's localStorage and applied immediately, with no - // server round trip. - await expect(settingsPage.groupSwitcherPositionSelect).toContainText('Top Right Corner'); + const disabled = waitForSetupPatch(page); + await settingsPage.uploaderToggle.click(); + expect((await disabled).ok()).toBe(true); + await expect(uploaderLink).toHaveCount(0); }); }); diff --git a/testing/src/specs/api-docs.spec.ts b/testing/src/specs/api-docs.spec.ts deleted file mode 100644 index 9149e77df..000000000 --- a/testing/src/specs/api-docs.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { apiURL } from '../support/env'; -import { expect, test } from '../support/fixtures'; - -test.describe('api docs', () => { - test('should render the fields of a request body from its Zod schema', async ({ page }) => { - await page.goto(`${apiURL}/#tag/Groups/operation/GroupsController_create`); - const operation = page.locator('[data-section-id="tag/Groups/operation/GroupsController_create"]'); - await expect(operation.getByRole('heading', { name: 'Create Group' })).toBeVisible(); - await expect(operation.getByRole('heading', { name: /^Request Body schema/ })).toBeVisible(); - await expect(operation.getByRole('row', { name: /^name required string/ })).toBeVisible(); - await expect( - operation.getByRole('row', { name: /^type required string Enum: "CLINICAL" "RESEARCH"/ }) - ).toBeVisible(); - }); -}); diff --git a/testing/src/specs/auth.spec.ts b/testing/src/specs/auth.spec.ts index 28b6c107c..032aabb5b 100644 --- a/testing/src/specs/auth.spec.ts +++ b/testing/src/specs/auth.spec.ts @@ -1,170 +1,81 @@ -import type { Locator } from '@playwright/test'; +import type { Locator, Page } from '@playwright/test'; import { expect, test } from '../support/fixtures'; +function waitForLoginResponse(page: Page) { + return page.waitForResponse( + (response) => response.url().endsWith('/v1/auth/login') && response.request().method() === 'POST' + ); +} + test.describe('authentication', () => { - test('should redirect unauthenticated users to the login page @smoke', async ({ page }) => { + test('should redirect unauthenticated users to the login page', async ({ page }) => { await page.goto('/'); await expect(page).toHaveURL('/auth/login'); }); - test('should log in through the UI @smoke', async ({ api, getPageModel }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id] }); - - const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm(credentials); - await expect(loginPage.$ref).toHaveURL('/dashboard'); - }); - - test('should log in with the username typed in a different case', async ({ api, getPageModel, uniqueId }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id], username: `casing_${uniqueId}` }); - + // A standard user's dashboard redirects to the start-session form, so pick a group manager, and + // the last one, so a button wired to the first row's user would show the wrong username. + test('should highlight only the demo rows that log in, and log in as the demo user whose row is chosen', async ({ + getPageModel, + page + }) => { const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm({ ...credentials, username: credentials.username.toUpperCase() }); - await expect(loginPage.$ref).toHaveURL('/dashboard'); - }); - - test.describe('demo instance', () => { - test('should show the demo information over the login page as soon as it loads', async ({ getPageModel }) => { - const loginPage = await getPageModel('/auth/login'); - await expect(loginPage.demoDialog).toBeVisible(); - }); - - test('should brand the demo information with the Open Data Capture logo and name', async ({ getPageModel }) => { - const loginPage = await getPageModel('/auth/login'); - await expect(loginPage.demoDialogBranding.locator('svg')).toBeVisible(); - await expect(loginPage.demoDialogBranding).toHaveText('Open Data Capture'); - }); - - test('should reveal the login form once the demo information is dismissed', async ({ getPageModel }) => { - const loginPage = await getPageModel('/auth/login'); - await expect(loginPage.demoDialog).toBeVisible(); - await loginPage.dismissDemoDialog(); - - const usernameField = loginPage.loginForm.getByLabel('username'); - await usernameField.click(); - await expect(usernameField).toBeFocused(); - }); - - // A standard user's dashboard redirects to the start-session form, so pick a group manager, and - // the last one, so a button wired to the first row's user would show the wrong username. - test('should log in as the demo user whose row is chosen in the demo information', async ({ - getPageModel, - page - }) => { - const loginPage = await getPageModel('/auth/login'); - const demoUserRow = loginPage.demoUserRows.filter({ hasText: 'Group Manager' }).last(); - const username = await demoUserRow.getByRole('cell').first().innerText(); - await demoUserRow.getByRole('button').click(); - - await expect(loginPage.$ref).toHaveURL('/dashboard'); - await expect(page.getByTestId('user-dropup-trigger')).toHaveText(username); - }); - - test('should log in from a click on the row itself, not only on its button', async ({ getPageModel, page }) => { - const loginPage = await getPageModel('/auth/login'); - const demoUserRow = loginPage.demoUserRows.filter({ hasText: 'Group Manager' }).last(); - const usernameCell = demoUserRow.getByRole('cell').first(); - const username = await usernameCell.innerText(); - await usernameCell.click(); - - await expect(loginPage.$ref).toHaveURL('/dashboard'); - await expect(page.getByTestId('user-dropup-trigger')).toHaveText(username); - }); + const backgroundColor = (row: Locator) => row.evaluate((element) => getComputedStyle(element).backgroundColor); + const restingColor = await backgroundColor(loginPage.demoTableHeaderRow); - test('should highlight only the rows that log in, so the header does not look clickable', async ({ - getPageModel - }) => { - const loginPage = await getPageModel('/auth/login'); - const backgroundColor = (row: Locator) => row.evaluate((element) => getComputedStyle(element).backgroundColor); - const restingColor = await backgroundColor(loginPage.demoTableHeaderRow); + await loginPage.demoTableHeaderRow.hover(); + expect(await backgroundColor(loginPage.demoTableHeaderRow)).toBe(restingColor); + await loginPage.demoUserRows.first().hover(); + expect(await backgroundColor(loginPage.demoUserRows.first())).not.toBe(restingColor); - await loginPage.demoTableHeaderRow.hover(); - expect(await backgroundColor(loginPage.demoTableHeaderRow)).toBe(restingColor); + const demoUserRow = loginPage.demoUserRows.filter({ hasText: 'Group Manager' }).last(); + const username = await demoUserRow.getByRole('cell').first().innerText(); + await demoUserRow.getByRole('button').click(); - await loginPage.demoUserRows.first().hover(); - expect(await backgroundColor(loginPage.demoUserRows.first())).not.toBe(restingColor); - }); - }); - - test.describe('invalid credentials', () => { - test('should show an error and stay on the login page for a wrong password', async ({ - api, - getPageModel, - page - }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id] }); - - const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm({ password: `wrong-${credentials.password}`, username: credentials.username }); - - await expect(page.getByRole('heading', { name: 'Unauthorized' })).toBeVisible(); - await expect(page).toHaveURL('/auth/login'); - }); - - test('should show an error and stay on the login page for an unknown username', async ({ - getPageModel, - page, - uniqueId - }) => { - const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm({ password: `NoSuchAccount${uniqueId}!`, username: `no-such-user-${uniqueId}` }); - - await expect(page.getByRole('heading', { name: 'Unauthorized' })).toBeVisible(); - await expect(page).toHaveURL('/auth/login'); - }); - - test('should not reveal that an account is archived to a wrong password', async ({ api, getPageModel, page }) => { - const group = await api.createGroup(); - const { credentials, user } = await api.createUser({ groupIds: [group.id] }); - await api.archiveUser(user.id); - - const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm({ password: `wrong-${credentials.password}`, username: credentials.username }); - - await expect(page.getByRole('heading', { name: 'Unauthorized' })).toBeVisible(); - await expect(page.getByRole('heading', { name: 'Account Archived' })).toHaveCount(0); - }); - }); - - // The web origin's /api proxy adds no X-Forwarded-For of its own, so each address below reaches the api the - // way the bundled Caddy forwards a distinct client in production. - test('should throttle failed logins per client, so one client exhausting the limit does not lock out another', async ({ - apiRequestContext, - uniqueId - }) => { - const attemptLogin = async (clientAddress: string) => { - const response = await apiRequestContext.post('/api/v1/auth/login', { - data: { password: `NoSuchAccount${uniqueId}!`, username: `no-such-user-${uniqueId}` }, - headers: { 'x-forwarded-for': clientAddress } - }); - return response.status(); - }; - - await expect.poll(() => attemptLogin('203.0.113.1'), { intervals: [0] }).toBe(429); - expect(await attemptLogin('198.51.100.77')).toBe(401); + await expect(loginPage.$ref).toHaveURL('/dashboard'); + await expect(page.getByTestId('user-dropup-trigger')).toHaveText(username); }); - test('should tell an archived user with the right password that their account is archived', async ({ + // One account walks every refusal in turn. A notification from an earlier attempt can still be on + // screen, so each attempt is read from its own response before the page is checked. + test('should refuse a wrong password without revealing an archived account, and name the archive only to the right one', async ({ api, getPageModel, page }) => { const group = await api.createGroup(); const { credentials, user } = await api.createUser({ groupIds: [group.id] }); - await api.archiveUser(user.id); + const wrongPassword = { password: `wrong-${credentials.password}`, username: credentials.username }; + const archivedHeading = page.getByRole('heading', { name: 'Account Archived' }); const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm(credentials); + const refused = waitForLoginResponse(page); + await loginPage.fillLoginForm(wrongPassword); + expect((await refused).status()).toBe(401); + await expect(page.getByRole('heading', { name: 'Unauthorized' })).toBeVisible(); + await expect(page).toHaveURL('/auth/login'); - await expect(page.getByRole('heading', { name: 'Account Archived' })).toBeVisible(); + await api.archiveUser(user.id); + const refusedWhileArchived = waitForLoginResponse(page); + await loginPage.fillLoginForm(wrongPassword); + const refusedWhileArchivedResponse = await refusedWhileArchived; + expect(refusedWhileArchivedResponse.status()).toBe(401); + expect(await refusedWhileArchivedResponse.text()).not.toContain('Archived'); + await expect(archivedHeading).toHaveCount(0); + + const archived = waitForLoginResponse(page); + await loginPage.fillLoginForm(credentials); + expect((await archived).status()).toBe(403); + await expect(archivedHeading).toBeVisible(); await expect(page).toHaveURL('/auth/login'); }); - test('should log out and require re-authentication for protected routes', async ({ api, getPageModel, page }) => { + test('should log in through the UI, log out and require re-authentication for protected routes', async ({ + api, + getPageModel, + page + }) => { // Logging in through the real UI form (rather than `getPageModel`, which injects the token via // `page.addInitScript`) matters here: an init script re-runs on every navigation, including the // hard reload `logout()` does, so it would silently re-authenticate the page and mask the very diff --git a/testing/src/specs/authorization.spec.ts b/testing/src/specs/authorization.spec.ts index 779554d6a..c64889c1e 100644 --- a/testing/src/specs/authorization.spec.ts +++ b/testing/src/specs/authorization.spec.ts @@ -1,148 +1,14 @@ import type { $CreateAssignmentData } from '@opendatacapture/schemas/assignment'; -import type { Permissions } from '@opendatacapture/schemas/core'; -import type { InstrumentInfo } from '@opendatacapture/schemas/instrument'; -import type { InstrumentRecord, UploadInstrumentRecordsData } from '@opendatacapture/schemas/instrument-records'; -import type { User } from '@opendatacapture/schemas/user'; -import type { APIRequestContext, APIResponse } from '@playwright/test'; +import type { APIRequestContext } from '@playwright/test'; -import { ApiClient } from '../support/api-client'; -import { HAPPINESS_RECORD } from '../support/constants'; import { expect, test } from '../support/fixtures'; -import type { Role } from '../support/types'; +// What each role may reach through the api itself, row scoping included, is asserted against a real +// database in `apps/api/test/suites/08-authorization.suite.ts`. This spec keeps what only the browser +// shows: the navigation each role is given and the routes it is kept out of. const API = '/api/v1'; -const NON_ADMIN_ROLES = ['GROUP_MANAGER', 'STANDARD'] as const satisfies Role[]; - -type PrivilegedRequest = { - /** The admin screen a non-admin role can render by navigating to it directly. */ - screen: string; - /** - * `userId` and `groupId` are a freshly seeded throwaway user and their group, so an unexpected - * success touches nothing another spec reads. - */ - send: ( - request: APIRequestContext, - options: { groupId: string; headers: { Authorization: string }; userId: string } - ) => Promise; - /** Completes the sentence "must not be able to ...". */ - what: string; -}; - -/** - * Every request the controls on those screens fire. Each is gated by a `@RouteAccess` action no - * non-admin base permission level grants, so each must be refused outright rather than scoped. - */ -const PRIVILEGED_REQUESTS: PrivilegedRequest[] = [ - { - screen: '/admin/users', - send: (request, { headers }) => { - return request.post(`${API}/users`, { - data: { - basePermissionLevel: 'ADMIN', - firstName: 'Escalation', - groupIds: [], - lastName: 'Probe', - password: 'DataCapture2025_Probe', - username: 'escalation-probe' - }, - headers - }); - }, - what: 'create an administrator' - }, - { - screen: '/admin/users', - send: (request, { headers, userId }) => { - return request.patch(`${API}/users/${userId}`, { data: { basePermissionLevel: 'ADMIN' }, headers }); - }, - what: "raise another user's permission level" - }, - { - screen: '/admin/users', - send: (request, { headers, userId }) => request.patch(`${API}/users/${userId}/archive`, { headers }), - what: 'archive a user' - }, - { - // Gated on `manage all` rather than `update User`: an `update User` grant is one of the things - // this route hands out, so holding one must not be enough to reach it. - screen: '/admin/users/$userId', - send: (request, { headers, userId }) => { - return request.put(`${API}/users/${userId}/permissions`, { - data: { permissions: [{ action: 'manage', groupId: null, subject: 'all' }] }, - headers - }); - }, - what: 'grant another user a permission' - }, - { - // Both screens save through the same endpoint (`useUpdateSetupStateMutation`). The value sent - // is the seeded one, so were this ever to succeed it would fail the assertion without also - // changing instance-wide state under the admin settings spec. - screen: '/admin/settings and /admin/branding', - send: (request, { headers }) => { - return request.patch(`${API}/setup`, { data: { isExperimentalFeaturesEnabled: false }, headers }); - }, - what: 'change instance-wide settings' - }, - { - // `POST /v1/groups` used to be declared `create Group`, which every group manager passes: the - // guard sees the subject type, and their `manage Group` rule's condition is invisible to it. - screen: '/admin/groups', - send: (request, { headers, userId }) => { - return request.post(`${API}/groups`, { data: { name: `Escalation Probe ${userId}`, type: 'CLINICAL' }, headers }); - }, - what: 'create a group' - }, - { - screen: '/admin/groups', - send: (request, { groupId, headers }) => request.patch(`${API}/groups/${groupId}/archive`, { headers }), - what: 'archive a group' - }, - { - screen: '/admin/groups', - send: (request, { groupId, headers }) => request.patch(`${API}/groups/${groupId}/unarchive`, { headers }), - what: 'unarchive a group' - }, - { - screen: '/admin/instrument-repos', - send: (request, { headers }) => { - return request.post(`${API}/instrument-repos`, { data: { url: 'https://github.com/example/example' }, headers }); - }, - what: 'import an instrument repository' - }, - { - screen: '/admin/audit/logs', - send: (request, { headers }) => request.get(`${API}/audit/logs`, { headers }), - what: 'read the audit log' - }, - { - // The response omits the password, but the host, username and sender address are still the - // institution's outbound mail identity. - screen: '/admin/mail', - send: (request, { headers }) => request.get(`${API}/mail/settings`, { headers }), - what: 'read the mail configuration' - }, - { - screen: '/admin/mail', - send: (request, { headers }) => { - return request.patch(`${API}/mail/settings`, { - data: { newUserEmailTemplate: { body: {}, subject: {} } }, - headers - }); - }, - what: 'rewrite the mail configuration or templates' - }, - { - // The one request that opens an outbound SMTP connection to a caller-supplied host using the - // stored credential. - screen: '/admin/mail', - send: (request, { headers }) => request.post(`${API}/mail/test`, { data: {}, headers }), - what: 'probe a mail server with the stored credential' - } -]; - /** Sidebar destinations a GROUP_MANAGER gets but a STANDARD user must not. */ // `/group/email-templates` is deliberately absent: its nav item only renders when mail is // enabled instance-wide, which it is not for this suite. `mail.spec.ts` covers it there. @@ -170,7 +36,10 @@ const ADMIN_ONLY_ROUTES = [ const SHARED_ROUTES = ['/user', '/instruments/accessible-instruments'] as const; test.describe('authorization', () => { - test('should give a group manager the management navigation @smoke', async ({ getPageModel, page }) => { + test('should give a group manager the management navigation, and none of the admin navigation', async ({ + getPageModel, + page + }) => { const dashboardPage = await getPageModel('/dashboard'); // Bulk remote assignments is enabled by default, which nests group links under a collapsible // "Group Actions" menu — expand it so the child nav buttons become visible. @@ -180,186 +49,52 @@ test.describe('authorization', () => { for (const route of GROUP_MANAGER_ONLY_ROUTES) { await expect(page.getByTestId(`nav-button-${route}`)).toBeVisible(); } - }); - - test('should not expose admin-only navigation to a group manager', async ({ getPageModel, page }) => { - await getPageModel('/dashboard'); for (const route of ADMIN_ONLY_ROUTES) { await expect(page.getByTestId(`nav-button-${route}`)).toHaveCount(0); } }); - test('should let a group manager reach their own group management page', async ({ authenticateAs, page }) => { - await authenticateAs('GROUP_MANAGER'); - await page.goto('/group/manage'); - await expect(page).toHaveURL('/group/manage'); - }); - // Audit logs is the one admin-only route whose client-side render is itself blocked for a // non-admin role, because its loader reads an endpoint gated on `manage all`. The other admin - // routes render for a non-admin who navigates to them directly; what stops them is the API - // refusing the requests those screens fire, asserted below under "server-side enforcement". - test.describe('non-admin roles', () => { - for (const role of ['GROUP_MANAGER', 'STANDARD'] as const) { - test(`should block a ${role} user from audit logs`, async ({ authenticateAs, page }) => { - await authenticateAs(role); - await page.goto('/admin/audit/logs'); - await expect(page.getByRole('heading', { name: '403 - Forbidden' })).toBeVisible(); - }); - } + // routes render for a non-admin who navigates to them directly; what stops them is the api + // refusing the requests those screens fire, which the api's integration suite asserts for both + // non-admin roles. + test('should block a group manager from audit logs', async ({ authenticateAs, page }) => { + await authenticateAs('GROUP_MANAGER'); + await page.goto('/admin/audit/logs'); + await expect(page.getByRole('heading', { name: '403 - Forbidden' })).toBeVisible(); }); for (const role of ['ADMIN', 'GROUP_MANAGER', 'STANDARD'] as const) { - test.describe(`${role} on unrestricted routes`, () => { - test.use({ actingRole: role }); - + test(`should let a ${role} user reach every unrestricted route`, async ({ authenticateAs, page }) => { + await authenticateAs(role); for (const route of SHARED_ROUTES) { - test(`should let a ${role} user reach ${route}`, async ({ getPageModel }) => { - await getPageModel(route); - }); + await page.goto(route); + await expect(page).toHaveURL(route); } }); } - test.describe('standard user', () => { - test.use({ actingRole: 'STANDARD' }); - - test('should not expose management navigation', async ({ authenticateAs, page }) => { - await authenticateAs('STANDARD'); - await page.goto('/session/start-session'); - - await expect(page.getByTestId('sidebar')).toBeVisible(); - await expect(page.getByTestId('nav-button-/session/start-session')).toBeVisible(); - for (const route of [...GROUP_MANAGER_ONLY_ROUTES, ...ADMIN_ONLY_ROUTES]) { - await expect(page.getByTestId(`nav-button-${route}`)).toHaveCount(0); - } - }); - - // Uses `authenticateAs` + a raw goto rather than `getPageModel`, which asserts it landed on the - // requested route -- here the whole point is that it does not. - test('should be redirected away from the dashboard', async ({ authenticateAs, page }) => { - await authenticateAs('STANDARD'); - await page.goto('/dashboard'); - await expect(page).toHaveURL('/session/start-session'); - }); - - test('should be redirected away from remote assignment', async ({ authenticateAs, page }) => { - await authenticateAs('STANDARD'); - await page.goto('/session/remote-assignment'); - await expect(page).toHaveURL('/session/start-session'); - }); - - // `GET /v1/instruments/info` is gated on `read Instrument`, which a standard user holds, but - // resolving `subjectId` reads instrument records, which they do not. Scoping that lookup to the - // caller must answer with an empty list rather than failing the request. - // - // The defect this endpoint was changed for -- a mongodb $lookup exceeding its 100 MiB per-document - // ceiling once one instrument holds ~182,000 records -- cannot be reproduced at this tier. That - // gap is deliberate; the evidence for it is in the PR, measured against a live instance. - test('should answer the subject-filtered instrument list for a caller who may read no records', async ({ - apiRequestContext, - roleAccount - }) => { - const { accessToken } = await roleAccount('STANDARD'); - - const response = await apiRequestContext.get('/api/v1/instruments/info?subjectId=any-subject', { - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status()).toBe(200); - expect(await response.json()).toStrictEqual([]); - }); - }); - - // The populated case -- a group manager reaching /datahub/subjects/$subjectId/table and picking an - // instrument from the list -- is covered end to end by `instrument-completion.spec.ts`, which - // administers one first so the list has something in it. This case only pins the contract for a - // subject with no visible records: an empty list, not an error. - test('should answer the subject-filtered instrument list for a group manager rather than erroring', async ({ - apiRequestContext, - roleAccount + // Uses `authenticateAs` + a raw goto rather than `getPageModel`, which asserts it landed on the + // requested route -- here the point is partly that it does not. + test('should give a standard user no management navigation, and send them away from the dashboard', async ({ + authenticateAs, + page }) => { - const { accessToken } = await roleAccount('GROUP_MANAGER'); + await authenticateAs('STANDARD'); + await page.goto('/session/start-session'); - const response = await apiRequestContext.get('/api/v1/instruments/info?subjectId=any-subject', { - headers: { Authorization: `Bearer ${accessToken}` } - }); + await expect(page.getByTestId('sidebar')).toBeVisible(); + await expect(page.getByTestId('nav-button-/session/start-session')).toBeVisible(); + for (const route of [...GROUP_MANAGER_ONLY_ROUTES, ...ADMIN_ONLY_ROUTES]) { + await expect(page.getByTestId(`nav-button-${route}`)).toHaveCount(0); + } - expect(response.status()).toBe(200); - expect(await response.json()).toStrictEqual([]); + await page.goto('/dashboard'); + await expect(page).toHaveURL('/session/start-session'); }); }); -/** The reduced token the playground uploads with, minted from a login token. */ -async function mintInstrumentToken(request: APIRequestContext, token: string): Promise { - const response = await request.get(`${API}/auth/create-instrument-token`, { - headers: { Authorization: `Bearer ${token}` } - }); - expect(response.ok()).toBe(true); - return ((await response.json()) as { accessToken: string }).accessToken; -} - -/** A real, already-interpretable bundle: the first seeded instrument's own compiled source. */ -async function readSeededBundle(request: APIRequestContext, token: string): Promise { - const headers = { Authorization: `Bearer ${token}` }; - const [instrument] = (await (await request.get(`${API}/instruments/info`, { headers })).json()) as { id: string }[]; - const container = await request.get(`${API}/instruments/bundle/${instrument!.id}`, { headers }); - expect(container.ok()).toBe(true); - return ((await container.json()) as { bundle: string }).bundle; -} - -/** The id of the latest edition of a seeded instrument, by its internal name. */ -async function findInstrumentId(request: APIRequestContext, token: string, name: string): Promise { - const response = await request.get(`${API}/instruments/info`, { headers: { Authorization: `Bearer ${token}` } }); - expect(response.ok()).toBe(true); - const instrument = ((await response.json()) as InstrumentInfo[]).find( - (info) => info.kind === 'FORM' && info.internal.name === name - ); - if (!instrument) { - throw new Error(`Instrument '${name}' is not in the seeded catalog`); - } - return instrument.id; -} - -/** One record satisfying the happiness questionnaire's validation schema, for the given subject. */ -function happinessRecord(subjectId: string): UploadInstrumentRecordsData['records'][number] { - return { - data: { isSatisfiedOverall: true, personalLifeSatisfaction: 8, professionalLifeSatisfaction: 7 }, - date: new Date('2024-01-15'), - subjectId - }; -} - -/** `POST /instrument-records/upload` as the holder of the given token, with the raw response. */ -function uploadRecord(request: APIRequestContext, token: string, data: UploadInstrumentRecordsData) { - return request.post(`${API}/instrument-records/upload`, { data, headers: { Authorization: `Bearer ${token}` } }); -} - -/** - * Signs in a STANDARD member of the group granted `update InstrumentRecord` there and nothing else on - * records, since no base level below admin holds `update` or `delete` on them. - */ -async function loginRecordEditor(api: ApiClient, request: APIRequestContext, groupId: string): Promise { - const { credentials, user } = await api.createUser({ basePermissionLevel: 'STANDARD', groupIds: [groupId] }); - await api.setUserPermissions(user.id, [{ action: 'update', groupId, subject: 'InstrumentRecord' }]); - return ApiClient.login(request, credentials); -} - -/** A happiness questionnaire record for a new subject in the group, seeded as admin. */ -async function seedHappinessRecord(api: ApiClient, groupId: string, subjectId: string): Promise { - const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); - const session = await api.createSession(groupId, { id: subjectId }); - return api.createRecord(groupId, instrumentId, session, HAPPINESS_RECORD); -} - -/** `PATCH /instrument-records/:id` as the holder of the given token, with the raw response. */ -function patchRecord(request: APIRequestContext, token: string, id: string, data: { [key: string]: unknown }) { - return request.patch(`${API}/instrument-records/${id}`, { - data: { data }, - headers: { Authorization: `Bearer ${token}` } - }); -} - /** `POST /assignments` as the holder of the given token, expiring tomorrow, with the raw response. */ function createAssignment(request: APIRequestContext, token: string, data: Omit<$CreateAssignmentData, 'expiresAt'>) { return request.post(`${API}/assignments`, { @@ -368,216 +103,9 @@ function createAssignment(request: APIRequestContext, token: string, data: Omit< }); } -/** The user list `/admin/users` renders, read with the given user's own token. */ -async function readUsernames(request: APIRequestContext, token: string): Promise { - const response = await request.get(`${API}/users`, { headers: { Authorization: `Bearer ${token}` } }); - expect(response.ok()).toBe(true); - return ((await response.json()) as User[]).map(({ username }) => username); -} - -// The sidebar hiding an admin link is cosmetic: a non-admin who navigates to `/admin/users`, -// `/admin/settings`, `/admin/branding`, `/admin/groups` or `/group/manage` directly gets the real -// screen (#1470). What makes that harmless is the API, so these tests drive it directly rather than -// through the UI -- they are the reason those screens are not treated as an access-control defect. test.describe('server-side authorization', () => { - for (const role of NON_ADMIN_ROLES) { - test(`should refuse every privileged request a ${role} user could fire from an admin screen`, async ({ - api, - apiRequestContext, - roleAccount - }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - const { accessToken } = await roleAccount(role); - const headers = { Authorization: `Bearer ${accessToken}` }; - - for (const { screen, send, what } of PRIVILEGED_REQUESTS) { - const response = await send(apiRequestContext, { groupId: group.id, headers, userId: user.id }); - expect.soft(response.status(), `a ${role} user must not be able to ${what} from ${screen}`).toBe(403); - } - }); - } - - // The requests above are refused because no base level grants a `User` write, and the permissions - // route refuses to store one. A grant stored before it did is still refused by the routes - // themselves, which the api's integration suite covers, since only it can write one directly. - test('should refuse to grant a write on users, which only an administrator may make', async ({ api }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - - await expect( - api.setUserPermissions(user.id, [{ action: 'manage', groupId: group.id, subject: 'User' }]) - ).rejects.toThrow(/got 400/); - expect((await api.findUserById(user.id)).additionalPermissions).toStrictEqual([]); - }); - - // Every route that writes a user is admin-only, so an administrator removing their own access could - // leave no account able to reach them again. Seeded rather than the shared admin, so a regression - // loses a throwaway account instead of the one every other spec logs in as. - test('should refuse an administrator archiving or disabling their own account', async ({ - api, - apiRequestContext - }) => { - const { credentials, user } = await api.createUser({ basePermissionLevel: 'ADMIN' }); - const headers = { Authorization: `Bearer ${await ApiClient.login(apiRequestContext, credentials)}` }; - - const disabled = await apiRequestContext.patch(`${API}/users/${user.id}`, { data: { disabled: true }, headers }); - const archived = await apiRequestContext.patch(`${API}/users/${user.id}/archive`, { headers }); - - expect.soft(disabled.status(), 'an administrator must not be able to disable themselves').toBe(403); - expect.soft(archived.status(), 'an administrator must not be able to archive themselves').toBe(403); - const reloaded = await api.findUserById(user.id); - expect(reloaded.disabled).not.toBe(true); - expect(reloaded.archivedAt).toBeFalsy(); - }); - - // The playground uploads a bundle with a token minted by `GET /auth/create-instrument-token`, and - // that token is the only non-interactive caller of `POST /instruments`. Nothing else in the suite - // exercises it, which is how #1392 severed the two: the route moved to `manage Instrument` while - // the token still carried `create`, and every upload answered 403. - test.describe('granular instrument token', () => { - test('should let an administrator mint a token their own bundle upload is accepted with', async ({ - adminToken, - apiRequestContext - }) => { - const minted = await apiRequestContext.get(`${API}/auth/create-instrument-token`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(minted.status()).toBe(200); - const { accessToken } = (await minted.json()) as { accessToken: string }; - - // Re-posting a seeded instrument's own bundle. The conflict is the point: reaching the - // duplicate check means the guard admitted the token and the bundle was really interpreted - // and validated server-side, which a 403 would have cut short. - const response = await apiRequestContext.post(`${API}/instruments`, { - data: { bundle: await readSeededBundle(apiRequestContext, adminToken) }, - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status(), 'a minted token must be accepted by the instrument create route').toBe(409); - }); - - test('should not mint a token for a group manager, whose create grant is for series instruments', async ({ - apiRequestContext, - roleAccount - }) => { - const { accessToken } = await roleAccount('GROUP_MANAGER'); - - const response = await apiRequestContext.get(`${API}/auth/create-instrument-token`, { - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status()).toBe(403); - }); - - test('should not let a minted token mint its successor, so it expires an hour after it was issued', async ({ - adminToken, - apiRequestContext - }) => { - const instrumentToken = await mintInstrumentToken(apiRequestContext, adminToken); - - const response = await apiRequestContext.get(`${API}/auth/create-instrument-token`, { - headers: { Authorization: `Bearer ${instrumentToken}` } - }); - - expect(response.status()).toBe(403); - }); - - // The token's `manage Instrument` satisfies this route's `delete Instrument` with no group - // condition, so a guard that admitted it would answer 404 for an id that exists in no group. - test('should refuse a minted token on a route its permissions satisfy other than the upload', async ({ - adminToken, - apiRequestContext - }) => { - const instrumentToken = await mintInstrumentToken(apiRequestContext, adminToken); - - const response = await apiRequestContext.delete(`${API}/instruments/${'0'.repeat(24)}`, { - headers: { Authorization: `Bearer ${instrumentToken}` } - }); - - expect(response.status()).toBe(403); - }); - }); - - // A group manager holds `manage Group` for their own groups, and `@RouteAccess` sees only the - // subject type, so the guard lets this through and the row scoping in `GroupsService` is the - // whole check. Asserted by effect rather than by status code for that reason. - test('should not let a group manager rename a group they do not belong to', async ({ - api, - apiRequestContext, - roleAccount, - uniqueId - }) => { - const group = await api.createGroup({ name: `Foreign Group ${uniqueId}` }); - const { accessToken } = await roleAccount('GROUP_MANAGER'); - - const response = await apiRequestContext.patch(`${API}/groups/${group.id}`, { - data: { name: `Hijacked Group ${uniqueId}` }, - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.ok()).toBe(false); - expect((await api.findGroupById(group.id)).name).toBe(group.name); - }); - - // Archiving replaced deletion, which a group manager could do to their own group through their - // `manage Group` rule. Their own group is the case a `Group` action at the guard would admit, so - // the routes must refuse it outright. - test('should refuse a group manager archiving or unarchiving their own group', async ({ api, apiRequestContext }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); - const headers = { Authorization: `Bearer ${await ApiClient.login(apiRequestContext, credentials)}` }; - - const archived = await apiRequestContext.patch(`${API}/groups/${group.id}/archive`, { headers }); - expect(archived.status(), 'a group manager must not be able to archive their own group').toBe(403); - expect((await api.findGroupById(group.id)).archivedAt).toBeFalsy(); - - await api.archiveGroup(group.id); - const unarchived = await apiRequestContext.patch(`${API}/groups/${group.id}/unarchive`, { headers }); - expect(unarchived.status(), 'a group manager must not be able to unarchive their own group').toBe(403); - expect((await api.findGroupById(group.id)).archivedAt).toBeTruthy(); - }); - - // `create Assignment` is satisfied at the guard by a group manager's rule for their own groups, so - // the service alone decides which group an assignment is filed under and who receives its link. - test('should not let a group manager file an assignment under a group they do not belong to', async ({ - api, - apiRequestContext, - roleAccount, - uniqueId - }) => { - const foreignGroup = await api.createGroup({ name: `Foreign Group ${uniqueId}` }); - const subjectId = await api.createSubject(foreignGroup.id); - const { accessToken } = await roleAccount('GROUP_MANAGER'); - - const response = await createAssignment(apiRequestContext, accessToken, { - groupId: foreignGroup.id, - instrumentId: await api.findInstrumentId('FORM'), - subjectId - }); - - expect(response.status()).toBe(404); - expect(await api.findAssignments(subjectId)).toStrictEqual([]); - }); - - test('should not let a group manager file an assignment under no group, even for their own subject', async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup({ name: `Manager Group ${uniqueId}` }); - const { credentials } = await api.createUser({ groupIds: [group.id] }); - const subjectId = await api.createSubject(group.id); - - const response = await createAssignment(apiRequestContext, await ApiClient.login(apiRequestContext, credentials), { - instrumentId: await api.findInstrumentId('FORM'), - subjectId - }); - - expect(response.status()).toBe(403); - expect(await api.findAssignments(subjectId)).toStrictEqual([]); - }); - + // Kept here rather than in the api's integration suite because the assignment is really pushed to + // the gateway, which only this tier runs. test('should let an administrator file an assignment under no group, which the web client sends without one selected', async ({ adminToken, api, @@ -601,279 +129,4 @@ test.describe('server-side authorization', () => { headers: { Authorization: `Bearer ${adminToken}` } }); }); - - // The web client exports only the current group and then keeps only the subjects its table lists, - // so no screen can show whether the api itself leaves another group's records out. Both records - // belong to one subject enrolled in both groups, so only each record's own group separates them. - // The body is msgpack, whose entries carry their record's group id as a plain string and never the - // subject's groups, so its bytes are searched. - test("should leave another group's records out of a group manager's export, even of a subject enrolled in both groups", async ({ - api, - apiRequestContext, - uniqueId - }) => { - const ownGroup = await api.createGroup({ name: `Exporting Group ${uniqueId}` }); - const foreignGroup = await api.createGroup({ name: `Foreign Group ${uniqueId}` }); - const { credentials } = await api.createUser({ groupIds: [ownGroup.id] }); - const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); - const sharedSubjectId = `Shared${uniqueId}`; - await api.uploadRecords(ownGroup.id, instrumentId, [happinessRecord(sharedSubjectId)]); - await api.uploadRecords(foreignGroup.id, instrumentId, [happinessRecord(sharedSubjectId)]); - - const response = await apiRequestContext.get(`${API}/instrument-records/export`, { - headers: { Authorization: `Bearer ${await ApiClient.login(apiRequestContext, credentials)}` } - }); - - expect(response.status()).toBe(200); - const body = await response.body(); - expect(body.includes(ownGroup.id)).toBe(true); - expect(body.includes(foreignGroup.id)).toBe(false); - }); - - // The datahub's instrument hub reads these endpoints with no `groupId`, so each decides on its own - // what the caller may see rather than being confined by the request. The group manager here is - // seeded into a group holding no records at all, so anything the summaries name came from the - // other group. - test("should leave another group's records out of every datahub summary a group manager reads", async ({ - api, - apiRequestContext, - uniqueId - }) => { - const ownGroup = await api.createGroup({ name: `Hub Group ${uniqueId}` }); - const foreignGroup = await api.createGroup({ name: `Foreign Hub Group ${uniqueId}` }); - const { credentials } = await api.createUser({ groupIds: [ownGroup.id] }); - const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); - await api.uploadRecords(foreignGroup.id, instrumentId, [happinessRecord(`ForeignHub${uniqueId}`)]); - const headers = { Authorization: `Bearer ${await ApiClient.login(apiRequestContext, credentials)}` }; - - for (const summary of ['by-instrument', 'by-series', 'by-subject']) { - const response = await apiRequestContext.get(`${API}/instrument-records/summary/${summary}`, { headers }); - expect.soft(response.status(), `summary/${summary} must answer a group manager`).toBe(200); - expect.soft(await response.json(), `summary/${summary} must name no record of another group`).toStrictEqual([]); - } - }); - - // `read Instrument` carries no group condition, so the ability cannot confine this lookup on its - // own: before the caller's groups narrowed it, a series another group owns answered 200 with an - // empty list where an unknown id answers 404 — the status alone reported that the series is real. - test("should answer for another group's series exactly as for one that does not exist", async ({ - api, - apiRequestContext, - uniqueId - }) => { - const ownGroup = await api.createGroup({ name: `Series Reader Group ${uniqueId}` }); - const foreignGroup = await api.createGroup({ name: `Series Owner Group ${uniqueId}` }); - const { credentials } = await api.createUser({ groupIds: [ownGroup.id] }); - const foreignSeriesId = await api.createSeries(foreignGroup.id, `Foreign Series ${uniqueId}`); - const headers = { Authorization: `Bearer ${await ApiClient.login(apiRequestContext, credentials)}` }; - - const records = (seriesInstrumentId: string) => { - return apiRequestContext.get(`${API}/instrument-records?seriesInstrumentId=${seriesInstrumentId}`, { headers }); - }; - - const foreign = await records(foreignSeriesId); - const absent = await records('0'.repeat(24)); - - expect(foreign.status(), "another group's series must not be distinguishable from an absent one").toBe( - absent.status() - ); - expect(foreign.status()).toBe(404); - }); - - // The same lookup must still answer for a series the caller's own group owns, or the narrowing - // above would have closed the leak by breaking the hub it exists to serve. - test("should answer for a series the caller's own group owns", async ({ api, apiRequestContext, uniqueId }) => { - const ownGroup = await api.createGroup({ name: `Series Own Group ${uniqueId}` }); - const { credentials } = await api.createUser({ groupIds: [ownGroup.id] }); - const seriesId = await api.createSeries(ownGroup.id, `Own Series ${uniqueId}`); - const headers = { Authorization: `Bearer ${await ApiClient.login(apiRequestContext, credentials)}` }; - - const response = await apiRequestContext.get(`${API}/instrument-records?seriesInstrumentId=${seriesId}`, { - headers - }); - - expect(response.status()).toBe(200); - expect(await response.json()).toStrictEqual([]); - }); - - // `/admin/users` renders a populated table for a non-admin, which is only acceptable because the - // rows it can read are the ones it may already see elsewhere in the app. - test('should scope the user list a group manager reads to their own group', async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup({ name: `Scoped Group ${uniqueId}` }); - const outsiderGroup = await api.createGroup({ name: `Outsider Group ${uniqueId}` }); - const { credentials } = await api.createUser({ groupIds: [group.id] }); - const { user: teammate } = await api.createUser({ basePermissionLevel: 'STANDARD', groupIds: [group.id] }); - const { user: outsider } = await api.createUser({ groupIds: [outsiderGroup.id] }); - - const usernames = await readUsernames(apiRequestContext, await ApiClient.login(apiRequestContext, credentials)); - - expect(usernames).toContain(teammate.username); - expect(usernames).not.toContain(outsider.username); - }); - - test('should limit the user list a standard user reads to their own account', async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup({ name: `Standard Group ${uniqueId}` }); - const { credentials, user } = await api.createUser({ basePermissionLevel: 'STANDARD', groupIds: [group.id] }); - await api.createUser({ groupIds: [group.id] }); - - const usernames = await readUsernames(apiRequestContext, await ApiClient.login(apiRequestContext, credentials)); - - expect(usernames).toStrictEqual([user.username]); - }); - - // Row-level scoping is not observable in the api's own tests, whose model is mocked, so the grant - // an admin confines to one group from `/admin/users/$userId` is exercised here against real rows. - test('should confine a granted permission to the group it names, where an unscoped one reads every group', async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup({ name: `Granted Group ${uniqueId}` }); - const outsiderGroup = await api.createGroup({ name: `Ungranted Group ${uniqueId}` }); - const { credentials, user } = await api.createUser({ basePermissionLevel: 'STANDARD', groupIds: [group.id] }); - const { user: teammate } = await api.createUser({ groupIds: [group.id] }); - const { user: outsider } = await api.createUser({ groupIds: [outsiderGroup.id] }); - - // Permissions are signed into the token at login, so every grant needs a fresh one. - const usernamesGranted = async (permissions: Permissions) => { - await api.setUserPermissions(user.id, permissions); - return readUsernames(apiRequestContext, await ApiClient.login(apiRequestContext, credentials)); - }; - - const scoped = await usernamesGranted([{ action: 'read', groupId: group.id, subject: 'User' }]); - expect(scoped).toContain(teammate.username); - expect(scoped).not.toContain(outsider.username); - - const unscoped = await usernamesGranted([{ action: 'read', groupId: null, subject: 'User' }]); - expect(unscoped).toContain(outsider.username); - }); - - test('should refuse a grant confined to a group the user does not belong to', async ({ api, uniqueId }) => { - const group = await api.createGroup({ name: `Member Group ${uniqueId}` }); - const otherGroup = await api.createGroup({ name: `Non-member Group ${uniqueId}` }); - const { user } = await api.createUser({ groupIds: [group.id] }); - - await expect( - api.setUserPermissions(user.id, [{ action: 'read', groupId: otherGroup.id, subject: 'User' }]) - ).rejects.toThrow(/got 400/); - }); - - test('should ignore permissions sent through a profile update, so only the permissions route can grant', async ({ - adminToken, - api, - apiRequestContext - }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ groupIds: [group.id] }); - - const response = await apiRequestContext.patch(`${API}/users/${user.id}`, { - data: { additionalPermissions: [{ action: 'manage', groupId: null, subject: 'all' }] }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - - expect(response.ok()).toBe(true); - expect((await api.findUserById(user.id)).additionalPermissions).toStrictEqual([]); - }); - - test('should drop a grant confined to a group the user is removed from, and keep an unscoped one', async ({ - api, - uniqueId - }) => { - const leavingGroup = await api.createGroup({ name: `Leaving Group ${uniqueId}` }); - const stayingGroup = await api.createGroup({ name: `Staying Group ${uniqueId}` }); - const { user } = await api.createUser({ groupIds: [leavingGroup.id, stayingGroup.id] }); - await api.setUserPermissions(user.id, [ - { action: 'read', groupId: leavingGroup.id, subject: 'User' }, - { action: 'create', groupId: null, subject: 'Instrument' } - ]); - - const updated = await api.updateUser(user.id, { groupIds: [stayingGroup.id] }); - - expect(updated.additionalPermissions).toStrictEqual([{ action: 'create', groupId: null, subject: 'Instrument' }]); - }); - - // A grant is the only way a non-admin reaches `PATCH /instrument-records/:id`. The write used to be - // scoped by the caller's `delete` rules rather than the `update` rules the route is gated on, so a - // holder of `update` alone passed the guard and then had every write refused. - test('should let a user granted update on records, without delete, edit one in the group the grant names', async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup({ name: `Editing Group ${uniqueId}` }); - const token = await loginRecordEditor(api, apiRequestContext, group.id); - const record = await seedHappinessRecord(api, group.id, `Edited${uniqueId}`); - - const response = await patchRecord(apiRequestContext, token, record.id, { personalLifeSatisfaction: 3 }); - - expect(response.status()).toBe(200); - expect((await api.findRecordById(record.id)).data).toStrictEqual({ - ...HAPPINESS_RECORD, - personalLifeSatisfaction: 3 - }); - }); - - // The lookup the payload is merged into is scoped as well. Before it was, an invalid payload for - // another group's record answered 400 where an unknown id answers 404, which reported the id real. - test('should answer an edit to a record outside the update grant exactly as for one that does not exist', async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup({ name: `Editing Group ${uniqueId}` }); - const foreignGroup = await api.createGroup({ name: `Foreign Editing Group ${uniqueId}` }); - const token = await loginRecordEditor(api, apiRequestContext, group.id); - const record = await seedHappinessRecord(api, foreignGroup.id, `Foreign${uniqueId}`); - const invalid = { personalLifeSatisfaction: 'not a number' }; - - const foreign = await patchRecord(apiRequestContext, token, record.id, invalid); - const absent = await patchRecord(apiRequestContext, token, '0'.repeat(24), invalid); - - expect(foreign.status(), 'a record outside the grant must not be distinguishable from an absent one').toBe( - absent.status() - ); - expect(foreign.status()).toBe(404); - }); - - // The import route answers with the rows it wrote, and used to find them by re-querying the - // instrument with the request's optional `groupId` as the only filter. Omitting `groupId` therefore - // returned every group's records for that instrument to the lowest role, which `create - // InstrumentRecord` admits and which holds no `read InstrumentRecord` rule at all. - test('should answer an upload with only the records it wrote, never those of another group', async ({ - api, - apiRequestContext, - roleAccount, - uniqueId - }) => { - const { accessToken: adminToken } = await roleAccount('ADMIN'); - const { accessToken } = await roleAccount('STANDARD'); - const foreignGroup = await api.createGroup({ name: `Foreign Group ${uniqueId}` }); - const instrumentId = await findInstrumentId(apiRequestContext, adminToken, 'DNP_HAPPINESS_QUESTIONNAIRE'); - const foreignSubjectId = `Foreign${uniqueId}`; - const ownSubjectId = `Own${uniqueId}`; - - const seeded = await uploadRecord(apiRequestContext, adminToken, { - groupId: foreignGroup.id, - instrumentId, - records: [happinessRecord(foreignSubjectId)] - }); - expect(seeded.status()).toBe(201); - - const response = await uploadRecord(apiRequestContext, accessToken, { - instrumentId, - records: [happinessRecord(ownSubjectId)] - }); - - expect(response.status()).toBe(201); - const subjectIds = ((await response.json()) as InstrumentRecord[]).map((record) => record.subjectId); - expect(subjectIds).toStrictEqual([ownSubjectId]); - }); }); diff --git a/testing/src/specs/bulk-remote-assignments.spec.ts b/testing/src/specs/bulk-remote-assignments.spec.ts index 6a33e76ed..32ede699e 100644 --- a/testing/src/specs/bulk-remote-assignments.spec.ts +++ b/testing/src/specs/bulk-remote-assignments.spec.ts @@ -1,96 +1,62 @@ -import { BULK_ASSIGNMENT_MAX_SUBJECTS } from '@opendatacapture/schemas/assignment'; import type { Assignment, BulkAssignmentFailure } from '@opendatacapture/schemas/assignment'; +import type { APIRequestContext } from '@playwright/test'; import { expect, test } from '../support/fixtures'; +import type { ApiClient } from '../support/api-client'; + /** * Bulk remote assignments, driven through the API rather than the UI. * * This is the tier that exercises the parts unit tests cannot: real Mongo scoping, the real gateway - * round trip, and the all-or-nothing guarantee across both. The wizard has its own coverage. + * round trip, and the all-or-nothing guarantee across both. The request schemas' refusals (past + * expiry, subject limit, plain-http redirect) are `packages/schemas` unit tests and + * `apps/api/test/suites/05-assignment-redirect.suite.ts`; the wizard has its own coverage. */ const API = '/api/v1'; const futureIso = (days: number) => new Date(Date.now() + days * 86_400_000).toISOString(); -test.describe('bulk remote assignments', () => { - /** - * A group with its own subjects. `api.createGroup` grants the group every instrument, and each - * subject is linked to the group by creating a session for it — the same path the app uses. - */ - const seedGroup = async ( - { adminToken, api, apiRequestContext }: { adminToken: string; api: any; apiRequestContext: any }, - uniqueId: string, - subjectCount: number - ) => { - const group = await api.createGroup({ name: `Bulk${uniqueId}` }); - const subjectIds: string[] = []; - for (let index = 0; index < subjectCount; index++) { - const id = `bulk_${uniqueId}_${index}`; - const response = await apiRequestContext.post(`${API}/sessions`, { - data: { - date: new Date().toISOString(), - groupId: group.id, - subjectData: { id }, - type: 'IN_PERSON' - }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(response.status(), await response.text()).toBe(201); - subjectIds.push(id); - } - return { group, subjectIds }; - }; - - const instrumentIdsFor = async (apiRequestContext: any, adminToken: string, count: number) => { - const response = await apiRequestContext.get(`${API}/instruments/info`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - const info = (await response.json()) as { id: string; kind: string }[]; - return info - .filter(({ kind }) => kind === 'FORM' || kind === 'INTERACTIVE') - .slice(0, count) - .map(({ id }) => id); - }; - - const post = (apiRequestContext: any, adminToken: string, path: string, data: unknown) => { - return apiRequestContext.post(`${API}${path}`, { data, headers: { Authorization: `Bearer ${adminToken}` } }); - }; - - test('should send every subject of a batch on to its redirect URL, and refuse one that is not https', async ({ - adminToken, - api, - apiRequestContext, - uniqueId - }) => { - const { group, subjectIds } = await seedGroup({ adminToken, api, apiRequestContext }, uniqueId, 2); - const [instrumentId] = await instrumentIdsFor(apiRequestContext, adminToken, 1); - const request = { groupId: group.id, subjectIds, timepoints: [{ expiresAt: futureIso(30), instrumentId }] }; - - const refused = await post(apiRequestContext, adminToken, '/assignments/bulk', { - ...request, - redirectUrl: 'http://example.com/done' - }); - expect(refused.status()).toBe(400); - - const redirectUrl = `https://example.com/done/${uniqueId}`; - const created = await post(apiRequestContext, adminToken, '/assignments/bulk', { ...request, redirectUrl }); - expect(created.status(), await created.text()).toBe(201); - expect(((await created.json()) as Assignment[]).map((assignment) => assignment.redirectUrl)).toEqual([ - redirectUrl, - redirectUrl - ]); +/** + * A group with its own subjects. `api.createGroup` grants the group every instrument, and each + * subject is linked to the group by creating a session for it — the same path the app uses. + */ +const seedGroup = async (api: ApiClient, uniqueId: string, subjectCount: number) => { + const group = await api.createGroup({ name: `Bulk${uniqueId}` }); + const subjectIds: string[] = []; + for (let index = 0; index < subjectCount; index++) { + const session = await api.createSession(group.id, { id: `bulk_${uniqueId}_${index}` }); + subjectIds.push(session.subjectId); + } + return { group, subjectIds }; +}; + +const instrumentIdsFor = async (apiRequestContext: APIRequestContext, adminToken: string, count: number) => { + const response = await apiRequestContext.get(`${API}/instruments/info`, { + headers: { Authorization: `Bearer ${adminToken}` } }); + const info = (await response.json()) as { id: string; kind: string }[]; + return info + .filter(({ kind }) => kind === 'FORM' || kind === 'INTERACTIVE') + .slice(0, count) + .map(({ id }) => id); +}; + +const post = (apiRequestContext: APIRequestContext, adminToken: string, path: string, data: unknown) => { + return apiRequestContext.post(`${API}${path}`, { data, headers: { Authorization: `Bearer ${adminToken}` } }); +}; - test('should create one assignment per subject per timepoint, and refuse a repeat as a conflict', async ({ +test.describe('bulk remote assignments', () => { + test('should create one assignment per subject per timepoint, each sending its subject on, and refuse a repeat as a conflict', async ({ adminToken, api, apiRequestContext, uniqueId }) => { - const { group, subjectIds } = await seedGroup({ adminToken, api, apiRequestContext }, uniqueId, 2); + const { group, subjectIds } = await seedGroup(api, uniqueId, 2); const [instrumentA, instrumentB] = await instrumentIdsFor(apiRequestContext, adminToken, 2); + const redirectUrl = `https://example.com/done/${uniqueId}`; const request = { groupId: group.id, subjectIds, @@ -104,20 +70,17 @@ test.describe('bulk remote assignments', () => { expect(preflight.status()).toBe(200); expect(await preflight.json()).toMatchObject({ assignmentCount: 4, subjectCount: 2, timepointCount: 2 }); - const created = await post(apiRequestContext, adminToken, '/assignments/bulk', request); - expect(created.status()).toBe(201); + const created = await post(apiRequestContext, adminToken, '/assignments/bulk', { ...request, redirectUrl }); + expect(created.status(), await created.text()).toBe(201); const assignments = (await created.json()) as Assignment[]; - expect(assignments).toHaveLength(4); + expect(assignments.map((assignment) => assignment.redirectUrl)).toEqual(Array(4).fill(redirectUrl)); // The private key that decrypts an assignment must never reach a client. expect(JSON.stringify(assignments)).not.toContain('encryptionKeyPair'); // Every assignment really exists, and is scoped to this group. for (const subjectId of subjectIds) { - const response = await apiRequestContext.get(`${API}/assignments?subjectId=${subjectId}`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - const existing = (await response.json()) as Assignment[]; + const existing = await api.findAssignments(subjectId); expect(existing.filter((assignment) => assignment.groupId === group.id)).toHaveLength(2); } @@ -142,8 +105,8 @@ test.describe('bulk remote assignments', () => { apiRequestContext, uniqueId }) => { - const { group, subjectIds } = await seedGroup({ adminToken, api, apiRequestContext }, uniqueId, 1); - const other = await seedGroup({ adminToken, api, apiRequestContext }, `${uniqueId}x`, 1); + const { group, subjectIds } = await seedGroup(api, uniqueId, 1); + const other = await seedGroup(api, `${uniqueId}x`, 1); const [instrumentId] = await instrumentIdsFor(apiRequestContext, adminToken, 1); const response = await post(apiRequestContext, adminToken, '/assignments/bulk', { @@ -157,50 +120,6 @@ test.describe('bulk remote assignments', () => { expect(issue?.kind === 'SUBJECT_UNAVAILABLE' && issue.subjectIds).toEqual(other.subjectIds); // All-or-nothing: the subject that *was* eligible must not have been assigned either. - const assignments = await apiRequestContext.get(`${API}/assignments?subjectId=${subjectIds[0]}`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect((await assignments.json()) as Assignment[]).toHaveLength(0); - }); - - test('should refuse an instrument the group has not been granted', async ({ - adminToken, - api, - apiRequestContext, - uniqueId - }) => { - const { group, subjectIds } = await seedGroup({ adminToken, api, apiRequestContext }, uniqueId, 1); - const response = await post(apiRequestContext, adminToken, '/assignments/bulk/preflight', { - groupId: group.id, - subjectIds, - timepoints: [{ expiresAt: futureIso(30), instrumentId: 'not-an-instrument' }] - }); - expect(response.status()).toBe(422); - const failure = (await response.json()) as BulkAssignmentFailure; - expect(failure.issues.some(({ kind }) => kind === 'INSTRUMENT_UNAVAILABLE')).toBe(true); - }); - - test('should reject an expiry in the past and a batch beyond the subject limit', async ({ - adminToken, - api, - apiRequestContext, - uniqueId - }) => { - const { group, subjectIds } = await seedGroup({ adminToken, api, apiRequestContext }, uniqueId, 1); - const [instrumentId] = await instrumentIdsFor(apiRequestContext, adminToken, 1); - - const expired = await post(apiRequestContext, adminToken, '/assignments/bulk', { - groupId: group.id, - subjectIds, - timepoints: [{ expiresAt: new Date(Date.now() - 86_400_000).toISOString(), instrumentId }] - }); - expect(expired.status()).toBe(400); - - const tooMany = await post(apiRequestContext, adminToken, '/assignments/bulk/preflight', { - groupId: group.id, - subjectIds: Array.from({ length: BULK_ASSIGNMENT_MAX_SUBJECTS + 1 }, (_, index) => `s-${index}`), - timepoints: [{ expiresAt: futureIso(30), instrumentId }] - }); - expect(tooMany.status()).toBe(400); + expect(await api.findAssignments(subjectIds[0]!)).toHaveLength(0); }); }); diff --git a/testing/src/specs/dashboard.spec.ts b/testing/src/specs/dashboard.spec.ts index 045b11a5e..86e884e9e 100644 --- a/testing/src/specs/dashboard.spec.ts +++ b/testing/src/specs/dashboard.spec.ts @@ -20,71 +20,42 @@ function scoreFormBundle({ edition, name, title }: { edition: number; name: stri } test.describe('dashboard', () => { - test('should display the dashboard header @smoke', async ({ getPageModel }) => { + // The default acting role's group is seeded as CLINICAL by the `api` fixture (api-client.ts). The + // geometry and computed styles here are what happy-dom cannot measure; what the page shows and + // requests is in apps/web's dashboard.test.tsx. + test("should lay out a group manager's dashboard: its summary, chart legends and windows, and borderless dialog tables", async ({ + getPageModel, + page + }) => { const dashboardPage = await getPageModel('/dashboard'); - await expect(dashboardPage.pageHeader).toBeVisible(); await expect(dashboardPage.pageHeader).toContainText('Dashboard'); - }); - - // The default acting role's group is seeded as CLINICAL by the `api` fixture (api-client.ts). - test('should summarize the acting group and its statistics for a group manager', async ({ getPageModel }) => { - const dashboardPage = await getPageModel('/dashboard'); await expect(dashboardPage.welcomeHeading).toContainText('Overview of Your Clinic'); await expect(dashboardPage.statisticUsers).toContainText(/Total Users/); await expect(dashboardPage.statisticSubjects).toContainText(/Total Subjects/); await expect(dashboardPage.statisticInstruments).toContainText(/Total Instruments/); await expect(dashboardPage.statisticRecords).toContainText(/Total Records/); - await expect(dashboardPage.recordsSessionsChart).toBeVisible(); await expect(dashboardPage.subjectsGrowthChart).toBeVisible(); - }); - test('should label the trend series in a visible legend without requiring hover', async ({ getPageModel }) => { - const dashboardPage = await getPageModel('/dashboard'); + // The trend series are labelled in a visible legend, without requiring hover. const legend = dashboardPage.recordsSessionsChart.locator('.recharts-default-legend'); await expect(legend).toBeVisible(); await expect(legend).toContainText('Records'); await expect(legend).toContainText('Sessions'); - const recordsIcon = legend.locator('.recharts-legend-item').filter({ hasText: 'Records' }).locator('line'); const sessionsIcon = legend.locator('.recharts-legend-item').filter({ hasText: 'Sessions' }).locator('line'); - await expect(recordsIcon).not.toHaveAttribute('stroke-dasharray', '6 4'); await expect(sessionsIcon).toHaveAttribute('stroke-dasharray', '6 4'); - await expect(legend.locator('..')).toHaveCSS('padding-top', '16px'); const legendItems = legend.locator('.recharts-legend-item'); await expect(legendItems.first()).toHaveCSS('margin-right', '16px'); await expect(legendItems.first()).toHaveCSS('margin-left', '16px'); - const wrapperBox = (await dashboardPage.recordsSessionsChart.boundingBox())!; const firstItemBox = (await legendItems.first().boundingBox())!; const lastItemBox = (await legendItems.last().boundingBox())!; const itemsCentre = (firstItemBox.x + lastItemBox.x + lastItemBox.width) / 2; expect(Math.abs(itemsCentre - (wrapperBox.x + wrapperBox.width / 2))).toBeLessThan(2); - }); - test('should redraw a chart over the time window chosen in its corner', async ({ getPageModel, page }) => { - const dashboardPage = await getPageModel('/dashboard'); - await expect(dashboardPage.recordsTrendWindow).toHaveText('Default'); - await expect(dashboardPage.subjectsTrendWindow).toHaveText('Default'); - - const response = page.waitForResponse((res) => { - return res.url().includes('/v1/summary') && res.url().includes('recordTrendWindow=past5Years'); - }); - await dashboardPage.recordsTrendWindow.click(); - await page.getByRole('option', { name: 'Last 5 Years' }).click(); - const summary = (await (await response).json()) as { trends: { records: unknown[]; subjects: unknown[] } }; - - expect(summary.trends.records).toHaveLength(6); - expect(summary.trends.subjects).toHaveLength(5); - await expect(dashboardPage.recordsTrendWindow).toHaveText('Last 5 Years'); - await expect(dashboardPage.subjectsTrendWindow).toHaveText('Default'); - // Only the x axis labels can hold a four-digit year; the y axis counts records. - await expect(dashboardPage.recordsSessionsChart.getByRole('application')).toContainText(/\d{4}/); - }); - - test('should line each time window up with its chart title, near the card edge', async ({ getPageModel }) => { - const dashboardPage = await getPageModel('/dashboard'); + // Each time window lines up with its chart title, near the card edge. for (const [chart, select] of [ [dashboardPage.recordsSessionsChart, dashboardPage.recordsTrendWindow], [dashboardPage.subjectsGrowthChart, dashboardPage.subjectsTrendWindow] @@ -95,40 +66,8 @@ test.describe('dashboard', () => { expect(Math.abs(selectBox.y + selectBox.height / 2 - (titleBox.y + titleBox.height / 2))).toBeLessThan(2); expect(cardBox.x + cardBox.width - (selectBox.x + selectBox.width)).toBeLessThanOrEqual(17); } - }); - test('should navigate to the data hub when the subjects statistic is clicked', async ({ getPageModel, page }) => { - const dashboardPage = await getPageModel('/dashboard'); - await dashboardPage.statisticSubjects.click(); - await expect(page).toHaveURL('/datahub/subjects'); - }); - - test('should open a dialog listing users when the users statistic is clicked', async ({ getPageModel }) => { - const dashboardPage = await getPageModel('/dashboard'); - await dashboardPage.statisticUsers.click(); - await expect(dashboardPage.usersDialog).toBeVisible(); - await expect(dashboardPage.usersDialog).toContainText('Users'); - }); - - test('should find a user of the group by searching the users dialog', async ({ api, authenticateAs, page }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); - const { credentials: colleague } = await api.createUser({ groupIds: [group.id] }); - await authenticateAs(credentials); - - const dashboardPage = new DashboardPage(page); - await dashboardPage.goto('/dashboard'); - await dashboardPage.statisticUsers.click(); - const userRows = dashboardPage.usersDialog.getByTestId('data-table-row'); - await expect(userRows).toHaveCount(2); - await dashboardPage.usersDialog.getByRole('searchbox').fill(colleague.username); - await expect(userRows).toHaveCount(1); - await expect(userRows).toHaveText(colleague.username); - }); - - // The dialogs show their tables as plain text, with larger bold headers in place of the borders. - test('should show the tables in the dashboard dialogs as text, without borders', async ({ getPageModel, page }) => { - const dashboardPage = await getPageModel('/dashboard'); + // The dialogs show their tables as plain text, with larger bold headers in place of the borders. for (const [statistic, dialog] of [ [dashboardPage.statisticUsers, dashboardPage.usersDialog], [dashboardPage.statisticInstruments, dashboardPage.instrumentsDialog], @@ -153,8 +92,9 @@ test.describe('dashboard', () => { // A record keeps the id of the edition it was collected with, so the records dialog once counted // only the latest edition's records, and the instruments card counted every edition in the database. - // Edition 2 is stored after the group exists, so the group gains it the way an upgrade does. - test('should count the records of every edition of an instrument, matching both cards to their dialogs', async ({ + // Edition 2 is stored after the group exists, so the group gains it the way an upgrade does. The + // users dialog is read from the same group, so it lists exactly the two members seeded here. + test("should match each card to its dialog over the group's own data, counting the records of every edition", async ({ api, authenticateAs, page, @@ -169,12 +109,24 @@ test.describe('dashboard', () => { for (const instrumentId of [editionOneId, editionOneId, editionOneId, editionTwoId, editionTwoId]) { await api.createRecord(group.id, instrumentId, session, { score: 1 }); } - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); + const [{ credentials }, { credentials: colleague }] = await Promise.all([ + api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }), + api.createUser({ groupIds: [group.id] }) + ]); await authenticateAs(credentials); const dashboardPage = new DashboardPage(page); await dashboardPage.goto('/dashboard'); + await expect(dashboardPage.statisticUsers.getByRole('heading')).toHaveText('2'); + await dashboardPage.statisticUsers.click(); + const userRows = dashboardPage.usersDialog.getByTestId('data-table-row'); + await expect(userRows).toHaveCount(2); + for (const username of [credentials.username, colleague.username]) { + await expect(userRows.filter({ hasText: username })).toHaveCount(1); + } + await page.keyboard.press('Escape'); + await expect(dashboardPage.statisticRecords.getByRole('heading')).toHaveText('5'); await dashboardPage.statisticRecords.click(); // The only instrument holding records in this group, so the default most-records-first order puts it on top. @@ -195,15 +147,4 @@ test.describe('dashboard', () => { await page.keyboard.press('Escape'); await expect(dashboardPage.statisticInstruments.getByRole('heading')).toHaveText(String(instrumentRowCount)); }); - - test.describe('as an administrator', () => { - test.use({ actingRole: 'ADMIN' }); - - // The seeded admin account belongs to no group, so the dashboard falls back to the - // application-wide summary rather than a clinic- or research-group-scoped one. - test('should summarize the whole application rather than a single group', async ({ getPageModel }) => { - const dashboardPage = await getPageModel('/dashboard'); - await expect(dashboardPage.welcomeHeading).toContainText('Summary of Application State'); - }); - }); }); diff --git a/testing/src/specs/datahub.spec.ts b/testing/src/specs/datahub.spec.ts index 9966b37b4..663a096a6 100644 --- a/testing/src/specs/datahub.spec.ts +++ b/testing/src/specs/datahub.spec.ts @@ -1,19 +1,9 @@ import { readFile } from 'node:fs/promises'; -import type { Page } from '@playwright/test'; - import { DatahubPage } from '../pages/_app/datahub/subjects/index.page'; import { HAPPINESS_RECORD, PHONE_VIEWPORT } from '../support/constants'; import { expect, test } from '../support/fixtures'; -function readSubjectIdFromUrl(page: Page) { - const [, subjectId] = /\/datahub\/subjects\/([^/]+)\/table/.exec(page.url()) ?? []; - if (!subjectId) { - throw new Error(`Failed to extract subjectId from URL: ${page.url()}`); - } - return subjectId; -} - test.describe('data hub on a phone', () => { test.use({ viewport: PHONE_VIEWPORT }); @@ -32,122 +22,6 @@ test.describe('data hub on a phone', () => { }); test.describe('data hub', () => { - test("should export the records of a group manager's own group, since the export applies the caller's read rules", async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const group = await api.createGroup({ name: `Group${uniqueId}` }); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); - const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); - await api.uploadRecords(group.id, instrumentId, [ - { data: HAPPINESS_RECORD, date: new Date(), subjectId: `export-${uniqueId}` } - ]); - - await authenticateAs(credentials); - await page.goto('/datahub/subjects'); - await expect(page.getByTestId('data-table-row')).toHaveCount(1); - - const downloadPromise = page.waitForEvent('download'); - await page.getByTestId('datahub-export-dropdown').getByRole('button').click(); - await page.getByRole('menuitem', { exact: true, name: 'JSON' }).click(); - const download = await downloadPromise; - - const exported = JSON.parse((await readFile(await download.path())).toString()) as { groupId: string }[]; - expect(exported.length).toBeGreaterThan(0); - expect(exported.every((entry) => entry.groupId === group.id)).toBe(true); - }); - - test('should display the data hub header', async ({ getPageModel }) => { - const datahubPage = await getPageModel('/datahub/subjects'); - await expect(datahubPage.pageHeader).toBeVisible(); - await expect(datahubPage.pageHeader).toContainText('Data Hub'); - }); - - test('should filter the subject list by search text, so a search narrows the table to matching subjects', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Search${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - const subjectId = readSubjectIdFromUrl(page); - const displayedId = subjectId.slice(0, 9); - - const datahubPage = await getPageModel('/datahub/subjects'); - await datahubPage.searchInput.fill(displayedId); - await expect(page.getByTestId('data-table-row').filter({ hasText: displayedId })).toBeVisible(); - - await datahubPage.searchInput.fill(`NoSuchSubject${uniqueId}`); - await expect(page.getByTestId('data-table-empty-state')).toBeVisible(); - }); - - // The search bar is a form, and a native submit reloaded the page, which drops the in-memory access - // token and lands on the login page. - test('should keep the search results, and the user signed in, when Enter is pressed in the search bar', async ({ - getPageModel, - page, - uniqueId - }) => { - const customIdentifier = `Enter${uniqueId}`; - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.fillCustomIdentifier(customIdentifier, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - const datahubPage = await getPageModel('/datahub/subjects'); - await datahubPage.searchInput.fill(customIdentifier); - await datahubPage.searchInput.press('Enter'); - - await expect(page).toHaveURL(/\/datahub\/subjects$/); - await expect(datahubPage.searchInput).toHaveValue(customIdentifier); - await expect(page.getByTestId('data-table-row')).toHaveCount(1); - await expect(page.getByTestId('data-table-row')).toContainText(customIdentifier.slice(0, 9)); - }); - - test('should open a subject from the list via the row action menu, landing on its record table', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`RowAction${uniqueId}`, `Subject${uniqueId}`, 'Male'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - const subjectId = readSubjectIdFromUrl(page); - - const datahubPage = await getPageModel('/datahub/subjects'); - await datahubPage.searchInput.fill(subjectId.slice(0, 9)); - await datahubPage.rowActionsTrigger.click(); - await page.getByRole('menuitem', { name: 'View' }).click(); - - await expect(page).toHaveURL(new RegExp(`/datahub/subjects/${subjectId}/table$`)); - - // `subject-table` used to be the Table tab link, so selecting the record table by it silently - // got the tab instead (#1475). It is the table alone now, and the tab is `subject-table-tab`. - await expect(page.getByTestId('subject-table')).toHaveCount(1); - await expect(page.getByTestId('subject-table').getByTestId('data-table')).toHaveCount(1); - await expect(page.getByTestId('subject-table-tab')).toHaveAttribute( - 'data-nav-url', - `/datahub/subjects/${subjectId}/table` - ); - }); - test('should navigate to a subject by custom identifier via the subject lookup dialog', async ({ getPageModel, page, @@ -161,7 +35,8 @@ test.describe('data hub', () => { await startSessionPage.submitForm(); await expect(startSessionPage.successMessage).toBeVisible(); - await getPageModel('/datahub/subjects'); + const datahubPage = await getPageModel('/datahub/subjects'); + await expect(datahubPage.pageHeader).toContainText('Data Hub'); await page.getByTestId('subject-lookup-search-button').click(); const identificationForm = page.getByTestId('identification-form'); @@ -172,9 +47,9 @@ test.describe('data hub', () => { await expect(page).toHaveURL(/\/datahub\/subjects\/.+\/table$/); }); - // The export endpoint returns every record in the group; which of them reach the file is decided - // client-side from the rows the table is currently listing. Nothing else covers that scoping, and - // getting it wrong hands the user another subject's data. + // The export endpoint returns every record the caller may read in the group; which of them reach + // the file is decided client-side from the rows the table is currently listing. Nothing else covers + // that scoping, and getting it wrong hands the user another subject's data. test('should export only the subjects the table is listing', async ({ api, isolatedGroupManager, @@ -199,40 +74,14 @@ test.describe('data hub', () => { await expect(datahubPage.rows).toHaveCount(1); const download = await datahubPage.exportAs('JSON'); - const payload = JSON.parse(await readFile(await download.path(), 'utf8')) as { subjectId: string }[]; + const payload = JSON.parse(await readFile(await download.path(), 'utf8')) as { + groupId: string; + subjectId: string; + }[]; expect(payload.length).toBeGreaterThan(0); expect([...new Set(payload.map((row) => row.subjectId))]).toStrictEqual([listed]); - }); - - test('should list only subjects holding records once a minimum record count is applied', async ({ - api, - isolatedGroupManager, - page, - uniqueId - }) => { - // A group of its own, so the row count is exactly what this test seeds. Two subjects exist only - // through sessions while a third holds a record, so the filter must drop exactly the recordless - // pair — hiding every subject or filtering none would both fail. - const group = await isolatedGroupManager(); - const withRecord = `hasrecord-${uniqueId}`; - for (const suffix of ['a', 'b']) { - await api.createSession(group.id, { id: `recordless-${uniqueId}-${suffix}` }); - } - await api.uploadRecords(group.id, await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'), [ - { data: HAPPINESS_RECORD, date: new Date(), subjectId: withRecord } - ]); - - const datahubPage = new DatahubPage(page); - await datahubPage.goto('/datahub/subjects'); - await expect(datahubPage.rows).toHaveCount(3); - - await datahubPage.requireAtLeastRecords(1); - - await expect(datahubPage.rows).toHaveCount(1); - // The cell renders at most the id's first nine characters (the subjectIdDisplayLength default), - // so the assertion matches the visible prefix rather than the full seeded id. - await expect(datahubPage.rows).toContainText(withRecord.slice(0, 9)); + expect([...new Set(payload.map((row) => row.groupId))]).toStrictEqual([group.id]); }); // The counts and the date window are read from the per-subject summary, so a filter that narrows @@ -247,15 +96,20 @@ test.describe('data hub', () => { const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); const twice = `twice-${uniqueId}`; const once = `once-${uniqueId}`; - await api.uploadRecords(group.id, instrumentId, [ - { data: HAPPINESS_RECORD, date: new Date(), subjectId: twice }, - { data: HAPPINESS_RECORD, date: new Date(), subjectId: twice }, - { data: HAPPINESS_RECORD, date: new Date(), subjectId: once } + // A subject that exists only through a session has no summary entry at all, so it is what + // catches a filter that lets a missing count through. + await Promise.all([ + api.createSession(group.id, { id: `recordless-${uniqueId}` }), + api.uploadRecords(group.id, instrumentId, [ + { data: HAPPINESS_RECORD, date: new Date(), subjectId: twice }, + { data: HAPPINESS_RECORD, date: new Date(), subjectId: twice }, + { data: HAPPINESS_RECORD, date: new Date(), subjectId: once } + ]) ]); const datahubPage = new DatahubPage(page); await datahubPage.goto('/datahub/subjects'); - await expect(datahubPage.rows).toHaveCount(2); + await expect(datahubPage.rows).toHaveCount(3); // Two records exist for one subject only, so a minimum of two must leave exactly that subject. await datahubPage.requireAtLeastRecords(2); @@ -266,20 +120,4 @@ test.describe('data hub', () => { await datahubPage.selectCollectedWindow('pastMonth'); await expect(datahubPage.rows).toHaveCount(1); }); - - // `GET /v1/subjects` is gated on `read Subject`, which a standard user holds, but resolving - // `hasRecord` reads instrument records, which they do not. The honest answer is an empty list. - test('should answer the with-records filter for a caller who may read no records', async ({ - apiRequestContext, - roleAccount - }) => { - const { accessToken } = await roleAccount('STANDARD'); - - const response = await apiRequestContext.get('/api/v1/subjects?hasRecord=true', { - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status()).toBe(200); - expect(await response.json()).toStrictEqual([]); - }); }); diff --git a/testing/src/specs/disclaimer.spec.ts b/testing/src/specs/disclaimer.spec.ts index f61809cb9..270dd0904 100644 --- a/testing/src/specs/disclaimer.spec.ts +++ b/testing/src/specs/disclaimer.spec.ts @@ -4,17 +4,6 @@ import { expect, test } from '../support/fixtures'; test.use({ appState: { isDisclaimerAccepted: false, isWalkthroughComplete: true } }); test.describe('disclaimer', () => { - test('should accept the disclaimer', async ({ getPageModel, page }) => { - const dashboardPage = await getPageModel('/dashboard'); - - const dialog = page.getByRole('dialog', { name: 'Disclaimer' }); - await expect(dialog).toBeVisible(); - await page.getByRole('button', { name: 'Accept' }).click(); - await expect(dialog).toBeHidden(); - - await expect(dashboardPage.pageHeader).toContainText('Dashboard'); - }); - test('should log the user out when declining, so a user cannot use the app without accepting', async ({ api, getPageModel, diff --git a/testing/src/specs/error-page.spec.ts b/testing/src/specs/error-page.spec.ts index c70e7f021..ccac25cfd 100644 --- a/testing/src/specs/error-page.spec.ts +++ b/testing/src/specs/error-page.spec.ts @@ -3,9 +3,8 @@ import { expect, test } from '../support/fixtures'; /** * The terminal error page, reached by failing a route loader's request with a status the axios * retry budget does not treat as transient — 500 rather than one of 502/503/504, which would show - * the self-healing connection screen instead. - * - * Not `@smoke`: the copy assertion needs `clipboard-read`, which Playwright grants on chromium only. + * the self-healing connection screen instead. The copy assertion needs `clipboard-read`, which + * Playwright grants on chromium only. */ test.describe('error page', () => { test.beforeEach(async ({ authenticateAs, page }) => { @@ -19,21 +18,16 @@ test.describe('error page', () => { }); }); - test('should offer the report for copying beside the download when a route fails', async ({ page }) => { - await page.goto('/datahub/subjects'); - const errorPage = page.getByTestId('error-page'); - await expect(errorPage).toBeVisible(); - await expect(page.getByTestId('download-error-report')).toContainText('Download Error Report'); - await expect(page.getByTestId('copy-error-report')).toContainText('Copy Error Report'); - }); - - test('should put the error report on the clipboard so it does not have to be downloaded to be shared', async ({ + test('should offer the error report beside its download, and put it on the clipboard so it need not be downloaded to be shared', async ({ context, page }) => { await context.grantPermissions(['clipboard-read', 'clipboard-write']); await page.goto('/datahub/subjects'); + await expect(page.getByTestId('error-page')).toBeVisible(); + await expect(page.getByTestId('download-error-report')).toContainText('Download Error Report'); const copyButton = page.getByTestId('copy-error-report'); + await expect(copyButton).toContainText('Copy Error Report'); await copyButton.click(); await expect(copyButton).toContainText('Copied'); diff --git a/testing/src/specs/form-block.spec.ts b/testing/src/specs/form-block.spec.ts index feb53bb0c..169a5f6ce 100644 --- a/testing/src/specs/form-block.spec.ts +++ b/testing/src/specs/form-block.spec.ts @@ -28,9 +28,5 @@ test.describe('form block', () => { await expect(instrumentPage.consentPreamble).toBeVisible(); await expect(instrumentPage.consentPreamble).toContainText('WHEREAS'); await expect(instrumentPage.consentPreamble).toContainText('Nothing contained in this preamble'); - - await instrumentPage.acceptConsent(); - await instrumentPage.submit(); - await expect(instrumentPage.summaryHeading).toBeVisible(); }); }); diff --git a/testing/src/specs/form-error-summary.spec.ts b/testing/src/specs/form-error-summary.spec.ts deleted file mode 100644 index a5d5aec8f..000000000 --- a/testing/src/specs/form-error-summary.spec.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { RenderInstrumentPage } from '../pages/_app/instruments/render/$id.page'; -import { expect, test } from '../support/fixtures'; - -const INSTRUMENT_TITLE = 'Happiness Questionnaire'; - -test.describe('form error summary', () => { - test('should tell the user under the submit button that fields need resolving and focus the first one', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Summary${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await expect(instrumentPage.errorSummary).toHaveCount(0); - - await instrumentPage.submit(); - await expect(instrumentPage.errorMessages.first()).toBeVisible(); - await expect(instrumentPage.errorSummary).toBeVisible(); - await expect(instrumentPage.firstInvalidField).toBeInViewport(); - await expect(instrumentPage.firstInvalidField.locator(':focus')).toHaveCount(1); - - await instrumentPage.completeHappinessQuestionnaire(); - await instrumentPage.submit(); - await expect(instrumentPage.summaryHeading).toBeVisible(); - await expect(instrumentPage.errorSummary).toHaveCount(0); - }); -}); diff --git a/testing/src/specs/form-reset.spec.ts b/testing/src/specs/form-reset.spec.ts deleted file mode 100644 index 983d4f79a..000000000 --- a/testing/src/specs/form-reset.spec.ts +++ /dev/null @@ -1,84 +0,0 @@ -import type { Page } from '@playwright/test'; - -import { RenderInstrumentPage } from '../pages/_app/instruments/render/$id.page'; -import { expect, test } from '../support/fixtures'; - -import type { GetPageModel } from '../support/fixtures'; - -/** Declares `resetButton: true`; it is the longest form in the library, which is why it opts in. */ -const RESETTABLE_INSTRUMENT_TITLE = 'Enhanced Demographics Questionnaire'; - -/** Declares no `resetButton`, so it stands in for every instrument authored before the option existed. */ -const NON_RESETTABLE_INSTRUMENT_TITLE = 'Happiness Questionnaire'; - -/** - * Starts a session and opens an instrument by the title on its card. The session lives in memory, so - * every step navigates through the sidebar rather than by loading a URL. - */ -async function openInstrument( - getPageModel: GetPageModel, - page: Page, - uniqueId: string, - title: string -): Promise { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Reset${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: title }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - return instrumentPage; -} - -test.describe('form reset button', () => { - test('should clear every answer when an instrument opts in @smoke', async ({ getPageModel, page, uniqueId }) => { - const instrumentPage = await openInstrument(getPageModel, page, uniqueId, RESETTABLE_INSTRUMENT_TITLE); - await expect(instrumentPage.resetButton).toBeVisible(); - - await instrumentPage.completeEnhancedDemographicsQuestionnaire(); - - const householdSize = page.locator('[name="householdSize"]'); - const maritalStatus = page.locator('[name="maritalStatus"]'); - await expect(householdSize).toHaveValue('3'); - await expect(maritalStatus).toHaveValue('married'); - - await instrumentPage.resetButton.click(); - - // Values, not just validation errors: this is what `preventResetValuesOnReset` would suppress if - // it were left on for an instrument that offers the button. - await expect(householdSize).toHaveValue(''); - await expect(maritalStatus).toHaveValue(''); - }); - - test('should not offer the button to an instrument that does not opt in', async ({ - getPageModel, - page, - uniqueId - }) => { - const instrumentPage = await openInstrument(getPageModel, page, uniqueId, NON_RESETTABLE_INSTRUMENT_TITLE); - - await expect(instrumentPage.submitButton).toBeVisible(); - await expect(instrumentPage.resetButton).toHaveCount(0); - }); - - test('should still submit normally after a reset', async ({ getPageModel, page, uniqueId }) => { - const instrumentPage = await openInstrument(getPageModel, page, uniqueId, RESETTABLE_INSTRUMENT_TITLE); - - await instrumentPage.completeEnhancedDemographicsQuestionnaire(); - await instrumentPage.resetButton.click(); - await instrumentPage.completeEnhancedDemographicsQuestionnaire(); - await instrumentPage.submit(); - - await expect(instrumentPage.summaryHeading).toBeVisible(); - }); -}); diff --git a/testing/src/specs/gateway-assignment.spec.ts b/testing/src/specs/gateway-assignment.spec.ts index 5baca11ff..dc33ffa36 100644 --- a/testing/src/specs/gateway-assignment.spec.ts +++ b/testing/src/specs/gateway-assignment.spec.ts @@ -1,119 +1,65 @@ -import type { Assignment } from '@opendatacapture/schemas/assignment'; -import type { InstrumentRecord } from '@opendatacapture/schemas/instrument-records'; -import type { Session } from '@opendatacapture/schemas/session'; -import type { Page } from '@playwright/test'; +import type { $CreateAssignmentData } from '@opendatacapture/schemas/assignment'; +import type { BrowserContext } from '@playwright/test'; import { RenderInstrumentPage } from '../pages/_app/instruments/render/$id.page'; -import { ApiClient } from '../support/api-client'; -import { HAPPINESS_RECORD } from '../support/constants'; -import { gatewayRefreshInterval, gatewayURL } from '../support/env'; +import { gatewayRefreshInterval } from '../support/env'; import { expect, test } from '../support/fixtures'; -import type { GetPageModel } from '../support/fixtures'; - -const INSTRUMENT_TITLE = 'Happiness Questionnaire'; +import type { ApiClient } from '../support/api-client'; /** Administers the Happiness Questionnaire twice, so one assignment produces two records. */ const SERIES_INSTRUMENT_TITLE = 'Happiness Questionnaire (Repeated)'; /** - * Starts a session and creates a remote assignment for the instrument titled `title` (by default - * the Happiness Questionnaire), returning the gateway link. Shared by every test in this file that - * needs a real link to drive the gateway origin against, so the expensive part (the Cap - * proof-of-work) is only ever solved where a test actually needs a verified assignment. - */ -async function createRemoteAssignmentLink( - getPageModel: GetPageModel, - page: Page, - uniqueId: string, - title = INSTRUMENT_TITLE -): Promise { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Gateway${uniqueId}`, `Patient${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: title }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const dialog = page.getByRole('dialog'); - await expect(dialog).toContainText('Create Remote Assignment'); - await dialog.getByRole('button', { name: 'Submit' }).click(); - - // The dialog swaps in place to reveal the gateway link (a different origin than the web app). - const linkInput = page.locator('input[readonly]'); - await expect(linkInput).toBeVisible(); - const assignmentUrl = await linkInput.inputValue(); - expect(assignmentUrl).toMatch(/^https?:\/\/.+\/assignments\/.+/); - - // Dismiss the dialog: its overlay covers the sidebar and would swallow later nav clicks. - await dialog.getByRole('button', { name: 'Close' }).first().click(); - await expect(dialog).toBeHidden(); - - return assignmentUrl; -} - -/** - * Which refusal arrives is a race with the API's gateway synchronizer, which runs every - * GATEWAY_REFRESH_INTERVAL (10s): until it sweeps the completed assignment across into a real - * record the gateway still holds it and answers 409, and once it has, the record is deleted - * from the gateway by design and the link is a 404. Asserting 409 alone made the test fail - * whenever a sync tick landed in the window, so both terminal answers are accepted and the - * invariant that actually matters — the instrument is not re-served — is asserted directly. + * How long the API's gateway synchronizer may take to sweep a completed assignment across. It runs + * every `gatewayRefreshInterval`, but a pass is slow locally under fullyParallel. */ -async function expectCompletedAssignmentRefused(gatewayPage: Page, assignmentUrl: string) { - const response = await gatewayPage.goto(assignmentUrl); - const status = response?.status(); - expect([404, 409]).toContain(status); - await expect(gatewayPage.getByRole('button', { name: 'Begin' })).toBeHidden(); - if (status === 409) { - await expect(gatewayPage.getByText('Assignment already completed')).toBeVisible(); - } -} +const SYNC_TIMEOUT = 15 * gatewayRefreshInterval; /** Seeds a new group with one subject and an outstanding assignment for it, over the API. */ -async function seedAssignment(api: ApiClient): Promise { +async function seedAssignment(api: ApiClient, data: Pick<$CreateAssignmentData, 'instrumentId' | 'redirectUrl'>) { const group = await api.createGroup(); return api.createAssignment({ expiresAt: new Date(Date.now() + 86_400_000), groupId: group.id, - instrumentId: await api.findInstrumentId('FORM'), - subjectId: await api.createSubject(group.id) + subjectId: await api.createSubject(group.id), + ...data }); } +/** + * Opens the link the patient was sent in a page of its own, as they would receive it out of band, + * and solves the Cap proof-of-work that gates submission (a custom element, which Playwright + * reaches through its open shadow root). + */ +async function openVerifiedAssignment(context: BrowserContext, url: string) { + const gatewayPage = await context.newPage(); + await gatewayPage.goto(url); + const capWidget = gatewayPage.locator('cap-widget'); + await capWidget.waitFor({ state: 'visible' }); + const gatewayInstrument = new RenderInstrumentPage(gatewayPage); + await expect(gatewayInstrument.beginButton).toBeDisabled(); + await expect(gatewayPage.getByRole('button', { name: "Click to verify you're a human" })).toBeVisible(); + await capWidget.click(); + await expect(gatewayInstrument.beginButton).toBeEnabled({ timeout: 120_000 }); + return { gatewayInstrument, gatewayPage }; +} + +// Each spans two origins and waits on the Cap proof-of-work, so it needs more than the default budget. +test.describe.configure({ timeout: 180_000 }); + test.describe('gateway remote assignment', () => { - // Spans two origins and waits on the Cap proof-of-work, so it needs more than the default budget. - test.describe.configure({ timeout: 180_000 }); + test.use({ actingRole: 'ADMIN' }); - test('should let a patient complete an assignment and report it back to the clinician @smoke', async ({ + test('should let a patient complete an assignment and report it back to the clinician', async ({ + api, context, - getPageModel, - page, - uniqueId + getPageModel }) => { - const assignmentUrl = await createRemoteAssignmentLink(getPageModel, page, uniqueId); - - // The patient receives the link out of band, so drive it from a separate page. - const gatewayPage = await context.newPage(); - await gatewayPage.goto(assignmentUrl); - - // The gateway gates submission behind a Cap proof-of-work widget (a custom element, which - // Playwright reaches through its open shadow root). Solving it is what enables "Begin". - const capWidget = gatewayPage.locator('cap-widget'); - await capWidget.waitFor({ state: 'visible' }); - await expect(gatewayPage.getByRole('button', { name: 'Begin' })).toBeDisabled(); - await expect(gatewayPage.getByRole('button', { name: "Click to verify you're a human" })).toBeVisible(); - await capWidget.click(); - - const gatewayInstrument = new RenderInstrumentPage(gatewayPage); - await expect(gatewayInstrument.beginButton).toBeEnabled({ timeout: 120_000 }); + const assignment = await seedAssignment(api, { + instrumentId: await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE') + }); + const { gatewayInstrument, gatewayPage } = await openVerifiedAssignment(context, assignment.url); await gatewayInstrument.begin(); // Submitting empty first, before answering, so the one expensive Cap proof-of-work above covers @@ -132,140 +78,40 @@ test.describe('gateway remote assignment', () => { await expect(gatewayInstrument.errorSummary).toHaveCount(0); // Created without a redirect URL, so the subject is left on the completion screen. await expect(gatewayPage.getByTestId('completion-redirect')).toHaveCount(0); - - // Revisiting the same link after completion must not serve the instrument again. Reuses this - // now-completed assignment rather than soloing a second, expensive Cap proof-of-work just to - // exercise this branch. - await expectCompletedAssignmentRefused(gatewayPage, assignmentUrl); - await gatewayPage.close(); - // Back on the clinician side the assignment should now read as complete. - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - await page.getByRole('link', { name: 'Assignments' }).click(); - await page.waitForURL('**/datahub/subjects/**/assignments'); + // The completion reaches the API on the synchronizer's next pass, not with the submission. + const findStatus = async () => { + const assignments = await api.findAssignments(assignment.subjectId); + return assignments.find(({ id }) => id === assignment.id)?.status; + }; + await expect.poll(findStatus, { timeout: SYNC_TIMEOUT }).toBe('COMPLETE'); - // The completion posts back from the gateway asynchronously (it can still read "Outstanding" - // for a while on a slow runner), and assertion retries re-query the locator rather than - // refetching -- so retry the reload itself until the status settles. Reloading is safe here: - // the page is addressed by subject id, not the (now irrelevant) in-memory session. - await expect(async () => { - await page.reload(); - await expect(page.getByTestId('data-table-row').first()).toContainText('Complete', { timeout: 5_000 }); - }).toPass({ timeout: 90_000 }); + const assignmentsPage = await getPageModel('/datahub/subjects/$subjectId/assignments', { + subjectId: assignment.subjectId + }); + await expect(assignmentsPage.assignmentRows).toHaveCount(1); + await expect(assignmentsPage.assignmentRows).toContainText('Complete'); }); }); -test.describe('gateway completion redirect', () => { - test.describe.configure({ timeout: 180_000 }); - - test('should send the subject on to the redirect URL the assignment was created with once they complete it', async ({ +test.describe('gateway assignment records', () => { + test('should link every record of a remote series to its assignment, record its completion, and send the subject on to its redirect URL', async ({ api, context, uniqueId }) => { const redirectUrl = `https://redirect.example.test/done/${uniqueId}`; - const group = await api.createGroup(); - const assignment = await api.createAssignment({ - expiresAt: new Date(Date.now() + 86_400_000), - groupId: group.id, - instrumentId: await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'), - redirectUrl, - subjectId: await api.createSubject(group.id) + const assignment = await seedAssignment(api, { + instrumentId: await api.findInstrumentIdByTitle(SERIES_INSTRUMENT_TITLE), + redirectUrl }); - const gatewayPage = await context.newPage(); + const { gatewayInstrument, gatewayPage } = await openVerifiedAssignment(context, assignment.url); // The site the subject is sent back to is not part of the stack, so answer for it here. await gatewayPage.route('https://redirect.example.test/**', (route) => { return route.fulfill({ body: '

Study portal

', contentType: 'text/html' }); }); - await gatewayPage.goto(assignment.url); - await gatewayPage.locator('cap-widget').click(); - - const gatewayInstrument = new RenderInstrumentPage(gatewayPage); - await expect(gatewayInstrument.beginButton).toBeEnabled({ timeout: 120_000 }); - await gatewayInstrument.begin(); - await gatewayInstrument.completeHappinessQuestionnaire(); - await gatewayInstrument.submit(); - - // The completion screen stays up through the countdown, so the subject sees it before leaving. - await expect(gatewayInstrument.summaryHeading).toBeVisible(); - await expect(gatewayPage.getByTestId('completion-redirect')).toBeVisible(); - await gatewayPage.waitForURL(redirectUrl); - await expect(gatewayPage.getByRole('heading', { name: 'Study portal' })).toBeVisible(); - - await gatewayPage.close(); - }); -}); - -test.describe('gateway malformed submission', () => { - test.describe.configure({ timeout: 180_000 }); - - // The API retries a submission it cannot ingest on every synchronization pass, so rejecting it - // after writing the session would leave one more session behind each time. - test('should not record a session for a submission shaped for another kind of instrument', async ({ - adminToken, - api, - apiRequestContext, - context - }) => { - const assignment = await seedAssignment(api); - const adminHeaders = { Authorization: `Bearer ${adminToken}` }; - - const gatewayPage = await context.newPage(); - await gatewayPage.goto(assignment.url); - await gatewayPage.locator('cap-widget').click(); - await expect(gatewayPage.getByRole('button', { name: 'Begin' })).toBeEnabled({ timeout: 120_000 }); - - // The page hands the patient this token; a hand-written request can then claim any shape. - const { token } = await gatewayPage.evaluate(() => Reflect.get(window, '__ROOT_PROPS__')); - const submission = await apiRequestContext.patch(`${gatewayURL}/api/assignments/${assignment.id}`, { - data: { data: { answer: 1 }, kind: 'SERIES', status: 'COMPLETE' }, - headers: { Authorization: `Bearer ${token}` } - }); - expect(submission.status()).toBe(200); - await gatewayPage.close(); - - await new Promise((resolve) => { - setTimeout(resolve, 2 * gatewayRefreshInterval); - }); - const response = await apiRequestContext.get('/api/v1/sessions', { headers: adminHeaders }); - const sessions = (await response.json()) as Session[]; - const remoteSessions = sessions.filter( - (session) => session.subjectId === assignment.subjectId && session.type === 'REMOTE' - ); - expect(remoteSessions).toHaveLength(0); - - // Cancelling removes the row from the gateway, so the rest of the run is not synchronizing it. - await apiRequestContext.patch(`/api/v1/assignments/${assignment.id}`, { - data: { status: 'CANCELED' }, - headers: adminHeaders - }); - }); -}); - -test.describe('gateway assignment records', () => { - test.describe.configure({ timeout: 180_000 }); - - test('should link every record of a remote series to its assignment, and record when it was completed', async ({ - adminToken, - api, - apiRequestContext, - context, - getPageModel, - page, - uniqueId - }) => { - const assignmentUrl = await createRemoteAssignmentLink(getPageModel, page, uniqueId, SERIES_INSTRUMENT_TITLE); - const assignmentId = assignmentUrl.split('/').pop()!; - - const gatewayPage = await context.newPage(); - await gatewayPage.goto(assignmentUrl); - await gatewayPage.locator('cap-widget').click(); - - const gatewayInstrument = new RenderInstrumentPage(gatewayPage); - await expect(gatewayInstrument.beginButton).toBeEnabled({ timeout: 120_000 }); await gatewayInstrument.begin(); await gatewayInstrument.completeHappinessQuestionnaire(); await gatewayInstrument.submit(); @@ -274,153 +120,28 @@ test.describe('gateway assignment records', () => { await gatewayInstrument.completeHappinessQuestionnaire(); await gatewayInstrument.submit(); await expect(gatewayInstrument.seriesCompletionHeading).toBeVisible(); - await gatewayPage.close(); - - const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); - const headers = { Authorization: `Bearer ${adminToken}` }; - let linkedRecords: InstrumentRecord[] = []; - await expect(async () => { - const response = await apiRequestContext.get('/api/v1/instrument-records', { headers, params: { instrumentId } }); - const records = (await response.json()) as InstrumentRecord[]; - linkedRecords = records.filter((record) => record.assignmentId === assignmentId); - expect(linkedRecords).toHaveLength(2); - }).toPass({ timeout: 6 * gatewayRefreshInterval }); - - // The status and completion date are written after the records, in the same pass. - const findCompletedAt = async () => { - const assignments = await api.findAssignments(linkedRecords[0]!.subjectId); - return assignments.find(({ id }) => id === assignmentId)?.completedAt; - }; - await expect.poll(findCompletedAt, { timeout: 2 * gatewayRefreshInterval }).toEqual(expect.any(String)); - }); - - // Only the gateway synchronizer may link a record to an assignment, so the create route drops the - // key, but a client that still sends one must not be refused for it. - test('should accept a record that names an assignment, without linking the two', async ({ - adminToken, - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup(); - const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); - const session = await api.createSession(group.id, { id: `assigned-${uniqueId}` }); - const assignment = await api.createAssignment({ - expiresAt: new Date(Date.now() + 86_400_000), - groupId: group.id, - instrumentId, - subjectId: session.subjectId - }); - - const response = await apiRequestContext.post('/api/v1/instrument-records', { - data: { - assignmentId: assignment.id, - data: HAPPINESS_RECORD, - date: new Date(), - groupId: group.id, - instrumentId, - sessionId: session.id, - subjectId: session.subjectId - }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(response.status(), await response.text()).toBe(201); - expect(await response.json()).toMatchObject({ assignmentId: null }); - }); -}); - -test.describe('gateway assignment errors', () => { - test('should respond with 404 when the assignment id in the link does not exist', async ({ - context, - getPageModel, - page, - uniqueId - }) => { - const assignmentUrl = await createRemoteAssignmentLink(getPageModel, page, uniqueId); - const invalidUrl = assignmentUrl.replace(/[^/]+$/, `nonexistent-${uniqueId}`); - - const gatewayPage = await context.newPage(); - const response = await gatewayPage.goto(invalidUrl); - expect(response?.status()).toBe(404); - - await gatewayPage.close(); - }); - - // The languages an assignment offers are stored on the gateway's own database and read back - // server-side, so `?lang=` must be honoured in the SSR'd HTML rather than swapped in after - // hydration. Asserting against the raw response body is what distinguishes the two. - test('should render the assignment server-side in a requested active language', async ({ - context, - getPageModel, - page, - uniqueId - }) => { - const assignmentUrl = await createRemoteAssignmentLink(getPageModel, page, uniqueId); - - // The stepper is the only translated copy in the first paint — everything below it waits on the - // instrument bundle, which loads client-side. - const gatewayPage = await context.newPage(); - const french = await gatewayPage.goto(`${assignmentUrl}?lang=fr`); - expect(await french!.text()).toContain('Aperçu'); - - // A language the instance does not offer falls back to the first active one rather than - // rendering the blank strings an unresolvable language would produce. - const unknown = await gatewayPage.goto(`${assignmentUrl}?lang=klingon`); - expect(await unknown!.text()).toContain('Overview'); + // The completion screen offers the redirect, so the subject sees it before leaving. Following + // the link rather than waiting out the countdown; CompletionRedirect.test.tsx owns the timer. + // The submission toast sits over the link until it fades, so follow it from the keyboard. + await gatewayPage.getByTestId('completion-redirect-link').press('Enter'); + await gatewayPage.waitForURL(redirectUrl); + await expect(gatewayPage.getByRole('heading', { name: 'Study portal' })).toBeVisible(); await gatewayPage.close(); - }); - - // `POST /v1/assignments/:id/email` mails a live assignment credential to a caller-supplied - // address, and its guard (`update Assignment`) is group-scoped rather than admin-only — so the - // control that matters is row scoping, which no blanket-403 table can assert. - test('should not let a manager outside the group email an assignment link', async ({ - api, - apiRequestContext, - getPageModel, - page, - uniqueId - }) => { - const assignmentUrl = await createRemoteAssignmentLink(getPageModel, page, uniqueId); - const assignmentId = assignmentUrl.split('/').pop()!; - - const outsiderGroup = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [outsiderGroup.id] }); - const token = await ApiClient.login(apiRequestContext, credentials); - - const response = await apiRequestContext.post(`/api/v1/assignments/${assignmentId}/email`, { - data: { language: 'en', recipient: 'outsider-target@example.org' }, - headers: { Authorization: `Bearer ${token}` } - }); - expect([403, 404]).toContain(response.status()); - }); - - // Cancelling deletes the gateway's copy of the assignment, which cannot be undone, so the row - // scoping has to be checked before that deletion rather than by the update that follows it. - test('should not let a manager outside the group cancel an assignment and kill its link', async ({ - api, - apiRequestContext - }) => { - const assignment = await seedAssignment(api); - - const outsiderGroup = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [outsiderGroup.id] }); - const token = await ApiClient.login(apiRequestContext, credentials); - const response = await apiRequestContext.patch(`/api/v1/assignments/${assignment.id}`, { - data: { status: 'CANCELED' }, - headers: { Authorization: `Bearer ${token}` } - }); - expect(response.status()).toBe(404); - expect((await apiRequestContext.get(assignment.url)).status()).toBe(200); - }); - - // Proof of work is checked after the body is parsed, so an oversized id is refused whatever token - // accompanies it. - test('should refuse to verify an id longer than any assignment id', async ({ apiRequestContext }) => { - const response = await apiRequestContext.post(`${gatewayURL}/api/auth/verify`, { - data: { id: 'x'.repeat(10_000), token: 'unsolved' } - }); - expect(response.status()).toBe(400); + // The status and completion date are written after the records, in the same synchronizer pass. + const findSyncedState = async () => { + const [records, assignments] = await Promise.all([ + api.findRecordsBySubject(assignment.subjectId), + api.findAssignments(assignment.subjectId) + ]); + return { + completedAt: assignments.find(({ id }) => id === assignment.id)?.completedAt, + linkedRecordCount: records.filter(({ assignmentId }) => assignmentId === assignment.id).length + }; + }; + await expect + .poll(findSyncedState, { timeout: SYNC_TIMEOUT }) + .toEqual({ completedAt: expect.any(String), linkedRecordCount: 2 }); }); }); diff --git a/testing/src/specs/gateway-landing.spec.ts b/testing/src/specs/gateway-landing.spec.ts index d6d38367e..343ecaace 100644 --- a/testing/src/specs/gateway-landing.spec.ts +++ b/testing/src/specs/gateway-landing.spec.ts @@ -12,30 +12,6 @@ const STALE_SETUP_STATE_TIMEOUT = 3 * gatewayRefreshInterval; test.describe('gateway landing page', () => { test.describe.configure({ timeout: STALE_SETUP_STATE_TIMEOUT + 30_000 }); - test('should point a patient who opens the gateway root to the link they were sent', async ({ page }) => { - const response = await page.goto(gatewayURL); - expect(response?.status()).toBe(200); - const landing = page.getByTestId('gateway-landing'); - await expect(landing).toBeVisible(); - await expect(landing).toContainText('open the secure link you were sent'); - }); - - test('should translate the landing page when a patient picks another language, so one who cannot read the default can follow it', async ({ - page - }) => { - // The toggle renders only while two languages are active. - const languageToggle = page.getByTestId('language-toggle'); - await expect(async () => { - await page.goto(gatewayURL); - await expect(languageToggle).toBeAttached({ timeout: 1_000 }); - }).toPass({ timeout: STALE_SETUP_STATE_TIMEOUT }); - const heading = page.getByTestId('gateway-landing').getByRole('heading'); - await expect(heading).toHaveText('Welcome'); - await languageToggle.getByRole('button').click(); - await page.getByRole('menuitem', { name: 'Français' }).click(); - await expect(heading).toHaveText('Bienvenue'); - }); - // The raw response body shows the server rendered French. The client must then hydrate in French // too, or React recovers from the mismatch by re-rendering in English; the shell only becomes // visible after that, in Root's post-hydration effect, so the visible heading is the final one. diff --git a/testing/src/specs/group-archive.spec.ts b/testing/src/specs/group-archive.spec.ts index 5135bd7cc..b8246a0e6 100644 --- a/testing/src/specs/group-archive.spec.ts +++ b/testing/src/specs/group-archive.spec.ts @@ -1,12 +1,7 @@ -import type { $LoginCredentials } from '@opendatacapture/schemas/auth'; -import type { APIRequestContext, Page } from '@playwright/test'; +import type { Page } from '@playwright/test'; -import { DashboardPage } from '../pages/_app/dashboard.page'; -import { ApiClient } from '../support/api-client'; import { expect, test } from '../support/fixtures'; -const API = '/api/v1'; - /** Opens the manage sheet of the one group the admin groups table is filtered down to. */ async function openManageSheet(page: Page, groupName: string) { await page.getByTestId('data-table-search-bar').getByRole('searchbox').fill(groupName); @@ -17,24 +12,6 @@ async function openManageSheet(page: Page, groupName: string) { return row; } -/** - * The statuses a member gets reading their group and a subject in it, signing in afresh each time: - * an archive reaches a member only through the token they are issued next. - */ -async function readAsMember( - request: APIRequestContext, - credentials: $LoginCredentials, - groupId: string, - subjectId: string -) { - const headers = { Authorization: `Bearer ${await ApiClient.login(request, credentials)}` }; - const [group, subject] = await Promise.all([ - request.get(`${API}/groups/${groupId}`, { headers }), - request.get(`${API}/subjects/${subjectId}`, { headers }) - ]); - return { group: group.status(), subject: subject.status() }; -} - test.describe('group archiving', () => { test('should let an admin archive a group from its manage sheet and unarchive it again', async ({ api, @@ -60,65 +37,4 @@ test.describe('group archiving', () => { await expect(row.getByTestId('group-status-active')).toBeVisible(); expect((await api.findGroupById(group.id)).archivedAt).toBeFalsy(); }); - - test('should take an archived group away from its members once they sign in again, and give it back on unarchive', async ({ - api, - apiRequestContext - }) => { - const group = await api.createGroup(); - const subjectId = await api.createSubject(group.id); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); - - expect(await readAsMember(apiRequestContext, credentials, group.id, subjectId)).toStrictEqual({ - group: 200, - subject: 200 - }); - - await api.archiveGroup(group.id); - expect(await readAsMember(apiRequestContext, credentials, group.id, subjectId)).toStrictEqual({ - group: 404, - subject: 404 - }); - - await api.unarchiveGroup(group.id); - expect(await readAsMember(apiRequestContext, credentials, group.id, subjectId)).toStrictEqual({ - group: 200, - subject: 200 - }); - }); - - test('should no longer offer a member an archived group as their current group', async ({ - api, - authenticateAs, - page - }) => { - const archivedGroup = await api.createGroup(); - const activeGroup = await api.createGroup(); - const { credentials } = await api.createUser({ - basePermissionLevel: 'GROUP_MANAGER', - groupIds: [archivedGroup.id, activeGroup.id] - }); - await api.archiveGroup(archivedGroup.id); - await authenticateAs(credentials); - - const dashboardPage = new DashboardPage(page); - await dashboardPage.goto('/dashboard'); - - // One group left to choose from, so the switcher shows it as the selected group's name. - await expect(dashboardPage.groupSwitcher).toContainText(activeGroup.name); - await expect(dashboardPage.groupSwitcher).not.toContainText(archivedGroup.name); - }); - - test('should not offer an admin an archived group in the group switcher', async ({ api, authenticateAs, page }) => { - const archivedGroup = await api.createGroup(); - await api.archiveGroup(archivedGroup.id); - const { credentials } = await api.createUser({ basePermissionLevel: 'ADMIN', groupIds: [] }); - await authenticateAs(credentials); - - const dashboardPage = new DashboardPage(page); - await dashboardPage.goto('/dashboard'); - const options = await dashboardPage.openGroupSwitcher(); - await expect(options.first()).toBeVisible(); - await expect(options.filter({ hasText: archivedGroup.name })).toHaveCount(0); - }); }); diff --git a/testing/src/specs/group-email-templates.spec.ts b/testing/src/specs/group-email-templates.spec.ts index 3600c4ad0..791847f02 100644 --- a/testing/src/specs/group-email-templates.spec.ts +++ b/testing/src/specs/group-email-templates.spec.ts @@ -1,89 +1,41 @@ +import { GroupEmailTemplatesPage } from '../pages/_app/group/email-templates.page'; import { expect, test } from '../support/fixtures'; -const BODY = 'Complete your assignment: {{url}} — it expires on {{expiresAt}}.'; - test.describe('group email templates', () => { - test('should start with the built-in template as the default', async ({ getPageModel }) => { - const templatesPage = await getPageModel('/group/email-templates'); - - await expect(templatesPage.root).toBeVisible(); + // A group of its own, since making a template the default changes what every member's participants + // receive. Adding a template must not silently change that; only setting it as default does, and + // deleting the default returns to the built-in message rather than promoting an arbitrary row. + test('should make a new template the default only when asked, and fall back to the built-in one when it is deleted', async ({ + isolatedGroupManager, + page, + uniqueId + }) => { + await isolatedGroupManager(); + const templatesPage = new GroupEmailTemplatesPage(page); + await templatesPage.goto('/group/email-templates'); await expect(templatesPage.builtInDefaultBadge).toBeVisible(); - }); - - // The link is the whole point of the email, so a template without it must not be creatable. - test('should refuse a template whose body omits the required placeholders', async ({ getPageModel, uniqueId }) => { - const templatesPage = await getPageModel('/group/email-templates'); - await templatesPage.name.fill(`Template${uniqueId}`); - await templatesPage.createSubject.fill('Your assignment'); - await templatesPage.createBody.fill('No placeholders here.'); - - await expect(templatesPage.createError).toBeVisible(); - await expect(templatesPage.createSubmit).toBeDisabled(); - }); - - test('should insert a placeholder into the body from the toolbar', async ({ getPageModel }) => { - const templatesPage = await getPageModel('/group/email-templates'); - - await templatesPage.createBody.fill('Link:'); + const name = `Template${uniqueId}`; + await templatesPage.name.fill(name); + await templatesPage.createSubject.fill(`Subject${uniqueId}`); + await templatesPage.createBody.fill('It expires on {{expiresAt}}. Complete your assignment: '); await templatesPage.insertUrl.click(); - await expect(templatesPage.createBody).toHaveValue(/\{\{url\}\}/); - }); - - // A template authored in only one language is allowed, but not silently. - test('should warn before creating a template that is missing a translation', async ({ getPageModel, uniqueId }) => { - const templatesPage = await getPageModel('/group/email-templates'); - const name = `Template${uniqueId}`; - - await templatesPage.fillCreateForm({ body: BODY, name, subject: `Subject${uniqueId}` }); - - await expect(templatesPage.createAnyway).toBeVisible(); - await expect(templatesPage.rowFor(name)).toBeHidden(); - }); - - // Adding a template must not silently change what participants receive. - test('should create a template without making it the default', async ({ getPageModel, uniqueId }) => { - const templatesPage = await getPageModel('/group/email-templates'); - const name = `Template${uniqueId}`; - - await templatesPage.createTemplate({ body: BODY, name, subject: `Subject${uniqueId}` }); - + await templatesPage.createSubmit.click(); + // Authored in one language, so the missing-translations warning has to be confirmed. + await templatesPage.createAnyway.click(); await expect(templatesPage.rowFor(name)).toBeVisible(); await expect(templatesPage.builtInDefaultBadge).toBeVisible(); - }); - - test('should make a template the default only when asked, and fall back on delete', async ({ - getPageModel, - uniqueId - }) => { - const templatesPage = await getPageModel('/group/email-templates'); - const name = `Template${uniqueId}`; - await templatesPage.createTemplate({ body: BODY, name, subject: `Subject${uniqueId}` }); await templatesPage.setActiveButtonFor(name).click(); await expect(templatesPage.builtInDefaultBadge).toBeHidden(); - // Deleting the default returns to the built-in message rather than promoting an arbitrary row. - await templatesPage.deleteButtonFor(name).click(); - await templatesPage.deleteConfirm.click(); - await expect(templatesPage.rowFor(name)).toBeHidden(); - await expect(templatesPage.builtInDefaultBadge).toBeVisible(); - }); - - test('should require confirmation before deleting a template', async ({ getPageModel, uniqueId }) => { - const templatesPage = await getPageModel('/group/email-templates'); - const name = `Template${uniqueId}`; - await templatesPage.createTemplate({ body: BODY, name, subject: `Subject${uniqueId}` }); - await expect(templatesPage.rowFor(name)).toBeVisible(); - - await templatesPage.deleteButtonFor(name).click(); - // Nothing is destroyed until the dialog is confirmed. + await templatesPage.deleteButtonFor(name).click(); await expect(templatesPage.deleteConfirm).toBeVisible(); await expect(templatesPage.rowFor(name)).toBeVisible(); - await templatesPage.deleteConfirm.click(); await expect(templatesPage.rowFor(name)).toBeHidden(); + await expect(templatesPage.builtInDefaultBadge).toBeVisible(); }); }); diff --git a/testing/src/specs/group-manage.spec.ts b/testing/src/specs/group-manage.spec.ts index bf857b0ed..0fabf7ee1 100644 --- a/testing/src/specs/group-manage.spec.ts +++ b/testing/src/specs/group-manage.spec.ts @@ -1,6 +1,5 @@ import type { Locator } from '@playwright/test'; -import { GroupManagePage } from '../pages/_app/group/manage.page'; import { expect, test } from '../support/fixtures'; /** Seeded as a series; `SCALAR_INSTRUMENT_TITLE` is one of the items it repeats. */ @@ -9,133 +8,50 @@ const SERIES_INSTRUMENT_TITLE = 'Happiness Questionnaire (Repeated)'; const SCALAR_INSTRUMENT_TITLE = 'Happiness Questionnaire'; test.describe('group manage', () => { - test('should give a newly created group access to every uploaded instrument, so a manager has something to administer', async ({ - adminToken, - apiRequestContext, - uniqueId + // Saving the selection and settings is the API suite's (apps/api/test/suites/09-group-access.suite.ts): + // the e2e instance is a demo, which a production build serves read-only. That gating is itself + // what only a production build against a real setup state can show. + test('should list every accessible instrument with its date, preview and availability, read-only on a production demo', async ({ + getPageModel, + page }) => { - // Deliberately not the `api.createGroup` fixture: that helper PATCHes the accessible instruments - // in after creating the group, so it passes whether or not creation connected anything itself. - // An uploaded instrument has no `sourceRepoId` key at all, and the selection query has to match - // that as well as an explicit null. - const response = await apiRequestContext.post('/api/v1/groups', { - data: { name: `RawGroup${uniqueId}`, type: 'CLINICAL' }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(response.status()).toBe(201); - - const group = (await response.json()) as { accessibleInstrumentIds: string[] }; - expect(group.accessibleInstrumentIds.length).toBeGreaterThan(0); - }); - - test('should let a group manager preview an accessible instrument', async ({ getPageModel }) => { - const groupManagePage = await getPageModel('/group/manage'); - - // Not asserting which instrument this is: form order is whatever the API returns, not something - // this test controls. - await groupManagePage.$ref.getByRole('button', { name: 'Preview instrument' }).first().click(); - - const dialog = groupManagePage.$ref.getByRole('dialog'); - await expect(dialog).toBeVisible(); - await expect(dialog.getByRole('button', { name: 'Preview Form' })).toBeVisible(); - }); - - // Every instrument carries the date it was stored, whether it was uploaded, imported from a - // repository, or built here as a series. - test('should tag every instrument with the date it was added', async ({ getPageModel }) => { const groupManagePage = await getPageModel('/group/manage'); + await expect(groupManagePage.instrumentCheckbox(SCALAR_INSTRUMENT_TITLE)).toBeDisabled(); + // Every instrument carries the date it was stored, whether it was uploaded, imported from a + // repository, or built here as a series. for (const title of [SERIES_INSTRUMENT_TITLE, SCALAR_INSTRUMENT_TITLE]) { - const createdAt = groupManagePage.instrumentCreatedAt(title); - await expect(createdAt).toBeVisible(); - await expect(createdAt).toHaveText(/^\d{4}-\d{2}-\d{2}$/); + await expect(groupManagePage.instrumentCreatedAt(title)).toHaveText(/^\d{4}-\d{2}-\d{2}$/); } - }); - - // The tags are what make the rows scannable, so they have to form columns rather than sit wherever - // each row's own content puts them — which is what a per-row grid did. - test('should line the tags and actions of every row into columns', async ({ getPageModel }) => { - const groupManagePage = await getPageModel('/group/manage'); - // `evaluateAll` reads the DOM as it finds it, with none of a locator's waiting, so the rows have - // to be on the page before it runs — and more than one of them, or a single edge would satisfy - // any assertion about a column. + // The tags are what make the rows scannable, so they have to form columns rather than sit + // wherever each row's own content puts them — which is what a per-row grid did. `evaluateAll` + // reads the DOM as it finds it, with none of a locator's waiting, so more than one row has to be + // on the page first, or a single edge would satisfy any assertion about a column. const distinctLeftEdges = async (locator: Locator) => { - await expect(locator.first()).toBeVisible(); - await expect.poll(async () => locator.count()).toBeGreaterThan(1); + await expect(locator.nth(1)).toBeVisible(); const edges = await locator.evaluateAll((elements) => { return elements.map((element) => Math.round(element.getBoundingClientRect().left)); }); return new Set(edges).size; }; - expect(await distinctLeftEdges(groupManagePage.instrumentPreviewButtons)).toBe(1); expect(await distinctLeftEdges(groupManagePage.instrumentCreatedAtTags)).toBe(1); - }); - // A series can be one this group built for itself or one shared across the whole instance, and the - // two are indistinguishable in the list. The seeded series is uploaded, so it belongs to no group. - test('should tell the previewer which groups a series is available to', async ({ getPageModel }) => { - const groupManagePage = await getPageModel('/group/manage'); - - await groupManagePage.instrumentPreviewButton(SERIES_INSTRUMENT_TITLE).click(); + // A series can be one this group built for itself or one shared across the whole instance, and + // the two are indistinguishable in the list. The seeded series is uploaded, so it belongs to no + // group. const dialog = groupManagePage.$ref.getByRole('dialog'); + await groupManagePage.instrumentPreviewButton(SERIES_INSTRUMENT_TITLE).click(); await expect(dialog.getByTestId('instrument-availability')).toHaveText('Available to: All groups'); // A plain date: the preview reports the day, not the minute the record happens to carry. await expect(dialog.getByTestId('instrument-created-at')).toHaveText(/^Added: \d{4}-\d{2}-\d{2}$/); - }); - - // Only a series is owned by a group, so the line would be meaningless on a form. - test('should not claim an availability for a scalar instrument', async ({ getPageModel }) => { - const groupManagePage = await getPageModel('/group/manage'); + await page.keyboard.press('Escape'); + await expect(dialog).toBeHidden(); + // Only a series is owned by a group, so the line would be meaningless on a form. await groupManagePage.instrumentPreviewButton(SCALAR_INSTRUMENT_TITLE).click(); - const dialog = groupManagePage.$ref.getByRole('dialog'); - await expect(dialog).toBeVisible(); + await expect(dialog.getByRole('button', { name: 'Preview Form' })).toBeVisible(); await expect(dialog.getByTestId('instrument-availability')).toHaveCount(0); }); - - test('should update accessible instruments and group settings, and keep them after navigating away and back', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - // A dedicated group + user, rather than the default `actingRole: 'GROUP_MANAGER'` fixture: that - // role's group is created once and cached per worker (see `roleAccount` in fixtures.ts) and shared - // by every other spec using the default role, so mutating it here would leak into their state. - const group = await api.createGroup({ name: `Group${uniqueId}` }); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); - - await authenticateAs(credentials); - await page.goto('/group/manage'); - await expect(page).toHaveURL('/group/manage'); - - const groupManagePage = new GroupManagePage(page); - // `api.createGroup` grants access to every instrument, so uncheck one to produce an observable, - // reversible change. Assert it starts checked first: `uncheck()` is a no-op on an already- - // unchecked box, so without this the rest of the test would still pass if the precondition - // silently stopped holding. - await expect(groupManagePage.instrumentCheckbox('Happiness Questionnaire')).toBeChecked(); - await groupManagePage.instrumentCheckbox('Happiness Questionnaire').uncheck(); - await groupManagePage.subjectIdDisplayLengthInput.fill('6'); - await groupManagePage.submitButton.click(); - await expect(page.getByRole('heading', { name: 'Success' })).toBeVisible(); - - // `currentGroup` is derived from the access token at boot rather than re-fetched on reload, so a - // hard reload with the same token would show what the group looked like at login time, not the - // update just made. Navigating away and back in the same session instead reflects the store, which - // the mutation's own response already updated. - await page.getByTestId('nav-button-/dashboard').click(); - // The group auto-closes on leaving /group/manage, so expanding before the URL settles could - // toggle a still-open group shut. - await expect(page).toHaveURL('/dashboard'); - // Bulk remote assignments is enabled by default, nesting group links under "Group Actions" - await groupManagePage.expandNavGroup('Group Actions'); - await page.getByTestId('nav-button-/group/manage').click(); - await expect(page).toHaveURL('/group/manage'); - - await expect(groupManagePage.instrumentCheckbox('Happiness Questionnaire')).not.toBeChecked(); - await expect(groupManagePage.subjectIdDisplayLengthInput).toHaveValue('6'); - }); }); diff --git a/testing/src/specs/group-switcher.spec.ts b/testing/src/specs/group-switcher.spec.ts index 98060ea45..664347d1b 100644 --- a/testing/src/specs/group-switcher.spec.ts +++ b/testing/src/specs/group-switcher.spec.ts @@ -26,39 +26,64 @@ test.describe('group switcher', () => { await scopedSummary; }); - test('should start an admin who belongs to no group with none selected, and offer them every group', async ({ + // The seeded admin account belongs to no group, so the dashboard falls back to the application-wide + // summary. An archived group grants nothing, so it is no group to switch into. + test('should start an admin who belongs to no group on the whole application, offering every group but an archived one', async ({ api, authenticateAs, page }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ basePermissionLevel: 'ADMIN', groupIds: [] }); + const [group, archivedGroup, { credentials }] = await Promise.all([ + api.createGroup(), + api.createGroup(), + api.createUser({ basePermissionLevel: 'ADMIN', groupIds: [] }) + ]); + await api.archiveGroup(archivedGroup.id); await authenticateAs(credentials); const dashboardPage = new DashboardPage(page); await dashboardPage.goto('/dashboard'); await expect(dashboardPage.groupSwitcher).toContainText('Select a group'); + await expect(dashboardPage.welcomeHeading).toContainText('Summary of Application State'); - await dashboardPage.switchGroup(group.name); + const options = await dashboardPage.openGroupSwitcher(); + await expect(options.filter({ hasText: archivedGroup.name })).toHaveCount(0); + await options.filter({ hasText: group.name }).click(); + await expect(dashboardPage.groupSwitcher).toContainText(group.name); }); - test('should offer a group manager only the groups they belong to', async ({ api, authenticateAs, page }) => { - const memberGroups = [await api.createGroup(), await api.createGroup()]; - const otherGroup = await api.createGroup(); + // The archived group is listed first, so a client that took the first of the user's groups as the + // current one without dropping archived groups would select it. + test('should offer a group manager only the active groups they belong to, and never start them in an archived one', async ({ + api, + authenticateAs, + page + }) => { + const [archivedGroup, firstGroup, secondGroup, otherGroup] = await Promise.all([ + api.createGroup(), + api.createGroup(), + api.createGroup(), + api.createGroup() + ]); + const memberGroups = [firstGroup, secondGroup]; const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', - groupIds: memberGroups.map((group) => group.id) + groupIds: [archivedGroup.id, ...memberGroups.map((group) => group.id)] }); + await api.archiveGroup(archivedGroup.id); await authenticateAs(credentials); const dashboardPage = new DashboardPage(page); await dashboardPage.goto('/dashboard'); + await expect(dashboardPage.groupSwitcher).toContainText(new RegExp(`${firstGroup.name}|${secondGroup.name}`)); + await expect(dashboardPage.groupSwitcher).not.toContainText(archivedGroup.name); const options = await dashboardPage.openGroupSwitcher(); await expect(options).toHaveCount(memberGroups.length); for (const group of memberGroups) { await expect(options.filter({ hasText: group.name })).toBeVisible(); } + await expect(options.filter({ hasText: archivedGroup.name })).toHaveCount(0); await expect(options.filter({ hasText: otherGroup.name })).toHaveCount(0); }); }); diff --git a/testing/src/specs/instrument-completion.spec.ts b/testing/src/specs/instrument-completion.spec.ts index fd8420703..2f307b1cd 100644 --- a/testing/src/specs/instrument-completion.spec.ts +++ b/testing/src/specs/instrument-completion.spec.ts @@ -1,8 +1,11 @@ -import { readFile } from 'node:fs/promises'; +import type { Page } from '@playwright/test'; import { RenderInstrumentPage } from '../pages/_app/instruments/render/$id.page'; +import { HAPPINESS_RECORD } from '../support/constants'; import { expect, test } from '../support/fixtures'; +import type { GetPageModel } from '../support/fixtures'; + // Title shown on the instrument card in the showcase (`details.title`), which differs from the // title shown while running it (`clientDetails.title`, "Questionnaire on Happiness"). const INSTRUMENT_TITLE = 'Happiness Questionnaire'; @@ -11,44 +14,61 @@ const INSTRUMENT_TITLE = 'Happiness Questionnaire'; // sections (select, radio and number inputs), unlike the flat Happiness Questionnaire. const DEMOGRAPHICS_INSTRUMENT_TITLE = 'Enhanced Demographics Questionnaire'; +/** + * Starts a session for a new subject and begins the instrument with this card title. The active + * session lives in memory, so every step navigates via the sidebar; a hard navigation would drop + * the session and disable these nav buttons. + */ +async function beginInstrument( + getPageModel: GetPageModel, + page: Page, + firstName: string, + title: string +): Promise { + const startSessionPage = await getPageModel('/session/start-session'); + await startSessionPage.sessionForm.waitFor({ state: 'visible' }); + await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); + await startSessionPage.fillSessionForm(firstName, `Subject${firstName}`, 'Female'); + await startSessionPage.submitForm(); + await expect(startSessionPage.successMessage).toBeVisible(); + + await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); + await page.waitForURL('**/instruments/accessible-instruments'); + + const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: title }).first(); + await expect(card).toBeVisible(); + await card.click(); + + const instrumentPage = new RenderInstrumentPage(page); + await instrumentPage.begin(); + return instrumentPage; +} + +/** Opens the current subject's record table on the records of the instrument with this title. */ +async function openSubjectRecords(page: Page, title: string): Promise { + // "View Current Subject" targets the active subject's record table directly, which is far more + // robust than matching a hashed row in the data hub list. + await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); + await page.waitForURL('**/datahub/subjects/**/table'); + + await page.getByRole('combobox').first().click(); + await page.getByRole('option', { name: title }).click(); +} + test.describe('instrument completion', () => { - test('should administer an instrument and surface the record for the subject @smoke', async ({ + test('should administer an instrument and surface the record for the subject', async ({ getPageModel, page, roleAccount, uniqueId }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Instrument${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - // The active session lives in memory, so every step from here navigates via the sidebar; a - // hard navigation would drop the session and disable these nav buttons. - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); + const instrumentPage = await beginInstrument(getPageModel, page, `Instrument${uniqueId}`, INSTRUMENT_TITLE); await instrumentPage.completeHappinessQuestionnaire(); await instrumentPage.submit(); await expect(instrumentPage.summaryHeading).toBeVisible(); - // "View Current Subject" targets the active subject's record table directly, which is far more - // robust than matching a hashed row in the data hub list. - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - - await page.getByRole('combobox').first().click(); - await page.getByRole('option', { name: INSTRUMENT_TITLE }).click(); - + await openSubjectRecords(page, INSTRUMENT_TITLE); await expect(page.getByTestId('data-table-body').getByTestId('data-table-row').first()).toBeVisible(); // The one place the api -> web username contract is exercised end to end. Both unit tiers mock @@ -59,78 +79,30 @@ test.describe('instrument completion', () => { await expect(page.getByTestId('subject-table-cell-username').first()).toHaveText(username); }); - // The set is serialized by the renderer, stored by the api, and revived by the web client, so - // these are the only tests that cross every hop a z.set() field takes before it is shown. - test.describe('z.set() fields', () => { - test.beforeEach(async ({ getPageModel, page, uniqueId }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`SetField${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await instrumentPage.completeHappinessQuestionnaireDissatisfied(['Money', 'Friends']); - await instrumentPage.submit(); - - await expect(instrumentPage.summaryHeading).toBeVisible(); - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - - await page.getByRole('combobox').first().click(); - await page.getByRole('option', { name: INSTRUMENT_TITLE }).click(); - }); - - test('should show the members of a z.set() field in the subject table rather than [object Set]', async ({ - page - }) => { - await expect(page.getByTestId('subject-table-cell-causesOfDissatisfaction').first()).toHaveText('MONEY, FRIENDS'); - }); - - test('should export a z.set() field as the subject table displays it rather than {}', async ({ page }) => { - await expect(page.getByTestId('data-table-body').getByTestId('data-table-row').first()).toBeVisible(); + // The set is serialized by the renderer, stored by the api, and revived by the web client, so this + // is the only test that crosses every hop a z.set() field takes before it is shown. + test('should show the members of a z.set() field in the subject table rather than [object Set]', async ({ + getPageModel, + page, + uniqueId + }) => { + const instrumentPage = await beginInstrument(getPageModel, page, `SetField${uniqueId}`, INSTRUMENT_TITLE); + await instrumentPage.completeHappinessQuestionnaireDissatisfied(['Money', 'Friends']); + await instrumentPage.submit(); - const downloadPromise = page.waitForEvent('download'); - await page.getByRole('button', { name: 'Download' }).click(); - await page.getByRole('menuitem', { exact: true, name: 'CSV' }).click(); - const download = await downloadPromise; + await expect(instrumentPage.summaryHeading).toBeVisible(); - const csv = (await readFile(await download.path())).toString(); - expect(csv).toContain('"MONEY, FRIENDS"'); - }); + await openSubjectRecords(page, INSTRUMENT_TITLE); + await expect(page.getByTestId('subject-table-cell-causesOfDissatisfaction').first()).toHaveText('MONEY, FRIENDS'); }); - test('should localize required-field errors from a zod v4 validation schema', async ({ + test('should report validation errors localized and under their own fields, focusing the first', async ({ getPageModel, page, uniqueId }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Validation${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); + const instrumentPage = await beginInstrument(getPageModel, page, `Validation${uniqueId}`, INSTRUMENT_TITLE); + await expect(instrumentPage.errorSummary).toHaveCount(0); // The happiness questionnaire is authored against /runtime/v1/zod@3.x/v4, so submitting with // nothing filled in exercises the v4 error map that apps/web/src/services/zod.ts registers on @@ -138,320 +110,78 @@ test.describe('instrument completion', () => { // ("Invalid input: ...") renders instead of the localized one. await instrumentPage.submit(); - const errorMessages = page.getByTestId('error-message-text'); - await expect(errorMessages.first()).toBeVisible(); - for (const message of await errorMessages.allTextContents()) { + await expect(instrumentPage.errorMessages.first()).toBeVisible(); + for (const message of await instrumentPage.errorMessages.allTextContents()) { expect(message).toBe('This field is required'); } - }); + await expect(instrumentPage.errorSummary).toBeVisible(); + // The first invalid field is a slider, whose focusable element is a Radix thumb rather than an + // input, which only a real browser lays out and focuses. + await expect(instrumentPage.firstInvalidField).toBeInViewport(); + await expect(instrumentPage.firstInvalidField.locator(':focus')).toHaveCount(1); - test('should administer a second instrument with a different mix of question types and surface its record', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Demographics${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page - .locator('[data-testid^="instrument-card-"]') - .filter({ hasText: DEMOGRAPHICS_INSTRUMENT_TITLE }) - .first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await instrumentPage.completeEnhancedDemographicsQuestionnaire(); + await instrumentPage.answerHappinessQuestionnaireDissatisfiedWithoutReason(); await instrumentPage.submit(); - await expect(instrumentPage.summaryHeading).toBeVisible(); - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - - await page.getByRole('combobox').first().click(); - await page.getByRole('option', { name: DEMOGRAPHICS_INSTRUMENT_TITLE }).click(); - - await expect(page.getByTestId('data-table-body').getByTestId('data-table-row').first()).toBeVisible(); - }); - - // Records collected in person carry no assignmentId, and MongoDB indexes a missing field as null, - // so a unique index on that field would reject the second record here as a duplicate key. See - // DATABASE_INDEXES in the api's src/core/prisma.ts. - test('should administer the same instrument twice for one subject, neither record having an assignment', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Repeat${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - for (const _ of [1, 2]) { - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await instrumentPage.completeHappinessQuestionnaire(); - await instrumentPage.submit(); - await expect(instrumentPage.summaryHeading).toBeVisible(); - } - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - - await page.getByRole('combobox').first().click(); - await page.getByRole('option', { name: INSTRUMENT_TITLE }).click(); - - await expect(page.getByTestId('data-table-body').getByTestId('data-table-row')).toHaveCount(2); - }); - - // Clinicians switch between the app and other programs during a visit. The bundle behind an - // instrument id never changes, and a refetch that failed — an expired token, say — would hand the - // open instrument, and everything entered into it, to the route error boundary. - test('should not request the bundle again after the tab is hidden and shown again', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Focus${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await expect(instrumentPage.submitButton).toBeVisible(); - - const bundleRequests: string[] = []; - page.on('request', (request) => { - if (request.url().includes('/v1/instruments/bundle/')) { - bundleRequests.push(request.url()); - } - }); - - // Headless Chromium keeps the tab visible, so the state is overridden and the event dispatched by - // hand. React Query's focusManager listens for visibilitychange on the window, which the event - // reaches from the document. - const setTabVisibility = (visibilityState: 'hidden' | 'visible') => { - return page.evaluate((state) => { - Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => state }); - document.dispatchEvent(new Event('visibilitychange', { bubbles: true })); - }, visibilityState); - }; - - await setTabVisibility('hidden'); - await setTabVisibility('visible'); - await page.evaluate(() => { - return new Promise((resolve) => { - requestAnimationFrame(() => { - requestAnimationFrame(resolve); - }); - }); - }); - - expect(bundleRequests).toEqual([]); - await expect(instrumentPage.submitButton).toBeVisible(); + // An issue without a path is rendered above the Submit button instead of under any field, so the + // single error must sit inside the reason field's group. + const reasonFieldGroup = page.locator('[data-field-group="reasonNotSatisfied"]'); + await expect(reasonFieldGroup.getByTestId('error-message-text')).toHaveText( + 'This field is required / Ce champ est obligatoire' + ); + await expect(instrumentPage.errorMessages).toHaveCount(1); + await expect(instrumentPage.summaryHeading).toBeHidden(); }); - test('should show a validation error for a postal code that does not match the expected format', async ({ + test('should refuse a malformed postal code, then store a lowercase one in uppercase alongside an age at immigration of 0', async ({ getPageModel, page, uniqueId }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Postal${uniqueId}`, `Subject${uniqueId}`, 'Male'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page - .locator('[data-testid^="instrument-card-"]') - .filter({ hasText: DEMOGRAPHICS_INSTRUMENT_TITLE }) - .first(); - await expect(card).toBeVisible(); - await card.click(); + const instrumentPage = await beginInstrument( + getPageModel, + page, + `Demographics${uniqueId}`, + DEMOGRAPHICS_INSTRUMENT_TITLE + ); + const postalCode = page.locator('[name="postalCode"]'); - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); // Every field on this instrument is optional, so an out-of-format postal code is the only value - // filled in, isolating the format error from any required-field error. - await instrumentPage.$ref.locator('[name="postalCode"]').fill('invalid'); + // filled in, isolating the format error from any required-field error. The instrument is + // authored against zod v3, whose own message for a failed regex is the bare word "Invalid": + // asserting the text is what proves the runtime-served v3 instance carries the shared error map, + // and that the map does not echo the pattern back at the clinician. + await postalCode.fill('invalid'); await instrumentPage.submit(); - - // This instrument is authored against zod v3, whose own message for a failed regex is the bare - // word "Invalid". Asserting the text is what proves the runtime-served v3 instance carries the - // shared error map, and that the map does not echo the pattern back at the clinician. - await expect(instrumentPage.errorMessages).toBeVisible(); await expect(instrumentPage.errorMessages).toHaveText('Does not match the expected format'); - }); - test('should accept a lowercase postal code and an age at immigration of 0, storing the postal code in uppercase', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Lowercase${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page - .locator('[data-testid^="instrument-card-"]') - .filter({ hasText: DEMOGRAPHICS_INSTRUMENT_TITLE }) - .first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await instrumentPage.$ref.locator('[name="postalCode"]').fill('h4h 1r3'); - await instrumentPage.$ref.locator('[name="ageAtImmigration"]').fill('0'); + await instrumentPage.completeEnhancedDemographicsQuestionnaire(); + await postalCode.fill('h4h 1r3'); + await page.locator('[name="ageAtImmigration"]').fill('0'); await instrumentPage.submit(); await expect(instrumentPage.summaryHeading).toBeVisible(); - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - - await page.getByRole('combobox').first().click(); - await page.getByRole('option', { name: DEMOGRAPHICS_INSTRUMENT_TITLE }).click(); - + await openSubjectRecords(page, DEMOGRAPHICS_INSTRUMENT_TITLE); await expect(page.getByTestId('subject-table-cell-postalCode').first()).toHaveText('H4H 1R3'); await expect(page.getByTestId('subject-table-cell-ageAtImmigration').first()).toHaveText('0'); }); - test('should label the marital status and age-at-immigration questions correctly in English', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Labels${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page - .locator('[data-testid^="instrument-card-"]') - .filter({ hasText: DEMOGRAPHICS_INSTRUMENT_TITLE }) - .first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - - await expect(instrumentPage.$ref.getByText('Marital Status', { exact: true })).toBeVisible(); - await expect(instrumentPage.$ref.getByText('Age at Immigration (if applicable)', { exact: true })).toBeVisible(); - }); - - test('should show the missing-reason error under the reason field when a dissatisfied subject leaves it empty', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Reason${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await instrumentPage.answerHappinessQuestionnaireDissatisfiedWithoutReason(); - await instrumentPage.submit(); - - // An issue without a path is rendered above the Submit button instead of under any field, so the - // single error must sit inside the reason field's group. - const reasonFieldGroup = page.locator('[data-field-group="reasonNotSatisfied"]'); - await expect(reasonFieldGroup.getByTestId('error-message-text')).toHaveText( - 'This field is required / Ce champ est obligatoire' - ); - await expect(instrumentPage.errorMessages).toHaveCount(1); - await expect(instrumentPage.summaryHeading).toBeHidden(); - }); -}); - -test.describe('file instrument completion', () => { - test('should report a selection over the file limit as too many files, keeping the earlier selection', async ({ - getPageModel, - page, + // Records collected in person carry no assignmentId, and MongoDB indexes a missing field as null, + // so a unique index on that field would reject the second record here as a duplicate key. See + // DATABASE_INDEXES in the api's src/core/prisma.ts. + test('should store the same instrument twice in one session for one subject, neither record having an assignment', async ({ + api, uniqueId }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`FileLimit${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: 'Arbitrary File Upload' }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - - const dropzone = page.getByTestId('dropzone'); - const fileInput = dropzone.locator('input[type="file"]'); - const createUpload = (name: string) => ({ buffer: Buffer.from('content'), mimeType: 'text/plain', name }); + const group = await api.createGroup({ name: `Repeat ${uniqueId}` }); + const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); + const session = await api.createSession(group.id, { id: `repeat-${uniqueId}` }); - await fileInput.setInputFiles(createUpload('first.txt')); - await expect(dropzone).toContainText('first.txt'); + const first = await api.createRecord(group.id, instrumentId, session, HAPPINESS_RECORD); + const second = await api.createRecord(group.id, instrumentId, session, HAPPINESS_RECORD); - await fileInput.setInputFiles([createUpload('a.txt'), createUpload('b.txt')]); - await expect(page.getByText('Only 1 file can be uploaded here; you selected 2')).toBeVisible(); - await expect(page.getByText('Unsupported file type')).toBeHidden(); - await expect(dropzone).toContainText('first.txt'); + expect(second.id).not.toBe(first.id); + expect([first.assignmentId ?? null, second.assignmentId ?? null]).toEqual([null, null]); }); }); diff --git a/testing/src/specs/instrument-hub.spec.ts b/testing/src/specs/instrument-hub.spec.ts index 810e105b0..cc33fb45b 100644 --- a/testing/src/specs/instrument-hub.spec.ts +++ b/testing/src/specs/instrument-hub.spec.ts @@ -9,7 +9,7 @@ import { expect, test } from '../support/fixtures'; const HAPPINESS = 'DNP_HAPPINESS_QUESTIONNAIRE'; test.describe('instrument hub', () => { - test('should list an instrument with the records and subjects collected for it @smoke', async ({ + test('should list an instrument with the records and subjects collected for it', async ({ api, isolatedGroupManager, page, @@ -30,10 +30,10 @@ test.describe('instrument hub', () => { const hubPage = new InstrumentHubPage(page); await hubPage.goto('/datahub/instruments'); - const row = hubPage.row('Happiness Questionnaire').first(); - await expect(row).toBeVisible(); - await expect(row).toContainText('3'); - await expect(row).toContainText('2'); + // Cells in column order after the pinned title: subjects, then records. + const cells = hubPage.row('Happiness Questionnaire').first().locator('> div'); + await expect(cells.nth(1)).toHaveText('2'); + await expect(cells.nth(2)).toHaveText('3'); }); // A series' own counts are what it orchestrated, not its members' global totals, so the row is @@ -67,28 +67,10 @@ test.describe('instrument hub', () => { await expect(page.getByTestId('instrument-hub-series-members')).toContainText('Happiness Questionnaire'); }); - // Sorting is the only way to find an instrument in a long catalog without searching for it, so - // the header affordance has to actually reorder the rows rather than merely render a chevron. - test('should reorder the list when a column header is clicked', async ({ isolatedGroupManager, page }) => { - await isolatedGroupManager(); - const hubPage = new InstrumentHubPage(page); - await hubPage.goto('/datahub/instruments'); - await expect(hubPage.rows.first()).toBeVisible(); - - await hubPage.sortBy('Instrument'); - const ascending = await hubPage.titles(); - expect(ascending.length).toBeGreaterThan(1); - // Compared case-insensitively because that is what tanstack's `text` sorting function does; - // asserting against `localeCompare` would flake on any mixed-case pair. - const caseInsensitive = [...ascending].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); - expect(ascending).toStrictEqual(caseInsensitive); - - // Sort removal is disabled on these tables, so a second click is descending rather than unsorted. - await hubPage.sortBy('Instrument'); - expect(await hubPage.titles()).toStrictEqual([...ascending].reverse()); - }); - - test("should show every subject's records for one instrument, with how each was collected", async ({ + // The table, its export and the graph tab are one set of records narrowed by one filter. The filter + // lives in the URL, and only a real router proves it survives the tab change rather than resetting + // with the newly mounted tab's own state. + test('should narrow the records table, its export and its graph by the collection method filter alike', async ({ api, isolatedGroupManager, page, @@ -97,11 +79,15 @@ test.describe('instrument hub', () => { const group = await isolatedGroupManager(); const instrumentId = await api.findInstrumentIdByName(HAPPINESS); // One record per collection method, so the column has to vary rather than printing a constant. - await api.uploadRecords(group.id, instrumentId, [ - { data: HAPPINESS_RECORD, date: new Date(), subjectId: `method-${uniqueId}-retro` } + const retrospective = `method-${uniqueId}-retro`; + await Promise.all([ + api.uploadRecords(group.id, instrumentId, [ + { data: HAPPINESS_RECORD, date: new Date(), subjectId: retrospective } + ]), + api.createSession(group.id, { id: `method-${uniqueId}-inperson` }, 'IN_PERSON').then((session) => { + return api.createRecord(group.id, instrumentId, session, HAPPINESS_RECORD); + }) ]); - const session = await api.createSession(group.id, { id: `method-${uniqueId}-inperson` }, 'IN_PERSON'); - await api.createRecord(group.id, instrumentId, session, HAPPINESS_RECORD); const hubPage = new InstrumentHubPage(page); await hubPage.goto('/datahub/instruments'); @@ -114,31 +100,6 @@ test.describe('instrument hub', () => { // Neither record came through a series, so both series cells name the absence. await expect(tablePage.seriesCells).toHaveCount(2); await expect(tablePage.seriesCells.first()).toHaveText('None'); - }); - - // The download is built from the rows the table holds, so a filter has to narrow both. Getting - // this wrong exports rows the user believed they had excluded. - test('should export only the records left by the collection method filter, with the metadata columns', async ({ - api, - isolatedGroupManager, - page, - uniqueId - }) => { - const group = await isolatedGroupManager(); - const instrumentId = await api.findInstrumentIdByName(HAPPINESS); - const retrospective = `export-${uniqueId}-retro`; - await api.uploadRecords(group.id, instrumentId, [ - { data: HAPPINESS_RECORD, date: new Date(), subjectId: retrospective } - ]); - const session = await api.createSession(group.id, { id: `export-${uniqueId}-inperson` }, 'IN_PERSON'); - await api.createRecord(group.id, instrumentId, session, HAPPINESS_RECORD); - - const hubPage = new InstrumentHubPage(page); - await hubPage.goto('/datahub/instruments'); - await hubPage.open('Happiness Questionnaire'); - - const tablePage = new InstrumentHubTablePage(page); - await expect(tablePage.rows).toHaveCount(2); await tablePage.excludeCollectionMethod('IN_PERSON'); await expect(tablePage.rows).toHaveCount(1); @@ -149,73 +110,22 @@ test.describe('instrument hub', () => { SeriesID: null | string; SubjectID: string; }[]; - expect(payload).toHaveLength(1); // Exports carry the stored value, not the translated label, so analysis keys on something that // does not change with the reader's language. expect(payload[0]!.CollectionMethod).toBe('RETROSPECTIVE'); expect(payload[0]!.SeriesID).toBeNull(); expect(payload[0]!.SubjectID).toBe(retrospective); - }); - - test('should plot the records of the instrument on its graph tab', async ({ - api, - isolatedGroupManager, - page, - uniqueId - }) => { - const group = await isolatedGroupManager(); - const instrumentId = await api.findInstrumentIdByName(HAPPINESS); - await api.uploadRecords( - group.id, - instrumentId, - ['a', 'b'].map((suffix) => ({ - data: HAPPINESS_RECORD, - date: new Date(), - subjectId: `graph-${uniqueId}-${suffix}` - })) - ); - - const hubPage = new InstrumentHubPage(page); - await hubPage.goto('/datahub/instruments'); - await hubPage.open('Happiness Questionnaire'); // Reached by clicking the tab rather than through `getPageModel`, whose exact-URL assertion the // route's own search-param defaults would fail. await page.getByTestId('instrument-hub-graph-tab').click(); + await expect(page).toHaveURL(/methods=/); const graphPage = new InstrumentHubGraphPage(page); await expect(graphPage.chart).toBeVisible(); - await expect(graphPage.scatterMarks).toHaveCount(2); + await expect(graphPage.scatterMarks).toHaveCount(1); await graphPage.selectChart('Distribution'); await expect(graphPage.bars.first()).toBeVisible(); }); - - test('should keep the filters when switching between the table and graph tabs, so both describe one set', async ({ - api, - isolatedGroupManager, - page, - uniqueId - }) => { - const group = await isolatedGroupManager(); - const instrumentId = await api.findInstrumentIdByName(HAPPINESS); - await api.uploadRecords(group.id, instrumentId, [ - { data: HAPPINESS_RECORD, date: new Date(), subjectId: `shared-${uniqueId}` } - ]); - - const hubPage = new InstrumentHubPage(page); - await hubPage.goto('/datahub/instruments'); - await hubPage.open('Happiness Questionnaire'); - - const tablePage = new InstrumentHubTablePage(page); - await expect(tablePage.rows).toHaveCount(1); - await tablePage.excludeCollectionMethod('RETROSPECTIVE'); - await expect(page.getByTestId('data-table-empty-state')).toBeVisible(); - - // The filter lives in the URL, so it survives the tab change rather than resetting with the - // newly mounted tab's own state. - await page.getByTestId('instrument-hub-graph-tab').click(); - await expect(page).toHaveURL(/methods=/); - await expect(page.getByTestId('instrument-hub-chart')).toContainText('No records match'); - }); }); diff --git a/testing/src/specs/instrument-navigation-block.spec.ts b/testing/src/specs/instrument-navigation-block.spec.ts index f425a09f9..9afbd5116 100644 --- a/testing/src/specs/instrument-navigation-block.spec.ts +++ b/testing/src/specs/instrument-navigation-block.spec.ts @@ -4,7 +4,11 @@ import { expect, test } from '../support/fixtures'; const INSTRUMENT_TITLE = 'Happiness Questionnaire'; test.describe('leaving a started instrument', () => { - test.beforeEach(async ({ getPageModel, page, uniqueId }) => { + test('should keep the clinician on the instrument, answers intact, until they confirm leaving', async ({ + getPageModel, + page, + uniqueId + }) => { const startSessionPage = await getPageModel('/session/start-session'); await startSessionPage.sessionForm.waitFor({ state: 'visible' }); await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); @@ -20,11 +24,8 @@ test.describe('leaving a started instrument', () => { await expect(card).toBeVisible(); await card.click(); - await new RenderInstrumentPage(page).begin(); - }); - - test('should keep the clinician on the instrument, answers intact, when they decline to leave', async ({ page }) => { const instrumentPage = new RenderInstrumentPage(page); + await instrumentPage.begin(); await instrumentPage.happinessSatisfiedRadio.click(); await page.getByTestId('nav-button-/dashboard').click(); @@ -34,10 +35,6 @@ test.describe('leaving a started instrument', () => { await expect(instrumentPage.leaveDialog).toBeHidden(); await expect(page).toHaveURL(/\/instruments\/render\//); await expect(instrumentPage.happinessSatisfiedRadio).toBeChecked(); - }); - - test('should leave the instrument once the clinician confirms', async ({ page }) => { - const instrumentPage = new RenderInstrumentPage(page); await page.getByTestId('nav-button-/dashboard').click(); await expect(instrumentPage.leaveDialog).toBeVisible(); @@ -45,29 +42,4 @@ test.describe('leaving a started instrument', () => { await page.waitForURL('**/dashboard'); }); - - test('should not warn about leaving once the responses have been submitted', async ({ page }) => { - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.completeHappinessQuestionnaire(); - await instrumentPage.submit(); - await expect(instrumentPage.summaryHeading).toBeVisible(); - - await page.getByTestId('nav-button-/dashboard').click(); - - await page.waitForURL('**/dashboard'); - await expect(instrumentPage.leaveDialog).toBeHidden(); - }); - - // Ending a session is confirmed by its own dialog, and an instrument cannot be submitted once the - // session is gone, so a second warning would protect nothing. - test('should end the session without also warning about leaving the instrument', async ({ page }) => { - const instrumentPage = new RenderInstrumentPage(page); - - await page.getByTestId('nav-button-#').click(); - await page.getByRole('button', { name: 'Yes' }).click(); - - await expect(page).not.toHaveURL(/\/instruments\/render\//); - await expect(instrumentPage.leaveDialog).toBeHidden(); - await expect(page.getByTestId('current-session-info')).toBeHidden(); - }); }); diff --git a/testing/src/specs/list-return.spec.ts b/testing/src/specs/list-return.spec.ts index b72abc22e..39b1f4807 100644 --- a/testing/src/specs/list-return.spec.ts +++ b/testing/src/specs/list-return.spec.ts @@ -44,30 +44,15 @@ test.describe('returning to a list', () => { await expect(firstRow).toHaveText(firstRowOnSecondPage); }); - test('should come back to the page of permission sets it left', async ({ api, authenticateAs, page, uniqueId }) => { - // The list is ordered by name, so a leading '!' keeps these eleven ahead of the sets other specs - // create meanwhile, and the second page starts on the eleventh of them whatever else is added. - await Promise.all( - Array.from({ length: 11 }, (_, index) => { - const name = `!List Return ${uniqueId} ${String(index).padStart(2, '0')}`; - return api.createPermissionSet([{ action: 'read', scope: 'HOLDER_GROUPS', subject: 'User' }], { name }); - }) - ); - await authenticateAs('ADMIN'); - await page.goto('/admin/permission-sets'); - const { firstRow, firstRowOnSecondPage } = await returnFromSecondPage(page, { - returnTestId: 'admin-permission-set-back' - }); - await expect(firstRow).toHaveText(firstRowOnSecondPage); - }); - test('should come back to the page of subjects it left', async ({ api, isolatedGroupManager, page, uniqueId }) => { const group = await isolatedGroupManager(); // The list shows only the first nine characters of an id, so these differ within them; ids from // createSubject all start `subject_`, and two pages of them can read alike. - for (let index = 0; index < 12; index++) { - await api.createSession(group.id, { id: `${String(index).padStart(2, '0')}-${uniqueId}` }); - } + await Promise.all( + Array.from({ length: 12 }, (_, index) => { + return api.createSession(group.id, { id: `${String(index).padStart(2, '0')}-${uniqueId}` }); + }) + ); await page.goto('/datahub/subjects'); const { firstRow, firstRowOnSecondPage } = await returnFromSecondPage(page, { returnTestId: 'subject-hub-back' }); await expect(firstRow).toHaveText(firstRowOnSecondPage); diff --git a/testing/src/specs/mail.spec.ts b/testing/src/specs/mail.spec.ts index d336e032f..19947c096 100644 --- a/testing/src/specs/mail.spec.ts +++ b/testing/src/specs/mail.spec.ts @@ -1,13 +1,5 @@ -import type { Language } from '@opendatacapture/schemas/core'; - -import { RemoteAssignmentsPage } from '../pages/_app/group/remote-assignments.page'; -import { RemoteAssignmentPage } from '../pages/_app/session/remote-assignment.page'; -import { E2E_MAIL_CONFIG, SEEDED_USER_PASSWORD } from '../support/constants'; import { expect, test } from '../support/fixtures'; -/** How a language picker labels each language with the interface in English. */ -const LANGUAGE_NAMES: { [L in Language]: string } = { en: 'English', es: 'Spanish', fr: 'French' }; - // Whether mail is on is a property of the instance, not of a test: it lives on `SetupState` and // `isMailEnabled` gates UI across the whole app. Two spec files touching it would be scheduled into // separate workers by `fullyParallel`, so one would flip the flag under the other — which is why @@ -29,68 +21,6 @@ test.describe('mail settings', () => { await api.setMailEnabled(false); }); - test('should hide the server configuration until email is enabled', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - - await expect(mailPage.enableToggle).toBeVisible(); - await expect(mailPage.serverCard).toBeHidden(); - - await mailPage.enableMail(); - await expect(mailPage.host).toBeVisible(); - }); - - // Cleared explicitly rather than assuming an unconfigured instance: other specs in this suite - // leave a stored configuration behind, and the assertion is about the required fields. - test('should reject an incomplete configuration without saving it', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - await mailPage.enableMail(); - await mailPage.host.fill(''); - await mailPage.username.fill(''); - - await mailPage.saveConfig.click(); - - await expect(mailPage.fieldError('host')).toBeVisible(); - await expect(mailPage.fieldError('username')).toBeVisible(); - - // "Without saving it" is only proven by the round trip: the stored host must survive. - await mailPage.$ref.reload(); - await mailPage.enableMail(); - await expect(mailPage.host).toHaveValue(E2E_MAIL_CONFIG.host); - }); - - // Pointing at a different server must not silently reuse the credential stored for the old one. - test('should require the password again when the server changes', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - await mailPage.enableMail(); - await mailPage.fillServerConfig({ - host: `smtp.somewhere-else.test`, - password: '', - port: '587', - senderAddress: 'noreply@example.org', - username: 'mailer' - }); - - await mailPage.saveConfig.click(); - - await expect(mailPage.fieldError('password')).toBeVisible(); - }); - - test('should reject a sender address that is not an email', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - await mailPage.enableMail(); - await mailPage.fillServerConfig({ - host: 'smtp.example.org', - password: 'hunter2', - port: '587', - senderAddress: 'not-an-email', - username: 'mailer' - }); - - await mailPage.saveConfig.click(); - - await expect(mailPage.fieldError('sender-address')).toBeVisible(); - }); - // The whole save path — validate, persist, re-seed from the response — while the secret // itself never round-trips back to the browser. test('should save a valid configuration without ever rendering the stored password', async ({ getPageModel }) => { @@ -117,27 +47,6 @@ test.describe('mail settings', () => { await expect(mailPage.password).not.toHaveValue('hunter2'); await expect(mailPage.$ref.locator('body')).not.toContainText('hunter2'); }); - - test('should not offer to send a test email until the recipient is a valid address', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - await mailPage.enableMail(); - - await expect(mailPage.sendTest).toBeDisabled(); - await mailPage.testRecipient.fill('nonsense'); - await expect(mailPage.sendTest).toBeDisabled(); - await mailPage.testRecipient.fill('recipient@example.org'); - await expect(mailPage.sendTest).toBeEnabled(); - }); - - test('should block saving a welcome template that drops the username placeholder', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - await mailPage.enableMail(); - await mailPage.templateCard.scrollIntoViewIfNeeded(); - - await mailPage.templateBody.fill('Welcome, but with no placeholders at all.'); - - await expect(mailPage.templateSave).toBeDisabled(); - }); }); test.describe('mail delivery', () => { @@ -152,34 +61,6 @@ test.describe('mail delivery', () => { test.describe('admin mail page', () => { test.use({ actingRole: 'ADMIN' }); - test('should show the saved configuration without ever returning the password', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - - await expect(mailPage.host).toHaveValue(E2E_MAIL_CONFIG.host); - await expect(mailPage.username).toHaveValue(E2E_MAIL_CONFIG.username); - // The stored password is real, so this is the assertion that it never reaches the browser. - await expect(mailPage.password).not.toHaveValue(E2E_MAIL_CONFIG.password); - await expect(mailPage.$ref.locator('body')).not.toContainText(E2E_MAIL_CONFIG.password); - }); - - // A subset rather than an exact list: `admin-settings.spec.ts` turns Spanish on and off in a - // worker of its own, so the enabled set is read at the same moment the picker is. Without the - // scoping the picker offers every language the application ships, whatever the instance offers. - test('should offer the welcome template in no language the instance has disabled', async ({ - api, - getPageModel - }) => { - const mailPage = await getPageModel('/admin/mail'); - const enabled = new Set((await api.findActiveLanguages()).map((code) => LANGUAGE_NAMES[code])); - - await mailPage.templateLanguage.click(); - await expect(mailPage.openLanguageOptions.first()).toBeVisible(); - const offered = await mailPage.openLanguageOptions.allTextContents(); - - expect(offered.length).toBeGreaterThan(0); - expect(offered.filter((name) => !enabled.has(name))).toStrictEqual([]); - }); - test('should report a failed connection test in the reader’s language, not raw SMTP prose', async ({ getPageModel }) => { @@ -193,154 +74,9 @@ test.describe('mail delivery', () => { await expect(notification.first()).toBeVisible({ timeout: 40_000 }); await expect(mailPage.$ref.locator('body')).not.toContainText('ENOTFOUND'); }); - - // Distinct from the connection test: this is the one path that calls `sendMail`, so the send - // button has to be driven all the way to an outcome, not merely checked enabled. - test('should report a failed test email in the reader’s language, not raw SMTP prose', async ({ getPageModel }) => { - const mailPage = await getPageModel('/admin/mail'); - - await mailPage.testRecipient.fill('recipient@example.org'); - await mailPage.sendTest.click(); - - const notification = mailPage.$ref.getByText(/could not be found|check and reconfigure/i); - await expect(notification.first()).toBeVisible({ timeout: 40_000 }); - await expect(mailPage.$ref.locator('body')).not.toContainText('ENOTFOUND'); - }); - }); - - test.describe('welcome email', () => { - // Delivery fails against the unroutable host, so the app must surface the rendered message - // for manual sending rather than silently losing the one copy of it. - test('should offer the rendered welcome message for manual delivery when sending fails', async ({ - authenticateAs, - page, - uniqueId - }) => { - const username = `welcome_${uniqueId}`; - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - const createUserForm = page.getByTestId('create-user-form'); - await createUserForm.getByLabel('Username').fill(username); - await createUserForm.getByLabel('Password', { exact: true }).fill(SEEDED_USER_PASSWORD); - await createUserForm.getByLabel('Confirm Password').fill(SEEDED_USER_PASSWORD); - await createUserForm.getByLabel('First Name').fill('Welcome'); - await createUserForm.getByLabel('Last Name').fill('Probe'); - await createUserForm.getByLabel('Email').fill(`${username}@example.org`); - // ADMIN so the form does not also demand a group. - await createUserForm.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-ADMIN').click(); - await createUserForm.getByRole('button', { name: 'Submit' }).click(); - - const fallback = page.getByTestId('welcome-email-fallback'); - await expect(fallback).toBeVisible({ timeout: 40_000 }); - // The message is the rendered template, so the admin can hand it over as-is. - await expect(fallback).toContainText(username); - await fallback.getByRole('button', { name: 'Done' }).click(); - await page.waitForURL('**/admin/users'); - }); - }); - - test.describe('group manager navigation', () => { - // The nav item is gated on `isMailEnabled`, so this is the only place it can be asserted. - test('should offer the email templates page once mail is on', async ({ getPageModel, page }) => { - const dashboardPage = await getPageModel('/dashboard'); - // Bulk remote assignments is enabled by default, nesting group links under "Group Actions" - await dashboardPage.expandNavGroup('Group Actions'); - await expect(page.getByTestId('nav-button-/group/email-templates')).toBeVisible(); - }); - }); - - // The whole bulk email path, which exists only while mail is on: addresses arrive by uploaded - // file, one message per subject carries every link, and a failure stays retryable. - test.describe('bulk assignment email', () => { - test('should carry uploaded addresses into the batch and keep a failed send retryable', async ({ - api, - isolatedGroupManager, - page - }) => { - // Each send waits out the server's whole SMTP failure budget against an unroutable host. - test.setTimeout(180_000); - const group = await isolatedGroupManager(); - const subjectId = await api.createSubject(group.id); - await page.goto('/group/remote-assignments?mode=create'); - const wizardPage = new RemoteAssignmentsPage(page); - await expect(wizardPage.wizard).toBeVisible(); - - await wizardPage.selectAllSubjects.click(); - await wizardPage.emailListToggle.click(); - await wizardPage.uploadEmailList({ - buffer: Buffer.from(`subject_id,email\n${subjectId},participant@example.org\n`), - mimeType: 'text/csv', - name: 'emails.csv' - }); - await wizardPage.emailListApply.click(); - await expect(wizardPage.emailListMatched).toContainText('1'); - - await wizardPage.useSelectedSubjects.click(); - await expect(wizardPage.timepointsStep).toBeVisible(); - await wizardPage.addFirstInstrument(); - await expect(wizardPage.submitBatch).toBeEnabled({ timeout: 30_000 }); - await wizardPage.submitBatch.click(); - - // The results table shows the address each link will go to, which is how the upload is proven - // to have reached the batch rather than just the panel. - await expect(wizardPage.emailTable).toBeVisible({ timeout: 30_000 }); - await expect(wizardPage.emailTable).toContainText('participant@example.org'); - await expect(wizardPage.emailAll).toHaveText('Email All (1)'); - - // The picker offers the instance's languages and no others: a template is authored in all - // three, and a language nobody has enabled cannot be sent in. - await wizardPage.emailAll.click(); - const languageSelect = page.getByTestId('bulk-email-language'); - await languageSelect.press('Enter'); - const activeLanguages = await api.findActiveLanguages(); - await expect(page.getByRole('option')).toHaveCount(activeLanguages.length); - await languageSelect.press('Escape'); - - await page.getByTestId('bulk-email-confirm').click(); - - // `smtp.invalid.test` never accepts the message, so the batch reports one failure - and the - // trigger offers that one recipient again rather than retiring with nobody written to. - await expect(wizardPage.emailAll).toHaveText('Retry Failed (1)', { timeout: 120_000 }); - await expect(wizardPage.emailAll).toBeEnabled(); - }); }); test.describe('assignment email', () => { - test('should offer the email form once mail is on, and report a delivery failure', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Mail${uniqueId}`, `Subject${uniqueId}`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); - await remoteAssignmentPage.clickFirstInstrumentCard(); - await remoteAssignmentPage.submitAssignmentForm(); - - // The form only renders when `isMailEnabled` is true, so its presence is the assertion that - // the saved configuration actually reached the client. - const emailForm = page.getByTestId('assignment-email-form'); - await expect(emailForm).toBeVisible(); - - await page.getByTestId('assignment-email').fill('participant@example.org'); - await page.getByTestId('assignment-email-submit').click(); - - const feedback = page.getByTestId('assignment-email-feedback'); - await expect(feedback).toBeVisible({ timeout: 40_000 }); - await expect(feedback).not.toContainText('ENOTFOUND'); - }); - // Every admin reads the audit log and can download it whole, so it records the domain an // assignment link went to, never the participant's address. test('should record only a masked recipient in the audit log', async ({ api, uniqueId }) => { diff --git a/testing/src/specs/password-reset.spec.ts b/testing/src/specs/password-reset.spec.ts index 608c9d3c6..085c7731e 100644 --- a/testing/src/specs/password-reset.spec.ts +++ b/testing/src/specs/password-reset.spec.ts @@ -1,116 +1,18 @@ import { ResetPasswordPage } from '../pages/auth/reset-password.page'; -import { ApiClient } from '../support/api-client'; import { expect, test } from '../support/fixtures'; /** Strong, not breached, and not the username -- so only the policy under test can reject it. */ const chosenPassword = (uniqueId: string) => `chosen-oyster-lantern-${uniqueId}`; -test.describe('generating a password', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should fill both password fields, so the admin never transcribes a passphrase', async ({ - authenticateAs, - page - }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - const passwordField = page.getByLabel('Password', { exact: true }); - await page.getByRole('button', { name: 'Generate Passphrase' }).click(); - - const generated = await passwordField.inputValue(); - expect(generated.split('-')).toHaveLength(5); - - // The field reveals what it generated, since a password nobody can read cannot be relayed. - await expect(passwordField).toHaveAttribute('type', 'text'); - // `setValue` fills only `password`; the confirmation is mirrored through the form's `subscribe`. - await expect(page.getByLabel('Confirm Password')).toHaveValue(generated); - }); - - test('should keep whatever else the admin already typed', async ({ authenticateAs, page, uniqueId }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - - const username = `user${uniqueId}`; - await page.getByLabel('Username').fill(username); - await page.getByRole('button', { name: 'Generate Passphrase' }).click(); - - await expect(page.getByLabel('Password', { exact: true })).not.toHaveValue(''); - await expect(page.getByLabel('Username')).toHaveValue(username); - }); -}); - test.describe('a user whose password was generated', () => { - test('should be sent to the reset page when they sign in @smoke', async ({ api, getPageModel, page }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id], mustResetPassword: true }); - - const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm(credentials); - - await expect(page).toHaveURL('/auth/reset-password'); - }); - - // `authenticateAs` rather than the login form: it injects the token through `page.addInitScript`, - // which re-runs on each hard navigation. Signing in through the form leaves the token in memory - // only, so `page.goto` would drop it and every route would redirect to login for the wrong reason. - test('should be kept there wherever they try to navigate', async ({ api, authenticateAs, page }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id], mustResetPassword: true }); - await authenticateAs(credentials); - - // Every in-app route hangs off `_app`, so the one guard there covers all of them. - for (const route of ['/dashboard', '/session/start-session', '/user']) { - await page.goto(route); - await expect(page).toHaveURL('/auth/reset-password'); - } - }); - - test('should be refused clinical data by the API, not only by the router', async ({ api, apiRequestContext }) => { - const group = await api.createGroup(); - const { credentials, user } = await api.createUser({ groupIds: [group.id], mustResetPassword: true }); - const headers = { Authorization: `Bearer ${await ApiClient.login(apiRequestContext, credentials)}` }; - - // The token such a user is issued carries no permission but to read themselves, so a client that - // simply ignores the redirect still reaches nothing. - for (const path of ['/subjects', '/sessions', '/groups', '/assignments']) { - const response = await apiRequestContext.get(`/api/v1${path}`, { headers }); - expect(response.status(), `GET ${path}`).toBe(403); - } - - // `read User` is granted, because the reset itself is gated on it. `@RouteAccess` only checks the - // subject type, so this route answers 200 -- what confines it is the rule's condition, applied by - // `accessibleQuery`, which reduces the list to the one user asking. - const users = await apiRequestContext.get('/api/v1/users', { headers }); - expect(users.status()).toBe(200); - expect(await users.json()).toMatchObject([{ id: user.id }]); - }); - - test('should refuse the password it was issued, so the reset cannot be a no-op', async ({ + // Signs in through the real form throughout: `authenticateAs` would re-inject the original token on + // the reload `logout()` performs, silently re-authenticating the page and hiding the sign-out. + test('should be sent to the reset page on sign-in, refused the issued password, and regain the app with a new one', async ({ api, - authenticateAs, + getPageModel, page, uniqueId }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id], mustResetPassword: true }); - await authenticateAs(credentials); - await page.goto('/auth/reset-password'); - - const resetPage = new ResetPasswordPage(page); - await resetPage.fillResetForm(credentials.password); - - await expect(page.getByText('Password must not be the same as your current password')).toBeVisible(); - await expect(page).toHaveURL('/auth/reset-password'); - - // The same page still accepts a genuinely new password. - await resetPage.fillResetForm(chosenPassword(uniqueId)); - await expect(resetPage.successMessage).toBeVisible(); - }); - - // Signs in through the real form throughout: `authenticateAs` would re-inject the original token on - // the reload `logout()` performs, silently re-authenticating the page and hiding the sign-out. - test('should regain the app after choosing a password @smoke', async ({ api, getPageModel, page, uniqueId }) => { const group = await api.createGroup(); const { credentials } = await api.createUser({ groupIds: [group.id], mustResetPassword: true }); const newPassword = chosenPassword(uniqueId); @@ -120,7 +22,12 @@ test.describe('a user whose password was generated', () => { await expect(page).toHaveURL('/auth/reset-password'); const resetPage = new ResetPasswordPage(page); + await resetPage.fillResetForm(credentials.password); + await expect(page.getByText('Password must not be the same as your current password')).toBeVisible(); + await expect(page).toHaveURL('/auth/reset-password'); + await resetPage.fillResetForm(newPassword); + await expect(resetPage.successMessage).toBeVisible(); await resetPage.signInButton.click(); await expect(page).toHaveURL('/auth/login'); diff --git a/testing/src/specs/permission-sets.spec.ts b/testing/src/specs/permission-sets.spec.ts index 2232c9219..a6a422c15 100644 --- a/testing/src/specs/permission-sets.spec.ts +++ b/testing/src/specs/permission-sets.spec.ts @@ -1,43 +1,16 @@ -import type { PermissionSetRule } from '@opendatacapture/schemas/permission-set'; -import type { APIRequestContext } from '@playwright/test'; - import { PermissionSetPage } from '../pages/_app/admin/permission-sets/$permissionSetId.page'; import { PermissionSetsPage } from '../pages/_app/admin/permission-sets/index.page'; -import { ApiClient } from '../support/api-client'; -import { SEEDED_USER_PASSWORD } from '../support/constants'; import { expect, test } from '../support/fixtures'; -const API = '/api/v1'; - -/** - * One rule per scope, each observable through a list route a standard user otherwise reads almost - * nothing of: users only in the holder's groups, groups in every group. - */ -const RULES: PermissionSetRule[] = [ - { action: 'read', scope: 'HOLDER_GROUPS', subject: 'User' }, - { action: 'read', scope: 'ALL_GROUPS', subject: 'Group' } -]; - -/** The users and groups a holder can read, with a token minted now, since permissions are frozen into it at login. */ -async function readAccess(request: APIRequestContext, credentials: { password: string; username: string }) { - const token = await ApiClient.login(request, credentials); - const readIds = async (path: 'groups' | 'users') => { - const response = await request.get(`${API}/${path}`, { headers: { Authorization: `Bearer ${token}` } }); - expect(response.ok()).toBe(true); - return ((await response.json()) as { id: string }[]).map(({ id }) => id); - }; - return { groupIds: await readIds('groups'), userIds: await readIds('users') }; -} - test.describe('permission sets', () => { test.use({ actingRole: 'ADMIN' }); - test("should create a permission set with a rule for the holder's groups and one for every group @smoke", async ({ + test("should create a permission set with a rule for the holder's groups and one for every group, clipping its long name in the list", async ({ getPageModel, page, uniqueId }) => { - const name = `Permission Set ${uniqueId}`; + const name = `Assistants de recherche cliniques des laboratoires partenaires du programme ${uniqueId}`; const createPage = await getPageModel('/admin/permission-sets/create'); await createPage.fillName(name); @@ -46,152 +19,9 @@ test.describe('permission sets', () => { await createPage.submit(); await expect(page).toHaveURL('/admin/permission-sets'); - await new PermissionSetsPage(page).chooseRowAction(name, 'Manage'); - const setPage = new PermissionSetPage(page); - await expect(setPage.ruleRows).toHaveCount(2); - await expect(setPage.ruleRows.nth(0).getByTestId('permission-set-rule-scope')).toHaveText("Holder's groups"); - await expect(setPage.ruleRows.nth(1).getByTestId('permission-set-rule-scope')).toHaveText('Every group'); - }); - - test('should link a permission set to a user created with one', async ({ api, authenticateAs, page, uniqueId }) => { - const group = await api.createGroup(); - const permissionSet = await api.createPermissionSet(RULES); - - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - await page.getByLabel('Username').fill(`holder${uniqueId}`); - await page.getByLabel('Password', { exact: true }).fill(SEEDED_USER_PASSWORD); - await page.getByLabel('Confirm Password').fill(SEEDED_USER_PASSWORD); - await page.getByLabel('First Name').fill('Set'); - await page.getByLabel('Last Name').fill('Holder'); - await page.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-STANDARD').click(); - await page.getByRole('checkbox', { name: group.name }).check(); - await page.getByTestId('permissionSetId-select-trigger').click(); - await page.getByTestId(`permissionSetId-select-item-${permissionSet.id}`).click(); - await page.getByRole('button', { name: 'Submit' }).click(); - - // Polled rather than awaited on a redirect: with mail enabled by a parallel spec, a welcome-email - // dialog may stand between the submit and the users list. - await expect.poll(async () => (await api.findPermissionSetById(permissionSet.id)).holderCount).toBe(1); - }); - - test("should confine a holder's-groups rule to the holder's groups, where an every-group rule reads every group", async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup({ name: `Holder Group ${uniqueId}` }); - const otherGroup = await api.createGroup({ name: `Other Group ${uniqueId}` }); - const { credentials, user } = await api.createUser({ basePermissionLevel: 'STANDARD', groupIds: [group.id] }); - const { user: teammate } = await api.createUser({ groupIds: [group.id] }); - const { user: outsider } = await api.createUser({ groupIds: [otherGroup.id] }); - - const before = await readAccess(apiRequestContext, credentials); - expect(before.userIds).not.toContain(teammate.id); - expect(before.groupIds).not.toContain(otherGroup.id); - - await api.setUserPermissionSet(user.id, (await api.createPermissionSet(RULES)).id); - - const after = await readAccess(apiRequestContext, credentials); - expect(after.userIds).toContain(teammate.id); - expect(after.userIds).not.toContain(outsider.id); - expect(after.groupIds).toEqual(expect.arrayContaining([group.id, otherGroup.id])); - }); - - test('should switch a user between a permission set and custom permissions from the user page', async ({ - api, - getPageModel - }) => { - const group = await api.createGroup(); - const { user } = await api.createUser({ basePermissionLevel: 'STANDARD', groupIds: [group.id] }); - await api.setUserPermissions(user.id, [{ action: 'read', groupId: null, subject: 'Session' }]); - const permissionSet = await api.createPermissionSet(RULES); - - const userPage = await getPageModel('/admin/users/$userId', { userId: user.id }); - await userPage.choosePermissionSource('permissionSet'); - await expect(userPage.discardCustomGrantsWarning).toBeVisible(); - await userPage.choosePermissionSet(permissionSet.id); - await expect(userPage.chosenPermissionSetRules).toHaveCount(RULES.length); - await userPage.saveProfile(); - - await expect - .poll(async () => api.findUserById(user.id)) - .toMatchObject({ - additionalPermissions: [], - permissionSetId: permissionSet.id - }); - - await userPage.$ref.reload(); - await userPage.choosePermissionSource('custom'); - await userPage.addPermission({ action: 'read', subject: 'Subject' }); - await userPage.saveProfile(); - - await expect - .poll(async () => api.findUserById(user.id)) - .toMatchObject({ - additionalPermissions: [{ action: 'read', groupId: group.id, subject: 'Subject' }], - permissionSetId: null - }); - }); - - test('should name the holder count before deleting a permission set, and leave its holders their base level', async ({ - api, - apiRequestContext, - getPageModel, - page - }) => { - const group = await api.createGroup(); - const permissionSet = await api.createPermissionSet(RULES); - const { credentials } = await api.createUser({ - basePermissionLevel: 'STANDARD', - groupIds: [group.id], - permissionSetId: permissionSet.id - }); - const { user: teammate } = await api.createUser({ groupIds: [group.id], permissionSetId: permissionSet.id }); - expect((await readAccess(apiRequestContext, credentials)).userIds).toContain(teammate.id); - - const setPage = await getPageModel('/admin/permission-sets/$permissionSetId', { - permissionSetId: permissionSet.id - }); - await setPage.deleteButton.click(); - await expect(setPage.destructiveActionDialog).toContainText('2 users hold this permission set'); - await setPage.confirmDelete(); - - await expect(page).toHaveURL('/admin/permission-sets'); - expect((await api.findUserById(teammate.id)).permissionSetId).toBeNull(); - expect((await readAccess(apiRequestContext, credentials)).userIds).not.toContain(teammate.id); - }); - - test('should delete a permission set from its row in the list, naming its holder count first', async ({ - api, - getPageModel - }) => { - const group = await api.createGroup(); - const permissionSet = await api.createPermissionSet(RULES); - const { user } = await api.createUser({ groupIds: [group.id], permissionSetId: permissionSet.id }); - - const listPage = await getPageModel('/admin/permission-sets'); - await listPage.chooseRowAction(permissionSet.name, 'Delete'); - await expect(listPage.destructiveActionDialog).toContainText('1 user holds this permission set'); - await listPage.destructiveActionDialog.getByRole('button', { name: 'Yes' }).click(); - - await expect(listPage.row(permissionSet.name)).toHaveCount(0); - expect((await api.findUserById(user.id)).permissionSetId).toBeNull(); - }); - - test('should clip a long permission set name to one line and name it in full on hover', async ({ - api, - getPageModel, - uniqueId - }) => { - const name = `Assistants de recherche cliniques des laboratoires partenaires du programme ${uniqueId}`; - await api.createPermissionSet(RULES, { name }); - - const listPage = await getPageModel('/admin/permission-sets'); + const listPage = new PermissionSetsPage(page); await listPage.search(name); const cell = listPage.nameCell(name); - await expect(cell).toHaveAttribute('title', name); const layout = await cell.evaluate((element) => ({ clipped: element.scrollWidth > element.clientWidth, @@ -199,23 +29,11 @@ test.describe('permission sets', () => { })); expect(layout.clipped).toBe(true); expect(layout.lines).toBe(1); - }); - test('should offer no permission set for an administrator', async ({ api, authenticateAs, getPageModel, page }) => { - await api.createPermissionSet(RULES); - - await authenticateAs('ADMIN'); - await page.goto('/admin/users/create'); - await page.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-STANDARD').click(); - await expect(page.getByTestId('permissionSetId-select-trigger')).toBeVisible(); - await page.getByTestId('basePermissionLevel-select-trigger').click(); - await page.getByTestId('basePermissionLevel-select-item-ADMIN').click(); - await expect(page.getByTestId('permissionSetId-select-trigger')).toHaveCount(0); - - const { user: admin } = await api.createUser({ basePermissionLevel: 'ADMIN' }); - const userPage = await getPageModel('/admin/users/$userId', { userId: admin.id }); - await expect(userPage.adminNotice).toBeVisible(); - await expect(userPage.permissionSourceTabs).toHaveCount(0); + await listPage.chooseRowAction(name, 'Manage'); + const setPage = new PermissionSetPage(page); + await expect(setPage.ruleRows).toHaveCount(2); + await expect(setPage.ruleRows.nth(0).getByTestId('permission-set-rule-scope')).toHaveText("Holder's groups"); + await expect(setPage.ruleRows.nth(1).getByTestId('permission-set-rule-scope')).toHaveText('Every group'); }); }); diff --git a/testing/src/specs/playground.spec.ts b/testing/src/specs/playground.spec.ts index 046a6bd03..68503ffbb 100644 --- a/testing/src/specs/playground.spec.ts +++ b/testing/src/specs/playground.spec.ts @@ -1,12 +1,16 @@ import { execFileSync } from 'node:child_process'; +import { readFileSync } from 'node:fs'; import * as path from 'node:path'; import { generatePlaygroundURL } from '@opendatacapture/playground-url'; +import type { Page } from '@playwright/test'; import { PlaygroundPage } from '../pages/playground/index.page'; import { playgroundURL } from '../support/env'; import { expect, test } from '../support/fixtures'; +const REPO_ROOT = path.resolve(import.meta.dirname, '../../..'); + /** * A form whose module scope records where it ran: `ran` on its own document, and `escaped` on the * editor's, if the editor's document is reachable at all. From a separate origin that access throws. @@ -37,68 +41,6 @@ export default defineInstrument({ `; } -/** - * A form that shows its measures by default and has two computed measures, one of them marked hidden, - * so the summary should list exactly the other one. - */ -function measureVisibilityInstrumentSource(title: string): string { - return ` -import { defineInstrument } from '/runtime/v1/@opendatacapture/runtime-core'; -import { z } from '/runtime/v1/zod@3.x'; - -export default defineInstrument({ - kind: 'FORM', - language: 'en', - tags: ['Measures'], - internal: { edition: 1, name: 'MEASURE_VISIBILITY' }, - clientDetails: { estimatedDuration: 1, instructions: ['Submit the form'] }, - content: { note: { kind: 'string', label: 'Note', variant: 'input' } }, - defaultMeasureVisibility: 'visible', - details: { description: 'Measure visibility', license: 'Apache-2.0', title: '${title}' }, - measures: { - hiddenMeasure: { kind: 'computed', label: 'Hidden Measure', value: () => 1, visibility: 'hidden' }, - shownMeasure: { kind: 'computed', label: 'Shown Measure', value: () => 2 } - }, - validationSchema: z.object({ note: z.string().optional() }) -}); -`; -} - -/** - * A form that asks for a pet's name only once the respondent says they have a pet. Both questions - * carry a labeled measure, so the toggle's measure is always in the summary and the name's only when - * the question was shown. - */ -function dynamicMeasureInstrumentSource(title: string): string { - return ` -import { defineInstrument } from '/runtime/v1/@opendatacapture/runtime-core'; -import { z } from '/runtime/v1/zod@3.x'; - -export default defineInstrument({ - kind: 'FORM', - language: 'en', - tags: ['Measures'], - internal: { edition: 1, name: 'DYNAMIC_MEASURE' }, - clientDetails: { estimatedDuration: 1, instructions: ['Submit the form'] }, - content: { - hasPet: { kind: 'boolean', label: 'Has Pet', variant: 'radio' }, - petName: { - kind: 'dynamic', - deps: ['hasPet'], - render: (data) => (data.hasPet ? { kind: 'string', label: 'Pet Name', variant: 'input' } : null) - } - }, - defaultMeasureVisibility: 'visible', - details: { description: 'Dynamic field measure', license: 'Apache-2.0', title: '${title}' }, - measures: { - hasPet: { kind: 'const', label: 'Has Pet Measure', ref: 'hasPet' }, - petName: { kind: 'const', label: 'Pet Name Measure', ref: 'petName' } - }, - validationSchema: z.object({ hasPet: z.boolean(), petName: z.string().optional() }) -}); -`; -} - /** A form whose title includes a value imported from a subpath of the scoped runtime-core package. */ function scopedSubpathInstrumentSource(title: string): string { return ` @@ -120,43 +62,6 @@ export default defineInstrument({ `; } -/** 1×1 PNG. The playground stores binary assets as base64 and decodes them before bundling. */ -const STIM_PNG_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='; - -/** - * An interactive instrument that paints an imported PNG. The extension stays upper case: that is - * the filename shape the playground bundles when it renders this preview. - */ -function uppercasePngInstrumentSource(title: string): string { - return ` -import { defineInstrument } from '/runtime/v1/@opendatacapture/runtime-core'; -import { z } from '/runtime/v1/zod@3.x'; - -import stim from './stim.PNG'; - -export default defineInstrument({ - kind: 'INTERACTIVE', - language: 'en', - tags: ['Stimulus'], - internal: { edition: 1, name: 'UPPERCASE_PNG' }, - clientDetails: { estimatedDuration: 1, instructions: ['Look at the image'] }, - content: { - render() { - const image = document.createElement('img'); - image.alt = 'Stimulus'; - image.src = stim; - image.width = 32; - image.height = 32; - document.body.appendChild(image); - } - }, - details: { description: 'Upper-case PNG import', license: 'Apache-2.0', title: '${title}' }, - measures: {}, - validationSchema: z.object({}) -}); -`; -} - /** * An SVG with a bare `%` and a `#` on a child. esbuild emits it as percent-encoded text * (`data:image/svg+xml,`) that escapes `#` but leaves the `%`, which only a lenient, @@ -194,62 +99,98 @@ export default defineInstrument({ `; } -// The first paint waits on the 13 MB esbuild download and the toolchain boot; the preview then -// compiles on a 2 s poll, so the whole chain is slower than the suite's default expect timeout. +const JSPSYCH_EXAMPLE_DIR = path.join( + REPO_ROOT, + 'apps/playground/src/instruments/examples/interactive/Interactive-With-JSPsych' +); + +/** + * The JSPsych example, with the waits its timeline spends in real time cut short: a random fixation + * of up to 2 s before each of its ten trials, and a 2 s gap after the instructions. Everything the + * task submits is left as it is. Throws if the example no longer contains what is replaced, so the + * test cannot silently fall back to the full-length timeline. + */ +function shortenedJsPsychSource(): string { + let source = readFileSync(path.join(JSPSYCH_EXAMPLE_DIR, 'index.ts'), 'utf-8'); + for (const [from, to] of [ + [ + 'sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)', + 'sampleWithoutReplacement([250], 1)' + ], + ['post_trial_gap: 2000', 'post_trial_gap: 0'] + ] as const) { + if (!source.includes(from)) { + throw new Error(`Expected the JSPsych example to contain '${from}'`); + } + source = source.replace(from, to); + } + return source; +} + +/** Runs `action`, which must make the editor raise an `alert`, and returns what the alert said. */ +async function readAlert(page: Page, action: () => Promise): Promise { + // The editor reports a submission with `alert`, which blocks the page until it is dismissed. + const message = new Promise((resolve) => { + page.once('dialog', (dialog) => { + resolve(dialog.message()); + void dialog.dismiss(); + }); + }); + await action(); + return message; +} + +// The viewer compiles once it sees the editor's files change, on a poll of `refreshInterval`, which +// defaults to 2 s. The suite sets it lower so that each new instrument is compiled sooner. +const PREVIEW_REFRESH_INTERVAL = 250; + +// The page boots on the 13 MB esbuild download and the toolchain, which together are slower than the +// suite's default expect timeout. const PREVIEW_TIMEOUT = 60_000; +/** The `details.title` of the examples picked from the instrument list, which each preview shows. */ +const EXAMPLE_TITLES = { + 'Interactive With Legacy Script': 'Interactive Instrument With Legacy Script', + 'Interactive With Static Assets': 'Interactive Instrument with Static Assets' +}; + test.describe('playground', () => { - // Every page here boots esbuild-wasm and Monaco. Four at once, under the rest of the suite, has - // starved a browser context of its start-up for longer than the test timeout. + // Every playground page boots esbuild-wasm and Monaco and takes about 1 GB, so the tests share one + // and load each instrument into it. A share link differs from the page's URL only in its + // fragment, which the editor reads on `hashchange`, so opening one does not reload the page. As + // the previous test's preview is still on screen until the next instrument compiles, each test + // waits for its own instrument's title before acting. test.describe.configure({ mode: 'default' }); test.slow(); - test('should find an instrument by its visible label in the selector', async ({ page }) => { - const playground = new PlaygroundPage(page); - await playground.goto(); - - await playground.searchInstruments('Form Reference'); - await expect(playground.instrumentOption('Form Reference')).toBeVisible(); - }); + let playground: PlaygroundPage; - test('should find nothing for a query only the hidden instrument ids match', async ({ page }) => { - const playground = new PlaygroundPage(page); + test.beforeAll(async ({ browser }) => { + test.setTimeout(PREVIEW_TIMEOUT * 2); + playground = new PlaygroundPage(await browser.newPage()); await playground.goto(); - - // Every v4 UUID carries a 4 in its version digit, and no built-in label contains one. - await playground.searchInstruments('4'); - await expect(page.getByText('No Instruments Found')).toBeVisible(); + await playground.setRefreshInterval(PREVIEW_REFRESH_INTERVAL); }); - test('should run the preview on an origin other than the editor and render the instrument there @smoke', async ({ - page - }) => { - const playground = new PlaygroundPage(page); - await playground.goto(); + test.afterAll(async () => { + await playground.$ref.close(); + }); + // This test reads the default instrument the page opened on, so it must run first. + test('should run the default instrument on a preview origin of its own, and hand its submission back to the editor', async () => { await expect(playground.previewFrame).toBeVisible({ timeout: PREVIEW_TIMEOUT }); const frameOrigin = new URL((await playground.previewFrame.getAttribute('src'))!).origin; - expect(frameOrigin).not.toBe(new URL(page.url()).origin); - - await expect(playground.preview.getByRole('button', { name: 'Begin' })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - }); + expect(frameOrigin).not.toBe(new URL(playground.$ref.url()).origin); - test('should read the preview origin from the server at load, and refuse one equal to the editor', async ({ - page - }) => { - await page.route('**/config.json', (route) => route.fulfill({ json: { previewOrigin: playgroundURL } })); - const playground = new PlaygroundPage(page); - await playground.goto(); - - await expect(page.getByRole('heading', { name: 'Preview Unavailable' })).toBeVisible({ - timeout: PREVIEW_TIMEOUT + await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); + const message = await readAlert(playground.$ref, () => { + return playground.preview.getByRole('button', { name: 'Submit' }).click(); }); - await expect(playground.previewFrame).toHaveCount(0); + expect(message).toContain('The following data will be submitted'); }); - test("should run a share link's code without letting it reach the editor page", async ({ page, uniqueId }) => { + test("should run a share link's code without letting it reach the editor page", async ({ uniqueId }) => { const title = `Probe ${uniqueId}`; - const playground = new PlaygroundPage(page); await playground.goto( generatePlaygroundURL({ baseURL: playgroundURL, @@ -258,44 +199,15 @@ test.describe('playground', () => { }) ); - await expect(playground.preview.getByRole('button', { name: 'Begin' })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - await expect(playground.preview.locator('body')).toHaveAttribute('data-probe', 'ran'); - await expect(page.locator('body')).not.toHaveAttribute('data-probe', /./); - }); - - test('should render a share link that imports an upper-case PNG, so the preview shows the stimulus', async ({ - page, - uniqueId - }) => { - const title = `Upper PNG ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [ - { content: uppercasePngInstrumentSource(title), name: 'index.ts' }, - { content: STIM_PNG_BASE64, name: 'stim.PNG' } - ], - label: title - }) - ); - await expect(playground.preview.getByRole('heading', { name: title })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - await playground.preview.getByRole('button', { name: 'Begin' }).click(); - - const stimulus = playground.preview - .frameLocator('iframe[name="interactive-instrument"]') - .getByRole('img', { name: 'Stimulus' }); - await expect(stimulus).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - await expect(stimulus).toHaveAttribute('src', `data:image/png;base64,${STIM_PNG_BASE64}`); + await expect(playground.preview.locator('body')).toHaveAttribute('data-probe', 'ran'); + await expect(playground.$ref.locator('body')).not.toHaveAttribute('data-probe', /./); }); test('should serve an SVG static asset through the service worker, so the image in its HTML loads', async ({ - page, uniqueId }) => { const title = `Static SVG ${uniqueId}`; - const playground = new PlaygroundPage(page); await playground.goto( generatePlaygroundURL({ baseURL: playgroundURL, @@ -321,174 +233,40 @@ test.describe('playground', () => { .toBeGreaterThan(0); }); - // An interactive instrument renders in a frame of its own inside the preview, which reads its - // parent's document. That only works because the preview is a real origin rather than an opaque - // sandbox, which is the reason the preview is served from a second host. - test('should render an interactive instrument inside the preview', async ({ page }) => { - const playground = new PlaygroundPage(page); - await playground.goto(); - await playground.selectInstrument('Interactive With React'); - - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - - const task = playground.preview.frameLocator('iframe[name="interactive-instrument"]'); - await expect(task.getByRole('heading', { name: 'Vite + React' })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - }); - // `test:e2e` builds `@opendatacapture/playground-url` first, so its CLI is in `dist`. The React // example imports `react.svg`; the CLI used to skip it as binary, so the link failed to compile. - test('should render an SVG asset from a link generated by the playground-url CLI', async ({ page }) => { - const repoRoot = path.resolve(import.meta.dirname, '../../..'); + // The task renders in a frame of its own inside the preview, which reads its parent's document: + // that only works because the preview is a real origin rather than an opaque sandbox. + test('should render the React example and its SVG asset from a link generated by the playground-url CLI', async () => { const href = execFileSync( process.execPath, [ - path.join(repoRoot, 'packages/playground-url/dist/cli.js'), - path.join(repoRoot, 'apps/playground/src/instruments/examples/interactive/Interactive-With-React'), + path.join(REPO_ROOT, 'packages/playground-url/dist/cli.js'), + path.join(REPO_ROOT, 'apps/playground/src/instruments/examples/interactive/Interactive-With-React'), '--base-url', playgroundURL ], { encoding: 'utf-8' } ).trim(); - const playground = new PlaygroundPage(page); await playground.goto(href); - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); + await expect(playground.preview.getByRole('heading', { name: 'React Click Task' })).toBeVisible({ + timeout: PREVIEW_TIMEOUT + }); + await playground.preview.getByRole('button', { name: 'Begin' }).click(); + const task = playground.preview.frameLocator('iframe[name="interactive-instrument"]'); + await expect(task.getByRole('heading', { name: 'Vite + React' })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); // A visibility check would also pass for an SVG that arrives base64-encoded and renders broken. - const logo = playground.preview - .frameLocator('iframe[name="interactive-instrument"]') - .getByRole('img', { name: 'React logo' }); - await expect(logo).toBeVisible({ timeout: PREVIEW_TIMEOUT }); + const logo = task.getByRole('img', { name: 'React logo' }); + await expect(logo).toBeVisible(); await expect.poll(() => logo.evaluate((img: HTMLImageElement) => img.naturalWidth)).toBeGreaterThan(0); }); - test('should hand a submission from the preview back to the editor', async ({ page }) => { - const playground = new PlaygroundPage(page); - await playground.goto(); - - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - // The editor reports the submission with `alert`, which blocks the page until it is dismissed, - // so the click below only returns once the handler has closed it. - const messages: string[] = []; - page.once('dialog', (dialog) => { - messages.push(dialog.message()); - void dialog.dismiss(); - }); - await playground.preview.getByRole('button', { name: 'Submit' }).click(); - - await expect.poll(() => messages).toHaveLength(1); - expect(messages[0]).toContain('The following data will be submitted'); - }); - - // The preview validates every instrument it renders, and apps/web does too in development. That - // validation is what used to strip `visibility` from computed measures, so the default applied. - test('should leave a computed measure marked hidden out of the summary', async ({ page, uniqueId }) => { - const title = `Measures ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [{ content: measureVisibilityInstrumentSource(title), name: 'index.ts' }], - label: title - }) - ); - - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - // Playwright dismisses the editor's submission `alert` on its own when nothing listens for it. - await playground.preview.getByRole('button', { name: 'Submit' }).click(); - - await expect(playground.preview.getByText('Shown Measure')).toBeVisible(); - await expect(playground.preview.getByText('Hidden Measure')).toHaveCount(0); - }); - - test('should leave the measure on a dynamic question the respondent never saw out of the summary', async ({ - page, - uniqueId - }) => { - const title = `Dynamic Measure ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [{ content: dynamicMeasureInstrumentSource(title), name: 'index.ts' }], - label: title - }) - ); - - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - // The radio items carry stable ids (`-true`), unlike their labels, which libui translates. - await playground.preview.locator('#hasPet-false').click(); - await playground.preview.getByRole('button', { name: 'Submit' }).click(); - - await expect(playground.preview.getByText('Has Pet Measure')).toBeVisible(); - await expect(playground.preview.getByText('Pet Name Measure')).toHaveCount(0); - }); - - test('should list the measure on a dynamic question in the summary once the respondent reveals it', async ({ - page, - uniqueId - }) => { - const title = `Dynamic Measure ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [{ content: dynamicMeasureInstrumentSource(title), name: 'index.ts' }], - label: title - }) - ); - - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - await playground.preview.locator('#hasPet-true').click(); - await playground.preview.getByLabel('Pet Name', { exact: true }).fill('Rex'); - await playground.preview.getByRole('button', { name: 'Submit' }).click(); - - await expect(playground.preview.getByText('Pet Name Measure')).toBeVisible(); - }); - - // react-resizable-panels reads a bare number as pixels, so a pane size written as `66` rather than - // `"66%"` still type-checks and renders, only at the wrong size. - test('should split the editor and preview two to one, and stop the editor at a quarter when dragged narrower', async ({ - page - }) => { - const playground = new PlaygroundPage(page); - await playground.goto(); - await expect(playground.editorPane).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - await expect.poll(() => playground.editorShare()).toBeCloseTo(0.66, 1); - - const editor = (await playground.editorPane.boundingBox())!; - await playground.dragPaneSeparatorTo(editor.x + editor.width / 2); - await expect.poll(() => playground.editorShare()).toBeCloseTo(0.33, 1); - - await playground.dragPaneSeparatorTo(editor.x); - await expect.poll(() => playground.editorShare()).toBeCloseTo(0.25, 1); - }); - - test("should report only the author's syntax error, without an internal bundler TypeError", async ({ - page, - uniqueId - }) => { - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [{ content: "export default { kind: 'FORM', ;", name: 'index.ts' }], - label: `Syntax Error ${uniqueId}` - }) - ); - - await expect(page.getByRole('heading', { name: 'Failed to Compile' })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - await page.getByRole('button', { name: 'Cause' }).click(); - await expect(page.getByText(/Build failed with 1 error/)).toBeVisible(); - await expect(page.getByText(/reading 'inputs'/)).toHaveCount(0); - }); - test('should load a subpath of a scoped runtime package, so its exports reach the instrument', async ({ - page, uniqueId }) => { const title = `Scoped Subpath ${uniqueId}`; - const playground = new PlaygroundPage(page); await playground.goto( generatePlaygroundURL({ baseURL: playgroundURL, @@ -501,45 +279,46 @@ test.describe('playground', () => { timeout: PREVIEW_TIMEOUT }); }); -}); -test.describe('playground interactive examples', () => { - test.describe.configure({ mode: 'default' }); - test.slow(); - - for (const label of ['Interactive With Static Assets', 'Interactive With Legacy Script']) { - test(`should submit the ${label} example when its Submit button is clicked`, async ({ page }) => { - const playground = new PlaygroundPage(page); - await playground.goto(); + for (const [label, title] of Object.entries(EXAMPLE_TITLES)) { + test(`should submit the ${label} example when its Submit button is clicked`, async () => { await playground.selectInstrument(label); - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - const messages: string[] = []; - page.once('dialog', (dialog) => { - messages.push(dialog.message()); - void dialog.dismiss(); + await expect(playground.preview.getByRole('heading', { name: title })).toBeVisible({ + timeout: PREVIEW_TIMEOUT }); + await playground.preview.getByRole('button', { name: 'Begin' }).click(); const task = playground.preview.frameLocator('iframe[name="interactive-instrument"]'); - await task.getByRole('button', { name: 'Submit' }).click({ timeout: PREVIEW_TIMEOUT }); - - await expect.poll(() => messages).toHaveLength(1); - expect(messages[0]).toContain('"message": "Hello"'); + const message = await readAlert(playground.$ref, () => { + return task.getByRole('button', { name: 'Submit' }).click({ timeout: PREVIEW_TIMEOUT }); + }); + expect(message).toContain('"message": "Hello"'); }); } // The task only submits once the respondent has pressed through every trial, and the preview // reports a submission only if it passes the example's own schema, an array of trials. - test('should submit the Interactive With JSPsych example once its timeline finishes', async ({ page }) => { - const playground = new PlaygroundPage(page); - await playground.goto(); - await playground.selectInstrument('Interactive With JSPsych'); + test('should submit the JSPsych example once its timeline finishes', async ({ uniqueId }) => { + const title = `JSPsych ${uniqueId}`; + await playground.goto( + generatePlaygroundURL({ + baseURL: playgroundURL, + files: [ + { content: shortenedJsPsychSource(), name: 'index.ts' }, + // The playground holds binary assets as base64, which is what a share link carries. + ...['blue.png', 'orange.png'].map((name) => ({ + content: readFileSync(path.join(JSPSYCH_EXAMPLE_DIR, name)).toString('base64'), + name + })) + ], + label: title + }) + ); - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - const messages: string[] = []; - page.once('dialog', (dialog) => { - messages.push(dialog.message()); - void dialog.dismiss(); + await expect(playground.preview.getByRole('heading', { name: 'Reaction Time Task' })).toBeVisible({ + timeout: PREVIEW_TIMEOUT }); + await playground.preview.getByRole('button', { name: 'Begin' }).click(); const task = playground.preview.frameLocator('iframe[name="interactive-instrument"]'); const body = task.locator('body'); @@ -548,159 +327,18 @@ test.describe('playground interactive examples', () => { await expect(task.getByText('In this experiment, a circle will appear')).toBeVisible(); await body.press('Space'); - // 2 stimuli x 5 repetitions, each shown after a fixation of up to 2 seconds + // 2 stimuli x 5 repetitions, each shown after its fixation const stimulus = task.locator('#jspsych-image-keyboard-response-stimulus'); for (let trial = 0; trial < 10; trial++) { - await expect(stimulus).toBeVisible({ timeout: 10_000 }); + await expect(stimulus).toBeVisible(); await body.press('f'); await expect(stimulus).toBeHidden(); } - await expect(task.getByText('Press any key to complete the experiment.')).toBeVisible({ timeout: 10_000 }); - await body.press('Space'); - - await expect.poll(() => messages).toHaveLength(1); - expect(messages[0]).toContain('The following data will be submitted'); + await expect(task.getByText('Press any key to complete the experiment.')).toBeVisible(); + const message = await readAlert(playground.$ref, () => body.press('Space')); + expect(message).toContain('The following data will be submitted'); // the dialog text is cut short (the trials embed the stimuli as data URLs), so only its start is checked - expect(messages[0]).toContain('"data": ['); - }); -}); - -test.describe('playground save button', () => { - test.describe.configure({ mode: 'default' }); - test.slow(); - - test('should disable Save after switching from an edited saved instrument to an example', async ({ - page, - uniqueId - }) => { - const title = `Save ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [{ content: measureVisibilityInstrumentSource(title), name: 'index.ts' }], - label: title - }) - ); - await expect(playground.preview.getByRole('heading', { name: title })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - - const saveButton = page.locator('button:has(svg.lucide-save)'); - await page.locator('.monaco-editor .view-lines').click(); - await page.keyboard.type('// edited'); - await expect(saveButton).toBeEnabled(); - - await playground.selectInstrument('Form Reference'); - await expect(saveButton).toBeDisabled(); - }); -}); - -test.describe('playground editor tabs', () => { - test.describe.configure({ mode: 'default' }); - test.slow(); - - test('should keep the active tab selected when a background tab is closed', async ({ page, uniqueId }) => { - const title = `Tabs ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [ - { content: measureVisibilityInstrumentSource(title), name: 'index.ts' }, - { content: 'export const a = 1;', name: 'a.ts' }, - { content: 'export const b = 2;', name: 'b.ts' } - ], - label: title - }) - ); - await expect(playground.preview.getByRole('heading', { name: title })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - - await page.locator('button:has(svg.lucide-columns-3)').click(); - const fileButton = (filename: string) => { - return page.locator('button', { has: page.locator(`input[value="${filename}"]`) }); - }; - await fileButton('a.ts').click(); - await fileButton('b.ts').click(); - - const tab = (filename: string) => page.locator('button[aria-current]', { hasText: filename }); - await expect(tab('b.ts')).toHaveAttribute('aria-current', 'true'); - - await tab('a.ts').locator('svg.lucide-x').click(); - await expect(tab('a.ts')).toHaveCount(0); - await expect(tab('b.ts')).toHaveAttribute('aria-current', 'true'); - }); -}); - -test.describe('playground instrument deletion', () => { - test.describe.configure({ mode: 'default' }); - test.slow(); - - test('should show the default template after the selected saved instrument is deleted', async ({ - page, - uniqueId - }) => { - const title = `Deleted ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [{ content: measureVisibilityInstrumentSource(title), name: 'index.ts' }], - label: title - }) - ); - await expect(playground.preview.getByRole('heading', { name: title })).toBeVisible({ timeout: PREVIEW_TIMEOUT }); - - await page.locator('button:has(svg.lucide-ellipsis-vertical)').click(); - await page.getByRole('menuitem', { name: 'Delete Instrument' }).click(); - await page.getByRole('dialog').getByRole('button', { exact: true, name: 'Delete' }).click(); - - await expect(playground.preview.getByRole('heading', { name: '' })).toBeVisible({ - timeout: PREVIEW_TIMEOUT - }); - await expect(playground.preview.getByRole('heading', { name: title })).toHaveCount(0); - }); -}); - -/** - * A form that leaves measures hidden by default and marks its one measure with the deprecated - * `hidden: false`, which must still show it as `visibility: 'visible'` would. - */ -function deprecatedHiddenFalseInstrumentSource(title: string): string { - return ` -import { defineInstrument } from '/runtime/v1/@opendatacapture/runtime-core'; -import { z } from '/runtime/v1/zod@3.x'; - -export default defineInstrument({ - kind: 'FORM', - language: 'en', - tags: ['Measures'], - internal: { edition: 1, name: 'DEPRECATED_HIDDEN_FALSE' }, - clientDetails: { estimatedDuration: 1, instructions: ['Submit the form'] }, - content: { note: { kind: 'string', label: 'Note', variant: 'input' } }, - details: { description: 'Deprecated hidden flag', license: 'Apache-2.0', title: '${title}' }, - measures: { - legacyMeasure: { hidden: false, kind: 'computed', label: 'Legacy Measure', value: () => 3 } - }, - validationSchema: z.object({ note: z.string().optional() }) -}); -`; -} - -test.describe('playground deprecated measure visibility', () => { - test('should show a measure declaring the deprecated hidden: false in the summary', async ({ page, uniqueId }) => { - const title = `Deprecated Flag ${uniqueId}`; - const playground = new PlaygroundPage(page); - await playground.goto( - generatePlaygroundURL({ - baseURL: playgroundURL, - files: [{ content: deprecatedHiddenFalseInstrumentSource(title), name: 'index.ts' }], - label: title - }) - ); - - await playground.preview.getByRole('button', { name: 'Begin' }).click({ timeout: PREVIEW_TIMEOUT }); - await playground.preview.getByRole('button', { name: 'Submit' }).click(); - - await expect(playground.preview.getByText('Legacy Measure', { exact: true })).toBeVisible(); + expect(message).toContain('"data": ['); }); }); diff --git a/testing/src/specs/record-file-deletion.spec.ts b/testing/src/specs/record-file-deletion.spec.ts deleted file mode 100644 index 1925ba806..000000000 --- a/testing/src/specs/record-file-deletion.spec.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { expect, test } from '../support/fixtures'; - -test('should delete a record carrying file metadata and make its files inaccessible', async ({ - adminToken, - api, - apiRequestContext, - uniqueId -}) => { - const headers = { Authorization: `Bearer ${adminToken}` }; - const session = await api.createSession(null, { id: `file-deletion-${uniqueId}` }); - const instrumentId = await api.findInstrumentIdByName('ARBITRARY_SINGLE_FILE'); - const created = await apiRequestContext.post('/api/v1/instrument-records', { - data: { - data: {}, - date: new Date(), - instrumentId, - sessionId: session.id, - subjectId: session.subjectId - }, - headers - }); - expect(created.status()).toBe(201); - const record = (await created.json()) as { id: string }; - const recordPath = `/api/v1/instrument-records/${record.id}`; - const name = `file-${uniqueId}.txt`; - - const completed = await apiRequestContext.post(`${recordPath}/files/upload-complete`, { - data: { uploads: { file: [{ location: { basename: 'file', index: 0 }, name, size: 4 }] } }, - headers - }); - expect(completed.status()).toBe(201); - const files = await apiRequestContext.get(`${recordPath}/files`, { headers }); - expect(files.status()).toBe(200); - expect(await files.json()).toMatchObject({ file: [{ name, size: 4 }] }); - - const deleted = await apiRequestContext.delete(recordPath, { headers }); - expect(deleted.status()).toBe(204); - const missingRecord = await apiRequestContext.get(recordPath, { headers }); - expect(missingRecord.status()).toBe(404); - const missingFiles = await apiRequestContext.get(`${recordPath}/files`, { headers }); - expect(missingFiles.status()).toBe(404); -}); diff --git a/testing/src/specs/remote-assignment.spec.ts b/testing/src/specs/remote-assignment.spec.ts index 6c796f473..a6c5cab92 100644 --- a/testing/src/specs/remote-assignment.spec.ts +++ b/testing/src/specs/remote-assignment.spec.ts @@ -1,12 +1,13 @@ import type { BulkAssignmentFailure } from '@opendatacapture/schemas/assignment'; -import { AccessibleInstrumentsPage } from '../pages/_app/instruments/accessible-instruments.page'; +import { SubjectAssignmentsPage } from '../pages/_app/datahub/subjects/$subjectId/assignments.page'; import { RemoteAssignmentPage } from '../pages/_app/session/remote-assignment.page'; import { baseURL } from '../support/env'; import { expect, test } from '../support/fixtures'; import type { GetPageModel } from '../support/fixtures'; +/** A session lives only in the client's memory, so a page that needs one starts it through the form. */ async function startSession(getPageModel: GetPageModel, firstName: string, lastName: string, sex: string) { const startSessionPage = await getPageModel('/session/start-session'); await startSessionPage.sessionForm.waitFor({ state: 'visible' }); @@ -17,7 +18,7 @@ async function startSession(getPageModel: GetPageModel, firstName: string, lastN } test.describe('remote assignment', () => { - test('should display the remote assignment page after starting a session @smoke', async ({ + test('should display the remote assignment page after starting a session', async ({ getPageModel, page, uniqueId @@ -32,83 +33,10 @@ test.describe('remote assignment', () => { await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); }); - test('should retain all tag options after searching, opening a remote assignment dialog, and clearing the search', async ({ - getPageModel, - page, - uniqueId - }) => { - await startSession(getPageModel, `Tag${uniqueId}`, `Subject${uniqueId}`, 'Male'); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); - const tagFilter = page.getByTestId('instrument-tag-filter'); - const tagButton = tagFilter.getByRole('button'); - await tagButton.click(); - await expect(page.getByRole('menuitemcheckbox').first()).toBeVisible(); - const initialOptions = await page.getByRole('menuitemcheckbox').allTextContents(); - expect(initialOptions.length).toBeGreaterThan(1); - await page.keyboard.press('Escape'); - - const searchInput = remoteAssignmentPage.instrumentShowcase.getByRole('searchbox'); - await searchInput.fill('Happiness Questionnaire'); - await remoteAssignmentPage.selectInstrument('Happiness Questionnaire'); - await expect(remoteAssignmentPage.createDialog).toBeVisible(); - await page.keyboard.press('Escape'); - await expect(remoteAssignmentPage.createDialog).toBeHidden(); - - await searchInput.fill(''); - await tagButton.click(); - await expect.poll(() => page.getByRole('menuitemcheckbox').allTextContents()).toEqual(initialOptions); - }); - - test('should not offer a file instrument for remote assignment, though an in-person session offers it', async ({ - getPageModel, - page, - uniqueId - }) => { - await startSession(getPageModel, `File${uniqueId}`, `Subject${uniqueId}`, 'Female'); - - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - await expect(new AccessibleInstrumentsPage(page).instrumentCard('Arbitrary File Upload')).toBeVisible(); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await expect(remoteAssignmentPage.instrumentCard('Happiness Questionnaire')).toBeVisible(); - await expect(remoteAssignmentPage.instrumentCard('Arbitrary File Upload')).toHaveCount(0); - }); - // `api.createGroup` grants every instrument, the file instrument included, so the refusal comes - // from the file rule rather than the group's opt-in list. - test('should refuse to assign a file instrument remotely, with or without a group, and create nothing', async ({ - adminToken, - api, - apiRequestContext - }) => { - const group = await api.createGroup(); - const subjectId = await api.createSubject(group.id); - const instrumentId = await api.findInstrumentId('FILE'); - - for (const groupId of [group.id, undefined]) { - const response = await apiRequestContext.post('/api/v1/assignments', { - data: { expiresAt: new Date(Date.now() + 86_400_000), groupId, instrumentId, subjectId }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(response.status()).toBe(422); - expect(((await response.json()) as BulkAssignmentFailure).issues).toContainEqual({ - instrumentIds: [instrumentId], - kind: 'INSTRUMENT_UNAVAILABLE' - }); - } - expect(await api.findAssignments(subjectId)).toHaveLength(0); - }); - - // Assigned without a group, so the group's opt-in list cannot be what refuses it. - test('should refuse to assign a series with a file item remotely', async ({ + // from the file rule rather than the group's opt-in list. The series is assigned without a group + // for the same reason. + test('should refuse to assign a file instrument, or a series with a file item, remotely, and create nothing', async ({ adminToken, api, apiRequestContext, @@ -118,221 +46,97 @@ test.describe('remote assignment', () => { const group = await api.createGroup(); const subjectId = await api.createSubject(group.id); const instruments = (await (await apiRequestContext.get('/api/v1/instruments/info', { headers })).json()) as { + id: string; internal?: { edition: number; name: string }; kind: string; }[]; - const items = ['FORM', 'FILE'].map((kind) => instruments.find((instrument) => instrument.kind === kind)!.internal); + const fileInstrument = instruments.find((instrument) => instrument.kind === 'FILE')!; const series = await apiRequestContext.post('/api/v1/instruments/series', { data: { confirmDuplicate: true, details: { title: `File Series ${uniqueId}` }, groupId: group.id, - items, + items: ['FORM', 'FILE'].map((kind) => instruments.find((instrument) => instrument.kind === kind)!.internal), language: 'en' }, headers }); expect(series.status(), await series.text()).toBe(201); - const { instrumentId } = (await series.json()) as { instrumentId: string }; - - const response = await apiRequestContext.post('/api/v1/assignments', { - data: { expiresAt: new Date(Date.now() + 86_400_000), instrumentId, subjectId }, - headers - }); - expect(response.status()).toBe(422); - expect(((await response.json()) as BulkAssignmentFailure).issues).toContainEqual({ - instrumentIds: [instrumentId], - kind: 'INSTRUMENT_UNAVAILABLE' - }); - }); - - test('should create a remote assignment and display the link', async ({ getPageModel, page, uniqueId }) => { - await startSession(getPageModel, `Assign${uniqueId}`, `User${uniqueId}`, 'Female'); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); - await remoteAssignmentPage.clickFirstInstrumentCard(); + const { instrumentId: seriesId } = (await series.json()) as { instrumentId: string }; - const dialog = remoteAssignmentPage.createDialog; - await expect(dialog).toBeVisible(); - await expect(dialog).toContainText('Create Remote Assignment'); - await remoteAssignmentPage.submitAssignmentForm(); - - await expect(page.getByRole('link', { name: 'Assignment Link' })).toBeVisible(); - const urlInput = page.locator('input[readonly]'); - await expect(urlInput).toBeVisible(); - expect(await urlInput.inputValue()).toMatch(/^https?:\/\/.+/); - }); - - test('should create a remote assignment that sends the subject on to the https redirect URL given', async ({ - api, - getPageModel, - page, - uniqueId - }) => { - await startSession(getPageModel, `Redirect${uniqueId}`, `User${uniqueId}`, 'Female'); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await remoteAssignmentPage.selectInstrument('Happiness Questionnaire'); - await remoteAssignmentPage.redirectUrlInput.fill('http://example.com/done'); - await remoteAssignmentPage.submitAssignmentForm(); - await expect(remoteAssignmentPage.validationError).toHaveText('Must start with "https://"'); - await expect(remoteAssignmentPage.createDialog).toBeVisible(); - - const redirectUrl = `https://example.com/done/${uniqueId}`; - await remoteAssignmentPage.redirectUrlInput.fill(redirectUrl); - await remoteAssignmentPage.submitAssignmentForm(); - await expect(remoteAssignmentPage.urlInput).toBeVisible(); - await remoteAssignmentPage.closeResultButton.click(); - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - const subjectId = page.url().split('/datahub/subjects/')[1]!.split('/')[0]!; - expect(await api.findAssignments(subjectId)).toMatchObject([{ redirectUrl }]); + for (const [instrumentId, groupId] of [ + [fileInstrument.id, group.id], + [seriesId, undefined] + ] as const) { + const response = await apiRequestContext.post('/api/v1/assignments', { + data: { expiresAt: new Date(Date.now() + 86_400_000), groupId, instrumentId, subjectId }, + headers + }); + expect(response.status()).toBe(422); + expect(((await response.json()) as BulkAssignmentFailure).issues).toContainEqual({ + instrumentIds: [instrumentId], + kind: 'INSTRUMENT_UNAVAILABLE' + }); + } + expect(await api.findAssignments(subjectId)).toHaveLength(0); }); - test('should offer a copy-to-clipboard control and a QR code alongside the assignment link', async ({ + test('should create a remote assignment shown as a link to copy and scan, and another in the same session', async ({ context, getPageModel, page, uniqueId }) => { await context.grantPermissions(['clipboard-read', 'clipboard-write'], { origin: baseURL }); - await startSession(getPageModel, `Copy${uniqueId}`, `User${uniqueId}`, 'Male'); + await startSession(getPageModel, `Assign${uniqueId}`, `User${uniqueId}`, 'Female'); await page.getByTestId('nav-button-/session/remote-assignment').click(); await page.waitForURL('**/session/remote-assignment'); const remoteAssignmentPage = new RemoteAssignmentPage(page); - await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); - await remoteAssignmentPage.clickFirstInstrumentCard(); + await remoteAssignmentPage.selectInstrument('Happiness Questionnaire'); + await expect(remoteAssignmentPage.createDialog).toContainText('Create Remote Assignment'); await remoteAssignmentPage.submitAssignmentForm(); - await expect(remoteAssignmentPage.urlInput).toBeVisible(); + await expect(page.getByRole('link', { name: 'Assignment Link' })).toBeVisible(); await expect(remoteAssignmentPage.qrCode).toBeVisible(); - - const assignmentUrl = await remoteAssignmentPage.urlInput.inputValue(); - await remoteAssignmentPage.copyLinkButton.click(); - const clipboardText = await page.evaluate(() => navigator.clipboard.readText()); - expect(clipboardText).toBe(assignmentUrl); - }); - - test('should allow creating more than one remote assignment in the same session', async ({ - getPageModel, - page, - uniqueId - }) => { - await startSession(getPageModel, `Multi${uniqueId}`, `User${uniqueId}`, 'Female'); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); - await remoteAssignmentPage.selectInstrument('Happiness Questionnaire'); - await remoteAssignmentPage.submitAssignmentForm(); const firstUrl = await remoteAssignmentPage.urlInput.inputValue(); + expect(firstUrl).toMatch(/^https?:\/\/.+\/assignments\/.+/); + await remoteAssignmentPage.copyLinkButton.click(); + expect(await page.evaluate(() => navigator.clipboard.readText())).toBe(firstUrl); await remoteAssignmentPage.closeResultButton.click(); - await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); await remoteAssignmentPage.selectInstrument('General Consent Form'); await remoteAssignmentPage.submitAssignmentForm(); - const secondUrl = remoteAssignmentPage.urlInput; - - await expect(secondUrl).not.toHaveValue(firstUrl); + await expect(remoteAssignmentPage.urlInput).toBeVisible(); + await expect(remoteAssignmentPage.urlInput).not.toHaveValue(firstUrl); }); // Entering by URL is the path a bookmarked link takes, and it runs the GATEWAY_ENABLED guard in // `beforeLoad` on a cold document load rather than on a client-side transition from the tab. - // `getPageModel` asserts it landed on the URL it asked for, so a guard that wrongly bounced the - // request to the table tab fails here. - test('should allow deep-linking to the assignments tab while the gateway is deployed', async ({ - getPageModel, - page, - uniqueId - }) => { - await startSession(getPageModel, `Deep${uniqueId}`, `Subject${uniqueId}`, 'Male'); - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - const subjectId = page.url().split('/datahub/subjects/')[1]!.split('/')[0]!; - - const assignmentsPage = await getPageModel('/datahub/subjects/$subjectId/assignments', { subjectId }); - await expect(assignmentsPage.assignmentRows).toHaveCount(0); - }); - - test('should not return the private key the submission is encrypted to when creating an assignment', async ({ - api - }) => { - const group = await api.createGroup(); - const assignment = await api.createAssignment({ - expiresAt: new Date(Date.now() + 86_400_000), - groupId: group.id, - instrumentId: await api.findInstrumentId('FORM'), - subjectId: await api.createSubject(group.id) - }); - expect(assignment).not.toHaveProperty('encryptionKeyPair'); - }); - - test('should not return the private key the submission is encrypted to when listing or cancelling assignments', async ({ - adminToken, + // `goto` asserts it landed on the URL it asked for, so a guard that wrongly bounced the request + // to the table tab fails here. + test("should let a group manager deep-link to a subject's assignments tab and cancel an outstanding assignment", async ({ api, - apiRequestContext + isolatedGroupManager, + page }) => { - const group = await api.createGroup(); + const group = await isolatedGroupManager(); const subjectId = await api.createSubject(group.id); - const { id } = await api.createAssignment({ + await api.createAssignment({ expiresAt: new Date(Date.now() + 86_400_000), groupId: group.id, - instrumentId: await api.findInstrumentId('FORM'), + instrumentId: await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'), subjectId }); - const listed = await api.findAssignments(subjectId); - expect(listed).toHaveLength(1); - expect(listed[0]).not.toHaveProperty('encryptionKeyPair'); - - const canceled = await apiRequestContext.patch(`/api/v1/assignments/${id}`, { - data: { status: 'CANCELED' }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(canceled.status()).toBe(200); - expect(await canceled.json()).not.toHaveProperty('encryptionKeyPair'); - }); - - test("should let a group manager cancel an outstanding assignment from the subject's assignments tab", async ({ - getPageModel, - page, - uniqueId - }) => { - await startSession(getPageModel, `Cancel${uniqueId}`, `User${uniqueId}`, 'Male'); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await expect(remoteAssignmentPage.instrumentShowcase).toBeVisible(); - await remoteAssignmentPage.selectInstrument('Happiness Questionnaire'); - await remoteAssignmentPage.submitAssignmentForm(); - await expect(remoteAssignmentPage.urlInput).toBeVisible(); - await remoteAssignmentPage.closeResultButton.click(); - - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - await page.getByRole('link', { name: 'Assignments' }).click(); - await page.waitForURL('**/datahub/subjects/**/assignments'); - - await page.getByTestId('data-table-row').filter({ hasText: 'Happiness Questionnaire' }).click(); - await page.getByRole('button', { name: 'Cancel' }).click(); + const assignmentsPage = new SubjectAssignmentsPage(page); + await assignmentsPage.goto('/datahub/subjects/$subjectId/assignments', { subjectId }); + await expect(assignmentsPage.assignmentRows).toHaveCount(1); + await assignmentsPage.assignmentRows.click(); + await assignmentsPage.cancelButton.click(); - await expect(page.getByTestId('data-table-row').filter({ hasText: 'Happiness Questionnaire' })).toContainText( - 'Canceled' - ); + await expect(assignmentsPage.assignmentRows).toContainText('Canceled'); + expect(await api.findAssignments(subjectId)).toMatchObject([{ status: 'CANCELED' }]); }); }); diff --git a/testing/src/specs/remote-assignments-wizard.spec.ts b/testing/src/specs/remote-assignments-wizard.spec.ts index f231f15f8..76eccb47a 100644 --- a/testing/src/specs/remote-assignments-wizard.spec.ts +++ b/testing/src/specs/remote-assignments-wizard.spec.ts @@ -13,16 +13,24 @@ const boundingBox = async (locator: Locator) => { return box; }; +/** One row under a header of `columnCount` columns, the first of which is the subject id. */ +const wideCsv = (columnCount: number, uniqueId: string) => { + const headers = ['subject_id', ...Array.from({ length: columnCount - 1 }, (_, index) => `extra_${index}`)]; + const row = [`wide_${uniqueId}`, ...Array.from({ length: columnCount - 1 }, (_, index) => `value_${index}`)]; + return `${headers.join(',')}\n${row.join(',')}\n`; +}; + /** - * The wizard through the UI. The API-level guarantees are covered by `bulk-remote-assignments.spec.ts`; - * this drives the page itself, so nothing is submitted and no assignment is created. + * The wizard through the UI, for what only a real browser shows: layout, and the subject picker + * filled from the real API. Its steps' behaviour is covered by the component tests in + * `apps/web/src/components/BulkRemoteAssignmentWizard/__tests__/`, and the API-level guarantees by + * `bulk-remote-assignments.spec.ts`; nothing is submitted here and no assignment is created. */ test.describe('remote assignments wizard', () => { - test('should fit the page column, span the stepper across the card, and advance to the instruments step', async ({ - adminToken, + test('should fit the page column, cap a wide column mapping, and advance with the subjects picked', async ({ api, apiRequestContext, - authenticateAs, + isolatedGroupManager, page, uniqueId }) => { @@ -32,24 +40,12 @@ test.describe('remote assignments wizard', () => { const setupState = (await setupResponse.json()) as { isBulkRemoteAssignmentsEnabled?: boolean }; expect(setupState.isBulkRemoteAssignmentsEnabled).toBe(true); - // A dedicated group + manager rather than the cached `GROUP_MANAGER` account: a one-group user - // lands on this group, so the picker lists exactly the subjects seeded here. - const group = await api.createGroup({ name: `Wizard${uniqueId}` }); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); + // A group of its own: a one-group manager lands on it, so the picker lists exactly these subjects. + const group = await isolatedGroupManager(); for (const index of [0, 1]) { - const response = await apiRequestContext.post(`${API}/sessions`, { - data: { - date: new Date().toISOString(), - groupId: group.id, - subjectData: { id: `wizard_${uniqueId}_${index}` }, - type: 'IN_PERSON' - }, - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(response.status(), await response.text()).toBe(201); + await api.createSession(group.id, { id: `wizard_${uniqueId}_${index}` }); } - await authenticateAs(credentials); await page.goto('/group/remote-assignments?mode=create'); await expect(page).toHaveURL(/\/group\/remote-assignments\?mode=create/); const wizardPage = new RemoteAssignmentsPage(page); @@ -65,168 +61,28 @@ test.describe('remote assignments wizard', () => { expect(wizard.width).toBeLessThanOrEqual(container.width); expect(breadcrumbs.width).toBeGreaterThanOrEqual(wizard.width * 0.9); - await wizardPage.selectAllSubjects.click(); - await wizardPage.useSelectedSubjects.click(); - await expect(wizardPage.timepointsStep).toBeVisible(); - await expect(wizardPage.breadcrumb('INSTRUMENTS')).toHaveAttribute('aria-current', 'step'); - await expect(wizardPage.breadcrumb('SUBJECTS')).toBeEnabled(); - }); - - /** One row under a header of `columnCount` columns, the first of which is the subject id. */ - const wideCsv = (columnCount: number, uniqueId: string) => { - const headers = ['subject_id', ...Array.from({ length: columnCount - 1 }, (_, index) => `extra_${index}`)]; - const row = [`wide_${uniqueId}`, ...Array.from({ length: columnCount - 1 }, (_, index) => `value_${index}`)]; - return `${headers.join(',')}\n${row.join(',')}\n`; - }; - - // A file with many columns grew the mapping table until the step's own controls sat below the - // fold, so the frame is capped at 28rem and scrolls within itself instead. - test('should cap the column mapping table and scroll it rather than lengthening the step', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const group = await api.createGroup({ name: `WizardWide${uniqueId}` }); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); - - await authenticateAs(credentials); - await page.goto('/group/remote-assignments?mode=create'); - const wizardPage = new RemoteAssignmentsPage(page); - await expect(wizardPage.wizard).toBeVisible(); - + // A file with many columns grew the mapping table until the step's own controls sat below the + // fold, so the frame is capped at 28rem and scrolls within itself instead. await wizardPage.sourceMode('FILE'); - await wizardPage.uploadFile({ - buffer: Buffer.from(wideCsv(24, uniqueId)), - mimeType: 'text/csv', - name: 'wide.csv' - }); - + await wizardPage.uploadFile({ buffer: Buffer.from(wideCsv(24, uniqueId)), mimeType: 'text/csv', name: 'wide.csv' }); await expect(wizardPage.mapStep).toBeVisible(); // One row per column of the file, so the grid is certainly taller than the cap. await expect(wizardPage.columnMapping.locator('tbody tr')).toHaveCount(24); - const mapping = await boundingBox(wizardPage.columnMapping); expect(mapping.height).toBeLessThanOrEqual(448); - const [scrollHeight, clientHeight] = await wizardPage.columnMapping.evaluate((element) => [ element.scrollHeight, element.clientHeight ]); expect(scrollHeight).toBeGreaterThan(clientHeight); - }); -}); - -test.describe('remote assignments wizard refusals', () => { - // A refusal above the input left the user reading it while looking at a file they had already - // chosen, with the button that produced it a screen below. - test('should put a refusal directly above the button that produced it', async ({ isolatedGroupManager, page }) => { - await isolatedGroupManager(); - await page.goto('/group/remote-assignments?mode=create'); - const wizardPage = new RemoteAssignmentsPage(page); - await expect(wizardPage.wizard).toBeVisible(); - - await wizardPage.pasteSubjects('subjectId\nduplicated-1\nduplicated-1\n'); - await expect(wizardPage.mapStep).toBeVisible(); - await wizardPage.confirmMapping.click(); - - await expect(wizardPage.errorList).toContainText('Nothing has been created'); - const [errors, confirm] = await Promise.all([ - boundingBox(wizardPage.errorList), - boundingBox(wizardPage.confirmMapping) - ]); - expect(errors.y + errors.height).toBeLessThanOrEqual(confirm.y); - }); - - test('should return to the tab the data came from, with the data still in it', async ({ - isolatedGroupManager, - page - }) => { - await isolatedGroupManager(); - await page.goto('/group/remote-assignments?mode=create'); - const wizardPage = new RemoteAssignmentsPage(page); - await expect(wizardPage.wizard).toBeVisible(); - const pasted = 'subjectId\npasted-1\n'; - await wizardPage.pasteSubjects(pasted); - await expect(wizardPage.mapStep).toBeVisible(); await page.getByRole('button', { name: 'Back' }).click(); - - await expect(wizardPage.pasteInput).toBeVisible(); - await expect(wizardPage.pasteInput).toHaveValue(pasted); - }); - - // A stray row used to stop the whole batch, and only three steps later, as an identifier the user - // never typed: it is named here by its row, and can be left out without redoing the file. - test('should hold back review until the redirect URL is https, then show it for review', async ({ - api, - isolatedGroupManager, - page - }) => { - const group = await isolatedGroupManager(); - await api.createSubject(group.id); - await page.goto('/group/remote-assignments?mode=create'); - const wizardPage = new RemoteAssignmentsPage(page); - await expect(wizardPage.wizard).toBeVisible(); - + await wizardPage.sourceMode('SELECT'); await wizardPage.selectAllSubjects.click(); await wizardPage.useSelectedSubjects.click(); await expect(wizardPage.timepointsStep).toBeVisible(); - await wizardPage.addFirstTimepoint(); - - await wizardPage.redirectUrlInput.fill('http://example.com/done'); - await wizardPage.redirectUrlInput.blur(); - await expect(wizardPage.redirectUrlError).toHaveText('Must start with "https://"'); - await expect(wizardPage.confirmTimepoints).toBeDisabled(); - - await wizardPage.redirectUrlInput.fill('https://example.com/done'); - await expect(wizardPage.redirectUrlError).toBeHidden(); - await wizardPage.confirmTimepoints.click(); - await expect(wizardPage.reviewRedirectUrl).toContainText('https://example.com/done'); - }); - - test('should warn about a row this group cannot place and continue once it is left out', async ({ - api, - isolatedGroupManager, - page - }) => { - const group = await isolatedGroupManager(); - const subjectId = await api.createSubject(group.id); - await page.goto('/group/remote-assignments?mode=create'); - const wizardPage = new RemoteAssignmentsPage(page); - await expect(wizardPage.wizard).toBeVisible(); - - await wizardPage.pasteSubjects(`subjectId\n${subjectId}\nstranger-not-in-this-group\n`); - await expect(wizardPage.mapStep).toBeVisible(); - await wizardPage.confirmMapping.click(); - - await expect(wizardPage.errorList).toContainText('name a subject this group does not have'); - await expect(wizardPage.errorList).toContainText('Row 3'); - await expect(wizardPage.mapStep).toBeVisible(); - - await wizardPage.skipUnplaceable.click(); - await wizardPage.confirmMapping.click(); - - await expect(wizardPage.timepointsStep).toBeVisible(); - // Only the row the group could place travels on. The count is in the step's own description, - // which `StepLayout` renders in the card header rather than inside the step body. - await expect(page.getByText('all 1 selected subjects')).toBeVisible(); - }); -}); - -test.describe('remote assignments wizard parse errors', () => { - test('should number a malformed pasted line as it is numbered in the pasted text', async ({ - authenticateAs, - page - }) => { - await authenticateAs('GROUP_MANAGER'); - await page.goto('/group/remote-assignments?mode=create'); - await expect(page.getByTestId('bulk-remote-assignment-wizard')).toBeVisible(); - - await page.getByTestId('bulk-source-mode-PASTE').click(); - await page.getByTestId('bulk-paste-input').fill('subjectId\n"subject-1'); - await page.getByTestId('bulk-parse-pasted').click(); - - await expect(page.getByTestId('bulk-error-list')).toContainText('Row 2: Quoted field unterminated'); + await expect(page.getByText('all 2 selected subjects')).toBeVisible(); + await expect(wizardPage.breadcrumb('INSTRUMENTS')).toHaveAttribute('aria-current', 'step'); + await expect(wizardPage.breadcrumb('SUBJECTS')).toBeEnabled(); }); }); diff --git a/testing/src/specs/series-instrument.spec.ts b/testing/src/specs/series-instrument.spec.ts index 1fb4c285d..82a1660a2 100644 --- a/testing/src/specs/series-instrument.spec.ts +++ b/testing/src/specs/series-instrument.spec.ts @@ -119,29 +119,4 @@ test.describe('series instrument', () => { // records rather than one. await expect(page.getByTestId('data-table-body').getByTestId('data-table-row')).toHaveCount(2); }); - - // A skipProgress series has no interstitial screen, so the bar on each item's form is the only - // place the subject can see how far through the series they are. - test('should show the position in a skipProgress series on each item it administers', async ({ - getPageModel, - page, - uniqueId - }) => { - const instrumentPage = await openSeries({ getPageModel, page, uniqueId }); - await expect(instrumentPage.seriesProgress).toBeHidden(); - await instrumentPage.begin(); - - await expect(instrumentPage.seriesProgress).toContainText('Instrument 1 of 2'); - await expect(instrumentPage.seriesProgressBar).toHaveAttribute('aria-valuenow', '0'); - await instrumentPage.completeHappinessQuestionnaire(); - await instrumentPage.submit(); - - await expect(instrumentPage.seriesProgress).toContainText('Instrument 2 of 2'); - await expect(instrumentPage.seriesProgressBar).toHaveAttribute('aria-valuenow', '1'); - await instrumentPage.completeHappinessQuestionnaire(); - await instrumentPage.submit(); - - await expect(instrumentPage.seriesCompletionHeading).toBeVisible(); - await expect(instrumentPage.seriesProgress).toBeHidden(); - }); }); diff --git a/testing/src/specs/sessions.spec.ts b/testing/src/specs/sessions.spec.ts index b9f681224..e89b3374d 100644 --- a/testing/src/specs/sessions.spec.ts +++ b/testing/src/specs/sessions.spec.ts @@ -1,101 +1,6 @@ -import type { $CreateSessionData } from '@opendatacapture/schemas/session'; -import type { $CreateSubjectData } from '@opendatacapture/schemas/subject'; - -import { ApiClient } from '../support/api-client'; import { PHONE_VIEWPORT } from '../support/constants'; import { expect, test } from '../support/fixtures'; -test.describe('sessions', () => { - // A session was previously given its groupId only when the subject was not already a member of - // that group, so every visit after a subject's first was created without one. A group manager's - // Session rule is `{ groupId: { in: [...] } }`, which made those sessions invisible to them and - // uncounted by every group-scoped query, the dashboard trends included. - // - // Seeded into a group of its own, so the count is exactly what this test created. - test("should keep a returning subject's later sessions visible to their group manager", async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [group.id] }); - const accessToken = await ApiClient.login(apiRequestContext, credentials); - const subjectId = `revisit-${uniqueId}`; - - await api.createSession(group.id, { id: subjectId }); - await api.createSession(group.id, { id: subjectId }); - - const response = await apiRequestContext.get(`/api/v1/sessions?groupId=${group.id}`, { - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status()).toBe(200); - const sessions = (await response.json()) as { subjectId: string }[]; - expect(sessions.filter((session) => session.subjectId === subjectId)).toHaveLength(2); - }); - - // Session creation resolves its subjects in a batch, and the batched path once kept only their ids. - test('should create a new subject with the demographics its first session names', async ({ - adminToken, - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup(); - const subjectData: $CreateSubjectData = { - dateOfBirth: new Date('1990-05-17T00:00:00.000Z'), - firstName: 'Ada', - id: `clinical-${uniqueId}`, - lastName: 'Lovelace', - sex: 'FEMALE' - }; - - await api.createSession(group.id, subjectData); - - const response = await apiRequestContext.get(`/api/v1/subjects/${subjectData.id}`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - - expect(response.status()).toBe(200); - expect(await response.json()).toMatchObject({ - dateOfBirth: '1990-05-17T00:00:00.000Z', - firstName: 'Ada', - lastName: 'Lovelace', - sex: 'FEMALE' - }); - }); - - test('should refuse to create a session in a group the caller does not belong to, and write nothing', async ({ - adminToken, - api, - apiRequestContext, - uniqueId - }) => { - const ownGroup = await api.createGroup(); - const foreignGroup = await api.createGroup(); - const { credentials } = await api.createUser({ basePermissionLevel: 'GROUP_MANAGER', groupIds: [ownGroup.id] }); - const accessToken = await ApiClient.login(apiRequestContext, credentials); - const subjectId = `foreign-${uniqueId}`; - const data: $CreateSessionData = { - date: new Date(), - groupId: foreignGroup.id, - subjectData: { id: subjectId }, - type: 'IN_PERSON' - }; - - const response = await apiRequestContext.post('/api/v1/sessions', { - data, - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status()).toBe(404); - const subjectResponse = await apiRequestContext.get(`/api/v1/subjects/${subjectId}`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(subjectResponse.status()).toBe(404); - }); -}); - test.describe('sessions on a phone', () => { test.use({ viewport: PHONE_VIEWPORT }); diff --git a/testing/src/specs/start-session.spec.ts b/testing/src/specs/start-session.spec.ts index 9d410871d..251c35c10 100644 --- a/testing/src/specs/start-session.spec.ts +++ b/testing/src/specs/start-session.spec.ts @@ -1,118 +1,11 @@ -import type { Page } from '@playwright/test'; - import { StartSessionPage } from '../pages/_app/session/start-session.page'; -import { ApiClient } from '../support/api-client'; import { expect, test } from '../support/fixtures'; -// The identifier combobox is filled from this endpoint. It must never carry personal information, -// and must list only the requested group's subjects, and only those the caller may read. -test.describe('custom identifier suggestions', () => { - test('should list a group’s custom-id subjects but not those identified by personal information', async ({ - api, - uniqueId - }) => { - const group = await api.createGroup(); - const bareId = `bare-${uniqueId}`; - const customId = `custom-${uniqueId}`; - const personalInfoId = `personal-${uniqueId}`; - // The start-session form sends a custom-id subject's date of birth and sex, but no name. - await api.createSession(group.id, { id: bareId }); - await api.createSession(group.id, { dateOfBirth: new Date('1990-01-01'), id: customId, sex: 'MALE' }); - await api.createSession(group.id, { - dateOfBirth: new Date('1990-01-01'), - firstName: 'Ada', - id: personalInfoId, - lastName: 'Lovelace', - sex: 'FEMALE' - }); - - const ids = await api.findSubjectCustomIds(group.id); - - expect(ids).toEqual(expect.arrayContaining([bareId, customId])); - expect(ids).not.toContain(personalInfoId); - }); - - test('should not list custom-id subjects from another group', async ({ api, uniqueId }) => { - const group = await api.createGroup(); - const otherGroup = await api.createGroup(); - const otherGroupId = `other-${uniqueId}`; - await api.createSession(otherGroup.id, { id: otherGroupId }); - - expect(await api.findSubjectCustomIds(group.id)).not.toContain(otherGroupId); - }); - - test('should list nothing to a group manager asking for a group they are not a member of', async ({ - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup(); - const subjectId = `private-${uniqueId}`; - await api.createSession(group.id, { id: subjectId }); - const { credentials } = await api.createUser({ - basePermissionLevel: 'GROUP_MANAGER', - groupIds: [(await api.createGroup()).id] - }); - const accessToken = await ApiClient.login(apiRequestContext, credentials); - - const response = await apiRequestContext.get(`/api/v1/subjects/groups/${group.id}/custom-ids`, { - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status()).toBe(200); - expect(await response.json()).toStrictEqual([]); - }); - - test('should list custom ids scoped to the default group, but not those scoped to a named group, when no group is given', async ({ - api, - uniqueId - }) => { - const rootScopedId = `root$default-${uniqueId}`; - const groupScopedId = `Clinic$named-${uniqueId}`; - await api.createSession(null, { id: rootScopedId }); - await api.createSession((await api.createGroup()).id, { id: groupScopedId }); - - const ids = await api.findDefaultGroupSubjectCustomIds(); - - expect(ids).toContain(rootScopedId); - expect(ids).not.toContain(groupScopedId); - }); - - test('should still list a default group custom id after its subject is seen in a group', async ({ - api, - uniqueId - }) => { - const rootScopedId = `root$regrouped-${uniqueId}`; - await api.createSession(null, { id: rootScopedId }); - // Starting a session in a group adds that group to the subject, but leaves its id alone. - await api.createSession((await api.createGroup()).id, { id: rootScopedId }); - - expect(await api.findDefaultGroupSubjectCustomIds()).toContain(rootScopedId); - }); - - test('should list no default group subjects to a group manager outside their groups', async ({ - api, - apiRequestContext, - uniqueId - }) => { - await api.createSession(null, { id: `root$default-${uniqueId}` }); - const { credentials } = await api.createUser({ - basePermissionLevel: 'GROUP_MANAGER', - groupIds: [(await api.createGroup()).id] - }); - const accessToken = await ApiClient.login(apiRequestContext, credentials); - - const response = await apiRequestContext.get('/api/v1/subjects/default-group/custom-ids', { - headers: { Authorization: `Bearer ${accessToken}` } - }); - - expect(response.status()).toBe(200); - expect(await response.json()).toStrictEqual([]); - }); -}); - +// The form's own validation and its handling of an existing subject's details are covered by +// `apps/web/src/components/StartSessionForm/__tests__/StartSessionForm.test.tsx`, and the identifier +// suggestions it is filled from by `apps/api/test/suites/07-subject-custom-ids.suite.ts`. test.describe('start session', () => { - test('should display the start session form @smoke', async ({ getPageModel }) => { + test('should display the start session form', async ({ getPageModel }) => { const startSessionPage = await getPageModel('/session/start-session'); await expect(startSessionPage.pageHeader).toBeVisible(); await expect(startSessionPage.pageHeader).toContainText('Start Session'); @@ -130,155 +23,19 @@ test.describe('start session', () => { await expect(startSessionPage.successMessage).toBeVisible(); }); - test('should start a session using a custom identifier', async ({ getPageModel, uniqueId }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.fillCustomIdentifier(`custom-${uniqueId}`, 'Male'); - await startSessionPage.submitForm(); - - await expect(startSessionPage.successMessage).toBeVisible(); - }); - - // The identifier combobox offers the subjects already enrolled in the group, so a brand new - // subject's identifier necessarily matches no option. Clicking away used to discard it. - test('should keep a custom identifier matching no existing subject when the options popup is dismissed', async ({ - getPageModel, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.typeSubjectId(`unmatched-${uniqueId}`); - await startSessionPage.dismissSubjectIdOptions(); - - await expect(startSessionPage.subjectIdField).toHaveValue(`unmatched-${uniqueId}`); - - await startSessionPage.fillSessionDetails('Male'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - }); - - test('should start a session for a subject chosen from the existing custom identifiers', async ({ - getPageModel, - page, - uniqueId - }) => { - const identifier = `returning-${uniqueId}`; - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.fillCustomIdentifier(identifier, 'Male'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - // The subject only becomes an option once it exists, and the options are read by the route - // loader, so the second visit has to be a fresh load rather than a client-side navigation. - await startSessionPage.endSession(); - await page.reload(); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.typeSubjectId(identifier); - await startSessionPage.subjectIdOption(identifier).click(); - await expect(startSessionPage.subjectIdField).toHaveValue(identifier); - - // The first session recorded the subject's date of birth and sex, so the form fills those in. - await startSessionPage.fillSessionTiming(); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - }); - - test('should suggest default group subjects to an admin who belongs to no group, even once seen in a group', async ({ + // The pattern is a group setting the form reads through the API, so it is seeded on the group + // rather than handed to the form. The identifier combobox offers the subjects already enrolled in + // the group, so a brand new subject's identifier necessarily matches no option, and clicking away + // used to discard it. + test("should start a session with a new custom identifier once it matches the group's pattern", async ({ api, authenticateAs, page, uniqueId }) => { - const identifier = `default-${uniqueId}`; - // With no current group the form scopes a custom id to the default group, as `root$`. - await api.createSession(null, { id: `root$${identifier}` }); - await api.createSession((await api.createGroup()).id, { id: `root$${identifier}` }); - const { credentials } = await api.createUser({ basePermissionLevel: 'ADMIN', groupIds: [] }); - await authenticateAs(credentials); - await page.goto('/session/start-session'); - - const startSessionPage = new StartSessionPage(page); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.typeSubjectId(identifier); - - await expect(startSessionPage.subjectIdOption(identifier)).toBeVisible(); - }); - - test('should refuse a date of birth in the future with that error alone, not a contradictory required-field one', async ({ - getPageModel, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Future${uniqueId}`, `Subject${uniqueId}`, 'Male'); - await startSessionPage.fillDateOfBirth(`${new Date().getFullYear() + 1}-01-01`); - await startSessionPage.submitForm(); - - await expect(startSessionPage.errorMessages).toHaveCount(1); - await expect(startSessionPage.errorMessages).toHaveText('Date of birth must be in the past'); - await expect(startSessionPage.successMessage).toHaveCount(0); - }); - - test('should show a required-field error for every missing field when submitting the personal information form empty', async ({ - getPageModel - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.submitForm(); - - // First name, last name, sex and date of birth are all required for this method. - await expect(startSessionPage.errorMessages).toHaveCount(4); - for (const message of await startSessionPage.errorMessages.allTextContents()) { - expect(message).toBe('This field is required'); - } - await expect(startSessionPage.successMessage).toBeHidden(); - }); - - test('should show a required-field error for a blank custom identifier', async ({ getPageModel }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.fillSubjectDetails('MALE'); - await startSessionPage.submitForm(); - - await expect(startSessionPage.errorMessages).toHaveCount(1); - await expect(startSessionPage.errorMessages).toHaveText('This field is required'); - }); - - test('should reject a custom identifier containing an illegal character with that error alone, not a contradictory required-field one', async ({ - getPageModel - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.typeSubjectId('abc$def'); - await startSessionPage.fillSubjectDetails('MALE'); - await startSessionPage.submitForm(); - - await expect(startSessionPage.errorMessages).toHaveCount(1); - await expect(startSessionPage.subjectIdErrors).toHaveText('Illegal character: $'); - await expect(startSessionPage.successMessage).toBeHidden(); - }); - - test("should enforce the group's identifier pattern, yet still reject an illegal character in an identifier that pattern accepts", async ({ - api, - authenticateAs, - page - }) => { + const pattern = '^custom-[0-9a-f]+$'; const group = await api.createGroup({ - settings: { defaultIdentificationMethod: 'CUSTOM_ID', idValidationRegex: '^[a-z$]+$' } + settings: { defaultIdentificationMethod: 'CUSTOM_ID', idValidationRegex: pattern } }); const { credentials } = await api.createUser({ groupIds: [group.id] }); await authenticateAs(credentials); @@ -287,76 +44,43 @@ test.describe('start session', () => { await startSessionPage.goto('/session/start-session'); await startSessionPage.sessionForm.waitFor({ state: 'visible' }); await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.typeSubjectId('ABC'); - await startSessionPage.fillSubjectDetails('MALE'); + await startSessionPage.fillCustomIdentifier(`CUSTOM-${uniqueId}`, 'Male'); await startSessionPage.submitForm(); - await expect(startSessionPage.subjectIdErrors).toHaveText('Must match regular expression: ^[a-z$]+$'); + await expect(startSessionPage.subjectIdErrors).toHaveText(`Must match regular expression: ${pattern}`); - await startSessionPage.typeSubjectId('abc$def'); + await startSessionPage.typeSubjectId(`custom-${uniqueId}`); await startSessionPage.dismissSubjectIdOptions(); - await startSessionPage.submitForm(); - - await expect(startSessionPage.errorMessages).toHaveCount(1); - await expect(startSessionPage.subjectIdErrors).toHaveText('Illegal character: $'); - await expect(startSessionPage.successMessage).toBeHidden(); - }); - - test('should disable the Start Session nav link while a clinical session is active', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - await startSessionPage.fillCustomIdentifier(`active-${uniqueId}`, 'Male'); + await expect(startSessionPage.subjectIdField).toHaveValue(`custom-${uniqueId}`); await startSessionPage.submitForm(); await expect(startSessionPage.successMessage).toBeVisible(); - - // There is no way to start a second session over an active one, so the link that would take you - // back to the form is disabled outright rather than showing the form again. Check it from a - // different route, since the nav items only re-derive their `disabled` state as an effect and a - // navigation is what reliably observes the settled state. - await page.getByTestId('nav-button-/dashboard').click(); - await page.waitForURL('**/dashboard'); - await expect(page.getByTestId('nav-button-/session/start-session')).toBeDisabled(); }); }); -// A custom id says nothing about the subject behind it, so the form fills in what an existing subject -// already records. These run as an admin with no group, whose custom ids carry the `root$` scope. +// A custom id says nothing about the subject behind it, so the form fills in what an existing +// subject already records. An admin with no group has their custom ids scoped as `root$`. test.describe('existing subject details', () => { - const openAsGrouplessAdmin = async ({ + test('should suggest a default group subject to an admin in no group, even once seen in a group, and fill in and lock its details', async ({ api, authenticateAs, - page - }: { - api: ApiClient; - authenticateAs: (credentials: Awaited>['credentials']) => Promise; - page: Page; + page, + uniqueId }) => { + const identifier = `known-${uniqueId}`; + // Midday UTC, so the date the form shows is the same in any timezone the suite runs in. + await api.createSession(null, { + dateOfBirth: new Date('1990-06-15T12:00:00Z'), + id: `root$${identifier}`, + sex: 'FEMALE' + }); + // Starting a session in a group adds that group to the subject, but leaves its id alone. + await api.createSession((await api.createGroup()).id, { id: `root$${identifier}` }); const { credentials } = await api.createUser({ basePermissionLevel: 'ADMIN', groupIds: [] }); await authenticateAs(credentials); - await page.goto('/session/start-session'); + const startSessionPage = new StartSessionPage(page); + await startSessionPage.goto('/session/start-session'); await startSessionPage.sessionForm.waitFor({ state: 'visible' }); await startSessionPage.selectIdentificationMethod('CUSTOM_ID'); - return startSessionPage; - }; - - // Midday UTC, so the date the form shows is the same in any timezone the suite runs in. - const dateOfBirth = new Date('1990-06-15T12:00:00Z'); - - test('should fill in and lock the details of a subject chosen from the suggestions', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const identifier = `known-${uniqueId}`; - await api.createSession(null, { dateOfBirth, id: `root$${identifier}`, sex: 'FEMALE' }); - const startSessionPage = await openAsGrouplessAdmin({ api, authenticateAs, page }); - await startSessionPage.typeSubjectId(identifier); await startSessionPage.subjectIdOption(identifier).click(); @@ -368,60 +92,4 @@ test.describe('existing subject details', () => { await startSessionPage.submitForm(); await expect(startSessionPage.successMessage).toBeVisible(); }); - - test('should fill in the details when a typed identifier matches an existing subject, as picking it would', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const identifier = `typed-${uniqueId}`; - await api.createSession(null, { dateOfBirth, id: `root$${identifier}`, sex: 'MALE' }); - const startSessionPage = await openAsGrouplessAdmin({ api, authenticateAs, page }); - - await startSessionPage.typeSubjectId(identifier); - await startSessionPage.dismissSubjectIdOptions(); - - await expect(startSessionPage.sexField).toHaveValue('MALE'); - await expect(startSessionPage.sexTrigger).toBeDisabled(); - }); - - test('should clear and unlock the details when the identifier changes to one no subject has', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const identifier = `before-${uniqueId}`; - await api.createSession(null, { dateOfBirth, id: `root$${identifier}`, sex: 'FEMALE' }); - const startSessionPage = await openAsGrouplessAdmin({ api, authenticateAs, page }); - await startSessionPage.typeSubjectId(identifier); - await startSessionPage.subjectIdOption(identifier).click(); - await expect(startSessionPage.sexTrigger).toBeDisabled(); - - await startSessionPage.typeSubjectId(`new-${uniqueId}`); - await startSessionPage.dismissSubjectIdOptions(); - - await expect(startSessionPage.dateOfBirthField).toHaveValue(''); - await expect(startSessionPage.dateOfBirthField).toBeEnabled(); - await expect(startSessionPage.sexField).not.toHaveValue('FEMALE'); - await expect(startSessionPage.sexTrigger).toBeEnabled(); - }); - - test('should lock only the details the subject records, leaving the rest to fill in', async ({ - api, - authenticateAs, - page, - uniqueId - }) => { - const identifier = `partial-${uniqueId}`; - await api.createSession(null, { id: `root$${identifier}`, sex: 'MALE' }); - const startSessionPage = await openAsGrouplessAdmin({ api, authenticateAs, page }); - - await startSessionPage.typeSubjectId(identifier); - await startSessionPage.subjectIdOption(identifier).click(); - - await expect(startSessionPage.sexTrigger).toBeDisabled(); - await expect(startSessionPage.dateOfBirthField).toBeEnabled(); - }); }); diff --git a/testing/src/specs/static-pages.spec.ts b/testing/src/specs/static-pages.spec.ts index 312b4a475..2cba66575 100644 --- a/testing/src/specs/static-pages.spec.ts +++ b/testing/src/specs/static-pages.spec.ts @@ -5,71 +5,10 @@ import { contactEmail } from '../support/env'; import { expect, test } from '../support/fixtures'; test.describe('about', () => { - test('should display web client, API and gateway release information', async ({ getPageModel }) => { - const aboutPage = await getPageModel('/about'); - await expect(aboutPage.pageHeader).toContainText('Platform Information'); - await expect(aboutPage.platformTitle).toBeVisible(); - await expect(aboutPage.webClientInfo).toContainText('Version:'); - await expect(aboutPage.coreApiInfo).toContainText('Version:'); - // The gateway is enabled in every dev/test environment (GATEWAY_ENABLED in .env), so its - // healthcheck should have resolved rather than the section only reflecting the config flag. - await expect(aboutPage.gatewayInfo).toContainText('Enabled: Yes'); - await expect(aboutPage.gatewayInfo).toContainText('Status: 200'); - }); - - // Nested under `_app` despite being "static" content, so it shares that layout's auth guard. - test('should redirect an unauthenticated visitor to login', async ({ page }) => { - await page.goto('/about'); - await expect(page).toHaveURL('/auth/login'); - }); -}); - -test.describe('contact', () => { - test('should display the contact form', async ({ getPageModel }) => { - const contactPage = await getPageModel('/contact'); - await expect(contactPage.pageHeader).toContainText('Contact Us'); - await expect(contactPage.reasonField).toBeVisible(); - await expect(contactPage.messageField).toBeVisible(); - }); - - test('should redirect an unauthenticated visitor to login', async ({ page }) => { - await page.goto('/contact'); - await expect(page).toHaveURL('/auth/login'); - }); - - test('should require a reason and a message before submitting', async ({ getPageModel, page }) => { - const contactPage = await getPageModel('/contact'); - await contactPage.submitButton.click(); - - // Both fields report it: the message textarea, and the reason select — whose unselected value - // raises an issue code that used to fall through to zod's untranslated default. - const errorMessages = page.getByTestId('error-message-text'); - await expect(errorMessages.first()).toBeVisible(); - for (const message of await errorMessages.allTextContents()) { - expect(message).toBe('This field is required'); - } - }); - - // The page never renders the contact address as visible text; it only surfaces in the mailto link - // a submission opens, so that link is the one place worth asserting the configured address reaches. - test('should open a mailto link to the configured contact address on submit', async ({ getPageModel, uniqueId }) => { - const message = `Test message ${uniqueId}`; - const contactPage = await getPageModel('/contact'); - await contactPage.captureMailtoLink(); - await contactPage.selectReason('Bug Report'); - await contactPage.messageField.fill(message); - await contactPage.submitButton.click(); - - await expect(async () => { - const mailtoLink = await contactPage.getCapturedMailtoLink(); - expect(mailtoLink).toContain(`mailto:${contactEmail}`); - expect(mailtoLink).toContain(encodeURIComponent(message)); - }).toPass(); - }); -}); - -test.describe('about uptime', () => { - test('should fetch the API uptime when the page is opened rather than reuse the value cached on app load', async ({ + // The uptime is fetched when the page opens rather than reusing the value cached on app load. The + // router keeps the About match cached for five minutes, so a quick revisit is where a stale uptime + // would survive; the API's response is rewritten to stand in for a restart between visits. + test('should show live release and uptime information for the web client, API and gateway on every visit', async ({ getPageModel, page }) => { @@ -83,24 +22,19 @@ test.describe('about uptime', () => { const { uptime } = (await (await setupResponse).json()) as SetupState; const aboutPage = new AboutPage(page); + await expect(aboutPage.pageHeader).toContainText('Platform Information'); + await expect(aboutPage.platformTitle).toBeVisible(); + await expect(aboutPage.webClientInfo).toContainText('Version:'); + await expect(aboutPage.coreApiInfo).toContainText('Version:'); + // The gateway is enabled in every dev/test environment (GATEWAY_ENABLED in .env), so its + // healthcheck should have resolved rather than the section only reflecting the config flag. + await expect(aboutPage.gatewayInfo).toContainText('Enabled: Yes'); + await expect(aboutPage.gatewayInfo).toContainText('Status: 200'); await expect(aboutPage.coreApiInfo).toContainText('Uptime:'); expect(await aboutPage.getCoreApiUptimeSeconds()).toBeGreaterThanOrEqual(uptime); - }); - // The router keeps the About match cached for five minutes, so a quick revisit is where a stale - // uptime would survive; the API's response is rewritten to stand in for a restart between visits. - test('should show the uptime fetched on a revisit rather than the one from the previous visit', async ({ - getPageModel, - page - }) => { - const dashboardPage = await getPageModel('/dashboard'); - const aboutPage = new AboutPage(page); - await dashboardPage.sidebar.getByTestId('user-dropup-trigger').click(); - await page.getByTestId('user-dropup-about').click(); - await expect(aboutPage.coreApiInfo).toContainText('Uptime:'); await page.goBack(); await expect(page).toHaveURL('/dashboard'); - const restartedUptime = 100 * 3600; await page.route('**/v1/setup', async (route) => { const response = await route.fetch(); @@ -112,31 +46,23 @@ test.describe('about uptime', () => { }); }); -test.describe('about uptime counter', () => { - test('should show the starting uptime plus the seconds elapsed since the page opened', async ({ +test.describe('contact', () => { + // The page never renders the contact address as visible text; it only surfaces in the mailto link + // a submission opens, so that link is the one place worth asserting the configured address reaches. + test('should open a mailto link to the configured contact address on submit', async ({ getPageModel, - page + page, + uniqueId }) => { - await page.clock.install(); - const dashboardPage = await getPageModel('/dashboard'); - await dashboardPage.sidebar.getByTestId('user-dropup-trigger').click(); - const aboutMenuItem = page.getByTestId('user-dropup-about'); - await expect(aboutMenuItem).toBeVisible(); - - // Paused before About mounts, so the counter's first tick waits for the test to advance a full - // second; the 10ms steps only flush the zero-delay timers the navigation and its fetch schedule. - // The target leaves a second's margin because the page clock keeps running while it travels - // back here, and pauseAt rejects a time that has already passed. - await page.clock.pauseAt(await page.evaluate(() => Date.now() + 1000)); - await aboutMenuItem.click(); - const aboutPage = new AboutPage(page); - await expect(async () => { - await page.clock.runFor(10); - await expect(aboutPage.coreApiInfo).toContainText('Uptime:', { timeout: 100 }); - }).toPass(); + const message = `Test message ${uniqueId}`; + const contactPage = await getPageModel('/contact'); + await contactPage.captureMailtoLink(); + await contactPage.selectReason('Bug Report'); + await contactPage.messageField.fill(message); + await contactPage.submitButton.click(); - const initialUptime = await aboutPage.getCoreApiUptimeSeconds(); - await page.clock.runFor(3000); - await expect.poll(() => aboutPage.getCoreApiUptimeSeconds()).toBe(initialUptime + 3); + const mailtoLink = await (await page.waitForFunction(() => window.__capturedMailto)).jsonValue(); + expect(mailtoLink).toContain(`mailto:${contactEmail}`); + expect(mailtoLink).toContain(encodeURIComponent(message)); }); }); diff --git a/testing/src/specs/subject-detail.spec.ts b/testing/src/specs/subject-detail.spec.ts index 3f7aa50d7..2548b9343 100644 --- a/testing/src/specs/subject-detail.spec.ts +++ b/testing/src/specs/subject-detail.spec.ts @@ -1,70 +1,30 @@ -import type { Page } from '@playwright/test'; - -import { SubjectAssignmentsPage } from '../pages/_app/datahub/subjects/$subjectId/assignments.page'; import { SubjectGraphPage } from '../pages/_app/datahub/subjects/$subjectId/graph.page'; import { SubjectRecordDetailPage } from '../pages/_app/datahub/subjects/$subjectId/table/$recordId.page'; -import { RenderInstrumentPage } from '../pages/_app/instruments/render/$id.page'; -import { RemoteAssignmentPage } from '../pages/_app/session/remote-assignment.page'; +import { HAPPINESS_RECORD } from '../support/constants'; import { expect, test } from '../support/fixtures'; -import type { GetPageModel } from '../support/fixtures'; - // Title shown on the instrument card in the showcase (`details.title`), which differs from the // title shown while running it (`clientDetails.title`, "Questionnaire on Happiness"). const INSTRUMENT_TITLE = 'Happiness Questionnaire'; const MEASURE = 'Overall Satisfaction Score'; -/** - * Starts a session, completes the Happiness Questionnaire for it, and lands on the resulting - * subject's record table — the precondition every test in this file needs. Mirrors the flow in - * instrument-completion.spec.ts; factored out here since three tests need it. - */ -async function seedSubjectWithRecord(getPageModel: GetPageModel, page: Page, namePrefix: string): Promise { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`${namePrefix}Instrument`, `${namePrefix}Subject`, 'Female'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - // The active session lives in memory, so every step from here navigates via the sidebar; a - // hard navigation would drop the session and disable these nav buttons. - await page.getByTestId('nav-button-/instruments/accessible-instruments').click(); - await page.waitForURL('**/instruments/accessible-instruments'); - - const card = page.locator('[data-testid^="instrument-card-"]').filter({ hasText: INSTRUMENT_TITLE }).first(); - await expect(card).toBeVisible(); - await card.click(); - - const instrumentPage = new RenderInstrumentPage(page); - await instrumentPage.begin(); - await instrumentPage.completeHappinessQuestionnaire(); - await instrumentPage.submit(); - await expect(instrumentPage.summaryHeading).toBeVisible(); - - // The datahub nav entry is a group over the subject and instrument views; only the current - // subject's own link carries a path segment after `subjects`. - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); -} - test.describe('subject detail', () => { - test('should plot a selected measure on the graph tab for a subject with a completed record @smoke', async ({ - getPageModel, + test('should plot a selected measure on the graph tab for a subject with a completed record', async ({ + api, + isolatedGroupManager, page, uniqueId }) => { - // Seeding through the UI, then driving three dropdowns, does not fit the default 30s on a - // two-core CI runner -- especially once `selectInstrument` spends any of it retrying. - test.slow(); - - await seedSubjectWithRecord(getPageModel, page, `Graph${uniqueId}`); - - await page.getByRole('link', { name: 'Graph' }).click(); - // The tab links carry the chart palette, so the glob has to allow a query string. - await page.waitForURL(/\/graph(\?|$)/); - + const group = await isolatedGroupManager(); + const subjectId = `graph-${uniqueId}`; + await api.uploadRecords(group.id, await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'), [ + { data: HAPPINESS_RECORD, date: new Date(), subjectId } + ]); + + // Raw navigation: the route adds the chart palette to the URL, which `goto`'s exact-URL check + // would reject. + await page.goto(`/datahub/subjects/${subjectId}/graph`); const graphPage = new SubjectGraphPage(page); await graphPage.selectInstrument(INSTRUMENT_TITLE); await graphPage.selectMeasure(MEASURE); @@ -74,58 +34,22 @@ test.describe('subject detail', () => { await expect(graphPage.measureDots(MEASURE)).toBeVisible(); }); - test('should list a remote assignment created for the subject and allow canceling it', async ({ - getPageModel, - page, - uniqueId - }) => { - const startSessionPage = await getPageModel('/session/start-session'); - await startSessionPage.sessionForm.waitFor({ state: 'visible' }); - await startSessionPage.selectIdentificationMethod('PERSONAL_INFO'); - await startSessionPage.fillSessionForm(`Assign${uniqueId}`, `Subject${uniqueId}`, 'Male'); - await startSessionPage.submitForm(); - await expect(startSessionPage.successMessage).toBeVisible(); - - await page.getByTestId('nav-button-/session/remote-assignment').click(); - await page.waitForURL('**/session/remote-assignment'); - - const remoteAssignmentPage = new RemoteAssignmentPage(page); - await remoteAssignmentPage.clickFirstInstrumentCard(); - await remoteAssignmentPage.submitAssignmentForm(); - await expect(page.getByRole('link', { name: 'Assignment Link' })).toBeVisible(); - - // The result slider is a modal sheet, so it blocks clicks on the sidebar underneath until dismissed. - await page.keyboard.press('Escape'); - await page.locator('[data-testid^="nav-button-/datahub/subjects/"]').click(); - await page.waitForURL('**/datahub/subjects/**/table'); - - await page.getByRole('link', { name: 'Assignments' }).click(); - await page.waitForURL(/\/assignments(\?|$)/); - - const assignmentsPage = new SubjectAssignmentsPage(page); - await expect(assignmentsPage.assignmentRows).toHaveCount(1); - await expect(assignmentsPage.assignmentRows.first()).toContainText('Outstanding'); - - await assignmentsPage.assignmentRows.first().click(); - await expect(assignmentsPage.assignmentLink).toBeVisible(); - await assignmentsPage.cancelButton.click(); - - await expect(assignmentsPage.assignmentRows.first()).toContainText('Canceled'); - }); - - test('should show the subject, instrument and results sections when opening a record from the table', async ({ - getPageModel, + test('should show the subject, instrument and results sections of a record', async ({ + api, + isolatedGroupManager, page, uniqueId }) => { - await seedSubjectWithRecord(getPageModel, page, `Record${uniqueId}`); - - await page.getByRole('combobox').first().click(); - await page.getByRole('option', { name: INSTRUMENT_TITLE }).click(); - - const row = page.getByTestId('data-table-body').getByTestId('data-table-row').first(); - await expect(row).toBeVisible(); - await row.dblclick(); + const group = await isolatedGroupManager(); + const session = await api.createSession(group.id, { id: `record-${uniqueId}` }); + const record = await api.createRecord( + group.id, + await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'), + session, + HAPPINESS_RECORD + ); + + await page.goto(`/datahub/subjects/${session.subjectId}/table/${record.id}`); const recordDetailPage = new SubjectRecordDetailPage(page); await expect(recordDetailPage.subjectGroupHeading).toBeVisible(); diff --git a/testing/src/specs/table-sorting.spec.ts b/testing/src/specs/table-sorting.spec.ts deleted file mode 100644 index 27ae0874f..000000000 --- a/testing/src/specs/table-sorting.spec.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { ApiClient } from '../support/api-client'; -import { ADMIN } from '../support/constants'; -import { expect, test } from '../support/fixtures'; - -test.describe('table sorting', () => { - test.use({ actingRole: 'ADMIN' }); - - test('should offer a working sort on every column of the audit log, ordered by the server', async ({ - getPageModel, - page - }) => { - const auditLogsPage = await getPageModel('/admin/audit/logs'); - const head = auditLogsPage.dataTable.getByTestId('data-table-head'); - - for (const [name, sortBy] of [ - ['User', 'user'], - ['Active Group', 'group'], - ['Action', 'action'], - ['Entity', 'entity'] - ] as const) { - const response = page.waitForResponse((res) => res.url().includes(`sortBy=${sortBy}`)); - await head.getByRole('button', { name }).click(); - expect((await response).status()).toBe(200); - await expect(auditLogsPage.dataTable.getByTestId('data-table-row').first()).toBeVisible(); - } - }); - - // Status is derived and failure count is an array length, so these reach Mongo as a composite - // ordering only a real database can prove accepted. - test('should accept every sync log ordering against the real database', async ({ apiRequestContext }) => { - const token = await ApiClient.login(apiRequestContext, { password: ADMIN.password, username: ADMIN.username }); - for (const sortBy of ['action', 'createdAt', 'failedCount', 'importedCount', 'repoName', 'status']) { - const response = await apiRequestContext.get('/api/v1/instrument-repos/sync/logs', { - headers: { Authorization: `Bearer ${token}` }, - params: { sortBy } - }); - expect(response.status()).toBe(200); - } - }); - - test('should reorder the users when a column header is clicked', async ({ authenticateAs, page }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/users'); - const head = page.getByTestId('data-table-head'); - const firstRow = page.getByTestId('data-table-row').first(); - await head.getByRole('button', { name: 'Username' }).click(); - const ascending = await firstRow.textContent(); - await head.getByRole('button', { name: 'Username' }).click(); - await expect(firstRow).not.toHaveText(ascending!); - }); - - test('should offer a sort on every column of the group table', async ({ authenticateAs, page }) => { - await authenticateAs('ADMIN'); - await page.goto('/admin/groups'); - const head = page.getByTestId('data-table-head'); - await expect(head.getByRole('button', { name: 'Group Name' })).toBeVisible(); - await expect(head.getByRole('button', { name: 'Group Type' })).toBeVisible(); - }); - - test('should offer a sort on every column of the instruments table', async ({ getPageModel, page }) => { - await getPageModel('/admin/instruments'); - const head = page.getByTestId('data-table-head'); - for (const name of ['Name', 'Source', 'Groups Using It', 'Kind', 'Edition', 'Added']) { - await expect(head.getByRole('button', { name })).toBeVisible(); - } - }); -}); diff --git a/testing/src/specs/upload.spec.ts b/testing/src/specs/upload.spec.ts index 15d6a88f3..f6d31886e 100644 --- a/testing/src/specs/upload.spec.ts +++ b/testing/src/specs/upload.spec.ts @@ -1,395 +1,123 @@ import { readFile } from 'node:fs/promises'; -import type { UploadInstrumentRecordsData } from '@opendatacapture/schemas/instrument-records'; - import { UploadInstrumentPage } from '../pages/_app/upload/$instrumentId.page'; import { UploadPage } from '../pages/_app/upload/index.page'; -import { HAPPINESS_RECORD } from '../support/constants'; import { expect, test } from '../support/fixtures'; // The plain "Happiness Questionnaire" (kind FORM); a similarly-named consent-bundled edition also // exists in the catalog but is kind SERIES, so it never appears in the upload instrument list. const INSTRUMENT_TITLE = 'Happiness Questionnaire'; +const HAPPINESS_HEADER = + 'subjectID,date,personalLifeSatisfaction,professionalLifeSatisfaction,isSatisfiedOverall,reasonNotSatisfied,causesOfDissatisfaction'; + test.describe('upload', () => { - test('should upload a valid CSV and create a record for the subject it names @smoke', async ({ + test('should report an invalid CSV, then accept a corrected one and create a record for the subject it names', async ({ getPageModel, page, uniqueId }) => { const subjectId = `Upload${uniqueId}`; - const csv = [ - 'subjectID,date,personalLifeSatisfaction,professionalLifeSatisfaction,isSatisfiedOverall,reasonNotSatisfied,causesOfDissatisfaction', - `${subjectId},2024-01-15,8,7,true,,` - ].join('\n'); - const uploadPage = await getPageModel('/upload'); await uploadPage.selectInstrument(INSTRUMENT_TITLE); await page.waitForURL('**/upload/**'); const uploadInstrumentPage = new UploadInstrumentPage(page); await expect(uploadInstrumentPage.pageHeader).toContainText(INSTRUMENT_TITLE); - await uploadInstrumentPage.uploadFile({ buffer: Buffer.from(csv), mimeType: 'text/csv', name: 'happiness.csv' }); - await uploadInstrumentPage.submitButton.click(); - - await expect(page.getByText('Success')).toBeVisible(); - - // The subject list truncates the displayed id to 9 characters by default, so match on the prefix. - const datahubPage = await getPageModel('/datahub/subjects'); - await datahubPage.searchInput.fill(subjectId); - await expect(page.getByTestId('data-table-row').filter({ hasText: subjectId.slice(0, 9) })).toBeVisible(); - }); - - test('should show a validation error and let the user try again after uploading a CSV with an invalid value', async ({ - getPageModel, - page, - uniqueId - }) => { - const csv = [ - 'subjectID,date,personalLifeSatisfaction,professionalLifeSatisfaction,isSatisfiedOverall,reasonNotSatisfied,causesOfDissatisfaction', - `BadUpload${uniqueId},2024-01-15,not-a-number,7,true,,` - ].join('\n'); - - const uploadPage = await getPageModel('/upload'); - await uploadPage.selectInstrument(INSTRUMENT_TITLE); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - await uploadInstrumentPage.uploadFile({ buffer: Buffer.from(csv), mimeType: 'text/csv', name: 'bad.csv' }); + await uploadInstrumentPage.uploadFile({ + buffer: Buffer.from([HAPPINESS_HEADER, `${subjectId},2024-01-15,not-a-number,7,true,,`].join('\n')), + mimeType: 'text/csv', + name: 'bad.csv' + }); await uploadInstrumentPage.submitButton.click(); - await expect(uploadInstrumentPage.errorHeading).toBeVisible(); await expect(page.getByText("Invalid number type: 'not-a-number'")).toBeVisible(); await uploadInstrumentPage.tryAgainButton.click(); await expect(uploadInstrumentPage.errorHeading).toBeHidden(); - await expect(uploadInstrumentPage.submitButton).toBeVisible(); - }); - - // A cell missing its closing parenthesis was once accepted with its last character cut off, so - // `quantity:12` was stored as 1. No seeded instrument has a record-array field, so the test stores - // its own before creating the group that will use it. - test('should reject a record array cell missing its closing parenthesis, naming its column', async ({ - api, - isolatedGroupManager, - page, - uniqueId - }) => { - const title = `Record Array Upload ${uniqueId}`; - await api.createInstrument( - formBundle({ - content: `{ - items: { - fieldset: { - name: { kind: 'string', label: 'Name', variant: 'input' }, - quantity: { kind: 'number', label: 'Quantity', variant: 'input' } - }, - kind: 'record-array', - label: 'Items' - } - }`, - name: `RECORD_ARRAY_UPLOAD_${uniqueId}`, - title, - validationSchema: 'z.object({ items: z.array(z.object({ name: z.string(), quantity: z.number() })) })' - }) - ); - await isolatedGroupManager(); - const csv = [ - 'subjectID,date,items', - `RecordArray${uniqueId},2024-01-15,"RECORD_ARRAY(name:item1,quantity:12"` - ].join('\n'); - - const uploadPage = new UploadPage(page); - await uploadPage.goto('/upload'); - await uploadPage.selectInstrument(title); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - await expect(uploadInstrumentPage.pageHeader).toContainText(title); - await uploadInstrumentPage.uploadFile({ buffer: Buffer.from(csv), mimeType: 'text/csv', name: 'items.csv' }); + await uploadInstrumentPage.uploadFile({ + buffer: Buffer.from([HAPPINESS_HEADER, `${subjectId},2024-01-15,8,7,true,,`].join('\n')), + mimeType: 'text/csv', + name: 'happiness.csv' + }); await uploadInstrumentPage.submitButton.click(); + await expect(page.getByText('Success')).toBeVisible(); - await expect(uploadInstrumentPage.errorHeading).toBeVisible(); - await expect( - page.getByText( - "Syntax error in RECORD_ARRAY declaration: RECORD_ARRAY(name:item1,quantity:12 at column name: 'items' and row number '1'" - ) - ).toBeVisible(); - }); - - // The template once failed with "Unexpected Error" for any field it could not describe, which named - // neither the field nor its type. - test('should name the column and Zod type of a field the template cannot describe', async ({ - api, - isolatedGroupManager, - page, - uniqueId - }) => { - const title = `Unsupported Field Upload ${uniqueId}`; - await api.createInstrument( - formBundle({ - content: `{ pair: { kind: 'string', label: 'Pair', variant: 'input' } }`, - name: `UNSUPPORTED_FIELD_UPLOAD_${uniqueId}`, - title, - validationSchema: 'z.object({ pair: z.tuple([z.string()]) })' - }) - ); - await isolatedGroupManager(); - - const uploadPage = new UploadPage(page); - await uploadPage.goto('/upload'); - await uploadPage.selectInstrument(title); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - await expect(uploadInstrumentPage.pageHeader).toContainText(title); - await uploadInstrumentPage.templateButton.click(); - - await expect(uploadInstrumentPage.templateErrorHeading).toBeVisible(); - await expect(page.getByText("Unsupported Zod type 'ZodTuple' at column name: 'pair'")).toBeVisible(); + // The subject list truncates the displayed id to 9 characters by default, so match on the prefix. + const datahubPage = await getPageModel('/datahub/subjects'); + await datahubPage.searchInput.fill(subjectId); + await expect(page.getByTestId('data-table-row').filter({ hasText: subjectId.slice(0, 9) })).toBeVisible(); }); - // A count recorded as a union of literals, or a native enum, once failed both the template and the - // import. Instruments reach the page built against either zod major, and each has its own path. - for (const zodEntry of ['index.js', 'v4.js'] as const) { - test.describe(`fixed-choice fields of a form written against zod@3.x/${zodEntry}`, () => { - let title: string; - - test.beforeEach(async ({ api, uniqueId }) => { - title = `Fixed Choice Upload ${uniqueId}`; - await api.createInstrument( - formBundle({ - content: `{ - putbacks: { kind: 'number', label: 'Putbacks', variant: 'input' }, - severity: { kind: 'number', label: 'Severity', variant: 'input' } - }`, - name: `FIXED_CHOICE_UPLOAD_${uniqueId}`, - title, - validationSchema: `z.object({ - putbacks: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]), - severity: z.nativeEnum({ 0: 'Mild', 1: 'Severe', Mild: 0, Severe: 1 }) - })`, - zodEntry - }) - ); - }); - - test('should list the allowed values of each field in the template', async ({ isolatedGroupManager, page }) => { - await isolatedGroupManager(); - const uploadPage = new UploadPage(page); - await uploadPage.goto('/upload'); - await uploadPage.selectInstrument(title); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - const downloadPromise = page.waitForEvent('download'); - await uploadInstrumentPage.templateButton.click(); - const download = await downloadPromise; - - const template = (await readFile(await download.path())).toString(); - expect(template).toBe('subjectID,date,putbacks,severity\r\nstring,yyyy-mm-dd,0/1/2/3,0/1'); - }); - - test('should store an uploaded value as the number its schema declares', async ({ - adminToken, - apiRequestContext, - isolatedGroupManager, - page, - uniqueId - }) => { - const group = await isolatedGroupManager(); - const csv = ['subjectID,date,putbacks,severity', `FixedChoice${uniqueId},2024-01-15,2,1`].join('\n'); - - const uploadPage = new UploadPage(page); - await uploadPage.goto('/upload'); - await uploadPage.selectInstrument(title); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - await uploadInstrumentPage.uploadFile({ buffer: Buffer.from(csv), mimeType: 'text/csv', name: 'choices.csv' }); - await uploadInstrumentPage.submitButton.click(); - await expect(page.getByText('Success')).toBeVisible(); - - const records = await apiRequestContext.get(`/api/v1/instrument-records?groupId=${group.id}`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(await records.json()).toMatchObject([{ data: { putbacks: 2, severity: 1 } }]); - }); - - test('should reject a value outside the allowed ones, naming its column and row', async ({ - isolatedGroupManager, - page, - uniqueId - }) => { - await isolatedGroupManager(); - const csv = ['subjectID,date,putbacks,severity', `FixedChoice${uniqueId},2024-01-15,7,1`].join('\n'); - - const uploadPage = new UploadPage(page); - await uploadPage.goto('/upload'); - await uploadPage.selectInstrument(title); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - await uploadInstrumentPage.uploadFile({ buffer: Buffer.from(csv), mimeType: 'text/csv', name: 'choices.csv' }); - await uploadInstrumentPage.submitButton.click(); - - await expect(uploadInstrumentPage.errorHeading).toBeVisible(); - await expect( - page.getByText("Invalid value '7': expected one of 0/1/2/3 at column name: 'putbacks' and row number '1'") - ).toBeVisible(); - }); - }); - } - - // The instrument guide validates a number-record field with a record keyed by an enum, which the - // page could neither describe nor import. + // Instruments reach the page built against either zod major, each through its own path, so one + // instrument per major carries every field kind that once failed the template or the import: a + // union of literals, a native enum, and a record keyed by an enum (the instrument guide's + // number-record field). The unit tests run the same zod, but only here does a bundle evaluated + // from the runtime reach them. for (const zodEntry of ['index.js', 'v4.js'] as const) { - test.describe(`record field of a form written against zod@3.x/${zodEntry}`, () => { - let title: string; - - test.beforeEach(async ({ api, uniqueId }) => { - title = `Record Upload ${uniqueId}`; - await api.createInstrument( - formBundle({ - content: `{ - ratings: { - items: { friday: { label: 'Friday' }, monday: { label: 'Monday' } }, - kind: 'number-record', - label: 'Ratings', - options: { 1: 'Poor', 2: 'Fair', 3: 'Good' }, - variant: 'likert' - } - }`, - name: `RECORD_UPLOAD_${uniqueId}`, - title, - validationSchema: `z.object({ - ratings: z.record(z.enum(['monday', 'friday']), z.union([z.literal(1), z.literal(2), z.literal(3)])) - })`, - zodEntry - }) - ); - }); - - test('should show every key of the record with its allowed values in the template', async ({ - isolatedGroupManager, - page - }) => { - await isolatedGroupManager(); - const uploadPage = new UploadPage(page); - await uploadPage.goto('/upload'); - await uploadPage.selectInstrument(title); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - const downloadPromise = page.waitForEvent('download'); - await uploadInstrumentPage.templateButton.click(); - const download = await downloadPromise; - - const template = (await readFile(await download.path())).toString(); - expect(template).toBe('subjectID,date,ratings\r\nstring,yyyy-mm-dd,"RECORD(monday: 1/2/3, friday: 1/2/3)"'); - }); - - test('should store an uploaded RECORD cell as an object of numbers', async ({ - adminToken, - apiRequestContext, - isolatedGroupManager, - page, - uniqueId - }) => { - const group = await isolatedGroupManager(); - const csv = ['subjectID,date,ratings', `Record${uniqueId},2024-01-15,"RECORD(monday: 3, friday: 1)"`].join( - '\n' - ); - - const uploadPage = new UploadPage(page); - await uploadPage.goto('/upload'); - await uploadPage.selectInstrument(title); - await page.waitForURL('**/upload/**'); - - const uploadInstrumentPage = new UploadInstrumentPage(page); - await uploadInstrumentPage.uploadFile({ buffer: Buffer.from(csv), mimeType: 'text/csv', name: 'ratings.csv' }); - await uploadInstrumentPage.submitButton.click(); - await expect(page.getByText('Success')).toBeVisible(); - - const records = await apiRequestContext.get(`/api/v1/instrument-records?groupId=${group.id}`, { - headers: { Authorization: `Bearer ${adminToken}` } - }); - expect(await records.json()).toMatchObject([{ data: { ratings: { friday: 1, monday: 3 } } }]); + test(`should describe and import the fixed-choice and record fields of a form written against zod@3.x/${zodEntry}`, async ({ + adminToken, + api, + apiRequestContext, + isolatedGroupManager, + page, + uniqueId + }) => { + const title = `Fixed Choice Upload ${uniqueId}`; + await api.createInstrument( + formBundle({ + content: `{ + putbacks: { kind: 'number', label: 'Putbacks', variant: 'input' }, + ratings: { + items: { friday: { label: 'Friday' }, monday: { label: 'Monday' } }, + kind: 'number-record', + label: 'Ratings', + options: { 1: 'Poor', 2: 'Fair', 3: 'Good' }, + variant: 'likert' + }, + severity: { kind: 'number', label: 'Severity', variant: 'input' } + }`, + name: `FIXED_CHOICE_UPLOAD_${uniqueId}`, + title, + validationSchema: `z.object({ + putbacks: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]), + severity: z.nativeEnum({ 0: 'Mild', 1: 'Severe', Mild: 0, Severe: 1 }), + ratings: z.record(z.enum(['monday', 'friday']), z.union([z.literal(1), z.literal(2), z.literal(3)])) + })`, + zodEntry + }) + ); + const group = await isolatedGroupManager(); + + const uploadPage = new UploadPage(page); + await uploadPage.goto('/upload'); + await uploadPage.selectInstrument(title); + await page.waitForURL('**/upload/**'); + + const uploadInstrumentPage = new UploadInstrumentPage(page); + const downloadPromise = page.waitForEvent('download'); + await uploadInstrumentPage.templateButton.click(); + const template = (await readFile(await (await downloadPromise).path())).toString(); + expect(template).toBe( + 'subjectID,date,putbacks,severity,ratings\r\nstring,yyyy-mm-dd,0/1/2/3,0/1,"RECORD(monday: 1/2/3, friday: 1/2/3)"' + ); + + const csv = [ + 'subjectID,date,putbacks,severity,ratings', + `FixedChoice${uniqueId},2024-01-15,2,1,"RECORD(monday: 3, friday: 1)"` + ].join('\n'); + await uploadInstrumentPage.uploadFile({ buffer: Buffer.from(csv), mimeType: 'text/csv', name: 'choices.csv' }); + await uploadInstrumentPage.submitButton.click(); + await expect(page.getByText('Success')).toBeVisible(); + + const records = await apiRequestContext.get(`/api/v1/instrument-records?groupId=${group.id}`, { + headers: { Authorization: `Bearer ${adminToken}` } }); + expect(await records.json()).toMatchObject([ + { data: { putbacks: 2, ratings: { friday: 1, monday: 3 }, severity: 1 } } + ]); }); } - - // The response was previously every record in the group for the instrument, so a second upload - // leaked the first one's records back to the caller. An earlier upload to the same group and - // instrument is what distinguishes the scoped response from the group-wide one. - test('should answer a batch upload with exactly the records it created', async ({ api, uniqueId }) => { - const group = await api.createGroup(); - const instrumentId = await api.findInstrumentIdByName('DNP_HAPPINESS_QUESTIONNAIRE'); - await api.uploadRecords(group.id, instrumentId, [ - { data: HAPPINESS_RECORD, date: new Date(), subjectId: `upload-${uniqueId}-earlier` } - ]); - - const batch = ['a', 'b', 'c'].map((suffix) => `upload-${uniqueId}-${suffix}`); - const records = await api.uploadRecords( - group.id, - instrumentId, - batch.map((subjectId) => ({ data: HAPPINESS_RECORD, date: new Date(), subjectId })) - ); - - expect(records).toHaveLength(3); - expect(new Set(records.map((record) => record.subjectId))).toStrictEqual(new Set(batch)); - }); - - // The bulk payload has nowhere to carry a file, so a record for a file instrument could only ever - // be incomplete. The upload is refused before anything is written. - test('should refuse a batch upload for a file instrument without writing a session or record', async ({ - adminToken, - api, - apiRequestContext, - uniqueId - }) => { - const group = await api.createGroup(); - const headers = { Authorization: `Bearer ${adminToken}` }; - const subjectId = `file-${uniqueId}`; - const data: UploadInstrumentRecordsData = { - groupId: group.id, - instrumentId: await api.findInstrumentIdByName('ARBITRARY_SINGLE_FILE'), - records: [{ data: {}, date: new Date(), subjectId }] - }; - - const response = await apiRequestContext.post('/api/v1/instrument-records/upload', { data, headers }); - - expect(response.status()).toBe(422); - // Sessions are created only after the subjects they name, so a missing subject means no session. - const subject = await apiRequestContext.get(`/api/v1/subjects/${subjectId}`, { headers }); - expect(subject.status()).toBe(404); - const records = await apiRequestContext.get(`/api/v1/instrument-records?groupId=${group.id}`, { headers }); - expect(await records.json()).toStrictEqual([]); - }); -}); - -test.describe('upload instrument selection while loading', () => { - // The page once copied its search results into state from an effect keyed on a fresh `data ?? []`, - // so it re-rendered in a loop for as long as the instrument list was still in flight. - test('should not re-render in a loop while the instrument list is loading', async ({ getPageModel, page }) => { - const errors: string[] = []; - page.on('console', (message) => { - if (message.type() === 'error') { - errors.push(message.text()); - } - }); - await page.route('**/v1/instruments/info*', async (route) => { - await new Promise((resolve) => { - setTimeout(resolve, 1500); - }); - await route.continue(); - }); - - await getPageModel('/upload'); - - await expect(page.getByRole('cell', { exact: true, name: INSTRUMENT_TITLE })).toBeVisible(); - expect(errors.filter((error) => error.includes('Maximum update depth exceeded'))).toStrictEqual([]); - }); }); /** diff --git a/testing/src/specs/user-account.spec.ts b/testing/src/specs/user-account.spec.ts index a9924b129..96d82b7df 100644 --- a/testing/src/specs/user-account.spec.ts +++ b/testing/src/specs/user-account.spec.ts @@ -1,27 +1,12 @@ -import type { Page } from '@playwright/test'; - import { UserPage } from '../pages/_app/user.page'; -import { SEEDED_USER_PASSWORD } from '../support/constants'; +import { ApiClient } from '../support/api-client'; import { expect, test } from '../support/fixtures'; -/** - * Navigates in-app, for a session established through the real login form: the access token is held - * in memory only, so a `page.goto` would drop it and land back on the login page. - */ -async function goToAccount(page: Page): Promise { - await page.getByTestId('user-dropup-trigger').click(); - await page.getByTestId('user-dropup-account').click(); - await expect(page).toHaveURL('/user'); -} - -async function logOut(page: Page): Promise { - await page.getByTestId('user-dropup-trigger').click(); - await page.getByTestId('user-dropup-logout').click(); - await expect(page).toHaveURL('/auth/login'); -} - test.describe('user account', () => { - test('should reach the account page from the sidebar dropup @smoke', async ({ getPageModel, page }) => { + test("should reach the account page from the sidebar dropup and show the user's own username and role", async ({ + getPageModel, + page + }) => { const dashboardPage = await getPageModel('/dashboard'); await dashboardPage.sidebar.getByTestId('user-dropup-trigger').click(); @@ -32,76 +17,41 @@ test.describe('user account', () => { const userPage = new UserPage(page); await expect(page).toHaveURL('/user'); await expect(userPage.pageHeader).toContainText('Account'); - }); - - test("should display the current user's own username and role", async ({ getPageModel }) => { - const userPage = await getPageModel('/user'); - await expect(userPage.username).toBeVisible(); await expect(userPage.role).toContainText('Group Manager'); }); - test('should describe the password dialog, so it is announced with more than its title', async ({ getPageModel }) => { - const userPage = await getPageModel('/user'); - - await userPage.openPasswordDialog(); - - await expect(userPage.passwordDialog).toHaveAccessibleDescription(/confirm it to save the change/); - }); - - test('should keep the dialog open when the new password is too weak', async ({ getPageModel }) => { - const userPage = await getPageModel('/user'); - - await userPage.openPasswordDialog(); - await userPage.submitNewPassword('password'); - - await expect(userPage.passwordDialog.getByTestId('error-message-text')).toContainText( - 'Insufficient password strength' - ); - await expect(userPage.passwordDialog).toBeVisible(); - }); - - test('should keep the dialog open when the confirmation does not match', async ({ getPageModel }) => { - const userPage = await getPageModel('/user'); - - await userPage.openPasswordDialog(); - await userPage.submitNewPassword(`${SEEDED_USER_PASSWORD}_Changed`, `${SEEDED_USER_PASSWORD}_Mismatch`); - - await expect(userPage.passwordDialog.getByTestId('error-message-text')).toContainText('Passwords Must Match'); - await expect(userPage.passwordDialog).toBeVisible(); - }); - - test('should change the password and let the user log in with the new one', async ({ + // A fresh, uniquely-named user rather than the shared worker-cached identity: changing that password + // would invalidate the token every other spec in the worker shares. + test('should change the password, so the user signs in with the new one and not the old', async ({ api, - getPageModel, + apiRequestContext, + authenticateAs, page, uniqueId }) => { const group = await api.createGroup(); const { credentials } = await api.createUser({ groupIds: [group.id] }); const newPassword = `Zq7#nGx${uniqueId}Rt9`; - - // A fresh, uniquely-named user logging in through the real form, rather than the shared - // worker-cached identity: changing that password would invalidate the token every other spec in - // the worker shares. - const loginPage = await getPageModel('/auth/login'); - await loginPage.fillLoginForm(credentials); - await expect(loginPage.$ref).toHaveURL('/dashboard'); - - await goToAccount(page); + await authenticateAs(credentials); const userPage = new UserPage(page); + await userPage.goto('/user'); await userPage.openPasswordDialog(); await userPage.submitNewPassword(newPassword); await expect(userPage.passwordDialog).toBeHidden(); - await logOut(page); - - await loginPage.fillLoginForm({ password: newPassword, username: credentials.username }); - await expect(loginPage.$ref).toHaveURL('/dashboard'); + await ApiClient.login(apiRequestContext, { password: newPassword, username: credentials.username }); + const oldPasswordLogin = await apiRequestContext.post('/api/v1/auth/login', { data: credentials }); + expect(oldPasswordLogin.status()).toBe(401); }); - test('should clear contact details that are saved blank', async ({ api, authenticateAs, page, uniqueId }) => { + test('should save contact details cleared to blank and an international phone number', async ({ + api, + authenticateAs, + page, + uniqueId + }) => { const email = `contact-${uniqueId}@example.org`; const group = await api.createGroup(); const { credentials } = await api.createUser({ @@ -110,6 +60,7 @@ test.describe('user account', () => { phoneNumber: '5145551234' }); await authenticateAs(credentials); + const savedNotice = page.getByRole('heading', { name: 'Success' }); const userPage = new UserPage(page); await userPage.goto('/user'); @@ -119,108 +70,15 @@ test.describe('user account', () => { await userPage.emailField.clear(); await userPage.phoneNumberField.clear(); await userPage.saveProfile(); - await expect(page.getByRole('heading', { name: 'Success' })).toBeVisible(); + await expect(savedNotice).toBeVisible(); await page.reload(); await expect(userPage.emailField).toHaveValue(''); await expect(userPage.phoneNumberField).toHaveValue(''); - }); -}); - -test.describe('user account profile form', () => { - test('should keep unsaved profile edits when the user query refetches on returning to the tab', async ({ - api, - authenticateAs, - page - }) => { - const group = await api.createGroup(); - const { credentials, user } = await api.createUser({ groupIds: [group.id] }); - await authenticateAs(credentials); - - const userPage = new UserPage(page); - await userPage.goto('/user'); - await expect(userPage.firstNameField).toHaveValue('Test'); - - await userPage.firstNameField.fill('Unsaved'); - // useSuspenseQuery clamps staleTime to at least 1s, so a focus event sooner than that refetches nothing - await expect(async () => { - const refetch = page.waitForResponse( - (response) => response.request().method() === 'GET' && response.url().endsWith(`/v1/users/${user.id}`), - { timeout: 1000 } - ); - await page.evaluate(() => window.dispatchEvent(new Event('visibilitychange'))); - await refetch; - }).toPass(); - await page.evaluate(() => { - return new Promise((resolve) => { - requestAnimationFrame(() => { - requestAnimationFrame(resolve); - }); - }); - }); - - await expect(userPage.firstNameField).toHaveValue('Unsaved'); - }); - - test('should keep the saved values in the profile form after saving it unchanged', async ({ - api, - authenticateAs, - page - }) => { - const group = await api.createGroup(); - const { credentials, user } = await api.createUser({ groupIds: [group.id] }); - await authenticateAs(credentials); - - const userPage = new UserPage(page); - await userPage.goto('/user'); - await expect(userPage.firstNameField).toHaveValue('Test'); - - const refetch = page.waitForResponse( - (response) => response.request().method() === 'GET' && response.url().endsWith(`/v1/users/${user.id}`) - ); - await userPage.saveProfile(); - await expect(page.getByRole('heading', { name: 'Success' })).toBeVisible(); - await refetch; - - await expect(userPage.firstNameField).toHaveValue('Test'); - }); -}); - -test.describe('user account password policy', () => { - test('should keep the dialog open and explain why when the new password matches the current one', async ({ - api, - authenticateAs, - page - }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id] }); - await authenticateAs(credentials); - const userPage = new UserPage(page); - await userPage.goto('/user'); - await userPage.openPasswordDialog(); - await userPage.submitNewPassword(credentials.password); - - await expect(page.getByText('Password must not be the same as your current password')).toBeVisible(); - await expect(userPage.passwordDialog).toBeVisible(); - }); -}); - -test.describe('user account phone number', () => { - test('should save an international phone number with a country code and spacing', async ({ - api, - authenticateAs, - page - }) => { - const group = await api.createGroup(); - const { credentials } = await api.createUser({ groupIds: [group.id] }); - await authenticateAs(credentials); - - const userPage = new UserPage(page); - await userPage.goto('/user'); await userPage.phoneNumberField.fill('+1 514 555 1234'); await userPage.saveProfile(); - await expect(page.getByRole('heading', { name: 'Success' })).toBeVisible(); + await expect(savedNotice).toBeVisible(); await page.reload(); await expect(userPage.phoneNumberField).toHaveValue('+1 514 555 1234'); diff --git a/testing/src/support/api-client.ts b/testing/src/support/api-client.ts index 4f353699f..047ed99d4 100644 --- a/testing/src/support/api-client.ts +++ b/testing/src/support/api-client.ts @@ -2,7 +2,7 @@ import type { $CreateAssignmentData, Assignment } from '@opendatacapture/schemas import { AUDIT_LOGS_MAX_PAGE_SIZE } from '@opendatacapture/schemas/audit'; import type { $AuditLog, $AuditLogAction, $AuditLogsPage } from '@opendatacapture/schemas/audit'; import type { $LoginCredentials } from '@opendatacapture/schemas/auth'; -import type { Language, Permissions } from '@opendatacapture/schemas/core'; +import type { Permissions } from '@opendatacapture/schemas/core'; import type { $CreateGroupData, Group } from '@opendatacapture/schemas/group'; import type { InstrumentInfo } from '@opendatacapture/schemas/instrument'; import type { @@ -12,14 +12,9 @@ import type { } from '@opendatacapture/schemas/instrument-records'; import { MAIL_CLIENT_TIMEOUT } from '@opendatacapture/schemas/mail'; import type { $SendAssignmentEmailData, EmailDeliveryResult } from '@opendatacapture/schemas/mail'; -import type { - $CreatePermissionSetData, - PermissionSet, - PermissionSetSummary -} from '@opendatacapture/schemas/permission-set'; import type { $CreateSessionData, Session, SessionType } from '@opendatacapture/schemas/session'; import type { $CreateSubjectData } from '@opendatacapture/schemas/subject'; -import type { $CreateUserData, $UpdateUserData, $UpdateUserPermissionsData, User } from '@opendatacapture/schemas/user'; +import type { $CreateUserData, $UpdateUserPermissionsData, User } from '@opendatacapture/schemas/user'; import type { APIRequestContext } from '@playwright/test'; import { E2E_MAIL_CONFIG, SEEDED_USER_PASSWORD } from './constants'; @@ -113,19 +108,6 @@ export class ApiClient { return instrument.id; } - /** Creates a uniquely named permission set holding the given rules. */ - async createPermissionSet( - rules: $CreatePermissionSetData['rules'], - overrides: Partial<$CreatePermissionSetData> = {} - ): Promise { - const data: $CreatePermissionSetData = { name: `Permission Set ${randomId()}`, rules, ...overrides }; - return this.expectJson( - this.request.post(`${API}/permission-sets`, { data, headers: this.authHeaders }), - 201, - 'create permission set' - ); - } - /** * Creates one record against a session, the way the app does when an instrument is completed in a * sitting. Unlike {@link uploadRecords}, which always writes a RETROSPECTIVE session, this lets a @@ -214,19 +196,6 @@ export class ApiClient { return { credentials: { password, username }, user }; } - /** - * The languages the instance currently offers. Read rather than assumed because - * `admin-settings.spec.ts` turns one on and off, in a worker of its own. - */ - async findActiveLanguages(): Promise { - const setupState = await this.expectJson<{ activeLanguages: Language[] }>( - this.request.get(`${API}/setup`, { headers: this.authHeaders }), - 200, - 'find the active languages' - ); - return setupState.activeLanguages; - } - /** Reads a subject's assignments back as admin, to check whether a write by another role took effect. */ async findAssignments(subjectId: string): Promise { return this.expectJson( @@ -249,15 +218,6 @@ export class ApiClient { return data; } - /** The ids the start-session form suggests to a user with no group: custom ids scoped to the default group. */ - async findDefaultGroupSubjectCustomIds(): Promise { - return this.expectJson( - this.request.get(`${API}/subjects/default-group/custom-ids`, { headers: this.authHeaders }), - 200, - 'find the custom subject ids scoped to the default group' - ); - } - /** Reads a group back as admin, to check whether a write by another role actually took effect. */ async findGroupById(id: string): Promise { return this.expectJson( @@ -291,29 +251,27 @@ export class ApiClient { return instrument.id; } - async findPermissionSetById(id: string): Promise { - return this.expectJson( - this.request.get(`${API}/permission-sets/${id}`, { headers: this.authHeaders }), - 200, - `find permission set '${id}'` - ); - } - - /** Reads a record back as admin, to check whether a write by another user actually took effect. */ - async findRecordById(id: string): Promise { - return this.expectJson( - this.request.get(`${API}/instrument-records/${id}`, { headers: this.authHeaders }), - 200, - `find instrument record '${id}'` - ); + /** + * The id of a seeded instrument, looked up by its English title. A series has no internal name of + * its own (its info carries `seriesItems` instead), so this is the lookup that reaches one. + */ + async findInstrumentIdByTitle(title: string): Promise { + const instruments = await this.getInstrumentInfo(); + const instrument = instruments.find(({ details }) => { + return (typeof details.title === 'string' ? details.title : details.title.en) === title; + }); + if (!instrument) { + throw new Error(`No instrument titled '${title}' among ${instruments.length} returned`); + } + return instrument.id; } - /** The ids the start-session form suggests for a group: its subjects identified by a custom id. */ - async findSubjectCustomIds(groupId: string): Promise { - return this.expectJson( - this.request.get(`${API}/subjects/groups/${groupId}/custom-ids`, { headers: this.authHeaders }), + /** A subject's records as admin, to check what a write by another origin (the gateway sync) stored. */ + async findRecordsBySubject(subjectId: string): Promise { + return this.expectJson( + this.request.get(`${API}/instrument-records`, { headers: this.authHeaders, params: { subjectId } }), 200, - `find the custom subject ids of group '${groupId}'` + `find the records of subject '${subjectId}'` ); } @@ -363,27 +321,6 @@ export class ApiClient { return this.putUserPermissions(id, { kind: 'custom', permissions }); } - /** Links a user to a permission set, removing any custom grants they held. */ - async setUserPermissionSet(id: string, permissionSetId: string): Promise { - return this.putUserPermissions(id, { kind: 'permissionSet', permissionSetId }); - } - - async unarchiveGroup(id: string): Promise { - await this.expectOk( - this.request.patch(`${API}/groups/${id}/unarchive`, { headers: this.authHeaders }), - `unarchive group '${id}'` - ); - } - - /** Updates a user's profile fields, which is everything except their permissions. */ - async updateUser(id: string, data: Partial<$UpdateUserData>): Promise { - const response = await this.request.patch(`${API}/users/${id}`, { data, headers: this.authHeaders }); - if (!response.ok()) { - throw new Error(`Failed to update user '${id}' (${response.status()}): ${await response.text()}`); - } - return (await response.json()) as User; - } - /** * Bulk-creates one record per entry, and with them the subjects and sessions they name. Returns * the records the api reports created, which the upload contract scopes to this request alone. diff --git a/testing/src/support/env.ts b/testing/src/support/env.ts index 66e841ee6..2f2969bd2 100644 --- a/testing/src/support/env.ts +++ b/testing/src/support/env.ts @@ -50,9 +50,6 @@ export const gatewayRefreshInterval = 2_000; // Tests run against the web origin; vite proxies `/api` through to the API server. export const baseURL = `http://localhost:${webPort}`; -// The API reference is served from the root of the API itself, which the web proxy does not reach. -export const apiURL = `http://localhost:${apiPort}`; - // The patient-facing gateway is its own origin; assignment links point here. export const gatewayURL = `http://localhost:${gatewayPort}`; From 68a1819ea5cdc7fbdbf61e9954ce7e79fd421fcd Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 23:24:15 -0500 Subject: [PATCH 4/9] docs: describe the api integration tier and the production-build e2e suite Co-Authored-By: Claude Opus 5.5 --- .../docs/architecture/auth-and-permissions.md | 5 ++-- .agents/docs/architecture/testing-strategy.md | 24 ++++++++++--------- .agents/docs/playbooks/add-e2e-test.md | 10 +++++--- .agents/docs/playbooks/add-web-route.md | 2 +- .agents/skills/troubleshooting/SKILL.md | 4 ++-- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/.agents/docs/architecture/auth-and-permissions.md b/.agents/docs/architecture/auth-and-permissions.md index 3a2c7278a..dedd85c91 100644 --- a/.agents/docs/architecture/auth-and-permissions.md +++ b/.agents/docs/architecture/auth-and-permissions.md @@ -139,9 +139,10 @@ that a grantable pair used to reach belongs in the same predicate. reads the hash; `AssignmentsService.findPrivateKeyById`, called only by the gateway synchronizer, is the only read of the key. - **Password policy is `UsersService.validatePassword`**: strength via libpasswd, not equal to the - username, and a Have I Been Pwned check that **fails open** when the API is unreachable. Failures + username, and a Have I Been Pwned check that **fails open** when the API is unreachable and is + skipped when `PASSWORD_BREACH_CHECK_ENABLED=false` (the e2e suite and the api vitest project). Failures carry a `code` from `PASSWORD_ERROR_CODES` (`packages/schemas/src/user/user.ts`); the web client localizes it, the API does not. Row scoping is invisible to a unit test with a mocked model, so a change to who can see what needs an -end-to-end test in `testing/`. +integration test against the real database in `apps/api/test/suites/` (see `08-authorization.suite.ts`). diff --git a/.agents/docs/architecture/testing-strategy.md b/.agents/docs/architecture/testing-strategy.md index 7a0725ffd..a0c9a169e 100644 --- a/.agents/docs/architecture/testing-strategy.md +++ b/.agents/docs/architecture/testing-strategy.md @@ -6,15 +6,16 @@ What is tested where, and what CI blocks a merge on. Writing an e2e test is ## Tiers -| Tier | Runner | Where | Command | -| ---------- | ---------- | --------------------------------------------------------------------- | -------------------- | -| Unit | vitest | `__tests__/.test.ts` beside `.ts`, or a package's `test/` | `pnpm test:coverage` | -| End-to-end | Playwright | `testing/src/specs/` | `pnpm test:e2e` | -| Type-check | oxlint | code that fails to compile if the type breaks, e.g. `*.test-d.ts` | `pnpm lint` | +| Tier | Runner | Where | Command | +| ----------- | ---------- | --------------------------------------------------------------------- | -------------------- | +| Unit | vitest | `__tests__/.test.ts` beside `.ts`, or a package's `test/` | `pnpm test:coverage` | +| Integration | vitest | `apps/api/test/suites/`, run through `apps/api/test/app.test.ts` | `pnpm test:coverage` | +| End-to-end | Playwright | `testing/src/specs/` | `pnpm test:e2e` | +| Type-check | oxlint | code that fails to compile if the type breaks, e.g. `*.test-d.ts` | `pnpm lint` | -**There is no integration tier.** `apps/api` unit tests mock the Prisma layer entirely, so Playwright -is the only code path that touches a real database: a permissions or row-scoping change is untested -until an e2e spec covers it. +`apps/api` unit tests mock the Prisma layer entirely. The integration suites boot the real app on an +in-memory replica set (`apps/api/AGENTS.md` §Tests), so a permissions or row-scoping change is tested +there, at a few milliseconds a case; an e2e spec is for what needs the browser and the other apps. ## Scoping a run @@ -109,8 +110,9 @@ a lint failure and every run may rewrite the working tree. To check part of the `pnpm lint` is `turbo run lint lint:root`. `//#lint:root` depends on `//#generated`, a task with no script that names what a tool run over the whole repo reads and source control does not hold: both Prisma clients, `testing`'s route types, and `instrument-library`'s build, which reaches `runtime-v1` -and `runtime-core` through `^build`. `test:e2e` depends on the same task, the gateway's `db:push`, and -`^build`, which builds the `playground-url` CLI that `testing/src/specs/playground.spec.ts` runs. +and `runtime-core` through `^build`. `test:e2e` depends on the same task, the gateway's `db:push`, the +`build` of gateway, web and playground, whose production builds the suite serves, and `^build`, which +builds the `playground-url` CLI that `testing/src/specs/playground.spec.ts` runs. `dev` and `dev:test` depend on `["^build", "db:generate"]`, to which `apps/gateway/turbo.json` adds `db:push`; `build` depends on `["^build", "db:generate"]` and `start` on `build`. `dev`, `dev:test`, `start`, `test:e2e` and `db:push` are `cache: false`. A workspace whose task differs from the root @@ -137,7 +139,7 @@ counts their functions twice in the merged report. `.github/workflows/ci.yaml`, one job, on `pull_request` to `main` or a `release/**` branch and `workflow_dispatch` only — a push alone runs nothing. In order: `./scripts/generate-env.sh`, `pnpm install --frozen-lockfile`, -`pnpm lint`, `pnpm test:coverage`, Playwright install (chromium + firefox, capped at 10 minutes, after +`pnpm lint`, `pnpm test:coverage`, Playwright install (chromium, capped at 10 minutes, after lint and unit tests so a hung download cannot hide them), `pnpm test:e2e`. `actions/cache` restores `.turbo/cache` before the first of them: a pull request reads the caches its own earlier runs saved and the one the Release workflow's `validate` job last saved on its base branch. diff --git a/.agents/docs/playbooks/add-e2e-test.md b/.agents/docs/playbooks/add-e2e-test.md index 19849eaea..903c6dd02 100644 --- a/.agents/docs/playbooks/add-e2e-test.md +++ b/.agents/docs/playbooks/add-e2e-test.md @@ -4,8 +4,10 @@ Conventions and fixtures live in `testing/AGENTS.md`; read it first. This file i operations. **Once per machine:** a repo-root `.env` (`pnpm generate:env`) and the browsers, -`pnpm --filter @opendatacapture/testing exec playwright install chromium firefox`. Nothing else needs -starting — `playwright.config.ts` boots api, gateway, web and playground itself. +`pnpm --filter @opendatacapture/testing exec playwright install chromium`. Nothing else needs +starting — `playwright.config.ts` boots api, gateway, web and playground itself, serving the last +build of the last three: `pnpm test:e2e` builds them first, `test:dev` and a bare `playwright test` do +not. ## Steps @@ -39,7 +41,9 @@ starting — `playwright.config.ts` boots api, gateway, web and playground itsel 8. **Set the role and first-run state** with `test.use({ actingRole: 'ADMIN' })` (default `GROUP_MANAGER`) and `test.use({ appState: {...} })` (`testing/src/specs/disclaimer.spec.ts`). -9. **Tag `@smoke` only a critical flow**; those are the only tests firefox runs. +9. **Check whether a cheaper tier proves it first.** A rule in one component, hook or route is a unit + test; a query's row scoping is an `apps/api/test/suites/` test. An e2e spec is for what needs the + browser and the other apps at once. ## Verify diff --git a/.agents/docs/playbooks/add-web-route.md b/.agents/docs/playbooks/add-web-route.md index 4835fad60..be20ac826 100644 --- a/.agents/docs/playbooks/add-web-route.md +++ b/.agents/docs/playbooks/add-web-route.md @@ -4,7 +4,7 @@ Conventions live in `apps/web/AGENTS.md`; read it first. This file is only the o **Plan around this:** `apps/web/src/route-tree.ts` is generated by the `tanstackRouter` plugin in `apps/web/vite.config.ts`, and **only the user regenerates it**. Every Vite run of `apps/web` rewrites -it — `pnpm dev`, `vite build`, and the `pnpm dev:test` that `pnpm test:e2e` boots. Until the user +it — `pnpm dev`, `pnpm dev:test`, and the `vite build` that `pnpm test:e2e` runs first. Until the user regenerates, `tsc` fails on your new file with: ``` diff --git a/.agents/skills/troubleshooting/SKILL.md b/.agents/skills/troubleshooting/SKILL.md index d8be485ef..b6b83b51c 100644 --- a/.agents/skills/troubleshooting/SKILL.md +++ b/.agents/skills/troubleshooting/SKILL.md @@ -10,8 +10,8 @@ that is deliberately left alone is recorded in the owning workspace's `AGENTS.md ## Loud and correct — leave it standing - **TS2345 on a route literal you just added** is expected until the user regenerates `route-tree.ts` - (`.agents/docs/playbooks/add-web-route.md`). Every Vite run in `apps/web` (`dev`, `dev:test`, the Playwright - webServer, `vite build`) rewrites that tracked file; if `git status` shows it after a debugging run, revert it. + (`.agents/docs/playbooks/add-web-route.md`). Every Vite run in `apps/web` (`dev`, `dev:test`, `vite build`, which + `pnpm test:e2e` runs first) rewrites that tracked file; if `git status` shows it after a debugging run, revert it. - **`pnpm lint` passed and the tree changed**: `oxlint --fix` sorted your new code. Keep the rewrite. - **`Failed to find .env file at default paths`**: run `pnpm generate:env` (root `AGENTS.md`). - **ENOENT while a Vite config resolves, or `/runtime/v1/*` unresolved**: `runtime/v1/dist` is unbuilt. Turbo From 9dda2ddfcdb7d7c2b1914f445568c13747f7e43a Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 23:30:29 -0500 Subject: [PATCH 5/9] build: let only one heavy test run use a machine at a time pnpm test:coverage and pnpm test:e2e now wait for a lock held in the git common directory, which every worktree shares. Two sessions running `pnpm check` at once exhausted an 18 GB machine's memory, and two e2e runs collide on the same ports regardless. The waiting run names the one it waits on, and a lock whose holder has exited is taken over. Co-Authored-By: Claude Opus 5.5 --- .agents/docs/architecture/testing-strategy.md | 6 +++ package.json | 4 +- scripts/with-test-lock.sh | 43 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100755 scripts/with-test-lock.sh diff --git a/.agents/docs/architecture/testing-strategy.md b/.agents/docs/architecture/testing-strategy.md index a0c9a169e..7a93898ac 100644 --- a/.agents/docs/architecture/testing-strategy.md +++ b/.agents/docs/architecture/testing-strategy.md @@ -25,6 +25,12 @@ from the `name` field in each package's `vitest.config.ts`, e.g. `api`, `web`, ` single file with `pnpm exec vitest path/to/file.test.ts`. A scoped run with `--coverage` measures only what it ran against the whole coverage scope, so add `--coverage.thresholds.100=false` to it. +**`pnpm test:coverage` and `pnpm test:e2e` take one lock per machine.** `scripts/with-test-lock.sh` +holds a directory in the git common directory, which every worktree shares, so a second heavy run — +from another session or worktree — waits and says whose run it is waiting on, rather than exhausting +memory or colliding on the e2e ports. A lock whose holder has exited is taken over. `vitest` without +`--coverage` and the scoped runs above take no lock. + ## A test nothing collects passes silently Root `vitest.config.ts` collects only `.github/vitest.config.ts`, `apps/*/vitest.config.ts`, diff --git a/package.json b/package.json index ae9b0ca49..6a3005ac1 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "prepare": "husky", "start": "turbo run start", "test": "pnpm build:exports && env-cmd vitest", - "test:coverage": "pnpm build:exports && env-cmd vitest --coverage", - "test:e2e": "env-cmd turbo run test:e2e", + "test:coverage": "pnpm build:exports && ./scripts/with-test-lock.sh env-cmd vitest --coverage", + "test:e2e": "./scripts/with-test-lock.sh env-cmd turbo run test:e2e", "ws": "./scripts/workspace.sh $@" }, "devDependencies": { diff --git a/scripts/with-test-lock.sh b/scripts/with-test-lock.sh new file mode 100755 index 000000000..473133728 --- /dev/null +++ b/scripts/with-test-lock.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# +# Runs a command while holding a lock that every checkout of this repository shares, so two heavy test +# runs (unit coverage, end-to-end) never overlap on one machine: together they exhaust its memory, and +# two end-to-end runs fight over the same ports. The lock is a directory in the git common directory, +# which every worktree shares, and `mkdir` either creates it or fails, so exactly one caller wins. A +# lock whose holder has exited without releasing it is taken over. + +set -euo pipefail + +lockDir="$(git rev-parse --path-format=absolute --git-common-dir)/opendatacapture-test.lock" +announced=false + +while ! mkdir "$lockDir" 2>/dev/null; do + holder="$(cat "$lockDir/pid" 2>/dev/null || true)" + # Only "No such process" proves the holder gone: a sandboxed caller is refused permission to signal + # a process it did not start, and that refusal must not read as a stale lock. + if [ -n "$holder" ]; then + case "$(kill -0 "$holder" 2>&1 || true)" in + *'No such process'*) + rm -rf "$lockDir" + continue + ;; + esac + # A holder writes its pid straight after creating the lock; one without a pid for a minute died in between. + elif [ -n "$(find "$lockDir" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then + rm -rf "$lockDir" + continue + fi + if [ "$announced" = false ]; then + echo "Waiting for another test run to finish (pid ${holder:-unknown}: $(cat "$lockDir/command" 2>/dev/null || echo unknown))" >&2 + announced=true + fi + sleep 2 +done + +trap 'rm -rf "$lockDir"' EXIT +trap 'exit 130' INT +trap 'exit 143' TERM +echo "$$" >"$lockDir/pid" +echo "$* in $PWD" >"$lockDir/command" + +"$@" From 2df4dc337580ca995d04e7847027b1988863d5d9 Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 23:30:30 -0500 Subject: [PATCH 6/9] docs: bring the agent guides in line with the production-build e2e suite Co-Authored-By: Claude Opus 5.5 --- apps/gateway/AGENTS.md | 2 +- apps/playground/AGENTS.md | 10 +++++----- packages/instrument-utils/AGENTS.md | 4 ++-- testing/AGENTS.md | 29 ++++++++++------------------- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/apps/gateway/AGENTS.md b/apps/gateway/AGENTS.md index 9e1200cf9..5982972a0 100644 --- a/apps/gateway/AGENTS.md +++ b/apps/gateway/AGENTS.md @@ -150,5 +150,5 @@ on port 0 and call it over real HTTP with `fetch`. The api and cap routers get t the end-to-end specs. Most of the coverage is `testing/src/specs/gateway-assignment.spec.ts`, which drives the real -two-origin flow through the Cap widget; `testing/src/specs/gateway-landing.spec.ts` covers `/`. See +two-origin flow through the Cap widget; `testing/src/specs/gateway-landing.spec.ts` covers `/` keeping its language through hydration. See `.agents/docs/architecture/testing-strategy.md`. diff --git a/apps/playground/AGENTS.md b/apps/playground/AGENTS.md index 2e6a4fd15..a4f006c66 100644 --- a/apps/playground/AGENTS.md +++ b/apps/playground/AGENTS.md @@ -113,7 +113,7 @@ editor's IndexedDB, where the store keeps the API token, or anything else on the `PLAYGROUND_PREVIEW_ORIGIN` (a second hostname serving the same `dist/`), read at runtime rather than at build time, because one published image serves every deployment: the Caddyfile answers `/config.json` from the container's environment, `vite.config.ts`'s `playgroundConfig` plugin does -the same for a dev server, and `Viewer.tsx` fetches it once and suspends until it arrives +the same for the dev server and `vite preview`, and `Viewer.tsx` fetches it once and suspends until it arrives (`src/preview/config.ts`). A static host with neither must serve that file itself, or the viewer throws. `docs/en/2-tutorials/2.4-playground-deployment.md` is the operator's side of this. With the variable empty, a dev server uses the other loopback name — `localhost` pairs with `127.0.0.1` — @@ -173,8 +173,8 @@ base64; the package's CLI leaves them out of the links it prints to keep them sh The editor/preview split in `src/components/MainContent/MainContent.tsx` gives every `react-resizable-panels` size as a percentage string. Version 4 reads a bare number as pixels, so -`defaultSize={66}` still type-checks and renders, at the wrong size; the split test in -`testing/src/specs/playground.spec.ts` measures the panes to catch it. +`defaultSize={66}` still type-checks and renders, at the wrong size; +`src/components/MainContent/__tests__/MainContent.test.tsx` pins the strings to catch it. `__APP_VERSION__` and `__GITHUB_REPO_URL__` are the build-time defines, declared in `src/vite-env.d.ts` and supplied in `vite.config.ts`; `GITHUB_REPO_URL` must be listed under the @@ -195,8 +195,8 @@ docblock on its first line and renders with `@testing-library/react`; `src/components/Editor/__tests__/MonacoEditor.test.tsx` does, against a mocked `monaco-editor`, because Monaco itself does not run under happy-dom. Anything that touches esbuild-wasm, Monaco workers or `/runtime/v1` has no test environment here; the frame itself, the origin split, the -runtime config, the message bridge and the resizable editor/preview split are exercised for real by -`testing/src/specs/playground.spec.ts`, which Playwright runs against this app's dev server. +runtime config and the message bridge are exercised for real by +`testing/src/specs/playground.spec.ts`, which Playwright runs against this app's production build. Storybook collects `*.stories.tsx` under `src/components/` centrally through `storybook/config/main.ts` under the `Playground Components` prefix. See diff --git a/packages/instrument-utils/AGENTS.md b/packages/instrument-utils/AGENTS.md index 01f3f90e4..c19b79627 100644 --- a/packages/instrument-utils/AGENTS.md +++ b/packages/instrument-utils/AGENTS.md @@ -32,5 +32,5 @@ appearing in two groups silently keeps the last one. ## Tests `pnpm exec vitest --project instrument-utils`. `src/__tests__/` holds one file per module. The -results summary that `computeInstrumentMeasures` feeds is exercised end to end by -`testing/src/specs/playground.spec.ts`. +results summary that `computeInstrumentMeasures` feeds is tested through +`packages/react-core/src/components/InstrumentSummary/__tests__/InstrumentSummary.test.tsx`. diff --git a/testing/AGENTS.md b/testing/AGENTS.md index ec26c056c..7d12a386e 100644 --- a/testing/AGENTS.md +++ b/testing/AGENTS.md @@ -23,9 +23,7 @@ is `.agents/docs/playbooks/add-e2e-test.md`; the tier-by-tier picture is testid over there in the same change. Roles and labels are used only where no testid exists. - **One database, shared by every worker.** `setup` seeds it once through the UI and `teardown` drops it; `fullyParallel` is on. Any data a test creates must be uniquely named — use the - `uniqueId` fixture (`Subject${uniqueId}`), never a fixed string. Instance-wide settings cannot be - uniquely named, so a test that writes one and reads it back must not be `@smoke`: outside CI - (`workers: 1` there) its firefox copy runs alongside the chromium one and overwrites its value. + `uniqueId` fixture (`Subject${uniqueId}`), never a fixed string. - **`getPageModel` asserts it landed on the route it asked for** (`RootPage.goto` does `expect(page).toHaveURL(url)`). When the expected outcome _is_ a redirect, use `authenticateAs` plus a raw `page.goto` instead — see the standard-user cases in `src/specs/authorization.spec.ts`. @@ -91,8 +89,7 @@ Auth is injected as `window.__PLAYWRIGHT_ACCESS_TOKEN__`, which `apps/web`'s `appState` is written to localStorage by an autouse fixture, so it applies to a spec that logs in through the real form just as much as to one using `authenticateAs`. Left to the app's own defaults, the disclaimer dialog and then the walkthrough overlay cover the page and swallow clicks — the -walkthrough only stays away locally because a developer `.env` tends to set -`VITE_DEV_DISABLE_TUTORIAL=true`, which `.env.template` (and therefore CI) does not. +suite serves the production build, which ignores `VITE_DEV_DISABLE_TUTORIAL`. **Never assert against a value only your `.env` holds.** CI generates `.env` from `.env.template`, so anything configurable — `CONTACT_EMAIL`, ports — is read through `src/support/env.ts` rather than @@ -101,28 +98,22 @@ written as a literal in a spec. ## Running `pnpm test:e2e` from the repo root (turbo), or `pnpm --filter @opendatacapture/testing test:dev` for -Playwright's UI mode. `playwright.config.ts` starts api, gateway and web itself through each app's -`pnpm dev:test`, which sets `NODE_ENV=test` and so gives the API an in-memory Mongo replica set — no -external database, and nothing to start by hand. It also starts the playground through its plain -`pnpm dev` (it has no backend and no test mode) for `src/specs/playground.spec.ts`, which reaches it -by absolute URL from `playgroundURL` in `src/support/env.ts`: the playground is not an `apps/web` +Playwright's UI mode. `playwright.config.ts` starts the API through its `pnpm dev:test`, which sets +`NODE_ENV=test` and so gives it an in-memory Mongo replica set, and serves the production builds of +gateway (`pnpm start`), web and playground (`pnpm preview`), which `pnpm test:e2e` builds first and +`test:dev` does not — no external database, and nothing to start by hand. The playground serves +`src/specs/playground.spec.ts`, which reaches it by absolute URL from `playgroundURL` in `src/support/env.ts`: the playground is not an `apps/web` route, so `src/pages/playground/index.page.ts` extends nothing and is not in `pageModels`. Its preview is an iframe on the other loopback name, so the spec drives it through a `FrameLocator`. -`src/specs/api-docs.spec.ts` likewise reaches the API reference by absolute URL, from `apiURL`: it -is served from the root of the API, and its Redoc page requests `/spec.json`, which the web origin's -`/api` proxy does not route. -**The suite runs on two workers, and one on CI.** Playwright's own default of half the cores logs in -faster than the API's login throttle allows (50 a minute), so tests partway through the suite fail -with `Login failed … (429)`, and each failure restarts a worker that logs in again. Set `E2E_WORKERS` +**The suite runs on two workers, and one on CI.** Each worker is a Chromium beside the four servers, +so memory runs out long before the cores do: two workers already peak near 7 GB. Set `E2E_WORKERS` in the root `.env` to change the count; `src/support/env.ts` reads it and throws on anything but a positive integer. A shell export loses to `.env` wherever `.env` carries the key, blank included: the `test:*` scripts load it through `env-cmd`, whose values win, and `.env.template` carries the key blank. For one run, pass `--workers=` after the script instead. -Projects: `setup` (with `teardown` attached), `chromium` (every spec), and `firefox`, which greps -`/@smoke/`. **Cross-browser coverage exists only for tests whose title contains `@smoke`** — add the -tag to a critical flow, leave it off everything else. +Projects: `setup` (with `teardown` attached) and `chromium` (every spec). There is **no `vitest.config.ts` here and no unit tier**; `pnpm test` never looks at this directory. From 67b8a34cf934366196afc66af938bdac46bbab87 Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 23:48:16 -0500 Subject: [PATCH 7/9] test: make the moved scoping and group-switcher tests fail when they should The series datahub summary is now checked against a foreign record that carries a series id, without which the summary returned nothing whether or not it was scoped. The archived group is created before the others, so it is the first of the user's groups as the test assumes. The gateway landing test no longer retries for a language toggle no spec makes any more, and the authorization suite's two session helpers share one. Co-Authored-By: Claude Opus 5.5 --- .../api/test/suites/08-authorization.suite.ts | 34 +++++++++---------- testing/src/specs/gateway-landing.spec.ts | 18 ++-------- testing/src/specs/group-switcher.spec.ts | 8 ++--- 3 files changed, 24 insertions(+), 36 deletions(-) diff --git a/apps/api/test/suites/08-authorization.suite.ts b/apps/api/test/suites/08-authorization.suite.ts index b970cdfce..b1243d324 100644 --- a/apps/api/test/suites/08-authorization.suite.ts +++ b/apps/api/test/suites/08-authorization.suite.ts @@ -141,20 +141,20 @@ export default defineSuite('authorization', function () { expect(response.statusCode).toBe(201); }; - /** A record for a new subject in the group, written the way a completed instrument is. */ - const createRecord = async (groupId: string): Promise => { + /** An in-person session for a new subject, which enrols that subject in the group. */ + const createSession = async (groupId: string): Promise => { const session = await request(adminHeaders, { method: 'POST', - payload: { - date: new Date(), - groupId, - subjectData: { id: `authorization-${randomUUID()}` }, - type: 'IN_PERSON' - }, + payload: { date: new Date(), groupId, subjectData: { id: `authorization-${randomUUID()}` }, type: 'IN_PERSON' }, url: '/v1/sessions' }); expect(session.statusCode).toBe(201); - const { id: sessionId, subjectId } = session.json(); + return session.json(); + }; + + /** A record for a new subject in the group, written the way a completed instrument is. */ + const createRecord = async (groupId: string): Promise => { + const { id: sessionId, subjectId } = await createSession(groupId); const record = await request(adminHeaders, { method: 'POST', payload: { data: { score: 1 }, date: new Date(), groupId, instrumentId, sessionId, subjectId }, @@ -166,13 +166,7 @@ export default defineSuite('authorization', function () { /** A subject enrolled in the group, holding no records. */ const createSubject = async (groupId: string): Promise => { - const session = await request(adminHeaders, { - method: 'POST', - payload: { date: new Date(), groupId, subjectData: { id: `authorization-${randomUUID()}` }, type: 'IN_PERSON' }, - url: '/v1/sessions' - }); - expect(session.statusCode).toBe(201); - return session.json().subjectId; + return (await createSession(groupId)).subjectId; }; /** @@ -416,11 +410,17 @@ export default defineSuite('authorization', function () { // The datahub's instrument hub reads these endpoints with no `groupId`, so each decides on its own // what the caller may see. The manager here belongs to a group holding no records at all, so - // anything the summaries name came from another group. + // anything the summaries name came from another group. The series summary counts only records + // collected as part of a series, so the other group holds one of those as well as plain records. it.each(['by-instrument', 'by-series', 'by-subject'])( "should leave another group's records out of the %s datahub summary a group manager reads", async (summary) => { await uploadRecords(foreignGroup.id, [`authorization-hub-${randomUUID()}`]); + const seriesRecord = await createRecord(foreignGroup.id); + await prisma.instrumentRecord.update({ + data: { seriesInstrumentId: await createSeries(foreignGroup.id) }, + where: { id: seriesRecord.id } + }); const emptyGroup = await createGroup(); const headers = await login((await createUser('GROUP_MANAGER', [emptyGroup.id])).username); diff --git a/testing/src/specs/gateway-landing.spec.ts b/testing/src/specs/gateway-landing.spec.ts index 343ecaace..c2ef82810 100644 --- a/testing/src/specs/gateway-landing.spec.ts +++ b/testing/src/specs/gateway-landing.spec.ts @@ -1,25 +1,13 @@ -import { gatewayRefreshInterval, gatewayURL } from '../support/env'; +import { gatewayURL } from '../support/env'; import { expect, test } from '../support/fixtures'; -/** - * admin-settings.spec.ts turns French off for a moment. The API pushes setup state at the start of - * each sync pass and starts the next pass one GATEWAY_REFRESH_INTERVAL after the previous one ends, - * so the gateway can hold that state for the rest of a pass plus an interval, and a pass is slow - * locally under fullyParallel. - */ -const STALE_SETUP_STATE_TIMEOUT = 3 * gatewayRefreshInterval; - test.describe('gateway landing page', () => { - test.describe.configure({ timeout: STALE_SETUP_STATE_TIMEOUT + 30_000 }); - // The raw response body shows the server rendered French. The client must then hydrate in French // too, or React recovers from the mismatch by re-rendering in English; the shell only becomes // visible after that, in Root's post-hydration effect, so the visible heading is the final one. test('should render a requested active language on the server and keep it through hydration', async ({ page }) => { - await expect(async () => { - const french = await page.goto(`${gatewayURL}/?lang=fr`); - expect(await french!.text()).toContain('Bienvenue'); - }).toPass({ timeout: STALE_SETUP_STATE_TIMEOUT }); + const french = await page.goto(`${gatewayURL}/?lang=fr`); + expect(await french!.text()).toContain('Bienvenue'); const landing = page.getByTestId('gateway-landing'); await expect(landing).toBeVisible(); await expect(landing.getByRole('heading')).toHaveText('Bienvenue'); diff --git a/testing/src/specs/group-switcher.spec.ts b/testing/src/specs/group-switcher.spec.ts index 664347d1b..e8e073d08 100644 --- a/testing/src/specs/group-switcher.spec.ts +++ b/testing/src/specs/group-switcher.spec.ts @@ -52,15 +52,15 @@ test.describe('group switcher', () => { await expect(dashboardPage.groupSwitcher).toContainText(group.name); }); - // The archived group is listed first, so a client that took the first of the user's groups as the - // current one without dropping archived groups would select it. + // The archived group is created, and so listed, first, so a client that took the first of the user's + // groups as the current one without dropping archived groups would select it. test('should offer a group manager only the active groups they belong to, and never start them in an archived one', async ({ api, authenticateAs, page }) => { - const [archivedGroup, firstGroup, secondGroup, otherGroup] = await Promise.all([ - api.createGroup(), + const archivedGroup = await api.createGroup(); + const [firstGroup, secondGroup, otherGroup] = await Promise.all([ api.createGroup(), api.createGroup(), api.createGroup() From b67e58b40e6a61d3b6404292cb0429bea3bb805b Mon Sep 17 00:00:00 2001 From: joshunrau Date: Wed, 18 Nov 2026 23:51:08 -0500 Subject: [PATCH 8/9] test(api): refuse the mail-delivery suite's SMTP connections on a fixed port The suite freed a probed port and assumed it stayed free until it sent mail. Another process started listening on it in between, and delivery failed on authentication instead of being refused. Port 1 lies outside the range the OS assigns to other sockets. Co-Authored-By: Claude Opus 5.5 --- .../api/test/suites/07-mail-delivery.suite.ts | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/apps/api/test/suites/07-mail-delivery.suite.ts b/apps/api/test/suites/07-mail-delivery.suite.ts index 9537eef3f..182064fec 100644 --- a/apps/api/test/suites/07-mail-delivery.suite.ts +++ b/apps/api/test/suites/07-mail-delivery.suite.ts @@ -1,5 +1,4 @@ import { randomUUID } from 'node:crypto'; -import { createServer } from 'node:net'; import { PRISMA_CLIENT_TOKEN } from '@douglasneuroinformatics/libnest'; import type { EmailDeliveryResult } from '@opendatacapture/schemas/mail'; @@ -15,21 +14,12 @@ const PASSWORD = 'DataCapture2025_Test'; /** Logins come from an address of their own, so they spend none of the default address's throttle budget. */ const LOGIN_ADDRESS = '192.0.2.70'; -/** A loopback port nothing listens on, so a delivery attempt is refused at once rather than timing out. */ -async function findClosedPort(): Promise { - const server = createServer(); - await new Promise((resolve) => { - server.listen(0, '127.0.0.1', resolve); - }); - const address = server.address(); - await new Promise((resolve) => { - server.close(resolve); - }); - if (typeof address !== 'object' || address === null) { - throw new Error('Expected the probe server to listen on a TCP port'); - } - return address.port; -} +/** + * A loopback port nothing listens on, so a delivery attempt is refused at once rather than timing out. + * Port 1 (tcpmux) lies outside the range the OS assigns to other sockets. A port freed by a probe + * server does not: another process can start listening on it before the suite connects. + */ +const CLOSED_PORT = 1; /** * The delivery results a caller relies on to fall back to manual delivery, over real HTTP: the @@ -58,8 +48,6 @@ export default defineSuite('mail delivery', function () { expect(response.statusCode).toBe(200); }; - let closedPort: number; - beforeAll(async () => { const login = await this.app.inject({ method: 'POST', @@ -69,13 +57,12 @@ export default defineSuite('mail delivery', function () { }); expect(login.statusCode).toBe(200); adminHeaders = { authorization: `Bearer ${login.json<{ accessToken: string }>().accessToken}` }; - closedPort = await findClosedPort(); - await setMailConfig(true, closedPort); + await setMailConfig(true, CLOSED_PORT); }); // Mail is instance-wide, and every later suite assumes it is off. afterAll(async () => { - await setMailConfig(false, closedPort); + await setMailConfig(false, CLOSED_PORT); }); afterEach(() => { From d29ce83be17dd63670f36b0e574015264adf4d54 Mon Sep 17 00:00:00 2001 From: joshunrau Date: Thu, 19 Nov 2026 00:02:51 -0500 Subject: [PATCH 9/9] build: generate the outreach content types before the root type-check `//#lint:root` type-checks apps/outreach, whose `astro:content` types live in the git-ignored `.astro/` that only `astro check` (outreach's own `lint`, running alongside) wrote. On a fresh checkout such as CI's, the root type-check could run first and read every collection as `any`. `@opendatacapture/outreach#gen:types` runs `astro sync`, caches `.astro/`, and `//#generated` now depends on it. Co-Authored-By: Claude Opus 5.5 --- .agents/docs/architecture/testing-strategy.md | 6 ++++-- apps/outreach/package.json | 1 + apps/outreach/turbo.json | 5 +++++ turbo.json | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.agents/docs/architecture/testing-strategy.md b/.agents/docs/architecture/testing-strategy.md index 7a93898ac..7c3d41eaf 100644 --- a/.agents/docs/architecture/testing-strategy.md +++ b/.agents/docs/architecture/testing-strategy.md @@ -115,8 +115,10 @@ a lint failure and every run may rewrite the working tree. To check part of the `pnpm lint` is `turbo run lint lint:root`. `//#lint:root` depends on `//#generated`, a task with no script that names what a tool run over the whole repo reads and source control does not hold: both -Prisma clients, `testing`'s route types, and `instrument-library`'s build, which reaches `runtime-v1` -and `runtime-core` through `^build`. `test:e2e` depends on the same task, the gateway's `db:push`, the +Prisma clients, `testing`'s route types, `apps/outreach`'s content collection types (`gen:types`, +`astro sync`, into the git-ignored `.astro/`), and `instrument-library`'s build, which reaches +`runtime-v1` and `runtime-core` through `^build`. Without the content types, the type-check reads every +`astro:content` import as `any` on a fresh checkout such as CI's. `test:e2e` depends on the same task, the gateway's `db:push`, the `build` of gateway, web and playground, whose production builds the suite serves, and `^build`, which builds the `playground-url` CLI that `testing/src/specs/playground.spec.ts` runs. `dev` and `dev:test` depend on `["^build", "db:generate"]`, to which `apps/gateway/turbo.json` adds diff --git a/apps/outreach/package.json b/apps/outreach/package.json index 513875cb4..9e633ef0a 100644 --- a/apps/outreach/package.json +++ b/apps/outreach/package.json @@ -6,6 +6,7 @@ "astro": "astro", "build": "astro build", "dev": "astro dev", + "gen:types": "astro sync", "format:translations": "find src/i18n/translations -name '*.json' -exec pnpm exec sort-json {} \\;", "lint": "astro check" }, diff --git a/apps/outreach/turbo.json b/apps/outreach/turbo.json index 3fbee9a89..ba43fccca 100644 --- a/apps/outreach/turbo.json +++ b/apps/outreach/turbo.json @@ -6,6 +6,11 @@ "build": { "inputs": ["$TURBO_DEFAULT$", "$TURBO_ROOT$/blog/**", "$TURBO_ROOT$/docs/**"] }, + // The content collection types the root type-check reads from `.astro/`, which is git-ignored + "gen:types": { + "inputs": ["$TURBO_DEFAULT$", "$TURBO_ROOT$/blog/**", "$TURBO_ROOT$/docs/**"], + "outputs": [".astro/**"] + }, "lint": { "dependsOn": ["^build"], "inputs": ["$TURBO_DEFAULT$", "$TURBO_ROOT$/blog/**", "$TURBO_ROOT$/docs/**"] diff --git a/turbo.json b/turbo.json index 898c12043..efee22b3d 100644 --- a/turbo.json +++ b/turbo.json @@ -31,6 +31,7 @@ "@opendatacapture/api#db:generate", "@opendatacapture/gateway#db:generate", "@opendatacapture/instrument-library#build", + "@opendatacapture/outreach#gen:types", "@opendatacapture/testing#gen:routes" ] },