- Migration 20261122130000_release_notes.sql: release_notes (version, title, grouped items, draft/published/withdrawn) and release_note_reads; RLS on, no table privilege for app/service roles; users go through service-role functions list_release_notes_for_user / mark_release_note_read (a version published before the account existed counts as seen); the admin writes through audited admin_save_release_note / admin_set_release_note_status gated by the new content.release_notes.write (owner, operations, content_editor) and reads via an admin_runtime select policy. - APIs: GET/POST /api/release-notes (signed-in user only) and GET/POST /api/admin/release-notes. - Admin page 更新说明: table + editor drawer with 新功能/修复/改进 lists and a live preview; 保存草稿 / 发布 / 撤回. - User side: 「本次更新」 dialog once per version (知道了 records the read); settings gets an 更新说明 pane (phone segmented nav becomes four). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
117 lines
8.0 KiB
TypeScript
117 lines
8.0 KiB
TypeScript
// Issue #331 「更新说明」: what CI must prove (it needs a database).
|
|
// - Only content.release_notes.write may create, edit, publish or withdraw a note, through
|
|
// audited admin functions executable by admin_runtime; admin_runtime reads the table
|
|
// through a select policy, and no runtime role writes the tables directly.
|
|
// - A user sees each published version once: unseen until mark_release_note_read, and a
|
|
// version published before the account existed counts as seen. Both user functions are
|
|
// service_role only.
|
|
import assert from "node:assert/strict";
|
|
import { spawnSync } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import test from "node:test";
|
|
|
|
import { startPostgresFixture } from "./helpers/postgres-fixture.ts";
|
|
|
|
const runnerPath = fileURLToPath(new URL("../scripts/db-migrate.mjs", import.meta.url));
|
|
const editorId = "33100000-0000-4000-8000-000000000331";
|
|
const outsiderId = "33100000-0000-4000-8000-000000010331";
|
|
const oldUserId = "33100000-0000-4000-8000-000000020331";
|
|
const newUserId = "33100000-0000-4000-8000-000000030331";
|
|
|
|
function dockerAvailable(): boolean {
|
|
return spawnSync("docker", ["version", "--format", "{{.Server.Version}}"], { encoding: "utf8", stdio: "ignore" }).status === 0;
|
|
}
|
|
|
|
test("release notes: editors publish through audited functions; each user sees each version once", {
|
|
skip: dockerAvailable() ? false : "docker unavailable on this host",
|
|
}, () => {
|
|
const fixture = startPostgresFixture();
|
|
try {
|
|
const migration = spawnSync(process.execPath, [runnerPath], {
|
|
encoding: "utf8",
|
|
env: { ...process.env, SCHEMA_DATABASE_URL: fixture.connectionUrl("schema_owner", "schema-owner-test-password") },
|
|
});
|
|
assert.equal(migration.status, 0, `${migration.stdout}${migration.stderr}`);
|
|
assert.match(migration.stdout, /applied 20261122130000_release_notes\.sql/);
|
|
|
|
const save = "public.admin_save_release_note(uuid, uuid, text, text, jsonb, text)";
|
|
const status = "public.admin_set_release_note_status(uuid, uuid, text, text)";
|
|
const list = "public.list_release_notes_for_user(uuid)";
|
|
const read = "public.mark_release_note_read(uuid, uuid)";
|
|
for (const signature of [save, status]) {
|
|
assert.equal(fixture.psql(`select has_function_privilege('admin_runtime', '${signature}', 'EXECUTE')`), "t");
|
|
assert.equal(fixture.psql(`select has_function_privilege('authenticated', '${signature}', 'EXECUTE')`), "f");
|
|
assert.equal(fixture.psql(`select has_function_privilege('app_runtime', '${signature}', 'EXECUTE')`), "f");
|
|
}
|
|
for (const signature of [list, read]) {
|
|
assert.equal(fixture.psql(`select has_function_privilege('service_role', '${signature}', 'EXECUTE')`), "t");
|
|
assert.equal(fixture.psql(`select has_function_privilege('authenticated', '${signature}', 'EXECUTE')`), "f");
|
|
assert.equal(fixture.psql(`select has_function_privilege('admin_runtime', '${signature}', 'EXECUTE')`), "f");
|
|
}
|
|
for (const table of ["public.release_notes", "public.release_note_reads"]) {
|
|
for (const role of ["app_runtime", "service_runtime", "authenticated"]) {
|
|
assert.equal(fixture.psql(`select has_table_privilege('${role}', '${table}', 'INSERT')`), "f", `${role} ${table}`);
|
|
}
|
|
}
|
|
assert.equal(fixture.psql(`select has_table_privilege('admin_runtime', 'public.release_notes', 'SELECT')`), "t");
|
|
assert.equal(fixture.psql(`select has_table_privilege('admin_runtime', 'public.release_notes', 'UPDATE')`), "f");
|
|
assert.equal(fixture.psql(`select count(*) from pg_policies where tablename = 'release_notes' and policyname = 'release_notes_admin_read'`), "1");
|
|
|
|
fixture.psql(`
|
|
insert into identity.users (id, name, email, email_verified, email_verified_at)
|
|
values
|
|
('${editorId}', 'Fictional Editor', 'editor-331@example.invalid', true, now()),
|
|
('${outsiderId}', 'Fictional Outsider', 'outsider-331@example.invalid', true, now()),
|
|
('${oldUserId}', 'Fictional Early User', 'early-331@example.invalid', true, now()),
|
|
('${newUserId}', 'Fictional Late User', 'late-331@example.invalid', true, now());
|
|
insert into public.admin_users (user_id, created_by) values ('${editorId}', '${editorId}');
|
|
insert into public.admin_user_roles (admin_user_id, role_id, assigned_by)
|
|
select '${editorId}', id, '${editorId}' from public.admin_roles where code = 'content_editor';
|
|
update auth.users set created_at = now() - interval '30 days' where id = '${oldUserId}';
|
|
`);
|
|
|
|
const items = JSON.stringify([{ group: "feature", text: "报告里可以选中一段直接提问" }, { group: "fix", text: "打开报告不再空白" }]);
|
|
// An outsider cannot write.
|
|
assert.throws(() => fixture.psql(`select public.admin_save_release_note('${outsiderId}', null, '2026.11.23', '上线', '${items}'::jsonb, 'r-331-0')`), /admin_permission_denied/);
|
|
// A malformed item list is refused.
|
|
assert.throws(() => fixture.psql(`select public.admin_save_release_note('${editorId}', null, '2026.11.23', '上线', '[{"group":"news","text":"x"}]'::jsonb, 'r-331-1')`));
|
|
|
|
const noteId = fixture.psql(`select public.admin_save_release_note('${editorId}', null, '2026.11.23', '上线', '${items}'::jsonb, 'r-331-2')`);
|
|
assert.match(noteId, /^[0-9a-f-]{36}$/);
|
|
// A draft is invisible to users.
|
|
assert.equal(fixture.psql(`select count(*) from public.list_release_notes_for_user('${oldUserId}')`), "0");
|
|
// Withdrawing a draft is refused.
|
|
assert.throws(() => fixture.psql(`select public.admin_set_release_note_status('${editorId}', '${noteId}', 'withdrawn', 'r-331-3')`));
|
|
|
|
assert.equal(fixture.psql(`select public.admin_set_release_note_status('${editorId}', '${noteId}', 'published', 'r-331-4')`), "published");
|
|
// The account created before publishing has not seen it; the one created after counts as seen.
|
|
fixture.psql(`update auth.users set created_at = now() + interval '1 minute' where id = '${newUserId}'`);
|
|
assert.equal(fixture.psql(`select seen from public.list_release_notes_for_user('${oldUserId}')`), "f");
|
|
assert.equal(fixture.psql(`select seen from public.list_release_notes_for_user('${newUserId}')`), "t");
|
|
|
|
assert.equal(fixture.psql(`select public.mark_release_note_read('${oldUserId}', '${noteId}')`), "t");
|
|
assert.equal(fixture.psql(`select public.mark_release_note_read('${oldUserId}', '${noteId}')`), "t");
|
|
assert.equal(fixture.psql(`select seen from public.list_release_notes_for_user('${oldUserId}')`), "t");
|
|
assert.equal(fixture.psql(`select count(*) from public.release_note_reads where user_id = '${oldUserId}'`), "1");
|
|
|
|
// Withdraw hides it; publishing again keeps the first publish time and the reads.
|
|
const firstPublished = fixture.psql(`select published_at from public.release_notes where id = '${noteId}'`);
|
|
assert.equal(fixture.psql(`select public.admin_set_release_note_status('${editorId}', '${noteId}', 'withdrawn', 'r-331-5')`), "withdrawn");
|
|
assert.equal(fixture.psql(`select count(*) from public.list_release_notes_for_user('${oldUserId}')`), "0");
|
|
assert.throws(() => fixture.psql(`select public.mark_release_note_read('${outsiderId}', '${noteId}')`));
|
|
assert.equal(fixture.psql(`select public.admin_set_release_note_status('${editorId}', '${noteId}', 'published', 'r-331-6')`), "published");
|
|
assert.equal(fixture.psql(`select published_at from public.release_notes where id = '${noteId}'`), firstPublished);
|
|
assert.equal(fixture.psql(`select seen from public.list_release_notes_for_user('${oldUserId}')`), "t");
|
|
|
|
// Every write is in the audit log.
|
|
assert.equal(
|
|
fixture.psql(`select string_agg(action, ',' order by created_at) from audit.admin_audit_logs where target_type = 'release_note'`),
|
|
"content.release_notes.create,content.release_notes.publish,content.release_notes.withdraw,content.release_notes.publish",
|
|
);
|
|
// A duplicate version is refused.
|
|
assert.throws(() => fixture.psql(`select public.admin_save_release_note('${editorId}', null, '2026.11.23', '重复', '${items}'::jsonb, 'r-331-7')`));
|
|
} finally {
|
|
fixture.stop();
|
|
}
|
|
});
|