// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini import { test, expect } from '../harness/fixtures'; import type { RecordInput } from '../harness/ui'; import { up } from '../harness/api'; import { addAddress, address, checkZone, createZone } from '../harness/personas'; test.use({ group: 'stupid' }); test('operator mistakes preserve DNS, user records and delegations', async ({ world }) => { const admin = await world.bootstrapAdmin(); const api = await world.apiAs(admin); const ui = await world.uiAs(admin); const zone = world.zoneName(); const zonePath = await createZone(api, zone); await addAddress(api, zonePath, 'www', '192.0.2.10'); await ui.openZone(zone); const before = await world.db.records(zone); const wrongRecords: RecordInput[] = [ { name: 'v6', type: 'A', fields: { ip: '2001:db8::1' } }, { name: 'www..', type: 'A', fields: { ip: '192.0.2.8' } }, { name: '@', type: 'CNAME', fields: { host: 'elsewhere.example.net' } }, { name: 'www', type: 'CNAME', fields: { host: 'elsewhere.example.net.' } }, ]; for (const entry of wrongRecords) { await ui.addRecord({ ...entry, ttl: '300' }, { expectStatus: 422 }); await expect(ui.page.locator('#record-dialog')).toBeVisible(); await ui.page.keyboard.press('Escape'); } const soa = (await api.get(zonePath)).json.soa; expect((await api.delete(`${zonePath}/records/${soa.id}`, undefined, { ifMatch: soa.etag })).status).toBe(422); const ns = (await api.get(`${zonePath}/records`)).json.records.find((r: { type: string }) => r.type === 'NS'); expect((await api.delete(`${zonePath}/records/${ns.id}`, undefined, { ifMatch: ns.etag })).status).toBe(422); expect(await world.db.records(zone)).toEqual(before); await ui.page.click('#record-add'); await ui.page.fill('#rec-name', `www.${zone.slice(0, -1)}`); await expect(ui.page.locator('#rec-fqdn')).toContainText(`www.${zone}${zone}`); await ui.page.fill('#rec-name', 'double'); await ui.page.fill('#rec-f-ip', '192.0.2.20'); const mark = await world.db.auditMaxId(); const response = ui.page.waitForResponse((res) => res.url().endsWith(`/api/v1${zonePath}/records`) && res.request().method() === 'POST'); await ui.page.locator('#rec-submit').evaluate((button: HTMLButtonElement) => { button.click(); button.click(); }); expect((await response).status()).toBe(201); await ui.settle(); expect((await world.db.auditSince(mark)).filter((row) => row.operation === 'record_create')).toHaveLength(1); await checkZone(world, api, zone); const member = await world.createUser(api, 'user'); expect((await api.post('/users', { username: 'short-password', role: 'user', password: world.secrets.register('tiny', 'short password') })).status).toBe(422); await ui.openUser(member.username); expect(await ui.deleteUserUi(member.username, 'another-user')).toBeNull(); expect((await ui.deleteUserUi(member.username))?.status).toBe(204); await ui.page.goBack(); await ui.settle(); expect((await world.db.user(member.username))?.deleted_at).not.toBeNull(); expect((await api.post('/users', { ...member })).status).toBe(409); expect((await api.put(`${up(admin.username)}/role`, { role: 'user' })).status).toBe(403); expect((await api.delete(up(admin.username), { confirm: admin.username })).status).toBe(403); expect((await api.put(`${zonePath}${up(admin.username)}`)).status).toBe(422); await ui.openZone(zone); expect(await ui.deleteZoneUi('wrong.example.net.')).toBeNull(); expect((await ui.deleteZoneUi(zone))?.status).toBe(204); await ui.page.goBack(); await expect(ui.page.locator('#records-body tr')).toHaveCount(0); expect((await api.get(zonePath)).status).toBe(404); expect(await world.db.records(zone)).toEqual([]); }); test('records page keeps its view in the URL and protects typed input', async ({ world }) => { const admin = await world.bootstrapAdmin(); const api = await world.apiAs(admin); const ui = await world.uiAs(admin); const zone = world.zoneName(); const zonePath = await createZone(api, zone); await addAddress(api, zonePath, 'zeta', '192.0.2.40'); await addAddress(api, zonePath, 'alpha', '192.0.2.41'); await addAddress(api, zonePath, 'www', '192.0.2.42'); await addAddress(api, zonePath, 'www2', '192.0.2.44'); const mixed = await api.post(`${zonePath}/records`, address('www', '192.0.2.43', 600)); expect(mixed.status, mixed.text).toBe(201); await ui.openZone(zone); const page = ui.page; const names = async () => (await ui.rows()).map((row) => row.name); const nameHeader = page.locator('#records-table th[data-sort=name]'); await nameHeader.getByRole('button').click(); await expect(nameHeader).toHaveAttribute('aria-sort', 'ascending'); expect(await names()).toEqual(['@', '@', 'alpha', 'www', 'www', 'www2', 'zeta']); await nameHeader.getByRole('button').click(); await expect(nameHeader).toHaveAttribute('aria-sort', 'descending'); expect(await names()).toEqual(['zeta', 'www2', 'www', 'www', 'alpha', '@', '@']); await page.locator('#filter-text').fill('alpha'); await expect(page).toHaveURL(/[?&]q=alpha(&|$)/); await expect(page).toHaveURL(/[?&]sort=-name(&|$)/); await page.reload(); await ui.settle(); await expect(page.locator('#filter-text')).toHaveValue('alpha'); await expect(nameHeader).toHaveAttribute('aria-sort', 'descending'); expect(await names()).toEqual(['alpha']); await page.getByRole('button', { name: 'Show www A in the records table' }).first().click(); await expect(page.locator('#filter-text')).toHaveValue(`www.${zone}`); await expect(page.locator('#filter-type')).toHaveValue('A'); await expect(page.locator('#filter-text')).toBeFocused(); expect(await names()).toEqual(['www', 'www']); await expect(page).toHaveURL(/[?&]name=www\./); await page.locator('#filter-text').fill(''); await page.locator('#filter-type').selectOption(''); const dialog = page.locator('#record-dialog'); await page.click('#record-add'); await page.fill('#rec-name', 'unsaved'); await page.mouse.click(4, 4); await expect(dialog, 'a backdrop click keeps an edited form open').toBeVisible(); await page.keyboard.press('Escape'); await expect(dialog).toBeHidden(); await page.click('#record-add'); await page.mouse.click(4, 4); await expect(dialog, 'a backdrop click closes an untouched form').toBeHidden(); await page.click('#record-add'); await page.fill('#rec-name', 'multi'); await page.fill('#rec-f-ip', '192.0.2.50'); const first = page.waitForResponse((res) => res.url().endsWith(`/api/v1${zonePath}/records`) && res.request().method() === 'POST'); await page.click('#rec-submit-another'); expect((await first).status()).toBe(201); await expect(dialog).toBeVisible(); await expect(page.locator('#rec-name')).toHaveValue('multi'); await expect(page.locator('#rec-f-ip')).toHaveValue(''); await expect(page.locator('#rec-f-ip')).toBeFocused(); await expect(page.locator('#rec-status')).toContainText('Record added.'); await page.fill('#rec-f-ip', '192.0.2.51'); const second = page.waitForResponse((res) => res.url().endsWith(`/api/v1${zonePath}/records`) && res.request().method() === 'POST'); await page.click('#rec-submit'); expect((await second).status()).toBe(201); await expect(dialog).toBeHidden(); await expect(page.locator('#records-body tr.row-flash')).toHaveCount(1); await expect(page.locator('#records-body tr.row-flash th')).toHaveText('multi'); await ui.settle(); expect((await world.db.records(zone)).filter((row) => row.name === 'multi').length).toBe(2); await page.click('#audit-link'); await ui.settle(); await expect(page.locator('#af-zone')).toHaveValue(zone); await expect(page.locator('#af-kind')).toHaveValue('change'); await expect(page.locator('#audit-body tr:not(.detail-row)').first()).toContainText(zone); });