wwAuth.test.cjs 3.49 KB
const assert = require('assert')

let shouldRequireWwAuth
try {
  const wwAuth = require('../src/store/modules/wwAuth')
  shouldRequireWwAuth = wwAuth.shouldRequireWwAuth
} catch (error) {
  if (error.code !== 'MODULE_NOT_FOUND' || !error.message.includes('wwAuth')) {
    throw error
  }
}

assert.strictEqual(
  typeof shouldRequireWwAuth,
  'function',
  'wwAuth should expose an authorization decision function'
)

const companyHref = 'http://scrm.dev.glsx.com.cn/wx-notification/#/evection/company'

assert.strictEqual(
  shouldRequireWwAuth({
    href: companyHref,
    hostname: 'scrm.dev.glsx.com.cn',
    pushId: '745'
  }),
  false,
  'Meituan reports on the development host should skip WeCom authorization'
)

for (const reportPath of ['personage', 'department', 'company']) {
  assert.strictEqual(
    shouldRequireWwAuth({
      href: `http://scrm.dev.glsx.com.cn/wx-notification/#/evection/${reportPath}`,
      hostname: 'scrm.dev.glsx.com.cn',
      pushId: '999'
    }),
    false
  )
}

for (const hostname of ['scrm.test.glsx.com.cn', 'scrm.glsx.com.cn']) {
  assert.strictEqual(
    shouldRequireWwAuth({
      href: `https://${hostname}/wx-notification/#/evection/company`,
      hostname,
      pushId: '745'
    }),
    true,
    `${hostname} should retain the existing authorization rule`
  )
}

assert.strictEqual(
  shouldRequireWwAuth({
    href: 'http://scrm.dev.glsx.com.cn/wx-notification/#/orderApproval',
    hostname: 'scrm.dev.glsx.com.cn'
  }),
  true,
  'the development-host bypass should not apply to approval pages'
)

for (const approvalHref of [
  'http://scrm.dev.glsx.com.cn/wx-notification/#/orderApproval?return=/company',
  'http://scrm.dev.glsx.com.cn/wx-notification/#/accountApproval?from=/personage',
  'http://scrm.dev.glsx.com.cn/wx-notification/#/orderApproval/',
  'http://scrm.dev.glsx.com.cn/wx-notification/#/accountApproval///',
  'http://scrm.dev.glsx.com.cn/wx-notification/#/OrderApproval'
]) {
  assert.strictEqual(
    shouldRequireWwAuth({
      href: approvalHref,
      hostname: 'scrm.dev.glsx.com.cn',
      pushId: '999'
    }),
    true,
    'approval pages must remain protected when query values contain report path names'
  )
}

for (const reportHref of [
  'https://scrm.test.glsx.com.cn/wx-notification/#/evection/company/',
  'https://scrm.test.glsx.com.cn/wx-notification/#/EVECTION/COMPANY'
]) {
  assert.strictEqual(
    shouldRequireWwAuth({
      href: reportHref,
      hostname: 'scrm.test.glsx.com.cn',
      pushId: '999'
    }),
    true,
    'Router-equivalent report paths must retain authorization outside development'
  )
}

for (const nonReportHref of [
  'http://scrm.dev.glsx.com.cn/wx-notification/#/evection/company-preview',
  'http://scrm.dev.glsx.com.cn/wx-notification/#/unrelated?next=/department'
]) {
  assert.strictEqual(
    shouldRequireWwAuth({
      href: nonReportHref,
      hostname: 'scrm.dev.glsx.com.cn',
      pushId: '999'
    }),
    false,
    'only exact Meituan report routes should participate in the report authorization rule'
  )
}

assert.strictEqual(
  shouldRequireWwAuth({
    href: companyHref,
    hostname: 'scrm.dev.glsx.com.cn.evil.example',
    pushId: '745'
  }),
  true,
  'the bypass must require an exact hostname match'
)

assert.strictEqual(
  shouldRequireWwAuth({
    href: 'https://scrm.test.glsx.com.cn/wx-notification/#/evection/company',
    hostname: 'scrm.test.glsx.com.cn',
    pushId: '624'
  }),
  false,
  'legacy report links should keep the existing pushId compatibility behavior'
)

console.log('wwAuth tests passed')