| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /** AqXGNNVRNb 容器重命名权限回归(2026-12-01 个人版属主);内存 mock Parse,不访问远程。 */
- import { readFileSync } from 'node:fs';
- import vm from 'node:vm';
- import assert from 'node:assert/strict';
- import { test } from 'node:test';
- const SOURCE = readFileSync(new URL('../docs/03-interface/cloud-functions/fmodecontainer-rename.js', import.meta.url), 'utf8');
- /** 组装一份内存数据:用户/公司/角色/容器 */
- function world() {
- const obj = (id, fields) => ({ id, get: (k) => fields[k], set: (k, v) => { fields[k] = v; }, save: async () => {}, fields });
- const companies = {
- trial: obj('trial', { enterpriseTrialClaimedAt: new Date() }),
- crowded: obj('crowded', { enterpriseTrialClaimedAt: new Date() }), // 试用组织后来进了别人
- corp: obj('corp', {}),
- };
- const users = {
- solo: obj('solo', {}), // 无组织个人
- staff: obj('staff', { company: companies.corp }), // 企业普通成员
- admin: obj('admin', { company: companies.corp }),
- other: obj('other', {}),
- trialMember: obj('trialMember', { company: companies.trial }), // 非管理员的个人试用组织用户
- crowdA: obj('crowdA', { company: companies.crowded }),
- crowdB: obj('crowdB', { company: companies.crowded }),
- gone: obj('gone', { isDeleted: true }),
- goneAdmin: obj('goneAdmin', { company: companies.corp, isDeleted: true }),
- root: obj('root', {}),
- };
- const roles = { admin: ['corp_Admin'], root: ['masters'], goneAdmin: ['corp_Admin'] };
- const agents = [
- obj('a1', { agentId: 'solo-box', user: users.solo, name: '旧' }),
- obj('a2', { agentId: 'trial-box', user: users.trialMember, name: '旧' }),
- obj('a5', { agentId: 'crowd-box', user: users.crowdA, name: '旧' }),
- obj('a6', { agentId: 'admin-box', user: users.admin, name: '旧' }),
- obj('a7', { agentId: 'gone-box', user: users.gone, name: '旧' }),
- obj('a8', { agentId: 'deleted-box', user: users.solo, name: '旧', isDeleted: true }),
- obj('a3', { agentId: 'staff-box', user: users.staff, name: '旧' }),
- obj('a4', { agentId: 'orphan-box', name: '旧' }),
- ];
- const sessions = [obj('s1', { sessionToken: 'tok-solo', user: { id: 'solo', get: () => undefined } })];
- return { users, companies, roles, agents, sessions, broken: false };
- }
- function makeParse(w) {
- class Query {
- constructor(cls) { this.cls = cls; this.eq = {}; this.ne = {}; }
- equalTo(k, v) { this.eq[k] = v; return this; }
- notEqualTo(k, v) { this.ne[k] = v; return this; }
- limit() { return this; }
- include() { return this; }
- rows() {
- if (w.broken && this.cls === 'Company') throw new Error('db down');
- if (w.userDown && this.cls === 'User') throw new Error('db down');
- const all = this.cls === 'User' ? Object.values(w.users)
- : this.cls === 'Session' ? w.sessions
- : this.cls === 'Company' ? Object.values(w.companies)
- : this.cls === 'FmodeAgent' ? w.agents : [];
- return all.filter((r) => Object.entries(this.eq).every(([k, v]) => r.get(k) === v)
- && Object.entries(this.ne).every(([k, v]) => r.get(k) !== v));
- }
- async find() {
- if (this.cls === 'Role') {
- const u = this.eq.users;
- return (w.roles[u.id] ?? []).map((name) => ({ get: () => name }));
- }
- return this.rows();
- }
- async first() { return this.rows()[0]; }
- async count() { return this.rows().length; }
- async get(id) {
- const r = this.rows().find((x) => x.id === id);
- if (!r) throw new Error('not found');
- return r;
- }
- }
- return { Query, User: 'User', Role: 'Role', Session: 'Session' };
- }
- /**
- * 线上 request.user / _Session 的 user 只是带 id 的代理,取不到 company 等字段;
- * mock 同样只给 id,迫使被测代码自己按 id 重查。
- */
- async function call(callerId, agentId, displayName = '新名字', opts = {}) {
- const w = world();
- if (opts.broken) w.broken = true;
- if (opts.userDown) w.userDown = true;
- let out = null;
- const response = { status: () => ({ json: (body) => { out = body; } }) };
- const proxy = { id: callerId, get: () => undefined };
- const request = opts.sessionToken
- ? { headers: {}, body: {} }
- : { headers: { 'x-parse-session-token': 't' }, user: proxy, body: {} };
- const params = { op: 'rename', agentId, displayName, ...(opts.sessionToken ? { sessionToken: opts.sessionToken } : {}) };
- const ctx = vm.createContext({ Parse: makeParse(w), request, params });
- vm.runInContext(SOURCE, ctx);
- await ctx.handler(request, response);
- return { out, agent: w.agents.find((a) => a.get('agentId') === agentId) };
- }
- test('无组织个人可改自己的容器', async () => {
- const { out, agent } = await call('solo', 'solo-box');
- assert.equal(out.code, 200);
- assert.equal(agent.get('name'), '新名字');
- });
- test('个人试用组织(只有本人、非管理员)可改自己的容器', async () => {
- assert.equal((await call('trialMember', 'trial-box')).out.code, 200);
- });
- test('试用组织已有 2 人时按企业版处理,属主不能改', async () => {
- assert.equal((await call('crowdA', 'crowd-box')).out.code, 403);
- });
- test('查组织失败时不放权', async () => {
- assert.equal((await call('trialMember', 'trial-box', '新名字', { broken: true })).out.code, 403);
- });
- test('已软删用户不算个人', async () => {
- assert.equal((await call('gone', 'gone-box')).out.code, 403);
- });
- test('已软删用户即使挂着管理员角色也不能改', async () => {
- assert.equal((await call('goneAdmin', 'staff-box')).out.code, 403);
- });
- test('重查调用者失败时不放权', async () => {
- assert.equal((await call('solo', 'solo-box', '新名字', { userDown: true })).out.code, 403);
- });
- test('已软删容器不能改名', async () => {
- assert.equal((await call('solo', 'deleted-box')).out.code, 403);
- assert.equal((await call('root', 'deleted-box')).out.code, 404);
- });
- test('走 params.sessionToken 查 _Session 的路径同样生效', async () => {
- assert.equal((await call('ignored', 'solo-box', '新名字', { sessionToken: 'tok-solo' })).out.code, 200);
- });
- test('超管可改任意容器', async () => {
- assert.equal((await call('root', 'staff-box')).out.code, 200);
- assert.equal((await call('root', 'orphan-box')).out.code, 200);
- });
- test('个人不能改别人的容器,且不暴露容器是否存在', async () => {
- const r = await call('other', 'solo-box');
- assert.equal(r.out.code, 403);
- assert.equal(r.agent.get('name'), '旧');
- assert.equal((await call('other', 'no-such-box')).out.code, 403);
- });
- test('企业版普通成员不能改自己的容器', async () => {
- const r = await call('staff', 'staff-box');
- assert.equal(r.out.code, 403);
- assert.equal(r.agent.get('name'), '旧');
- });
- test('公司管理员可改本公司容器,不能改外公司或无主容器', async () => {
- assert.equal((await call('admin', 'staff-box')).out.code, 200);
- assert.equal((await call('admin', 'solo-box')).out.code, 403);
- assert.equal((await call('admin', 'orphan-box')).out.code, 403);
- assert.equal((await call('admin', 'admin-box')).out.code, 200);
- assert.equal((await call('admin', 'no-such-box')).out.code, 403, '管理员也不能借 404 枚举容器');
- });
- test('名称校验不变', async () => {
- assert.equal((await call('solo', 'solo-box', ' ')).out.code, 400);
- assert.equal((await call('solo', 'solo-box', 'x'.repeat(25))).out.code, 400);
- });
|