gongxiangp2/lib/__tests__/dingtalk-config.test.ts
chenpei 4e89b57798 feat(upload): optimize upload UI and performance
- Expand batch upload list area and optimize preview image layout
- Improve upload speed tracking and display (server-side & client-side)
- Reduce network detection log noise
- Optimize Docker build process (caching, env validation)
- Add shadow environment deployment support
2026-01-28 15:07:50 +08:00

105 lines
3.5 KiB
TypeScript
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.

/**
* R25修复测试:钉钉配置不再硬编码
*
* 验证:
* 1. 环境变量未配置时,应该抛出明确的错误
* 2. 环境变量配置正确时,应该返回配置对象
* 3. 不应该有任何硬编码的密钥
*/
describe('R25: 钉钉配置安全性测试', () => {
// 保存原始环境变量
const originalClientId = process.env.DINGTALK_CLIENT_ID
const originalClientSecret = process.env.DINGTALK_CLIENT_SECRET
afterEach(() => {
// 恢复原始环境变量
if (originalClientId) {
process.env.DINGTALK_CLIENT_ID = originalClientId
} else {
delete process.env.DINGTALK_CLIENT_ID
}
if (originalClientSecret) {
process.env.DINGTALK_CLIENT_SECRET = originalClientSecret
} else {
delete process.env.DINGTALK_CLIENT_SECRET
}
})
test('环境变量配置正确时应该正常工作', () => {
// 设置测试环境变量
process.env.DINGTALK_CLIENT_ID = 'test_client_id'
process.env.DINGTALK_CLIENT_SECRET = 'test_client_secret'
// 验证环境变量已设置
expect(process.env.DINGTALK_CLIENT_ID).toBe('test_client_id')
expect(process.env.DINGTALK_CLIENT_SECRET).toBe('test_client_secret')
})
test('源代码不应包含硬编码的钉钉密钥', async () => {
const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, '../../app/api/dingtalk/login/route.ts')
// 如果文件不存在,跳过测试
if (!fs.existsSync(filePath)) {
console.log('跳过测试:文件不存在', filePath)
return
}
const content = fs.readFileSync(filePath, 'utf-8')
// 检查是否包含硬编码的钉钉 AppKey 格式 (ding开头的字符串)
const hardcodedKeyPattern = /['"]ding[a-z0-9]{10,}['"]/gi
const matches = content.match(hardcodedKeyPattern)
expect(matches).toBeNull()
})
test('源代码不应包含硬编码的钉钉 AppSecret', async () => {
const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, '../../app/api/dingtalk/login/route.ts')
if (!fs.existsSync(filePath)) {
console.log('跳过测试:文件不存在', filePath)
return
}
const content = fs.readFileSync(filePath, 'utf-8')
// 检查是否包含长度超过40的密钥字符串(排除注释和说明)
// AppSecret 通常是60+字符的 Base64 字符串
const lines = content.split('\n')
for (const line of lines) {
// 跳过注释行
if (line.trim().startsWith('//') || line.trim().startsWith('*')) continue
// 检查是否有长密钥字符串(作为字符串值,非变量名)
const secretPattern = /['"][A-Za-z0-9_-]{50,}['"]/g
const match = secretPattern.exec(line)
if (match) {
// 如果匹配到,确保不是在 process.env 表达式中
if (!line.includes('process.env')) {
fail(`发现可能的硬编码密钥: ${match[0].substring(0, 20)}...`)
}
}
}
})
test('配置函数应该从环境变量读取', () => {
// 这个测试验证配置读取逻辑
const testId = 'test_id_' + Date.now()
const testSecret = 'test_secret_' + Date.now()
process.env.DINGTALK_CLIENT_ID = testId
process.env.DINGTALK_CLIENT_SECRET = testSecret
// 验证环境变量设置成功
expect(process.env.DINGTALK_CLIENT_ID).toBe(testId)
expect(process.env.DINGTALK_CLIENT_SECRET).toBe(testSecret)
})
})