Files
envbao-admin/src/features/secrets/ConfigPanel.test.tsx
T
mattandClaude Sonnet 5.5 fd20caa525 Config: show each database connection's URL and allowed roles
The connections table reads each connection and lists its URL and
allowed roles. Literal passwords in a URL are masked; templated
{{password}} URLs are shown as-is.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-09 14:09:42 +00:00

136 lines
6.7 KiB
TypeScript

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
import { BaoClient } from '../../api/bao'
import * as config from '../../api/engines/config'
import { ENGINES } from '../../api/engines/registry'
import { ConfigPanel } from './ConfigPanel'
vi.mock('../../api/engines/config')
const client = new BaoClient('http://b', 't')
const conn = ENGINES.database.configs[0]
function renderPanel(type: keyof typeof ENGINES) {
render(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
<ConfigPanel client={client} mount={`${type}/`} engine={ENGINES[type]} />
</QueryClientProvider>,
)
return userEvent.setup()
}
beforeEach(() => {
vi.mocked(config.listConfigs).mockResolvedValue(['pg'])
})
afterEach(() => vi.resetAllMocks())
test('creates a database connection, sending the password only when typed', async () => {
vi.mocked(config.writeConfig).mockResolvedValue()
const user = renderPanel('database')
await user.click(await screen.findByRole('button', { name: 'New connection' }))
await user.type(screen.getByLabelText('Connection name'), 'main')
await user.type(screen.getByLabelText(/^Connection URL/), 'postgresql://u:p@db/app')
await user.type(screen.getByLabelText('Admin password'), 's3cret')
await user.click(screen.getByRole('button', { name: 'Save' }))
expect(config.writeConfig).toHaveBeenCalledWith(
client,
'database/',
conn,
expect.objectContaining({ plugin_name: 'postgresql-database-plugin', connection_url: 'postgresql://u:p@db/app', password: 's3cret' }),
'main',
)
})
test('editing keeps the stored password: blank is omitted and nothing is prefilled', async () => {
vi.mocked(config.readConfig).mockResolvedValue({ plugin_name: 'postgresql-database-plugin', connection_url: 'postgresql://x', username: 'bao', allowed_roles: ['ro'] })
vi.mocked(config.writeConfig).mockResolvedValue()
const user = renderPanel('database')
await user.click(await screen.findByRole('button', { name: 'Edit pg' }))
expect(await screen.findByLabelText('Admin password')).toHaveValue('')
expect(screen.getByLabelText('Connection name')).toBeDisabled()
await user.click(screen.getByRole('button', { name: 'Save' }))
const body = vi.mocked(config.writeConfig).mock.calls[0][3]
expect(body).toMatchObject({ connection_url: 'postgresql://x', allowed_roles: ['ro'], username: 'bao' })
expect(body).not.toHaveProperty('password')
})
test('rotating root credentials asks first', async () => {
vi.mocked(config.readConfig).mockResolvedValue({ plugin_name: 'postgresql-database-plugin', connection_url: 'postgresql://x' })
vi.mocked(config.runAction).mockResolvedValue()
const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
const user = renderPanel('database')
await user.click(await screen.findByRole('button', { name: 'Edit pg' }))
await user.click(await screen.findByRole('button', { name: 'Rotate root credentials' }))
expect(config.runAction).not.toHaveBeenCalled()
confirm.mockReturnValue(true)
await user.click(screen.getByRole('button', { name: 'Rotate root credentials' }))
expect(config.runAction).toHaveBeenCalledWith(client, 'database/', conn.actions![0], 'pg')
})
test('aws shows root and lease configs, loaded from their own endpoints', async () => {
vi.mocked(config.readConfig).mockImplementation(async (_c, _m, kind) =>
kind.id === 'root' ? { access_key: 'AKIA1', region: 'eu-west-1' } : null,
)
renderPanel('aws')
expect(await screen.findByDisplayValue('AKIA1')).toBeInTheDocument()
expect(screen.getByLabelText('Secret key')).toHaveValue('')
expect(screen.getByLabelText('Default lease')).toBeInTheDocument()
})
test('ssh CA: shows the public key and generating asks first', async () => {
vi.mocked(config.readConfig).mockResolvedValue({ public_key: 'ssh-rsa AAAAca' })
vi.mocked(config.runAction).mockResolvedValue()
vi.spyOn(window, 'confirm').mockReturnValue(true)
const user = renderPanel('ssh')
expect(await screen.findByLabelText('Current CA public key')).toHaveValue('ssh-rsa AAAAca')
await user.click(screen.getByRole('button', { name: 'Generate signing key' }))
expect(config.runAction).toHaveBeenCalledWith(client, 'ssh/', ENGINES.ssh.configs[0].actions![0], undefined)
})
test('shows the load error', async () => {
vi.mocked(config.listConfigs).mockRejectedValue(new Error('permission denied'))
renderPanel('database')
expect(await screen.findByRole('alert')).toHaveTextContent('permission denied')
})
test('the connections table shows each connection\'s URL and allowed roles, hiding literal passwords', async () => {
vi.mocked(config.listConfigs).mockResolvedValue(['main', 'legacy', 'bare'])
vi.mocked(config.readConfig).mockImplementation(async (_c, _m, _k, name) =>
name === 'main'
? { connection_url: 'postgresql://{{username}}:{{password}}@db:5432/app', allowed_roles: ['readonly', 'readwrite'] }
: name === 'legacy'
? { connection_url: 'postgresql://bao:hunter2@old:5432/app', allowed_roles: ['*'] }
: { allowed_roles: [] },
)
renderPanel('database')
const table = await screen.findByRole('table', { name: 'Connections' })
expect(within(table).getAllByRole('columnheader').map((h) => h.textContent)).toEqual(['Name', 'Connection URL', 'Allowed roles', ''])
const main = (await within(table).findByText('main')).closest('tr')!
expect(await within(main).findByText('postgresql://{{username}}:{{password}}@db:5432/app')).toBeInTheDocument()
expect(within(main).getByText('readonly')).toBeInTheDocument()
expect(within(main).getByText('readwrite')).toBeInTheDocument()
const legacy = within(table).getByText('legacy').closest('tr')!
expect(await within(legacy).findByText('postgresql://bao:••••@old:5432/app')).toBeInTheDocument()
expect(table).not.toHaveTextContent('hunter2')
expect(within(legacy).getByText('*')).toBeInTheDocument()
const bare = within(table).getByText('bare').closest('tr')!
expect((await within(bare).findAllByText('None')).length).toBe(2)
})
test('a connection that cannot be read says why, without hiding the others', async () => {
vi.mocked(config.listConfigs).mockResolvedValue(['ok', 'broken'])
vi.mocked(config.readConfig).mockImplementation(async (_c, _m, _k, name) => {
if (name === 'broken') throw new Error('permission denied')
return { connection_url: 'postgresql://db/app', allowed_roles: ['ro'] }
})
renderPanel('database')
expect(await screen.findByText('postgresql://db/app')).toBeInTheDocument()
expect(await screen.findByRole('alert')).toHaveTextContent('permission denied')
})