test: 权限模型验收测试(管理员/非管理员入口可见性)

This commit is contained in:
2026-11-14 20:33:38 +08:00
parent 97f5b1aabd
commit 8e72bfe908
@@ -0,0 +1,83 @@
// 验收测试:账号权限体系
// 1. 非管理员:首页无创建活动入口
// 2. 管理员:首页有创建活动入口(快捷卡片 + 悬浮按钮)
// 3. 非管理员:从首页 navigateTo 创建页会被拦截弹回
// 4. 非管理员:直接调后端创建接口被 403 拒绝
const automator = require('miniprogram-automator');
describe('账号权限体系验收', () => {
let mp;
beforeAll(async () => {
mp = await automator.connect({ wsEndpoint: 'ws://localhost:9420' });
}, 30000);
afterAll(async () => {
if (mp) await mp.disconnect();
});
// 在应用上下文读取首页状态 + 创建入口元素数量
async function inspectHome() {
return mp.evaluate(() => new Promise(resolve => {
const pages = getCurrentPages();
const p = pages[pages.length - 1];
const base = {
path: p.route,
isAdmin: p.data.isAdmin,
auditMode: p.data.auditMode,
};
p.createSelectorQuery()
.selectAll('.create-btn-wrapper, .shortcut-create')
.boundingClientRect(rects => resolve({ ...base, entries: (rects || []).filter(r => r).length }))
.exec();
}));
}
test('非管理员:首页不显示创建活动入口', async () => {
await mp.callWxMethod('setStorageSync', 'isAdmin', false);
await mp.callWxMethod('setStorageSync', 'audit_mode', false);
await mp.reLaunch('/pages/home/index');
await new Promise(r => setTimeout(r, 3500));
const info = await inspectHome();
expect(info.path).toBe('pages/home/index');
expect(info.isAdmin).toBe(false);
expect(info.entries).toBe(0); // 悬浮按钮 + 快捷卡片都不渲染
}, 60000);
test('管理员:首页显示创建活动入口', async () => {
await mp.callWxMethod('setStorageSync', 'isAdmin', true);
await mp.reLaunch('/pages/home/index');
await new Promise(r => setTimeout(r, 3500));
const info = await inspectHome();
expect(info.isAdmin).toBe(true);
expect(info.entries).toBe(2); // 快捷卡片 + 悬浮按钮
}, 60000);
test('非管理员:进入创建页被拦截返回首页', async () => {
await mp.callWxMethod('setStorageSync', 'isAdmin', false);
await mp.reLaunch('/pages/home/index');
await new Promise(r => setTimeout(r, 2500));
await mp.callWxMethod('navigateTo', { url: '/pages/create-activity/index' });
await new Promise(r => setTimeout(r, 3500));
const stack = await mp.evaluate(() => getCurrentPages().map(p => p.route));
// onLoad 检测到非管理员 → toast + navigateBack,页面栈应回到 home
expect(stack).toContain('pages/home/index');
expect(stack[stack.length - 1]).not.toBe('pages/create-activity/index');
}, 60000);
test('非管理员调用创建接口被后端拒绝(403)', async () => {
const res = await mp.callWxMethod('request', {
url: 'http://localhost:3002/activities',
method: 'POST',
header: { 'x-user-id': 'user-2', 'Authorization': 'Bearer token_user-2_999' },
data: { title: 'x', location: 'x', start_time: '2026-11-20T10:00:00Z' },
});
expect(res.statusCode).toBe(403);
expect(res.data.success).toBe(false);
expect(res.data.message).toContain('管理员');
}, 60000);
});