Commit b6b9b12c by 张立顺

test: guard store daily report live config

1 parent c373c1e8
import path from 'node:path'
export const ALLOWED_BASE_URL =
'http://dj.test.glsx.com.cn/rop-dj-car-boutique'
export const EXPECTED_CONTEXT = Object.freeze({
parkName: '明明崇文园区店',
parkMerchantId: '44209880',
storeName: '明明崇文4s店A',
sceneName: '精品升级'
})
const asText = value => String(value ?? '').trim()
const isExactAllowedBaseUrl = value => {
try {
const actual = new URL(value)
const allowed = new URL(ALLOWED_BASE_URL)
return (
actual.protocol === allowed.protocol &&
actual.hostname === allowed.hostname &&
actual.port === allowed.port &&
actual.pathname === allowed.pathname &&
actual.search === '' &&
actual.hash === '' &&
actual.username === '' &&
actual.password === ''
)
} catch {
return false
}
}
export const loadDailyReportUiConfig = (env = process.env) => {
const portText = asText(env.DAILY_REPORT_UI_PORT) || '9420'
const port = Number(portText)
return Object.freeze({
enabled: env.RUN_DAILY_REPORT_UI_E2E === '1',
baseUrl: asText(env.DAILY_REPORT_E2E_BASE_URL) || ALLOWED_BASE_URL,
cliPath: asText(env.DAILY_REPORT_UI_CLI_PATH),
projectPath: asText(env.DAILY_REPORT_UI_PROJECT_PATH) || process.cwd(),
port,
wsEndpoint:
asText(env.DAILY_REPORT_UI_WS_ENDPOINT) || `ws://127.0.0.1:${portText}`,
mobile: asText(env.DAILY_REPORT_E2E_MOBILE),
code: asText(env.DAILY_REPORT_E2E_CODE),
expectedMobile: asText(env.DAILY_REPORT_E2E_EXPECTED_MOBILE),
parkName: EXPECTED_CONTEXT.parkName,
parkMerchantId: EXPECTED_CONTEXT.parkMerchantId,
storeName: EXPECTED_CONTEXT.storeName,
sceneName: EXPECTED_CONTEXT.sceneName,
allowSave: env.DAILY_REPORT_E2E_ALLOW_SAVE === '1',
allowPush: env.DAILY_REPORT_E2E_ALLOW_PUSH === '1',
launchDevTools: env.DAILY_REPORT_UI_LAUNCH === '1',
evidenceDir:
asText(env.DAILY_REPORT_E2E_EVIDENCE_DIR) ||
path.resolve('outputs', 'store-daily-report-e2e', 'latest')
})
}
export const requireDailyReportUiConfig = (env = process.env) => {
const config = loadDailyReportUiConfig(env)
const missing = []
if (!config.enabled) missing.push('RUN_DAILY_REPORT_UI_E2E')
if (!config.cliPath) missing.push('DAILY_REPORT_UI_CLI_PATH')
if (!config.projectPath) missing.push('DAILY_REPORT_UI_PROJECT_PATH')
if (!config.mobile) missing.push('DAILY_REPORT_E2E_MOBILE')
if (!config.expectedMobile) missing.push('DAILY_REPORT_E2E_EXPECTED_MOBILE')
if (!config.code) missing.push('DAILY_REPORT_E2E_CODE')
if (!config.evidenceDir) missing.push('DAILY_REPORT_E2E_EVIDENCE_DIR')
if (missing.length) {
throw new Error(
`Missing daily report UI runtime values: ${missing.join(', ')}`
)
}
if (!isExactAllowedBaseUrl(config.baseUrl)) {
throw new Error('Daily report E2E base URL is not allowlisted')
}
if (
!Number.isInteger(config.port) ||
config.port < 1024 ||
config.port > 65535
) {
throw new Error('Daily report E2E port must be an integer from 1024 to 65535')
}
let endpoint
try {
endpoint = new URL(config.wsEndpoint)
} catch {
throw new Error('Daily report E2E websocket endpoint must be loopback')
}
if (
endpoint.protocol !== 'ws:' ||
!['127.0.0.1', 'localhost', '::1'].includes(endpoint.hostname)
) {
throw new Error('Daily report E2E websocket endpoint must be loopback')
}
if (Number(endpoint.port || 80) !== config.port) {
throw new Error('Daily report E2E websocket port must match automation port')
}
if (config.mobile !== config.expectedMobile) {
throw new Error('Daily report E2E mobile does not match approved target')
}
return config
}
import assert from 'node:assert/strict'
import path from 'node:path'
import test from 'node:test'
import {
ALLOWED_BASE_URL,
EXPECTED_CONTEXT,
loadDailyReportUiConfig,
requireDailyReportUiConfig
} from './liveConfig.mjs'
const validEnv = Object.freeze({
RUN_DAILY_REPORT_UI_E2E: '1',
DAILY_REPORT_E2E_BASE_URL: 'http://dj.test.glsx.com.cn/rop-dj-car-boutique',
DAILY_REPORT_UI_CLI_PATH: 'C:\\Program Files (x86)\\Tencent\\微信web开发者工具\\cli.bat',
DAILY_REPORT_UI_PROJECT_PATH: process.cwd(),
DAILY_REPORT_UI_PORT: '9420',
DAILY_REPORT_UI_WS_ENDPOINT: 'ws://127.0.0.1:9420',
DAILY_REPORT_E2E_MOBILE: 'approved-mobile',
DAILY_REPORT_E2E_EXPECTED_MOBILE: 'approved-mobile',
DAILY_REPORT_E2E_CODE: '8888',
DAILY_REPORT_E2E_ALLOW_SAVE: '1',
DAILY_REPORT_E2E_ALLOW_PUSH: '1',
DAILY_REPORT_E2E_EVIDENCE_DIR: path.resolve('outputs', 'store-daily-report-e2e', 'test')
})
test('loads the approved test context and write authorization', () => {
const config = requireDailyReportUiConfig(validEnv)
assert.equal(ALLOWED_BASE_URL, validEnv.DAILY_REPORT_E2E_BASE_URL)
assert.equal(config.enabled, true)
assert.equal(config.parkName, '明明崇文园区店')
assert.equal(config.parkMerchantId, '44209880')
assert.equal(config.storeName, '明明崇文4s店A')
assert.equal(config.sceneName, '精品升级')
assert.equal(config.allowSave, true)
assert.equal(config.allowPush, true)
assert.equal(config.port, 9420)
assert.deepEqual(EXPECTED_CONTEXT, {
parkName: '明明崇文园区店',
parkMerchantId: '44209880',
storeName: '明明崇文4s店A',
sceneName: '精品升级'
})
})
test('load returns a disabled safe shape when live execution is not enabled', () => {
const config = loadDailyReportUiConfig({})
assert.equal(config.enabled, false)
assert.equal(config.allowSave, false)
assert.equal(config.allowPush, false)
assert.equal(config.port, 9420)
assert.equal(config.wsEndpoint, 'ws://127.0.0.1:9420')
})
test('rejects missing runtime values without printing credential values', () => {
assert.throws(
() => requireDailyReportUiConfig({ RUN_DAILY_REPORT_UI_E2E: '1' }),
error => {
assert.match(error.message, /Missing daily report UI runtime values/)
assert.doesNotMatch(error.message, /approved-mobile|8888/)
return true
}
)
})
test('rejects a mobile mismatch without exposing either number', () => {
assert.throws(
() =>
requireDailyReportUiConfig({
...validEnv,
DAILY_REPORT_E2E_EXPECTED_MOBILE: 'different-mobile'
}),
error => {
assert.match(error.message, /mobile does not match approved target/)
assert.doesNotMatch(error.message, /approved-mobile|different-mobile/)
return true
}
)
})
test('rejects non-test base URLs', () => {
assert.throws(
() =>
requireDailyReportUiConfig({
...validEnv,
DAILY_REPORT_E2E_BASE_URL: 'https://dj.glsx.com.cn/rop-dj-car-boutique'
}),
/not allowlisted/
)
})
test('rejects invalid ports and non-loopback websocket endpoints', () => {
assert.throws(
() => requireDailyReportUiConfig({ ...validEnv, DAILY_REPORT_UI_PORT: '80' }),
/port/
)
assert.throws(
() =>
requireDailyReportUiConfig({
...validEnv,
DAILY_REPORT_UI_WS_ENDPOINT: 'ws://192.168.1.9:9420'
}),
/loopback/
)
})
test('write flags are opt-in even when live execution is enabled', () => {
const config = requireDailyReportUiConfig({
...validEnv,
DAILY_REPORT_E2E_ALLOW_SAVE: '0',
DAILY_REPORT_E2E_ALLOW_PUSH: '0'
})
assert.equal(config.allowSave, false)
assert.equal(config.allowPush, false)
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!