127 lines
6.4 KiB
JavaScript
127 lines
6.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { createRequire } from 'node:module';
|
|
import { resolve } from 'node:path';
|
|
import test from 'node:test';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
import vm from 'node:vm';
|
|
|
|
const require = createRequire(process.env.KEFU_SSO_TYPESCRIPT
|
|
? pathToFileURL(resolve(process.env.KEFU_SSO_TYPESCRIPT, 'package.json'))
|
|
: new URL('../package.json', import.meta.url));
|
|
const ts = require('typescript');
|
|
const vue = require('vue');
|
|
const { parse, compileScript, compileTemplate } = require('@vue/compiler-sfc');
|
|
const { renderToString } = require('@vue/server-renderer');
|
|
|
|
function load(source, dependencies) {
|
|
const compiled = ts.transpileModule(source, { compilerOptions: {
|
|
module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022,
|
|
}, reportDiagnostics: true });
|
|
assert.deepEqual(compiled.diagnostics, []);
|
|
const module = { exports: {} };
|
|
vm.runInNewContext(compiled.outputText, { module, exports: module.exports,
|
|
require(name) { assert.ok(name in dependencies, `Unexpected import ${name}`); return dependencies[name]; },
|
|
// Normal password login must not read browser redirect/storage APIs.
|
|
get window() { assert.fail('Password login accessed window'); },
|
|
get localStorage() { assert.fail('Password login accessed localStorage'); },
|
|
console });
|
|
return module.exports;
|
|
}
|
|
|
|
const filename = fileURLToPath(new URL('../src/views/_core/authentication/login.vue', import.meta.url));
|
|
const { descriptor } = parse(await readFile(filename, 'utf8'), { filename });
|
|
const script = compileScript(descriptor, { id: 'password-login-page-test' });
|
|
const template = compileTemplate({ source: descriptor.template.content, filename,
|
|
id: 'password-login-page-test', compilerOptions: { bindingMetadata: script.bindings } });
|
|
assert.deepEqual(template.errors, []);
|
|
|
|
async function page(authLogin) {
|
|
let submit;
|
|
let formProps;
|
|
const store = vue.reactive({ loginLoading: false, authLogin });
|
|
// Render the real page SFC. The shared form adapter displays its schema/slots
|
|
// and exposes the submit listener assigned by the compiled page template.
|
|
const AuthenticationLogin = vue.defineComponent({
|
|
props: ['formSchema', 'loading', 'title', 'subTitle', 'showCodeLogin', 'showForgetPassword',
|
|
'showQrcodeLogin', 'showRegister', 'showRememberMe', 'showThirdPartyLogin'],
|
|
emits: ['submit'],
|
|
setup(props, { slots }) {
|
|
formProps = props;
|
|
const instance = vue.getCurrentInstance();
|
|
submit = values => instance.vnode.props.onSubmit(values);
|
|
return () => vue.h('section', {}, [
|
|
vue.h('h1', {}, props.title), vue.h('p', {}, props.subTitle),
|
|
vue.h('form', {}, [
|
|
...props.formSchema.map(field => vue.h('input', { name: field.fieldName,
|
|
type: field.component === 'VbenInputPassword' ? 'password' : 'text',
|
|
placeholder: field.componentProps.placeholder })),
|
|
vue.h('button', { type: 'submit', disabled: props.loading }, '登录'),
|
|
]),
|
|
slots['third-party-login']?.(),
|
|
]);
|
|
},
|
|
});
|
|
// No config API, group URL helper or router is provided: an accidental import
|
|
// or use fails this actual SFC test instead of silently becoming a mock call.
|
|
const component = load(script.content, { vue,
|
|
'@vben/common-ui': { AuthenticationLogin, z: { string: () => ({ min: () => ({}) }) } },
|
|
'#/locales/t': { t: value => value }, '#/store': { useAuthStore: () => store },
|
|
}).default;
|
|
component.render = load(template.code, { vue }).render;
|
|
const html = await renderToString(vue.createSSRApp(component));
|
|
return { html, submit, store, formProps };
|
|
}
|
|
|
|
test('实际登录页只有一份账号密码表单和登录按钮,ZYT/本地均走普通提交', async () => {
|
|
const calls = [];
|
|
const view = await page(async values => { calls.push(values); });
|
|
assert.equal((view.html.match(/<form(?:\s|>)/g) || []).length, 1);
|
|
assert.equal((view.html.match(/<input\b/g) || []).length, 2);
|
|
assert.equal((view.html.match(/<button\b/g) || []).length, 1);
|
|
assert.match(view.html, /name="username"[^>]*placeholder="ZYT 账号或客服本地账号"/);
|
|
assert.match(view.html, /name="password" type="password"[^>]*placeholder="ZYT 密码或客服本地密码"/);
|
|
assert.match(view.html, /ZYT 首次登录为基础用户,业务权限由客服管理员分配/);
|
|
assert.doesNotMatch(view.html, /集团账号登录|重试获取集团入口|href=|group-login/);
|
|
for (const name of ['showCodeLogin', 'showForgetPassword', 'showQrcodeLogin', 'showRegister',
|
|
'showRememberMe', 'showThirdPartyLogin']) assert.equal(view.formProps[name], false);
|
|
for (const username of ['local_operator', 'zyt_operator']) {
|
|
await view.submit({ username, password: 'Fixture@2026' });
|
|
}
|
|
assert.deepEqual(calls, [
|
|
{ username: 'local_operator', password: 'Fixture@2026' },
|
|
{ username: 'zyt_operator', password: 'Fixture@2026' },
|
|
]);
|
|
});
|
|
|
|
test('实际登录页普通提交失败后仍可用同一份表单重试', async () => {
|
|
let attempts = 0;
|
|
const view = await page(async () => {
|
|
if (++attempts === 1) throw new Error('账号或密码错误');
|
|
return { userInfo: { username: 'zyt_operator' } };
|
|
});
|
|
const credentials = { username: 'zyt_operator', password: 'Fixture@2026' };
|
|
await assert.rejects(view.submit(credentials), /账号或密码错误/);
|
|
assert.equal((await view.submit(credentials)).userInfo.username, 'zyt_operator');
|
|
assert.equal(attempts, 2);
|
|
});
|
|
|
|
test('ZYT/本地密码只随统一POST body发送,不进入URL或localStorage', async () => {
|
|
const requests = [];
|
|
const api = load(await readFile(new URL('../src/api/core/auth.ts', import.meta.url), 'utf8'), {
|
|
'#/locales/t': { t: value => value },
|
|
'#/api/request': {
|
|
requestClient: { async post(...args) { requests.push(args); return { access_token: 'current-token' }; } },
|
|
baseRequestClient: { get() { assert.fail('Password login fetched group config'); } },
|
|
},
|
|
});
|
|
for (const username of ['local_operator', 'zyt_operator']) {
|
|
const result = await api.loginApi({ username, password: 'Fixture@2026', redirect: 'https://unused.example/' });
|
|
assert.equal(result.accessToken, 'current-token');
|
|
}
|
|
assert.deepEqual(JSON.parse(JSON.stringify(requests)), [
|
|
['/auth/login', { username: 'local_operator', password: 'Fixture@2026', device_name: 'web' }],
|
|
['/auth/login', { username: 'zyt_operator', password: 'Fixture@2026', device_name: 'web' }],
|
|
]);
|
|
});
|