/** * Access callbacks of a project (`cookieScanner.access`, the doctor's `access`), owner's decision for 1.18.0: only a * literal `true` allows; an answer that is not a boolean is reported once per option; `strictAccessAnswers: false` * gives the old truthy reading back. Every call site goes through one function. */ import { readFileSync } from 'node:fs' import { beforeEach, describe, expect, test, vi } from 'vitest' import { accessAllowed, resetAccessWarnings, strictAccessAnswers } from '../src/modules/shared/accessAnswer.js' beforeEach(() => resetAccessWarnings()) describe('accessAllowed', () => { test('true and false are read as they are, without a word', () => { const warn = vi.fn() expect(accessAllowed('cookieScanner.access', true, { warn })).toBe(true) expect(accessAllowed('cookieScanner.access', false, { warn })).toBe(false) expect(warn).not.toHaveBeenCalled() }) // the owner's decision for 1.18.0: only a literal `true` allows (the old reading in the third column) const answers: [string, unknown, boolean, string][] = [ ['an object (req.user)', { id: 1 }, true, 'an object'], ['1', 1, true, 'a number'], ['a string', 'yes', true, 'a string'], ['null', null, false, 'null'], ['undefined', undefined, false, 'an undefined'], ] test.each(answers)('%s refuses (strict, the default) and is reported once, naming the option and the fix', (_, answer, _old, kind) => { const warn = vi.fn() expect(accessAllowed('doctor.access', answer, { warn })).toBe(false) expect(accessAllowed('doctor.access', answer, { warn })).toBe(false) expect(warn).toHaveBeenCalledTimes(1) expect(warn.mock.calls[0][0]).toContain(`doctor.access returned ${kind}`) expect(warn.mock.calls[0][0]).toContain('Boolean(req.user)') }) test.each(answers)('%s with strictAccessAnswers: false is read as before 1.18.0 (truthy allows)', (_, answer, old) => { expect(accessAllowed('cookieScanner.access', answer, { strict: false, warn: vi.fn() })).toBe(old) }) test('a Promise is never `true`: an un-awaited answer refuses, whatever it holds', () => { const warn = vi.fn() for (const value of [true, { id: 1 }, 1, 'yes']) { const p = Promise.resolve(value) expect(accessAllowed('doctor.access', p, { warn })).toBe(false) } expect(warn.mock.calls[0][0]).toContain('a Promise (not awaited)') }) test('async callbacks: the call sites await the answer, so a Promise of each reads like the value itself', async () => { const asCalled = async (cb: () => Promise) => accessAllowed('cookieScanner.access', await cb(), { warn: vi.fn() }) expect(await asCalled(async () => true)).toBe(true) for (const value of [{ id: 1 }, 1, 'yes', false, null]) {expect(await asCalled(async () => value)).toBe(false)} }) test('strictAccessAnswers(req) reads the option from the request\'s config', () => { expect(strictAccessAnswers(undefined)).toBe(true) expect(strictAccessAnswers({ payload: { config: { custom: {} } } })).toBe(true) expect(strictAccessAnswers({ payload: { config: { custom: { iapl: { strictAccessAnswers: false } } } } })).toBe(false) }) test('each option is reported on its own', () => { const warn = vi.fn() accessAllowed('doctor.access', 'x', { warn }) accessAllowed('cookieScanner.access', 'x', { warn }) expect(warn).toHaveBeenCalledTimes(2) }) }) test('every call of a project access callback goes through accessAllowed', () => { const sites = ['src/modules/consent/scanner/endpoint.ts', 'src/modules/consent/scanner/approval.ts', 'src/modules/consent/scanner/crawl/endpoints.ts', 'src/modules/doctor/status.ts'] for (const file of sites) { const text = readFileSync(file, 'utf8') const calls = text.match(/await options\.access\(\{ req \}\)/g) ?? [] expect(calls.length, file).toBeGreaterThan(0) expect(text.match(/accessAllowed\('[\w.]+', await options\.access\(\{ req \}\), \{ strict: strictAccessAnswers\(req\) \}\)/g)?.length, file).toBe(calls.length) } })