统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
1 parent
28cd9d3406
commit
f60d3b0880
603 files changed
+2351
-660
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
AUTH_STATUS,
|
||||
classifyAuthenticationError,
|
||||
classifyAuthenticationResponse,
|
||||
runSharedAuthRefresh
|
||||
} from "../../frontend/src/utils/auth.mjs";
|
||||
|
||||
test("认证响应会区分已登录、未登录和服务不可用", () => {
|
||||
assert.equal(
|
||||
classifyAuthenticationResponse({errorNo: 0, data: {account: "user@example.com"}}),
|
||||
AUTH_STATUS.AUTHENTICATED
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationResponse({errorNo: 403}),
|
||||
AUTH_STATUS.UNAUTHENTICATED
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationResponse({errorNo: 402}),
|
||||
AUTH_STATUS.NEEDS_SETUP
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationResponse({errorNo: 500}),
|
||||
AUTH_STATUS.UNAVAILABLE
|
||||
);
|
||||
});
|
||||
|
||||
test("只有认证接口的 HTTP 403 或明确业务码会被识别为未登录", () => {
|
||||
assert.equal(
|
||||
classifyAuthenticationError(
|
||||
{response: {status: 403}},
|
||||
{authenticationRequest: true}
|
||||
),
|
||||
AUTH_STATUS.UNAUTHENTICATED
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationError({response: {status: 403}}, {authenticationRequest: false}),
|
||||
AUTH_STATUS.UNAVAILABLE
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationError({response: {status: 200, data: {errorNo: 403}}}),
|
||||
AUTH_STATUS.UNAUTHENTICATED
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationError({response: {status: 402}}, {authenticationRequest: true}),
|
||||
AUTH_STATUS.NEEDS_SETUP
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationError({response: {status: 500}}),
|
||||
AUTH_STATUS.UNAVAILABLE
|
||||
);
|
||||
assert.equal(
|
||||
classifyAuthenticationError(new Error("Network Error")),
|
||||
AUTH_STATUS.UNAVAILABLE
|
||||
);
|
||||
});
|
||||
|
||||
test("并发认证刷新只执行一次请求并返回同一个结果", async () => {
|
||||
let requestCount = 0;
|
||||
let resolveRequest;
|
||||
const request = () => {
|
||||
requestCount += 1;
|
||||
return new Promise((resolve) => {
|
||||
resolveRequest = resolve;
|
||||
});
|
||||
};
|
||||
|
||||
const first = runSharedAuthRefresh(request);
|
||||
const second = runSharedAuthRefresh(request);
|
||||
|
||||
assert.strictEqual(first, second);
|
||||
await Promise.resolve();
|
||||
assert.equal(requestCount, 1);
|
||||
|
||||
resolveRequest(AUTH_STATUS.AUTHENTICATED);
|
||||
assert.equal(await first, AUTH_STATUS.AUTHENTICATED);
|
||||
assert.equal(await second, AUTH_STATUS.AUTHENTICATED);
|
||||
|
||||
const third = runSharedAuthRefresh(async () => {
|
||||
requestCount += 1;
|
||||
return AUTH_STATUS.UNAUTHENTICATED;
|
||||
});
|
||||
assert.equal(await third, AUTH_STATUS.UNAUTHENTICATED);
|
||||
assert.equal(requestCount, 2);
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { compileFunction } from 'node:vm'
|
||||
import { computed, reactive, ref } from './vue.mjs'
|
||||
|
||||
const source = readFileSync(new URL('../../frontend/src/views/components/ConfigSettings/ConfigSettings.js', import.meta.url), 'utf8')
|
||||
const createPage = compileFunction(
|
||||
source.replace(/^import .*$/gm, '').replace('export function usePage', 'function usePage') + '\nreturn usePage()',
|
||||
['computed', 'onMounted', 'reactive', 'ref', 'ElMessage', 'ElMessageBox', 'http', 'useGlobalStatusStore']
|
||||
)
|
||||
|
||||
function fixture(get) {
|
||||
const messages = []
|
||||
const page = createPage(computed, () => {}, reactive, ref,
|
||||
{ error: message => messages.push(message) }, {}, { get }, () => ({ hasCapability: () => true }))
|
||||
return { page, messages }
|
||||
}
|
||||
|
||||
test('旧版配置仅请求状态,不并发请求分区、备份和审计', async () => {
|
||||
const requests = []
|
||||
const { page, messages } = fixture(async path => {
|
||||
requests.push(path)
|
||||
return { errorNo: 0, data: { available: false, unavailableReason: '在线配置管理尚未启用' } }
|
||||
})
|
||||
await page.loadAll()
|
||||
assert.deepEqual(requests, ['/api/v1/config/status'])
|
||||
assert.equal(page.availabilityError.value, '在线配置管理尚未启用')
|
||||
assert.deepEqual(messages, [])
|
||||
assert.equal(page.loading.value, false)
|
||||
})
|
||||
|
||||
test('备份和审计失败只显示局部错误,不阻挡配置编辑区域', async () => {
|
||||
const { page, messages } = fixture(async path => {
|
||||
if (path.endsWith('/status')) return { errorNo: 0, data: { available: true } }
|
||||
if (path.endsWith('/base')) return { errorNo: 0, data: {} }
|
||||
throw new Error('配置服务暂时不可用')
|
||||
})
|
||||
await page.loadAll()
|
||||
assert.equal(page.availabilityError.value, '')
|
||||
assert.equal(page.editorText.value, '{}')
|
||||
assert.equal(page.backupsError.value, '配置服务暂时不可用')
|
||||
assert.equal(page.auditError.value, '配置服务暂时不可用')
|
||||
assert.deepEqual(messages, [])
|
||||
await page.loadBackups()
|
||||
assert.deepEqual(messages, [])
|
||||
assert.equal(page.backupsError.value, '配置服务暂时不可用')
|
||||
})
|
||||
|
||||
test('附属模块失败清除旧数据和回滚选择,重试成功清除错误', async () => {
|
||||
let fail = true
|
||||
const { page } = fixture(async () => {
|
||||
if (fail) throw new Error('网络不可用')
|
||||
return { errorNo: 0, data: [{ generation: 2 }] }
|
||||
})
|
||||
page.backups.value = [{ generation: 1 }]
|
||||
page.auditItems.value = [{ id: 'old' }]
|
||||
page.rollbackGeneration.value = 1
|
||||
page.rollbackPreview.value = { targetGeneration: 1 }
|
||||
page.rollbackConfirmed.value = true
|
||||
await Promise.all([page.loadBackups(), page.loadAudit()])
|
||||
assert.deepEqual(page.backups.value, [])
|
||||
assert.deepEqual(page.auditItems.value, [])
|
||||
assert.equal(page.rollbackGeneration.value, 0)
|
||||
assert.equal(page.rollbackPreview.value, null)
|
||||
assert.equal(page.rollbackConfirmed.value, false)
|
||||
fail = false
|
||||
await Promise.all([page.loadBackups(), page.loadAudit()])
|
||||
assert.equal(page.backupsError.value, '')
|
||||
assert.equal(page.auditError.value, '')
|
||||
assert.equal(page.backups.value.length, 1)
|
||||
assert.equal(page.auditItems.value.length, 1)
|
||||
assert.equal(page.backupsLoading.value, false)
|
||||
assert.equal(page.auditLoading.value, false)
|
||||
})
|
||||
|
||||
test('状态请求失败后可重试恢复,不保留不可用提示', async () => {
|
||||
let available = false
|
||||
const { page } = fixture(async path => {
|
||||
if (!available) throw new Error('网络不可用')
|
||||
return { errorNo: 0, data: path.endsWith('/status') ? { available: true } : {} }
|
||||
})
|
||||
await page.loadAll()
|
||||
assert.equal(page.availabilityError.value, '网络不可用')
|
||||
available = true
|
||||
await page.loadAll()
|
||||
assert.equal(page.availabilityError.value, '')
|
||||
})
|
||||
@@ -0,0 +1,85 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
createLoginRedirectHandler,
|
||||
getCurrentLoginRedirect,
|
||||
getSafeLoginRedirect,
|
||||
createSetupLocation
|
||||
} from "../../frontend/src/utils/navigation.mjs";
|
||||
|
||||
test("登录重定向只接受非登录页的站内路径", () => {
|
||||
assert.equal(getSafeLoginRedirect("/list?tag=inbox"), "/list?tag=inbox");
|
||||
assert.equal(getSafeLoginRedirect("https://example.com"), "/");
|
||||
assert.equal(getSafeLoginRedirect("//example.com"), "/");
|
||||
assert.equal(getSafeLoginRedirect("/%2fexample.com"), "/");
|
||||
assert.equal(getSafeLoginRedirect("/login?redirect=/list"), "/");
|
||||
assert.equal(getSafeLoginRedirect("/%6Cogin-verification"), "/");
|
||||
assert.equal(getSafeLoginRedirect("/login/extra"), "/");
|
||||
});
|
||||
|
||||
test("已在登录页时保留原有安全重定向", () => {
|
||||
assert.equal(getCurrentLoginRedirect({
|
||||
name: "login",
|
||||
fullPath: "/login?redirect=/list",
|
||||
query: {redirect: "/list"}
|
||||
}), "/list");
|
||||
|
||||
assert.equal(getCurrentLoginRedirect({
|
||||
name: "login",
|
||||
fullPath: "/login?redirect=/login",
|
||||
query: {redirect: "/login"}
|
||||
}), "/");
|
||||
});
|
||||
|
||||
test("初始化跳转使用安全的站内重定向", () => {
|
||||
assert.deepEqual(createSetupLocation("/setup?redirect=/list"), {
|
||||
name: "setup",
|
||||
query: {redirect: "/"}
|
||||
});
|
||||
assert.deepEqual(createSetupLocation("/list?tag=inbox"), {
|
||||
name: "setup",
|
||||
query: {redirect: "/list?tag=inbox"}
|
||||
});
|
||||
});
|
||||
|
||||
test("并发 403 只执行一次登录跳转且不会嵌套 redirect", async () => {
|
||||
let replaceCount = 0;
|
||||
let finishNavigation;
|
||||
const router = {
|
||||
currentRoute: {
|
||||
value: {
|
||||
name: "list",
|
||||
fullPath: "/list?tag=inbox",
|
||||
query: {}
|
||||
}
|
||||
},
|
||||
replace(location) {
|
||||
replaceCount += 1;
|
||||
return new Promise((resolve) => {
|
||||
finishNavigation = () => {
|
||||
router.currentRoute.value = {
|
||||
name: "login",
|
||||
fullPath: "/login?redirect=/list?tag=inbox",
|
||||
query: location.query
|
||||
};
|
||||
resolve();
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const redirectToLogin = createLoginRedirectHandler(router);
|
||||
const first = redirectToLogin();
|
||||
const second = redirectToLogin();
|
||||
|
||||
assert.strictEqual(first, second);
|
||||
assert.equal(replaceCount, 1);
|
||||
finishNavigation();
|
||||
assert.equal(await first, true);
|
||||
assert.equal(router.currentRoute.value.query.redirect, "/list?tag=inbox");
|
||||
|
||||
assert.equal(await redirectToLogin(), false);
|
||||
assert.equal(replaceCount, 1);
|
||||
assert.equal(router.currentRoute.value.query.redirect, "/list?tag=inbox");
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import test from 'node:test'
|
||||
|
||||
const root = fileURLToPath(new URL('../../', import.meta.url))
|
||||
const read = relative => readFile(path.join(root, relative), 'utf8')
|
||||
|
||||
test('跨平台构建输出不得越过项目 out 目录', async () => {
|
||||
const content = await read('Makefile')
|
||||
const commands = [...content.matchAll(/cd ([^\s]+) && [^\n]*go build [^\n]* -o ([^\s]+) /g)]
|
||||
assert.equal(commands.length, 30)
|
||||
for (const [, directory, output] of commands) {
|
||||
const relative = path.relative(path.join(root, 'out'), path.resolve(root, directory, output))
|
||||
assert.ok(relative && !relative.startsWith('..') && !path.isAbsolute(relative), output)
|
||||
}
|
||||
})
|
||||
|
||||
test('前端构建必须自动同步 Go 嵌入资源', async () => {
|
||||
const frontend = JSON.parse(await read('frontend/package.json'))
|
||||
const telegram = JSON.parse(await read('plugins/pmail_telegram_push/package.json'))
|
||||
assert.equal(frontend.scripts.postbuild, 'node ../deploy/prepare-frontend.mjs')
|
||||
assert.equal(telegram.scripts['postbuild-only'], 'node ../../deploy/prepare-frontend.mjs telegram')
|
||||
assert.match(await read('frontend/vite.config.js'), /\.\.\/out\/frontend/)
|
||||
assert.match(await read('plugins/pmail_telegram_push/vite.config.ts'), /\.\.\/\.\.\/out\/plugins\/telegram-ui/)
|
||||
})
|
||||
|
||||
test('开发启动必须固定运行配置根目录并集中保存日志', async () => {
|
||||
const content = await read('deploy/start-dev.ps1')
|
||||
assert.match(content, /\$env:SMAIL_ROOT = Join-Path \$root 'backend'/)
|
||||
assert.match(content, /out\\backend\\smail-dev\.exe/)
|
||||
assert.match(content, /test-log\\runtime/)
|
||||
assert.match(content, /-WindowStyle Hidden/)
|
||||
assert.ok(content.charCodeAt(0) === 0xfeff, 'Windows PowerShell 5 脚本应有 UTF-8 BOM')
|
||||
})
|
||||
|
||||
test('安全工作流必须先构建嵌入资源再执行 Go 测试', async () => {
|
||||
for (const file of ['.github/workflows/release.yml', '.github/workflows/security.yml']) {
|
||||
const content = (await read(file)).split(' security:')[1]
|
||||
assert.ok(content)
|
||||
const prepare = content.indexOf('yarn build')
|
||||
const goTest = content.indexOf('node test/run.mjs backend')
|
||||
assert.ok(prepare >= 0 && goTest > prepare, file)
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,69 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { compileFunction } from 'node:vm'
|
||||
import { computed, reactive, ref } from './vue.mjs'
|
||||
|
||||
const source = readFileSync(new URL('../../frontend/src/views/components/SecuritySetupPrompt/SecuritySetupPrompt.js', import.meta.url), 'utf8')
|
||||
const createPage = compileFunction(
|
||||
source.replace(/^import .*$/gm, '').replace('export function usePage', 'function usePage') + '\nreturn usePage()',
|
||||
['computed', 'ref', 'http', 'useGlobalStatusStore']
|
||||
)
|
||||
|
||||
function fixture(post = async () => ({ errorNo: 0 })) {
|
||||
const store = reactive({
|
||||
isLogin: true,
|
||||
role: 'super_admin',
|
||||
userInfos: { account: 'root', showSecuritySetupPrompt: true, must_change_password: 0, webAuthnEnabled: false },
|
||||
settingsDrawerVisible: false,
|
||||
dismissRecoveryPrompt() { this.recoveryPromptDismissed = true }
|
||||
})
|
||||
return { store, page: createPage(computed, ref, { post }, () => store) }
|
||||
}
|
||||
|
||||
test('初始化超级管理员首次登录显示三项引导,强制改密时延后', () => {
|
||||
const { store, page } = fixture()
|
||||
assert.equal(page.visible.value, true)
|
||||
assert.deepEqual(Array.from(page.options.value, item => item.key), ['question', 'external-email', 'webauthn'])
|
||||
store.userInfos.must_change_password = 1
|
||||
assert.equal(page.visible.value, false)
|
||||
store.userInfos.must_change_password = 0
|
||||
assert.equal(page.visible.value, true)
|
||||
})
|
||||
|
||||
test('确认成功后关闭并打开目标设置,刷新后的持久化状态不再弹出', async () => {
|
||||
const requests = []
|
||||
const { store, page } = fixture(async path => { requests.push(path); return { errorNo: 0 } })
|
||||
await page.acknowledge('external-email')
|
||||
assert.equal(page.visible.value, false)
|
||||
assert.equal(store.settingsActiveTab, 'security')
|
||||
assert.equal(store.securityActiveSection, 'external-email')
|
||||
assert.equal(store.settingsDrawerVisible, true)
|
||||
assert.deepEqual(requests, ['/api/v1/security/setup/acknowledge'])
|
||||
store.userInfos = { account: 'root', showSecuritySetupPrompt: false }
|
||||
assert.equal(page.visible.value, false)
|
||||
})
|
||||
|
||||
test('保存失败保留引导,可重试且不将查看状态误记为设置完成', async () => {
|
||||
const { store, page } = fixture(async () => { throw new Error('请求失败') })
|
||||
await page.acknowledge()
|
||||
assert.equal(page.visible.value, true)
|
||||
assert.equal(page.error.value, '请求失败')
|
||||
assert.equal(page.saving.value, false)
|
||||
assert.equal(store.settingsDrawerVisible, false)
|
||||
assert.equal(store.userInfos.recoveryCodesConfigured, undefined)
|
||||
})
|
||||
|
||||
test('并发点击只提交一次,切换账号后不修改新账号状态', async () => {
|
||||
let finish
|
||||
let calls = 0
|
||||
const { store, page } = fixture(() => { calls++; return new Promise(resolve => { finish = resolve }) })
|
||||
const pending = page.acknowledge('question')
|
||||
await page.acknowledge('webauthn')
|
||||
assert.equal(calls, 1)
|
||||
store.userInfos = { account: 'alice', showSecuritySetupPrompt: true }
|
||||
finish({ errorNo: 0 })
|
||||
await pending
|
||||
assert.equal(page.visible.value, true)
|
||||
assert.equal(store.settingsDrawerVisible, false)
|
||||
})
|
||||
@@ -0,0 +1,5 @@
|
||||
import { createRequire } from 'node:module'
|
||||
|
||||
// 共用前端依赖,不在测试目录安装第二份 Vue。
|
||||
const require = createRequire(new URL('../../frontend/package.json', import.meta.url))
|
||||
export const { computed, reactive, ref } = require('vue')
|
||||
Reference in new issue
Block a user