51ec4598df
- digital.profile.ts: 未保存偏好时默认开启,原子合并 JSON 保留其他资料 - digital.controller.ts、digital.users.ts: 接入本人和管理员偏好读写、事务及审计 - digital.chat.ts、digital.prompt.ts、digital.suggestions.ts: 各生成入口读取服务端账号偏好,保留游客鉴权 - tests/digital-*.test.ts: 覆盖默认值、关闭保留、旧客户端兼容与越权拦截 - README.md: 同步模块职责和发布要求
209 lines
6.9 KiB
TypeScript
209 lines
6.9 KiB
TypeScript
/** NSFW 账号偏好的默认值、真实路由保存、账号隔离与游客鉴权回归。 */
|
||
import "./digital-env";
|
||
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import { createApp } from "../src/app";
|
||
import { pool } from "../src/db/pool";
|
||
import { authService } from "../src/modules/auth/auth.service";
|
||
import { parseDigitalProfile } from "../src/modules/digital/digital.profile";
|
||
import {
|
||
digitalUsers,
|
||
digitalUserInput,
|
||
digitalUserUpdate,
|
||
updateDigitalUser,
|
||
} from "../src/modules/digital/digital.users";
|
||
|
||
test("新账号和旧版资料默认开启,已保存的关闭不被默认值覆盖", () => {
|
||
for (const body of [
|
||
undefined,
|
||
null,
|
||
{},
|
||
{ favorites: ["card"], userName: "旧用户" },
|
||
])
|
||
assert.equal(parseDigitalProfile(body).nsfwEnabled, true);
|
||
assert.equal(parseDigitalProfile({ nsfwEnabled: false }).nsfwEnabled, false);
|
||
assert.equal(parseDigitalProfile('{"nsfwEnabled":false}').nsfwEnabled, false);
|
||
assert.equal(
|
||
digitalUserInput.parse({
|
||
username: "new-user",
|
||
displayName: "新用户",
|
||
password: "test-password",
|
||
}).nsfwEnabled,
|
||
true,
|
||
);
|
||
assert.equal(
|
||
digitalUserUpdate.parse({ displayName: "用户", status: "ACTIVE" })
|
||
.nsfwEnabled,
|
||
undefined,
|
||
);
|
||
});
|
||
|
||
test("前台保存持久化账号偏好;旧客户端改收藏保留关闭,伪造账号不能越权", async (t) => {
|
||
const profiles = new Map<string, Record<string, unknown>>();
|
||
t.mock.method(authService, "getCurrentUser", async () => ({
|
||
accountType: "DIGITAL_USER",
|
||
}));
|
||
t.mock.method(
|
||
pool,
|
||
"query",
|
||
async (query: { sql: string }, args: unknown[]) => {
|
||
if (query.sql.startsWith("SELECT body FROM dh_profiles"))
|
||
return [
|
||
profiles.has(String(args[0]))
|
||
? [{ body: profiles.get(String(args[0])) }]
|
||
: [],
|
||
[],
|
||
];
|
||
if (query.sql.startsWith("INSERT INTO dh_profiles")) {
|
||
assert.match(query.sql, /JSON_MERGE_PATCH/);
|
||
const account = String(args[0]);
|
||
profiles.set(account, {
|
||
...profiles.get(account),
|
||
...JSON.parse(String(args[1])),
|
||
});
|
||
return [{ affectedRows: 1 }, []];
|
||
}
|
||
if (query.sql.startsWith("SELECT")) return [[], []];
|
||
throw new Error("未覆盖 SQL:" + query.sql);
|
||
},
|
||
);
|
||
const app = createApp();
|
||
try {
|
||
await app.ready();
|
||
const token = app.jwt.sign({
|
||
sub: "user:7",
|
||
accountType: "DIGITAL_USER",
|
||
scene: "DIGITAL_APP",
|
||
});
|
||
const headers = { authorization: `Bearer ${token}` };
|
||
const initial = await app.inject({
|
||
method: "GET",
|
||
url: "/api/digital/bootstrap",
|
||
headers,
|
||
});
|
||
assert.equal(initial.statusCode, 200);
|
||
assert.equal(initial.json().data.profile.nsfwEnabled, true);
|
||
const base = { favorites: [], userName: "旅人", persona: "测试身份" };
|
||
const disabled = await app.inject({
|
||
method: "PUT",
|
||
url: "/api/digital/profile",
|
||
headers,
|
||
payload: { ...base, nsfwEnabled: false, account: "user:999" },
|
||
});
|
||
assert.equal(disabled.statusCode, 200);
|
||
assert.equal(disabled.json().data.nsfwEnabled, false);
|
||
assert.equal(profiles.has("user:999"), false);
|
||
const favorite = await app.inject({
|
||
method: "PUT",
|
||
url: "/api/digital/profile",
|
||
headers,
|
||
payload: { ...base, favorites: ["card"] },
|
||
});
|
||
assert.equal(favorite.json().data.nsfwEnabled, false);
|
||
assert.deepEqual(favorite.json().data.favorites, ["card"]);
|
||
const bootstrap = await app.inject({
|
||
method: "GET",
|
||
url: "/api/digital/bootstrap",
|
||
headers,
|
||
});
|
||
assert.equal(bootstrap.json().data.profile.nsfwEnabled, false);
|
||
const invalid = await app.inject({
|
||
method: "PUT",
|
||
url: "/api/digital/profile",
|
||
headers,
|
||
payload: { ...base, nsfwEnabled: "false" },
|
||
});
|
||
assert.equal(invalid.statusCode, 400);
|
||
const enabled = await app.inject({
|
||
method: "PUT",
|
||
url: "/api/digital/profile",
|
||
headers,
|
||
payload: { ...base, nsfwEnabled: true },
|
||
});
|
||
assert.equal(enabled.json().data.nsfwEnabled, true);
|
||
} finally {
|
||
await app.close();
|
||
}
|
||
});
|
||
|
||
test("后台用户列表返回同一账号偏好,不附带用户身份正文", async (t) => {
|
||
t.mock.method(pool, "query", async (query: { sql: string }) => {
|
||
if (query.sql.includes("COUNT(*) total")) return [[{ total: 2 }], []];
|
||
assert.match(
|
||
query.sql,
|
||
/LEFT JOIN dh_profiles p ON p.account=CONCAT\('user:',u.id\)/,
|
||
);
|
||
return [
|
||
[
|
||
{ id: 1, profileBody: { nsfwEnabled: false, persona: "私密身份" } },
|
||
{ id: 2, profileBody: null },
|
||
],
|
||
[],
|
||
];
|
||
});
|
||
const result = await digitalUsers({ q: "", page: 1, size: 20 });
|
||
assert.equal(result.items[0].nsfwEnabled, false);
|
||
assert.equal(result.items[1].nsfwEnabled, true);
|
||
assert.equal(JSON.stringify(result).includes("私密身份"), false);
|
||
});
|
||
|
||
test("游客不能读取或修改内容偏好,也不能通过请求体开启生成", async () => {
|
||
const app = createApp();
|
||
try {
|
||
for (const request of [
|
||
{ method: "GET" as const, url: "/api/digital/bootstrap" },
|
||
{
|
||
method: "PUT" as const,
|
||
url: "/api/digital/profile",
|
||
payload: { nsfwEnabled: true },
|
||
},
|
||
{
|
||
method: "POST" as const,
|
||
url: "/api/digital/conversations/chat/stream",
|
||
payload: { nsfwEnabled: true },
|
||
},
|
||
]) {
|
||
const response = await app.inject(request);
|
||
assert.equal(response.statusCode, 401, request.url);
|
||
}
|
||
} finally {
|
||
await app.close();
|
||
}
|
||
});
|
||
|
||
test("管理员修改与本人资料使用同一账号偏好,基础资料编辑不重置开关", async (t) => {
|
||
let profile = { favorites: ["card"], userName: "旅人", nsfwEnabled: true };
|
||
const audits: Record<string, unknown>[] = [];
|
||
t.mock.method(pool, "getConnection", async () => ({
|
||
beginTransaction: async () => {},
|
||
commit: async () => {},
|
||
rollback: async () => {},
|
||
release: () => {},
|
||
query: async (query: { sql: string }, args: unknown[]) => {
|
||
if (query.sql.startsWith("SELECT id FROM dh_users"))
|
||
return [[{ id: 7 }], []];
|
||
if (query.sql.startsWith("INSERT INTO dh_profiles")) {
|
||
assert.equal(args[0], "user:7");
|
||
assert.match(query.sql, /JSON_MERGE_PATCH/);
|
||
profile = { ...profile, ...JSON.parse(String(args[1])) };
|
||
}
|
||
if (query.sql.startsWith("INSERT INTO dh_audits"))
|
||
audits.push(JSON.parse(String(args[3])));
|
||
return [{ affectedRows: 1 }, []];
|
||
},
|
||
}));
|
||
await updateDigitalUser("super_admin:1", 7, {
|
||
displayName: "用户甲",
|
||
status: "ACTIVE",
|
||
nsfwEnabled: false,
|
||
});
|
||
assert.equal(profile.nsfwEnabled, false);
|
||
assert.deepEqual(profile.favorites, ["card"]);
|
||
assert.equal(audits[0].nsfwEnabled, false);
|
||
await updateDigitalUser("super_admin:1", 7, {
|
||
displayName: "用户乙",
|
||
status: "ACTIVE",
|
||
});
|
||
assert.equal(profile.nsfwEnabled, false);
|
||
});
|