新增前端密码加密工具
This commit is contained in:
55
ruoyi-ui/tests/password-transfer-api.test.js
Normal file
55
ruoyi-ui/tests/password-transfer-api.test.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const assert = require('assert')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const vm = require('vm')
|
||||
|
||||
function loadModule(filePath, stubs = {}) {
|
||||
const source = fs.readFileSync(filePath, 'utf8')
|
||||
const exportedNames = []
|
||||
const transformed = source
|
||||
.replace(/^import .*$/gm, '')
|
||||
.replace(/export function\s+([A-Za-z0-9_]+)\s*\(/g, (_, name) => {
|
||||
exportedNames.push(name)
|
||||
return `function ${name}(`
|
||||
})
|
||||
.replace(/export default\s+/g, 'module.exports = ')
|
||||
|
||||
const sandbox = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require,
|
||||
console,
|
||||
process: {
|
||||
env: {
|
||||
VUE_APP_PASSWORD_TRANSFER_KEY: '1234567890abcdef'
|
||||
}
|
||||
},
|
||||
...stubs
|
||||
}
|
||||
|
||||
vm.runInNewContext(
|
||||
`${transformed}\nmodule.exports = { ${exportedNames.join(', ')} };`,
|
||||
sandbox,
|
||||
{ filename: filePath }
|
||||
)
|
||||
|
||||
return sandbox.module.exports
|
||||
}
|
||||
|
||||
const passwordTransferModule = loadModule(
|
||||
path.resolve(__dirname, '../src/utils/passwordTransfer.js'),
|
||||
{ CryptoJS: require('crypto-js') }
|
||||
)
|
||||
|
||||
const { encryptPasswordFields } = passwordTransferModule
|
||||
|
||||
const encrypted = encryptPasswordFields(
|
||||
{ password: 'admin123', code: '8888' },
|
||||
['password'],
|
||||
'1234567890abcdef'
|
||||
)
|
||||
|
||||
assert.notStrictEqual(encrypted.password, 'admin123')
|
||||
assert.strictEqual(encrypted.code, '8888')
|
||||
|
||||
console.log('password-transfer-api test passed')
|
||||
Reference in New Issue
Block a user