Files
ipal-kit/test/dashboardAlerts.test.ts
T
Radosław SmolińskiandClaude Fable 5.2 0b4c4e8295 M1 (9): dashboard alerts by role
IntegrationAlerts reads the admin-only Site integrations global with access
control off and showed its error texts to everyone who opened the dashboard.
Now: an admin sees what is failing and the link, an editor a neutral "an
integration needs attention — ask an administrator", anyone else nothing.
CookieCrawlAlert is shown to editors and above (the role that may read the crawl
result). Both use userMeetsRole, so a session that still owes its second factor
sees neither. dashboardAlerts: { integrations: 'admin' | 'editor' | 'all',
cookies: 'editor' | 'all' } loosens it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 00:39:12 +01:00

111 lines
6.1 KiB
TypeScript

/**
* M1: dashboard alerts by role. The widgets read data only some roles may read (with access control off), so
* they decide themselves who sees what: an admin the detail, an editor a neutral line, nobody else anything —
* and a session that still owes its second factor nothing at all.
*/
import type React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, test } from 'vitest'
import { CookieCrawlAlert } from '../src/modules/consent/scanner/crawl/CookieCrawlAlert.js'
import { IntegrationAlerts } from '../src/modules/integrations/IntegrationAlerts.js'
import { ipalKit } from '../src/plugin.js'
const ERROR_TEXT = 'SMTP-ERROR-DETAIL: 535 authentication failed for [email protected]'
const payloadWith = (twoFactor: boolean) => ({
config: {
admin: { user: 'users' },
collections: [{ slug: 'users', auth: true, fields: [{ name: 'roles', type: 'select' }] }],
custom: { ipal: { staffCollections: ['users'], ...(twoFactor ? { twoFactor: { collection: 'users', requireSetup: true } } : {}) } },
},
count: async () => ({ totalDocs: 2 }),
findGlobal: async ({ slug }: { slug: string }) =>
slug === 'site-integrations'
? { emailStatus: { code: 'custom', message: ERROR_TEXT, state: 'degraded' }, turnstileSecretKey: 's', turnstileSiteKey: 'k' }
: { error: 'CRAWL-ERROR-DETAIL', status: 'failed', summary: { newUnknown: 0, violations: 0 } },
})
const user = (roles: string[], more: Record<string, unknown> = {}) => ({ id: 1, collection: 'users', roles, ...more })
const html = async (node: Promise<React.ReactNode>) => {
const out = await node
return out ? renderToStaticMarkup(out as React.ReactElement) : ''
}
const integrations = (u: unknown, props: Record<string, unknown> = {}, twoFactor = false) => html(IntegrationAlerts({ payload: payloadWith(twoFactor) as never, user: u as never, ...props }))
const cookies = (u: unknown, props: Record<string, unknown> = {}, twoFactor = false) => html(CookieCrawlAlert({ payload: payloadWith(twoFactor) as never, user: u as never, ...props }))
describe('IntegrationAlerts', () => {
test('an admin sees what is failing and the link', async () => {
const out = await integrations(user(['admin']))
expect(out).toContain('SMTP-ERROR-DETAIL')
expect(out).toContain('/admin/globals/site-integrations')
expect(out).toContain('role="alert"')
})
test('an editor sees that something needs attention and whom to ask — no detail, no link', async () => {
const out = await integrations(user(['editor']))
expect(out).toContain('An integration needs attention')
expect(out).toContain('Ask an administrator')
expect(out).not.toContain('SMTP-ERROR-DETAIL')
expect(out).not.toContain('[email protected]')
expect(out).not.toContain('site-integrations')
expect(out).toContain('role="status"')
})
test('a plain user, a visitor account, nobody: nothing', async () => {
expect(await integrations(user(['user']))).toBe('')
expect(await integrations({ id: 1, collection: 'customers', roles: ['admin'] })).toBe('')
expect(await integrations(null)).toBe('')
expect(await integrations(undefined)).toBe('')
})
test('a session that still owes its second factor sees nothing — admin or editor', async () => {
const owes = { _strategy: 'local-jwt', hasTotp: true }
expect(await integrations(user(['admin'], owes), {}, true)).toBe('')
expect(await integrations(user(['editor'], owes), {}, true)).toBe('')
expect(await integrations(user(['admin'], { _strategy: 'local-jwt', hasTotp: false }), {}, true)).toBe('') // TOTP not set up
expect(await integrations(user(['admin'], { _strategy: 'totp', hasTotp: true }), {}, true)).toContain('SMTP-ERROR-DETAIL')
})
test('details: editor / all loosen it; nothing failing renders nothing for anyone', async () => {
expect(await integrations(user(['editor']), { details: 'editor' })).toContain('SMTP-ERROR-DETAIL')
expect(await integrations(user(['user']), { details: 'editor' })).toBe('')
expect(await integrations(user(['user']), { details: 'all' })).toContain('SMTP-ERROR-DETAIL')
expect(await integrations(null, { details: 'all' })).toContain('SMTP-ERROR-DETAIL') // as before the change
const fine = { ...payloadWith(false), findGlobal: async () => ({ turnstileSecretKey: 's', turnstileSiteKey: 'k' }) }
expect(await html(IntegrationAlerts({ payload: fine as never, user: user(['admin']) as never }))).toBe('')
})
test('Polish for a Polish panel', async () => {
expect(await integrations(user(['editor']), { i18n: { language: 'pl' } })).toContain('Poproś administratora')
})
})
describe('CookieCrawlAlert', () => {
test('editors and admins see it; others, and a session that owes its code, do not', async () => {
expect(await cookies(user(['admin']))).toContain('cookie-crawl-alert')
expect(await cookies(user(['editor']))).toContain('CRAWL-ERROR-DETAIL') // the crawl result is theirs to read
expect(await cookies(user(['user']))).toBe('')
expect(await cookies(null)).toBe('')
expect(await cookies(user(['editor'], { _strategy: 'local-jwt', hasTotp: true }), {}, true)).toBe('')
expect(await cookies(user(['user']), { audience: 'all' })).toContain('cookie-crawl-alert')
})
})
describe('ipalKit({ dashboardAlerts })', () => {
const i18n = { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }
const propsOf = async (options: Record<string, unknown>) => {
const config = await ipalKit({ cookieScanner: { crawl: true }, i18n, twoFactor: false, ...options } as never)({ collections: [], secret: 'x' } as never)
return JSON.stringify(config.admin?.components?.beforeDashboard)
}
test('default: the components decide (admin detail, editor cookies); the option is passed on', async () => {
const plain = await propsOf({})
expect(plain).toContain('IntegrationAlerts')
expect(plain).not.toContain('"details"')
expect(plain).not.toContain('"audience"')
const loose = await propsOf({ dashboardAlerts: { cookies: 'all', integrations: 'all' } })
expect(loose).toContain('"details":"all"')
expect(loose).toContain('"audience":"all"')
})
})