feat(jd): 京东工具台纳入后台菜单权限——V209 app菜单组jd_tools+两子行, 预授权沿V163全授组行; 用户端工具台卡片/首页入口按权限显隐(接口失败兜底全显, 同亚马逊形状); 首页computePermissionState抽薄home-permission.ts并修只授京东误亮亚马逊的兜底缺陷
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import { computePermissionState } from '../src/pages/home/home-permission.ts'
|
||||
|
||||
/**
|
||||
* 桌面首页一级入口权限计算(V209 把京东纳入 app 菜单权限)。
|
||||
* 判据形状:组行直授 / 子行直授带 root 元数据 / 无 root 元数据旧接口兜底,三条路都过。
|
||||
*/
|
||||
|
||||
type Item = Record<string, unknown>
|
||||
|
||||
function item(columnKey: string, rootColumnKey?: string, routePath?: string): Item {
|
||||
const it: Item = { id: columnKey, column_key: columnKey }
|
||||
if (rootColumnKey) it.root_column_key = rootColumnKey
|
||||
if (routePath) it.route_path = routePath
|
||||
return it
|
||||
}
|
||||
|
||||
test('组行 jd_tools 直授 ⇒ 京东入口可见', () => {
|
||||
const state = computePermissionState([item('jd_tools', 'jd_tools', 'jd-tools')])
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
|
||||
test('只授子行(带 root_column_key=jd_tools 祖先元数据)⇒ 京东入口仍可见', () => {
|
||||
// V209 预授权授的是组行,但管理员收组后单授采集卡是合法配置:入口不能凭空消失。
|
||||
const state = computePermissionState([item('jd-collect', 'jd_tools', 'jd-collect')])
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
|
||||
test('权限集与京东无关 ⇒ 京东入口隐藏', () => {
|
||||
const state = computePermissionState([item('wb', 'wb', 'image-video'), item('collect-data', 'brand_front_tools')])
|
||||
assert.equal(state.jd, false)
|
||||
assert.equal(state.video, true)
|
||||
})
|
||||
|
||||
test('只授京东 ⇒ 亚马逊入口不得假显示(有 root 元数据路)', () => {
|
||||
const state = computePermissionState([item('jd_tools', 'jd_tools', 'jd-tools')])
|
||||
assert.equal(state.jd, true)
|
||||
assert.equal(state.amazon, false)
|
||||
})
|
||||
|
||||
test('只授京东 ⇒ 亚马逊入口不得假显示(旧接口无 root 元数据兜底路,V209 顺手钉的判据)', () => {
|
||||
// 原实现兜底只排除 'wb':V209 后 jd 键混进兜底集合会被误判 amazon=true(点进去空卡片墙)。
|
||||
const state = computePermissionState([item('jd_tools'), item('jd-collect')])
|
||||
assert.equal(state.amazon, false, '无 root 元数据时,仅授京东键不得点亮亚马逊入口')
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
|
||||
test('旧接口兜底不误伤既有语义:亚马逊子键仍点亮亚马逊入口', () => {
|
||||
const state = computePermissionState([item('collect-data')])
|
||||
assert.equal(state.amazon, true)
|
||||
})
|
||||
|
||||
test('大小写/空白归一:JD_TOOLS 照命中', () => {
|
||||
const state = computePermissionState([{ id: 1, column_key: ' JD_TOOLS ' }])
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
@@ -3,7 +3,12 @@ import test from 'node:test'
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { JD_TOOLS, JD_TOOL_MAP } from '../src/pages/jd/tool-catalog.ts'
|
||||
import {
|
||||
filterJdToolsByPermission,
|
||||
JD_GROUP_COLUMN_KEY,
|
||||
JD_TOOLS,
|
||||
JD_TOOL_MAP,
|
||||
} from '../src/pages/jd/tool-catalog.ts'
|
||||
|
||||
/** 京东一级入口(v1.5):首页「京东」→ 京东工作台两个菜单(获取店铺数据 / 商品上架占位)。 */
|
||||
|
||||
@@ -54,3 +59,40 @@ test('获取店铺数据页是一键触发 + 服务端进度(任务面板改
|
||||
assert.ok(page.includes('jd-shop-collect-run'), '建单后必须推送本机客户端队列')
|
||||
assert.ok(page.includes('TaskCenterPanel'), '改版后右侧必须是共享任务面板(含历史任务层)')
|
||||
})
|
||||
|
||||
/* ===== V209 菜单权限:permissionKeys 与后端 app 菜单逐字对撞 + 过滤判据 ===== */
|
||||
|
||||
test('两张卡片都声明 permissionKeys 且与 V209 迁移 column_key 逐字一致', () => {
|
||||
// 跨仓对撞(后管 company-menu-route.test.ts 同族判据):迁移插的 app 菜单键与前端
|
||||
// permissionKeys 任何一侧改名而另一侧不动 ⇒ 这里红,而不是线上静默"勾了不显示"。
|
||||
const sqlPath = join(ROOT, '..', 'backend-java', 'src', 'main', 'resources', 'db', 'V209__jd_app_menu.sql')
|
||||
assert.ok(existsSync(sqlPath), `迁移缺失:${sqlPath}(V209 与前端权限键必须同窗改动)`)
|
||||
const sql = readFileSync(sqlPath, 'utf8')
|
||||
const frontendKeys = new Set<string>([JD_GROUP_COLUMN_KEY, ...JD_TOOLS.flatMap((tool) => tool.permissionKeys)])
|
||||
for (const key of frontendKeys) {
|
||||
assert.ok(sql.includes(`'${key}'`), `V209 迁移缺 column_key:${key}`)
|
||||
}
|
||||
for (const route of ['jd-tools', 'jd-collect', 'jd-publish']) {
|
||||
assert.ok(sql.includes(`'${route}'`), `V209 迁移缺 route_path:${route}`)
|
||||
}
|
||||
for (const tool of JD_TOOLS) {
|
||||
assert.ok(tool.permissionKeys.length > 0, `卡片 ${tool.id} 必须声明 permissionKeys`)
|
||||
}
|
||||
})
|
||||
|
||||
test('按菜单权限过滤京东卡片(与亚马逊 filterGroupsByPermission 同形判据)', () => {
|
||||
// 空权限集 ⇒ 全隐藏
|
||||
assert.deepEqual(filterJdToolsByPermission(JD_TOOLS, []), [])
|
||||
// 组键命中 ⇒ 整组放行(V209 预授权授的就是组行)
|
||||
assert.equal(filterJdToolsByPermission(JD_TOOLS, ['jd_tools']).length, 2)
|
||||
// 只授采集 ⇒ 只剩采集卡
|
||||
const collectOnly = filterJdToolsByPermission(JD_TOOLS, ['jd-collect'])
|
||||
assert.deepEqual(collectOnly.map((tool) => tool.id), ['jd-collect'])
|
||||
// 只授上架 ⇒ 只剩上架卡(占位卡转正前同样受控)
|
||||
const publishOnly = filterJdToolsByPermission(JD_TOOLS, ['jd-publish'])
|
||||
assert.deepEqual(publishOnly.map((tool) => tool.id), ['jd-publish'])
|
||||
// 大小写/空白归一(亚马逊侧同款 normalize:trim + lowercase)
|
||||
assert.equal(filterJdToolsByPermission(JD_TOOLS, [' JD_TOOLS ']).length, 2)
|
||||
// 无关键不放行任何卡片
|
||||
assert.deepEqual(filterJdToolsByPermission(JD_TOOLS, ['collect-data', 'wb']), [])
|
||||
})
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* 京东工作台卡片按 app 菜单权限(V209)显隐的挂载用例。
|
||||
*
|
||||
* 四态判据,与 AmazonConsolePage 同形:
|
||||
* ① 组键 jd_tools 命中 ⇒ 两张卡都在(V209 预授权授的就是组行,线上默认形态);
|
||||
* ② 只授 jd-collect ⇒ 只剩采集卡(上架占位同样受控,转正后行为不变);
|
||||
* ③ 权限集与京东无关 ⇒ 两张卡都隐藏;
|
||||
* ④ 权限接口异常 ⇒ 展示全部(现用人不因接口抖动丢功能)。
|
||||
*
|
||||
* 跑法:cd frontend-vue && npx vitest run tests/mount/jd-console-permission.mount.test.ts
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mount, type VueWrapper } from '@vue/test-utils'
|
||||
|
||||
vi.mock('@/shared/api/permission', () => ({
|
||||
getCurrentUserAppColumnKeys: vi.fn(),
|
||||
}))
|
||||
|
||||
import { getCurrentUserAppColumnKeys } from '@/shared/api/permission'
|
||||
import JdConsolePage from '@/pages/jd/JdConsolePage.vue'
|
||||
|
||||
let wrapper: VueWrapper | null = null
|
||||
|
||||
const mockKeys = (keys: string[]) =>
|
||||
vi.mocked(getCurrentUserAppColumnKeys).mockResolvedValue(keys)
|
||||
|
||||
async function mountConsole() {
|
||||
wrapper = mount(JdConsolePage, {
|
||||
global: {
|
||||
stubs: {
|
||||
NotificationBell: true,
|
||||
RouterLink: { template: '<a class="router-link-stub"><slot /></a>' },
|
||||
'router-link': { template: '<a class="router-link-stub"><slot /></a>' },
|
||||
},
|
||||
},
|
||||
})
|
||||
// onMounted 里权限是异步 await,等过滤落地后再断言
|
||||
await vi.waitFor(() => {
|
||||
expect(vi.mocked(getCurrentUserAppColumnKeys).mock.calls.length).toBeGreaterThan(0)
|
||||
})
|
||||
return wrapper
|
||||
}
|
||||
|
||||
describe('京东工作台按菜单权限显隐(V209)', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.setItem('username', 'admin')
|
||||
vi.mocked(getCurrentUserAppColumnKeys).mockReset()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
wrapper?.unmount()
|
||||
wrapper = null
|
||||
window.localStorage.clear()
|
||||
})
|
||||
|
||||
it('组键命中 ⇒ 两张卡都在', async () => {
|
||||
mockKeys(['jd_tools', 'jd-tools'])
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(2))
|
||||
})
|
||||
|
||||
it('只授 jd-collect ⇒ 只剩获取店铺数据卡', async () => {
|
||||
mockKeys(['jd-collect'])
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(1))
|
||||
// 断言圈在卡片节点上:hero 文案("店铺采集 · 商品上架")与计数是页面静态区,
|
||||
// 不参与权限过滤,拿整页 text() 判"商品上架消失"会误红。
|
||||
const cards = w.findAll('.tool-card')
|
||||
expect(cards[0].text()).toContain('获取店铺数据')
|
||||
expect(cards.every((card) => !card.classes().includes('tool-card--pending'))).toBe(true)
|
||||
expect(w.find('.group-count').text()).toContain('1 个工具')
|
||||
})
|
||||
|
||||
it('权限集与京东无关 ⇒ 两张卡都隐藏', async () => {
|
||||
mockKeys(['wb', 'collect-data'])
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(0))
|
||||
expect(w.findAll('.tool-card').length).toBe(0)
|
||||
})
|
||||
|
||||
it('权限接口异常 ⇒ 展示全部(兜底不让现用人丢入口)', async () => {
|
||||
vi.mocked(getCurrentUserAppColumnKeys).mockRejectedValue(new Error('获取菜单权限失败'))
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(2))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user