This commit is contained in:
1 parent
fd67cca2d4
commit
d92dfdacff
11 files changed
+885
-83
No files matched your search
@@ -0,0 +1,115 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { compileFunction } from 'node:vm'
|
||||
import { createRequire } from 'node:module'
|
||||
|
||||
const root = new URL('../../frontend/src/views/components/ConfigSettings/', import.meta.url)
|
||||
const source = readFileSync(new URL('ConfigForm/ConfigForm.js', root), 'utf8')
|
||||
const { readFormConfig, getFieldGroups, updateFormConfig, validateFormConfig } = compileFunction(
|
||||
source.replaceAll('export function ', 'function ') + '\nreturn { readFormConfig, getFieldGroups, updateFormConfig, validateFormConfig }'
|
||||
)()
|
||||
|
||||
test('五个分区使用分组表单,控件和样式能编译且不再提供 JSON 编辑器', () => {
|
||||
const require = createRequire(new URL('../../frontend/package.json', import.meta.url))
|
||||
const { parse, compileScript, compileStyle } = require('@vue/compiler-sfc')
|
||||
for (const name of ['ConfigForm', 'ConfigField', 'ConfigEditor']) {
|
||||
const template = readFileSync(new URL(`${name}/${name}.vue`, root), 'utf8')
|
||||
const css = readFileSync(new URL(`${name}/${name}.css`, root), 'utf8')
|
||||
const { descriptor, errors } = parse(template)
|
||||
assert.deepEqual(errors, [])
|
||||
assert.doesNotThrow(() => compileScript(descriptor, { id: name, inlineTemplate: true }))
|
||||
assert.deepEqual(compileStyle({ source: css, id: `data-v-${name}`, scoped: true }).errors, [])
|
||||
assert.doesNotMatch(template, /&#x[0-9a-f]+;|type="textarea"|配置 JSON 编辑器/i)
|
||||
}
|
||||
for (const section of ['base', 'smtp', 'email', 'ssl', 'plugins']) {
|
||||
const groups = getFieldGroups(section, { plugins: { spam_block: { settings: null } } })
|
||||
assert.ok(groups.length)
|
||||
const keys = groups.flatMap(group => group.fields.map(field => field.path.join('.')))
|
||||
assert.equal(keys.length, new Set(keys).size)
|
||||
assert.ok(groups.every(group => /[\u4e00-\u9fff]/.test(group.title)))
|
||||
assert.ok(!keys.some(key => /generation|password|encryptionKey|capabilities/.test(key)))
|
||||
}
|
||||
})
|
||||
|
||||
test('单字段更新完整保留内部数据,锁定字段和污染路径无法写入', () => {
|
||||
const config = {
|
||||
generation: 8, schemaVersion: 2, database: { type: 'sqlite', localRef: 'test.db', password: null },
|
||||
domains: { primary: 'example.com', accepted: ['example.com'] },
|
||||
security: { passwordVersion: 'argon2id', encryptionKeyRef: 'system-managed' },
|
||||
future: { nested: [true, 1, null] }
|
||||
}
|
||||
const text = JSON.stringify(config, null, 2)
|
||||
const updated = readFormConfig(updateFormConfig(text, 'base', ['domains', 'primary'], 'mail.example.com'))
|
||||
assert.deepEqual(updated, { ...config, domains: { ...config.domains, primary: 'mail.example.com' } })
|
||||
for (const path of [['database', 'type'], ['database', 'localRef'], ['generation'], ['security', 'encryptionKeyRef'], ['__proto__', 'polluted'], ['constructor', 'prototype', 'polluted'], ['relay', 'relayHost']]) {
|
||||
assert.equal(updateFormConfig(text, 'base', path, 'changed'), text)
|
||||
}
|
||||
assert.equal({}.polluted, undefined)
|
||||
})
|
||||
|
||||
test('开关、数字、空数字和列表保持真实类型,代理 HTTPS 正确联动', () => {
|
||||
let text = JSON.stringify({ http: { tlsMode: 'direct', httpsEnabled: true, httpsPort: 443 } })
|
||||
text = updateFormConfig(text, 'base', ['http', 'tlsMode'], 'proxy')
|
||||
assert.deepEqual(readFormConfig(text).http, { tlsMode: 'proxy', httpsEnabled: false, httpsPort: 0 })
|
||||
assert.equal(updateFormConfig(text, 'base', ['http', 'httpsEnabled'], true), text)
|
||||
text = updateFormConfig(text, 'base', ['security', 'externalEmailEnabled'], true)
|
||||
assert.equal(readFormConfig(text).security.externalEmailEnabled, true)
|
||||
text = updateFormConfig(text, 'base', ['http', 'port'], 8080)
|
||||
assert.equal(readFormConfig(text).http.port, 8080)
|
||||
text = updateFormConfig(text, 'base', ['http', 'port'], undefined)
|
||||
assert.equal(readFormConfig(text).http.port, null)
|
||||
assert.ok(validateFormConfig('base', readFormConfig(text))['http.port'])
|
||||
for (const value of ['25', 0, -1, 65536, 1.5, null]) {
|
||||
assert.ok(validateFormConfig('email', { policy: { smtpPort: value, imapPort: 143, pop3Port: 110 } })['policy.smtpPort'])
|
||||
}
|
||||
assert.ok(validateFormConfig('email', { policy: { smtpPort: 143, imapPort: 143, pop3Port: 110 } })['policy.imapPort'])
|
||||
assert.deepEqual(validateFormConfig('email', { policy: { smtpPort: 25, imapPort: 143, pop3Port: 110 } }), {})
|
||||
})
|
||||
|
||||
test('域名校验兼容大小写和末尾点,拒绝重复项、空白和路径', () => {
|
||||
const valid = { database: { type: 'sqlite' }, domains: { primary: 'Example.com.', web: 'mail.example.com', accepted: ['example.com'] } }
|
||||
assert.deepEqual(validateFormConfig('base', valid), {})
|
||||
for (const accepted of [[], ['example.com', 'Example.com.'], [''], [' https://example.com'], ['other.example.com'], 'example.com', [null]]) {
|
||||
assert.ok(validateFormConfig('base', { ...valid, domains: { ...valid.domains, accepted } })['domains.accepted'])
|
||||
}
|
||||
for (const primary of ['https://example.com', 'example.com/path', 'bad..com', '-bad.com', '127.0.0.1', '例子.com']) {
|
||||
assert.ok(validateFormConfig('base', { ...valid, domains: { ...valid.domains, primary } })['domains.primary'])
|
||||
}
|
||||
})
|
||||
|
||||
test('插件网络列表拒绝无效和重复目标,空设置可编辑且非标准设置不覆盖', () => {
|
||||
const make = network => ({ plugins: { telegram_push: { permissions: { network }, settings: null } } })
|
||||
const valid = { scheme: 'https', host: 'api.example.com', port: 443 }
|
||||
assert.deepEqual(validateFormConfig('plugins', make([valid])), {})
|
||||
for (const network of [{}, [null], [{ ...valid, host: '127.0.0.1' }], [{ ...valid, host: 'bad..com' }], [{ ...valid, scheme: 'http' }], [{ ...valid, port: null }], [valid, { ...valid, host: 'API.EXAMPLE.COM.' }]]) {
|
||||
assert.ok(validateFormConfig('plugins', make(network))['plugins.telegram_push.permissions.network'])
|
||||
}
|
||||
const text = JSON.stringify(make([]))
|
||||
const updated = readFormConfig(updateFormConfig(text, 'plugins', ['plugins', 'telegram_push', 'settings', 'timeout'], 30))
|
||||
assert.equal(updated.plugins.telegram_push.settings.timeout, 30)
|
||||
const legacy = JSON.stringify({ plugins: { telegram_push: { settings: ['preserve'] } } })
|
||||
assert.equal(updateFormConfig(legacy, 'plugins', ['plugins', 'telegram_push', 'settings', 'timeout'], 30), legacy)
|
||||
})
|
||||
|
||||
test('不同插件超时字段使用各自实际单位,不把毫秒当作秒', () => {
|
||||
const groups = getFieldGroups('plugins', { plugins: { spam_block: { settings: { timeout: 10000 } }, telegram_push: { settings: { timeout: 30 } } } })
|
||||
const spamTimeout = groups.find(group => group.id === 'spam_block').fields.find(field => field.path.at(-1) === 'timeout')
|
||||
const telegramTimeout = groups.find(group => group.id === 'telegram_push').fields.find(field => field.path.at(-1) === 'timeout')
|
||||
assert.equal(spamTimeout.label, '请求超时(毫秒)')
|
||||
assert.equal(telegramTimeout.label, '请求超时(秒)')
|
||||
assert.deepEqual(validateFormConfig('plugins', { plugins: { spam_block: { settings: { timeout: 10000 } } } }), {})
|
||||
})
|
||||
|
||||
test('证书路径始终受管且引用成对,切回默认来源清除自定义目录', () => {
|
||||
const tls = { mode: 'managed_upload', defaultDirectory: 'config/ssl', customDirectory: 'config/ssl/imported', certificateRef: 'config/ssl/imported/cert.pem', privateKeyRef: 'config/ssl/imported/key.pem' }
|
||||
assert.deepEqual(validateFormConfig('ssl', { tls }), {})
|
||||
for (const customDirectory of ['C:/certs', '/etc/ssl', '../certs', 'config/../certs', 'config\\ssl', 'config//ssl']) {
|
||||
assert.ok(validateFormConfig('ssl', { tls: { ...tls, customDirectory } })['tls.customDirectory'])
|
||||
}
|
||||
assert.ok(validateFormConfig('ssl', { tls: { ...tls, privateKeyRef: '' } })['tls.privateKeyRef'])
|
||||
assert.ok(validateFormConfig('ssl', { tls: { ...tls, certificateRef: 'config/ssl/outside.pem' } })['tls.certificateRef'])
|
||||
const updated = readFormConfig(updateFormConfig(JSON.stringify({ tls }), 'ssl', ['tls', 'mode'], 'managed_default'))
|
||||
assert.equal(updated.tls.customDirectory, '')
|
||||
assert.equal(updated.tls.privateKeyRef, tls.privateKeyRef)
|
||||
})
|
||||
@@ -5,11 +5,14 @@ import { compileFunction } from 'node:vm'
|
||||
import { createRequire } from 'node:module'
|
||||
import { computed, reactive, ref } from './vue.mjs'
|
||||
|
||||
const formSource = readFileSync(new URL('../../frontend/src/views/components/ConfigSettings/ConfigForm/ConfigForm.js', import.meta.url), 'utf8')
|
||||
const formHelpers = compileFunction(formSource.replaceAll('export function ', 'function ') + '\nreturn { readFormConfig, updateFormConfig, validateFormConfig }')()
|
||||
const source = readFileSync(new URL('../../frontend/src/views/components/ConfigSettings/ConfigSettings.js', import.meta.url), 'utf8')
|
||||
const createPageWithKey = compileFunction(
|
||||
const compilePage = compileFunction(
|
||||
source.replace(/^import .*$/gm, '').replace('export function usePage', 'function usePage') + '\nreturn usePage(pageKey)',
|
||||
['computed', 'onMounted', 'reactive', 'ref', 'ElMessage', 'ElMessageBox', 'http', 'useGlobalStatusStore', 'pageKey', 'onBeforeUnmount']
|
||||
['computed', 'onMounted', 'reactive', 'ref', 'ElMessage', 'ElMessageBox', 'http', 'useGlobalStatusStore', 'pageKey', 'onBeforeUnmount', 'readFormConfig', 'updateFormConfig', 'validateFormConfig']
|
||||
)
|
||||
const createPageWithKey = (...args) => compilePage(...args, formHelpers.readFormConfig, formHelpers.updateFormConfig, formHelpers.validateFormConfig)
|
||||
const createPage = (...args) => createPageWithKey(...args, 'base', () => {})
|
||||
|
||||
function fixture(get, pageKey = 'base', options = {}) {
|
||||
@@ -323,3 +326,114 @@ test('独立页保持验证、保存、应用端点和代次协议,敏感值
|
||||
assert.equal(page.confirmed.value, false)
|
||||
assert.equal(page.savedGeneration.value, 0)
|
||||
})
|
||||
|
||||
test('可视化编辑按权限和工作状态保护,编辑会清除过期确认及候选代次', async () => {
|
||||
const { page, dispose } = fixture(async () => ({ errorNo: 0, data: { relay: { relayHost: 'smtp.example.com' } } }), 'smtp')
|
||||
await page.loadAll()
|
||||
const original = page.editorText.value
|
||||
for (const state of ['loading', 'sectionLoading', 'working', 'sectionError']) {
|
||||
page[state].value = true
|
||||
page.updateField(['relay', 'relayHost'], 'blocked.example.com')
|
||||
page.updateSecret('blocked-value')
|
||||
assert.equal(page.editorText.value, original)
|
||||
assert.equal(page.secretValue.value, '')
|
||||
page[state].value = false
|
||||
}
|
||||
const readonly = fixture(async () => ({ errorNo: 0, data: {} }), 'smtp', { hasCapability: () => false }).page
|
||||
readonly.updateField(['relay', 'relayHost'], 'blocked.example.com')
|
||||
assert.equal(readonly.editorText.value, '')
|
||||
page.savedGeneration.value = 8
|
||||
page.confirmed.value = true
|
||||
page.validationResult.value = { valid: true }
|
||||
page.updateField(['relay', 'relayHost'], 'new.example.com')
|
||||
assert.equal(page.formConfig.value.relay.relayHost, 'new.example.com')
|
||||
assert.equal(page.savedGeneration.value, 0)
|
||||
assert.equal(page.confirmed.value, false)
|
||||
assert.equal(page.validationResult.value, null)
|
||||
dispose()
|
||||
page.updateField(['relay', 'relayHost'], 'blocked.example.com')
|
||||
assert.equal(page.formConfig.value.relay.relayHost, 'new.example.com')
|
||||
})
|
||||
|
||||
test('前端无效字段阻止网络请求,修正字段后同步清除错误', async () => {
|
||||
const requests = []
|
||||
const { page } = fixture(async () => ({ errorNo: 0, data: { policy: { smtpPort: 25, imapPort: 143, pop3Port: 110 } } }), 'email', {
|
||||
http: { post: async path => { requests.push(path); return { errorNo: 0, data: { valid: true } } } }
|
||||
})
|
||||
await page.loadAll()
|
||||
page.updateField(['policy', 'smtpPort'], null)
|
||||
await page.validateConfig()
|
||||
await page.saveConfig()
|
||||
assert.deepEqual(requests, [])
|
||||
assert.ok(page.formErrors.value['policy.smtpPort'])
|
||||
page.updateField(['policy', 'smtpPort'], 2525)
|
||||
assert.deepEqual(page.formErrors.value, {})
|
||||
await page.validateConfig()
|
||||
assert.deepEqual(requests, ['/api/v1/config/email/validate'])
|
||||
})
|
||||
|
||||
test('插件读取状态字段不传回严格解析接口,未展示字段和权限仍保留', async () => {
|
||||
let request
|
||||
const plugin = { enabled: true, executable: 'pmail_telegram_push.exe', permissions: { capabilities: ['network', 'userSettings', 'settingsHtml'], network: [] }, settings: { timeout: 30, future: 'preserved' }, secretStatus: { configured: true }, settingsConfigured: true }
|
||||
const { page } = fixture(async () => ({ errorNo: 0, data: { meta: { generation: 7 }, plugins: { telegram_push: plugin } } }), 'plugins', {
|
||||
http: { put: async (path, body) => { request = body; return { errorNo: 0, data: { generation: 8 } } } }
|
||||
})
|
||||
await page.loadSection()
|
||||
page.updateField(['plugins', 'telegram_push', 'settings', 'timeout'], 40)
|
||||
await page.saveConfig()
|
||||
const actual = request.payload.plugins.telegram_push
|
||||
assert.equal(actual.secretStatus, undefined)
|
||||
assert.equal(actual.settingsConfigured, undefined)
|
||||
assert.deepEqual(actual.permissions, plugin.permissions)
|
||||
assert.deepEqual(actual.settings, { timeout: 40, future: 'preserved' })
|
||||
assert.equal(request.payload.meta.generation, 7)
|
||||
assert.deepEqual(request.secretChanges, {})
|
||||
assert.equal(page.formConfig.value.plugins.telegram_push.secretStatus.configured, true)
|
||||
})
|
||||
|
||||
test('SQLite 不显示或发送密码,网络数据库和中继密码留空保留且不裁剪空格', async () => {
|
||||
for (const type of ['sqlite', 'mysql', 'postgresql']) {
|
||||
let request
|
||||
const { page } = fixture(async () => ({ errorNo: 0, data: {
|
||||
database: { type, host: 'localhost', port: 3306, username: 'test' },
|
||||
domains: { primary: 'example.com', web: 'mail.example.com', accepted: ['example.com'] }
|
||||
} }), 'base', {
|
||||
http: { post: async (path, body) => { request = body; return { errorNo: 0, data: { valid: true } } } }
|
||||
})
|
||||
await page.loadSection()
|
||||
assert.equal(!!page.secretField.value, type !== 'sqlite')
|
||||
await page.validateConfig()
|
||||
assert.deepEqual(request.secretChanges, {})
|
||||
page.secretValue.value = ' test-only-value '
|
||||
await page.validateConfig()
|
||||
assert.deepEqual(request.secretChanges, type === 'sqlite' ? {} : { 'database.password': { operation: 'replace', value: { plaintext: ' test-only-value ' } } })
|
||||
}
|
||||
let request
|
||||
const { page } = fixture(async () => ({ errorNo: 0, data: {} }), 'smtp', {
|
||||
http: { post: async (path, body) => { request = body; return { errorNo: 0, data: { valid: true } } } }
|
||||
})
|
||||
await page.loadSection()
|
||||
page.updateSecret(' smtp-test-only ')
|
||||
await page.validateConfig()
|
||||
assert.equal(request.secretChanges['relay.password'].value.plaintext, ' smtp-test-only ')
|
||||
})
|
||||
|
||||
test('保存后更改密码或填写内容不能应用旧候选', async () => {
|
||||
const requests = []
|
||||
const { page } = fixture(async () => ({ errorNo: 0, data: {} }), 'smtp', {
|
||||
http: {
|
||||
put: async () => ({ errorNo: 0, data: { generation: 8 } }),
|
||||
post: async path => { requests.push(path); return { errorNo: 0 } }
|
||||
}
|
||||
})
|
||||
await page.loadSection()
|
||||
await page.saveConfig()
|
||||
page.updateSecret('test-only-value')
|
||||
assert.equal(page.savedGeneration.value, 0)
|
||||
await page.applyConfig()
|
||||
await page.saveConfig()
|
||||
page.editorText.value = '{"relay":{"relayHost":"new.example.com"}}'
|
||||
await page.applyConfig()
|
||||
assert.equal(page.savedGeneration.value, 0)
|
||||
assert.deepEqual(requests, [])
|
||||
})
|
||||
Reference in new issue
Block a user