Files
memind/page-access-policy.test.mjs
T
john 6b0c633a75 feat(page-data): complete Phase 4-5, ops UI, and publish integration
Add visitor roles, row-level scope, owner ops APIs, MySQL policy index,
Turnstile captcha, browser client SDK, publish-panel dataset binding,
acceptance tests, and usage documentation.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 14:52:49 +08:00

83 lines
2.5 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
buildEffectiveDataset,
closePolicyDataset,
normalizePageAccessPolicy,
policyAllowsAction,
} from './page-access-policy.mjs';
test('normalizePageAccessPolicy validates access mode and datasets', () => {
const policy = normalizePageAccessPolicy(
{
pageId: 'page-1',
ownerUserId: 'user-1',
accessMode: 'public',
datasets: {
registrations: {
insert: true,
columns: { insert: ['name', 'phone'] },
},
},
},
{ fallbackPageId: 'page-1', fallbackOwnerUserId: 'user-1' },
);
assert.equal(policy.accessMode, 'public');
assert.equal(policy.datasets.registrations.insert, true);
assert.equal(policy.datasets.registrations.read, false);
});
test('buildEffectiveDataset intersects registry actions with policy permissions', () => {
const effective = buildEffectiveDataset(
{
name: 'registrations',
table: 'form_registrations',
description: '报名',
actions: ['read', 'insert', 'update', 'soft_delete'],
columns: {
read: ['id', 'name', 'phone', 'secret'],
insert: ['name', 'phone', 'secret'],
update: ['status'],
soft_delete: ['id'],
},
},
{
read: false,
insert: true,
update: false,
softDelete: false,
columns: { insert: ['name', 'phone'] },
},
);
assert.deepEqual(effective.actions, ['insert']);
assert.deepEqual(effective.columns.insert, ['name', 'phone']);
});
test('policyAllowsAction respects per-dataset flags', () => {
const policy = normalizePageAccessPolicy({
pageId: 'page-1',
ownerUserId: 'user-1',
accessMode: 'password',
datasets: {
ledger: { read: true, insert: true, update: true, softDelete: true, columns: {} },
},
});
assert.equal(policyAllowsAction(policy, 'ledger', 'read'), true);
assert.equal(policyAllowsAction(policy, 'ledger', 'insert'), true);
assert.equal(policyAllowsAction(policy, 'missing', 'read'), false);
});
test('closePolicyDataset disables all dataset actions', () => {
const policy = normalizePageAccessPolicy({
pageId: 'page-1',
ownerUserId: 'user-1',
accessMode: 'password',
datasets: {
ledger: { read: true, insert: true, update: true, softDelete: true, columns: {} },
},
});
const closed = closePolicyDataset(policy, 'ledger');
assert.equal(closed.datasets.ledger.closed, true);
assert.equal(policyAllowsAction(closed, 'ledger', 'insert'), false);
});