Files
client-web/src/modules/overseas-warehouse/permissions.test.mjs
T

116 lines
7.0 KiB
JavaScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import ts from 'typescript'
const routes = readFileSync(new URL('./routes.ts', import.meta.url), 'utf8')
const services = readFileSync(new URL('./views/WarehouseServiceView.vue', import.meta.url), 'utf8')
const returns = readFileSync(new URL('./views/ReturnOrdersView.vue', import.meta.url), 'utf8')
const contracts = readFileSync(new URL('../tenant-management/contracts.ts', import.meta.url), 'utf8')
test('protects every overseas warehouse page with its independent list permission', () => {
for (const permission of [
'merchant:overseas-warehouse:relabel:list',
'merchant:overseas-warehouse:transfer:list',
'merchant:overseas-warehouse:discard:list',
'merchant:overseas-warehouse:return-order:list',
'merchant:overseas-warehouse:return-inventory:list',
]) assert.match(routes, new RegExp(`permission: '${permission}'`))
assert.doesNotMatch(routes, /permission: 'dashboard:view'/)
})
test('opens labeling and return relabeling on dedicated protected create pages', () => {
assert.match(routes, /path: 'overseas-warehouse\/labeling\/create'[\s\S]*?props: \{ service: 'labeling', createPage: true \}[\s\S]*?permission: 'merchant:overseas-warehouse:relabel:create'/)
assert.match(routes, /path: 'overseas-warehouse\/return-relabel\/create'[\s\S]*?ReturnOrdersView\.vue'[\s\S]*?props: \{ createPage: true \}[\s\S]*?title: '新建退货预报'[\s\S]*?permission: 'merchant:overseas-warehouse:return-order:create'/)
assert.match(services, /props\.createPage \? 'create' : 'list'/)
assert.match(services, /function returnToList\(\)/)
assert.match(returns, /props\.createPage/)
assert.match(returns, /class="return-create-surface"/)
})
test('opens warehouse service and return receipt details as dedicated protected workspace pages', () => {
assert.match(routes, /path: 'overseas-warehouse\/labeling\/:detailNo\/detail'[\s\S]*?name: 'overseas-labeling-detail'[\s\S]*?props: \{ service: 'labeling', detailPage: true \}[\s\S]*?permission: 'merchant:overseas-warehouse:relabel:list'/)
assert.match(routes, /path: 'overseas-warehouse\/return-relabel\/:returnNo\/detail'[\s\S]*?name: 'overseas-return-relabel-detail'[\s\S]*?props: \{ detailPage: true \}[\s\S]*?permission: 'merchant:overseas-warehouse:return-order:list'/)
assert.match(routes, /path: 'overseas-warehouse\/storage-transfer\/:detailNo\/detail'[\s\S]*?name: 'overseas-storage-transfer-detail'[\s\S]*?props: \{ service: 'transfer', detailPage: true \}/)
assert.match(routes, /path: 'overseas-warehouse\/cargo-disposal\/:detailNo\/detail'[\s\S]*?name: 'overseas-cargo-disposal-detail'[\s\S]*?props: \{ service: 'disposal', detailPage: true \}/)
assert.match(services, /labeling: 'overseas-labeling-detail'/)
assert.match(services, /transfer: 'overseas-storage-transfer-detail'/)
assert.match(services, /disposal: 'overseas-cargo-disposal-detail'/)
assert.match(services, /class="warehouse-detail-error"/)
assert.match(returns, /name: 'overseas-return-relabel-detail'/)
assert.doesNotMatch(returns, /title="退货签收详情"/)
})
test('returns to the labeling list after a labeling application is submitted', () => {
assert.match(services, /ElMessage\.success\(`\$\{config\.value\.title\}提交成功`\)[\s\S]*?await goToServiceList\(\)/)
// Return-carton sources use the unified relabel application and its file endpoint.
assert.match(services, /returnCartonId: item\.returnCartonId/)
assert.match(services, /uploadRelabelFile\(created\.application\.relabelNo,[\s\S]*?await goToServiceList\(\)/)
assert.match(services, /申请已创建,附件待补传/)
})
test('service list return navigates standalone pages and refreshes embedded lists', async () => {
const source = services.slice(services.indexOf('function serviceListRouteName('), services.indexOf('async function openEdit('))
const javascript = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText
for (const props of [
{ service: 'labeling', createPage: true, detailPage: false },
{ service: 'labeling', createPage: false, detailPage: true },
{ service: 'labeling', createPage: false, detailPage: false },
]) {
const calls = [], mode = { value: 'create' }
const goToServiceList = new Function('props', 'router', 'mode', 'loadPage', `${javascript}; return goToServiceList`)(
props, { push: async route => calls.push(route) }, mode, async () => calls.push('refresh'),
)
await goToServiceList()
assert.deepEqual(calls, props.createPage || props.detailPage ? [{ name: 'overseas-labeling' }] : ['refresh'])
if (!props.createPage && !props.detailPage) assert.equal(mode.value, 'list')
}
})
test('keeps overseas warehouse writes behind operation-specific button permissions', () => {
for (const permission of [
'merchant:overseas-warehouse:relabel:create',
'merchant:overseas-warehouse:relabel:files',
'merchant:overseas-warehouse:relabel:confirm',
'merchant:overseas-warehouse:transfer:create',
'merchant:overseas-warehouse:transfer:confirm',
'merchant:overseas-warehouse:discard:create',
'merchant:overseas-warehouse:discard:cancel',
]) assert.match(services, new RegExp(permission))
assert.match(services, /v-if="canCreate" type="primary" @click="openCreate"/)
assert.match(services, /canManageRelabelFiles && props\.service === 'labeling'/)
assert.match(services, /canConfirmTransfer && props\.service === 'transfer'/)
assert.match(services, /canCancelDiscard && props\.service === 'disposal'/)
assert.match(returns, /merchant:overseas-warehouse:return-order:create/)
assert.match(returns, /v-if="canCreate" type="primary" @click="openCreate"/)
})
test('keeps all four overseas warehouse create buttons in the shared left-aligned table toolbar', () => {
for (const service of ['labeling', 'transfer', 'disposal']) {
assert.match(routes, new RegExp(`props: \\{ service: '${service}' \\}`))
}
assert.match(services, /<DataTable[\s\S]*?<template #toolbar-actions><el-button v-if="canCreate"/)
assert.match(returns, /<DataTable[\s\S]*?<template #toolbar-actions><el-button v-if="canCreate"/)
})
test('publishes all overseas operation permissions in the enterprise role editor', () => {
for (const permission of [
'merchant:overseas-warehouse:last-mile-order:list',
'merchant:overseas-warehouse:relabel:list',
'merchant:overseas-warehouse:relabel:create',
'merchant:overseas-warehouse:relabel:files',
'merchant:overseas-warehouse:relabel:confirm',
'merchant:overseas-warehouse:transfer:list',
'merchant:overseas-warehouse:transfer:create',
'merchant:overseas-warehouse:transfer:confirm',
'merchant:overseas-warehouse:discard:list',
'merchant:overseas-warehouse:discard:create',
'merchant:overseas-warehouse:discard:cancel',
'merchant:overseas-warehouse:return-order:list',
'merchant:overseas-warehouse:return-order:create',
'merchant:overseas-warehouse:return-order:relabel',
'merchant:overseas-warehouse:return-inventory:list',
]) assert.match(contracts, new RegExp(`code: '${permission}'`))
})