62 lines
2.6 KiB
JavaScript
62 lines
2.6 KiB
JavaScript
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const pages = [
|
|
'login.html',
|
|
'register.html',
|
|
'forgot-password.html'
|
|
];
|
|
|
|
function readPage(fileName) {
|
|
return fs.readFileSync(path.join(__dirname, '..', fileName), 'utf8');
|
|
}
|
|
|
|
function run() {
|
|
const expectedFormIds = {
|
|
'login.html': ['login-password-form', 'login-sms-form'],
|
|
'register.html': ['register-form'],
|
|
'forgot-password.html': ['password-reset-form']
|
|
};
|
|
|
|
pages.forEach((fileName) => {
|
|
const html = readPage(fileName);
|
|
const formCaptchaBox = /<form[\s\S]*?data-captcha-box[\s\S]*?<\/form>/i.test(html);
|
|
const pageCaptchaBox = /data-captcha-box/.test(html);
|
|
const hasLayuiCss = html.includes('href="public/layui/css/layui.css"');
|
|
const hasTacCss = html.includes('href="public/tac/css/tac.css"');
|
|
const hasCaptchaModalCss = html.includes('href="public/css/captcha-modal.css"');
|
|
const layuiScriptIndex = html.indexOf('src="public/layui/layui.js"');
|
|
const authScriptIndex = html.indexOf('src="public/js/auth-pages.js"');
|
|
|
|
assert.strictEqual(formCaptchaBox, false, `${fileName} 不应把 TAC 挂载点放在表单内部`);
|
|
assert.strictEqual(pageCaptchaBox, false, `${fileName} 不应保留会将 TAC 渲染到页面底部的静态挂载点`);
|
|
assert.strictEqual(hasLayuiCss, true, `${fileName} 应加载 layui.css 作为 layer 提示样式依赖`);
|
|
assert.strictEqual(hasTacCss, true, `${fileName} 应加载 TAC 自带样式`);
|
|
assert.strictEqual(hasCaptchaModalCss, false, `${fileName} 不应加载自定义验证码样式`);
|
|
assert.notStrictEqual(layuiScriptIndex, -1, `${fileName} 应加载 layui.js 使用 layer.msg`);
|
|
assert.ok(layuiScriptIndex < authScriptIndex, `${fileName} 应在 auth-pages.js 之前加载 layui.js`);
|
|
assert.deepStrictEqual(
|
|
Array.from(html.matchAll(/<form[^>]*id="([^"]+)"/g)).map((match) => match[1]),
|
|
expectedFormIds[fileName],
|
|
`${fileName} 应使用表单 id 作为 JS 挂载点`
|
|
);
|
|
assert.strictEqual(/data-auth-form=|data-captcha-scene=|data-success-url=|data-scene-code=|data-login-mode/.test(html), false, `${fileName} 不应把认证业务配置写在 HTML data 属性上`);
|
|
assert.strictEqual(
|
|
(html.match(/<input type="hidden" name="validToken" \/>/g) || []).length,
|
|
expectedFormIds[fileName].length,
|
|
`${fileName} 每个认证表单都应携带 validToken 隐藏字段`
|
|
);
|
|
});
|
|
|
|
assert.strictEqual(
|
|
readPage('register.html').includes('id="register-form"'),
|
|
true,
|
|
'register.html 应使用 register-form 挂载注册逻辑'
|
|
);
|
|
|
|
console.log('auth-page-structure tests passed');
|
|
}
|
|
|
|
run();
|