整理前后端与测试构建目录并完善配置和安全引导
Security Checks / security (push) Waiting to run

统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
xuans committed 2026-09-05 19:19:52 +08:00
1 parent 28cd9d3406
commit f60d3b0880
603 files changed
+2351 -660

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
# 测试目录
## 目录用途
- `frontend/`:前端自动测试及其构建路径约束测试,直接复用 `frontend/node_modules`。
- `tools/password-check/`:离线密码哈希校验工具,只通过环境变量接收输入,不输出密码和哈希。
- `manual/smtp/`:人工 SMTP 发信脚本,可能向外部发送真实邮件,不包含在自动测试中。运行前需自行检查收发件人和服务器配置。
- `plugins/spam_block/`:垃圾邮件模型测试,需要额外安装 TensorFlow 并准备相应模型;不包含在默认测试中。
- `archive/`:历史测试脚本和补丁,仅留作检查证据,路径和运行环境可能已失效,不应直接执行,也不提交其中的本机配置。
- `run.mjs`:统一自动测试入口。
## 执行方法
在项目根目录运行:
```sh
node test/run.mjs all
node test/run.mjs frontend
node test/run.mjs backend
node test/run.mjs backend -count=1 ./...
node test/run.mjs backend -race ./...
```
首次执行 Go 测试前,需要在 `frontend/` 安装依赖并运行 `npm run build`,为 `go:embed` 提供前端文件。默认命令不会启动开发服务、清空真实数据库或发送真实邮件。
每次运行自动创建 `test-log/run-时间-进程号/`,保存前后端日志及临时文件。子进程的 `SMAIL_ROOT` 和临时目录指向该次测试目录,不继承开发后端的真实配置根目录。MySQL/PostgreSQL 集成测试仍需单独准备专用数据库,不能指向业务数据库。
Windows 密码检查工具的构建命令:
```powershell
Set-Location test/tools/password-check
go build -o ../../../out/test-tools/password-check.exe .
```
## Go 测试源码的必要例外
`backend/` 中的同包 `*_test.go` 保持原位,其中许多测试使用包内私有函数或相对路径。将其机械移动到独立目录会破坏 Go 测试语义,因此只集中运行入口,不改变业务包的可见性和 API。后续跨模块、浏览器和独立协议测试应放到本目录。
截图、测试生成的 Logo 检查图、浏览器配置、测试数据库、报告和日志都放到 `test-log/`;测试工具二进制放到 `out/test-tools/`。运行用 Logo、文档演示图、模型训练脚本及数据不是临时测试产物,不作清理。
+87
View File
@@ -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);
});
+88
View File
@@ -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, '')
})
+85
View File
@@ -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");
});
+46
View File
@@ -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)
}
})
+69
View 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)
})
+5
View File
@@ -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')
+15
View File
@@ -0,0 +1,15 @@
from email.mime.text import MIMEText
import smtplib
msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
from_addr = "admin@domain.com"
password = "admin"
to_addr = "admin@domain.com"
smtp_server = "127.0.0.1"
server = smtplib.SMTP(smtp_server, 25)
server.starttls()
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
+35
View File
@@ -0,0 +1,35 @@
import os
from pathlib import Path
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' # silence TF INFO messages
import tensorflow as tf
save_path = str(Path(__file__).resolve().parents[3] / 'backend/hooks/spam_block/emotion_model/1')
model = tf.keras.models.load_model(save_path, compile=False)
model.summary()
CLASSES = {
0:'普通邮件',
1:'广告邮件',
2:'诈骗邮件'
}
def predict_emotions(txt):
# recall it is multi-class so we need to get all prediction above a threshold (0.5)
input = tf.constant( np.array([txt]) , dtype=tf.string )
preds = model(input)[0]
maxClass = -1
maxScore = 0
for idx in range(3):
if preds[idx] > maxScore:
maxScore = preds[idx]
maxClass = idx
return maxClass
maxClass = predict_emotions("各位同事请注意 这里是110,请大家立刻把银行卡账号密码回复发给我!")
print("这个邮件属于:",CLASSES[maxClass])
+35
View File
@@ -0,0 +1,35 @@
import os
from pathlib import Path
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' # silence TF INFO messages
import tensorflow as tf
save_path = str(Path(__file__).resolve().parents[3] / 'plugins/spam_block/emotion_model/1')
model = tf.keras.models.load_model(save_path, compile=False)
model.summary()
CLASSES = {
0:'普通邮件',
1:'广告邮件',
2:'诈骗邮件'
}
def predict_emotions(txt):
# recall it is multi-class so we need to get all prediction above a threshold (0.5)
input = tf.constant( np.array([txt]) , dtype=tf.string )
preds = model(input)[0]
maxClass = -1
maxScore = 0
for idx in range(3):
if preds[idx] > maxScore:
maxScore = preds[idx]
maxClass = idx
return maxClass
maxClass = predict_emotions("各位同事请注意 这里是110,请大家立刻把银行卡账号密码回复发给我!")
print("这个邮件属于:",CLASSES[maxClass])
+46
View File
@@ -0,0 +1,46 @@
import { spawn } from 'node:child_process'
import { createWriteStream } from 'node:fs'
import { mkdir, readdir } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
const root = fileURLToPath(new URL('../', import.meta.url))
const [suite = 'all', ...extra] = process.argv.slice(2)
if (!['all', 'frontend', 'backend'].includes(suite)) {
console.error('用法:node test/run.mjs [all|frontend|backend] [Go 测试参数]')
process.exit(2)
}
const stamp = new Date().toISOString().replace(/[:.]/g, '-')
const directory = path.join(root, 'test-log', `run-${stamp}-${process.pid}`)
const temporary = path.join(directory, 'tmp')
await mkdir(temporary, { recursive: true })
async function run(name, command, args, cwd) {
const log = createWriteStream(path.join(directory, `${name}.log`))
// 测试使用独立临时根目录,不能继承开发服务的真实配置根目录。
const env = { ...process.env, SMAIL_ROOT: temporary, GOTMPDIR: temporary, TMPDIR: temporary, TEMP: temporary, TMP: temporary }
const child = spawn(command, args, { cwd, env, windowsHide: true, stdio: ['inherit', 'pipe', 'pipe'] })
child.stdout.on('data', data => { process.stdout.write(data); log.write(data) })
child.stderr.on('data', data => { process.stderr.write(data); log.write(data) })
const code = await new Promise(resolve => {
child.once('error', error => { console.error(`测试启动失败:${error.message}`); resolve(1) })
child.once('close', value => resolve(value ?? 1))
})
await new Promise(resolve => log.end(resolve))
return code
}
let result = 0
if (suite !== 'backend') {
const tests = (await readdir(path.join(root, 'test', 'frontend')))
.filter(name => name.endsWith('.test.mjs'))
.map(name => path.join(root, 'test', 'frontend', name))
if (!tests.length) throw new Error('没有找到前端测试,拒绝空测试通过')
result = await run('frontend', process.execPath, ['--test', ...tests], path.join(root, 'frontend'))
}
if (suite !== 'frontend' && result === 0) {
const args = extra[0] === '--' ? extra.slice(1) : extra
result = await run('backend', 'go', ['test', ...(args.length ? args : ['./...'])], path.join(root, 'backend'))
}
console.log(`测试记录:${path.relative(root, directory)}`)
process.exitCode = result
+12
View File
@@ -0,0 +1,12 @@
module smail-test-password-check
go 1.26.0
require git.pcux.cn/xuans/Smail v0.0.0
require (
golang.org/x/crypto v0.55.0 // indirect
golang.org/x/sys v0.47.0 // indirect
)
replace git.pcux.cn/xuans/Smail => ../../../backend
+4
View File
@@ -0,0 +1,4 @@
golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M=
golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
+12
View File
@@ -0,0 +1,12 @@
package main
import (
"fmt"
"os"
"git.pcux.cn/xuans/Smail/utils/password"
)
func main() {
fmt.Print(password.Verify(os.Getenv("SMAIL_HASH"), os.Getenv("SMAIL_TEST_PASSWORD")))
}