GuDingZiChan/backend/scripts/__tests__/sheet2RemoteScriptSafety.test.js

81 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const test = require('node:test')
const assert = require('node:assert/strict')
const prepareScript = require('../prepareSheet2SupplementDryRun')
const importScript = require('../importSheet2PublicAssetsToLive')
const explicitConnectionArgs = [
'--plink-path',
'plink-test.exe',
'--ssh-host',
'deploy@example.invalid',
'--ssh-hostkey',
'SHA256:test-host-key',
'--ssh-password',
'test-secret-value'
]
test('Sheet2 导入脚本默认只能 preview,只有显式 --apply 才允许写入', () => {
assert.deepEqual(importScript.parseArgs([]), {
inputDir: '',
apply: false,
dryRun: true,
plinkPath: '',
sshHost: '',
sshHostkey: '',
sshPassword: ''
})
const applyOptions = importScript.parseArgs(['--apply'])
assert.equal(applyOptions.apply, true)
assert.equal(applyOptions.dryRun, false)
const previewOptions = importScript.parseArgs(['--dry-run'])
assert.equal(previewOptions.apply, false)
assert.equal(previewOptions.dryRun, true)
})
test('Sheet2 导入脚本拒绝冲突或拼错的写入参数', () => {
assert.throws(
() => importScript.parseArgs(['--apply', '--dry-run']),
/不能同时使用/
)
assert.throws(() => importScript.parseArgs(['--aply']), /不支持的参数/)
assert.throws(() => prepareScript.parseArgs(['--ssh-host']), /缺少参数值/)
})
test('两个 Sheet2 脚本缺少连接参数时应在联网前明确失败且不包含秘密', () => {
for (const script of [prepareScript, importScript]) {
const options = script.parseArgs(['--ssh-password', 'test-secret-value'])
assert.throws(
() => script.resolveRemoteConnection(options, {}),
(error) => {
assert.match(error.message, /缺少固定资产远程连接参数/)
assert.match(error.message, /FIXED_ASSET_SSH_HOST/)
assert.match(error.message, /未发起网络连接/)
assert.doesNotMatch(error.message, /test-secret-value/)
return true
}
)
}
})
test('两个 Sheet2 脚本支持显式连接参数且 CLI 优先于环境变量', () => {
const env = {
FIXED_ASSET_PLINK_PATH: 'env-plink.exe',
FIXED_ASSET_SSH_HOST: 'env@example.invalid',
FIXED_ASSET_SSH_HOSTKEY: 'SHA256:env-key',
FIXED_ASSET_SSH_PASSWORD: 'env-secret'
}
for (const script of [prepareScript, importScript]) {
const options = script.parseArgs(explicitConnectionArgs)
assert.deepEqual(script.resolveRemoteConnection(options, env), {
plinkPath: 'plink-test.exe',
sshHost: 'deploy@example.invalid',
sshHostkey: 'SHA256:test-host-key',
sshPassword: 'test-secret-value'
})
}
})