82 lines
2.8 KiB
JavaScript
82 lines
2.8 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
assessPageDataAuthenticationContract,
|
|
buildSmokeInsertRow,
|
|
extractInjectedPageIdFromHtml,
|
|
normalizePageDataApiBase,
|
|
} from './page-data-delivery-assess.mjs';
|
|
|
|
test('password pages must authenticate with the server before data reads', () => {
|
|
assert.deepEqual(
|
|
assessPageDataAuthenticationContract({
|
|
policy: { accessMode: 'password' },
|
|
html: '<script>if (pwd === ADMIN_PASSWORD) loadData()</script>',
|
|
}),
|
|
[
|
|
'password_page_missing_server_authentication',
|
|
'password_page_uses_client_side_password_gate',
|
|
],
|
|
);
|
|
assert.deepEqual(
|
|
assessPageDataAuthenticationContract({
|
|
policy: { accessMode: 'password' },
|
|
html: '<script>client.authenticate(password).then(loadData)</script>',
|
|
}),
|
|
[],
|
|
);
|
|
assert.deepEqual(
|
|
assessPageDataAuthenticationContract({
|
|
policy: { accessMode: 'password' },
|
|
html: '<script>if (passwordInput.value !== "88888888") return; client.authenticate(passwordInput.value)</script>',
|
|
}),
|
|
['password_page_uses_client_side_password_gate'],
|
|
);
|
|
});
|
|
|
|
test('public pages cannot use a client-side password gate as authorization', () => {
|
|
assert.deepEqual(
|
|
assessPageDataAuthenticationContract({
|
|
policy: { accessMode: 'public' },
|
|
html: '<script>if (pwd === DIARY_PASSWORD) client.insertRow("diary_entries", row)</script>',
|
|
}),
|
|
['public_page_uses_client_side_password_gate'],
|
|
);
|
|
});
|
|
|
|
test('extractInjectedPageIdFromHtml reads injected pageId', () => {
|
|
const html = `<html><head>
|
|
<meta name="mindspace-page-data-page-id" content="page-abc">
|
|
<script>window.__MINDSPACE_PAGE_DATA__={"pageId":"page-abc","accessMode":"public"};</script>
|
|
</head></html>`;
|
|
assert.equal(extractInjectedPageIdFromHtml(html), 'page-abc');
|
|
});
|
|
|
|
test('extractInjectedPageIdFromHtml reads unquoted pageId keys', () => {
|
|
const html =
|
|
'<script>window.__MINDSPACE_PAGE_DATA__={pageId:"page-unquoted",accessMode:"public"};</script>';
|
|
assert.equal(extractInjectedPageIdFromHtml(html), 'page-unquoted');
|
|
});
|
|
|
|
test('normalizePageDataApiBase appends /api to public base url', () => {
|
|
assert.equal(normalizePageDataApiBase('https://m.tkmind.cn'), 'https://m.tkmind.cn/api');
|
|
assert.equal(normalizePageDataApiBase('https://m.tkmind.cn/api'), 'https://m.tkmind.cn/api');
|
|
assert.equal(normalizePageDataApiBase('/api'), '/api');
|
|
});
|
|
|
|
test('buildSmokeInsertRow fills insert columns', () => {
|
|
const row = buildSmokeInsertRow(
|
|
{
|
|
datasets: {
|
|
dining_survey: {
|
|
columns: { insert: ['q1_frequency', 'overall_rating', 'phone'] },
|
|
},
|
|
},
|
|
},
|
|
'dining_survey',
|
|
);
|
|
assert.equal(row.q1_frequency, 'smoke-test');
|
|
assert.equal(row.overall_rating, 3);
|
|
assert.equal(row.phone, '13800000000');
|
|
});
|