File size: 6,425 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const axios = require('axios')

const BASE_URL = 'http://localhost:8082/api/v1'

// 配置 - 请替换为你的有效凭证
const VALID_TOKEN = 'U2FsdGVkX1+26QCTo4JlGxKnQFcDjOxHyZthx/zucoUMh4CVGc3d4DdBPG2LK52gLuO5RRtmnclYoZESSyQrP7RDoJxh4PLkF6Dm7m3PiO112i1DWxp/xANsRQBzotJ1soPa1ym82BNQRNoziZHbG2RmC/wM953x1zEBgK0wD6LW0I9cQMNIaiO+TV8f/qYt9m4QEoG3UBnLWCN5+oHU0DTOMJQgXBl7zFVwj/9YENt0rN5tBLZSSZv6krM94lmTO7V/mXycUqa/fWJuz7+B0ThlxFNFwzHdizGB+iqTHLoFrDL9Nw0d4oHnBOEkzSmwjIzNysVttfESpjAjNZ6gfmpV7+qERGxt9VHTogWP2cIaT5Dqjkc1ldG63yfYzXCx'
const VALID_SESSION = 'b6374f59-06d3-44a1-8da5-c1ace74cd0bc'

// 测试用例
const testCases = [
  {
    name: '1. 无Token访问受保护接口',
    request: {
      url: `${ BASE_URL }/host-list`,
      headers: {
        Cookie: `session=${ VALID_SESSION }`
      }
    },
    expected: { status: 401, msg: '未登录(token)' }
  },
  {
    name: '2. 无Session访问受保护接口',
    request: {
      url: `${ BASE_URL }/host-list`,
      headers: {
        token: VALID_TOKEN
      }
    },
    expected: { status: 401, msg: '未登录(session)' }
  },
  {
    name: '3. 伪造Token访问',
    request: {
      url: `${ BASE_URL }/host-list`,
      headers: {
        token: 'fake-invalid-token-12345',
        Cookie: `session=${ VALID_SESSION }`
      }
    },
    expected: { status: 403, msg: 'TOKEN校验失败, 请重新登录' }
  },
  {
    name: '4. 白名单路由访问(无需认证)',
    request: {
      url: `${ BASE_URL }/get-pub-pem`
    },
    expected: { status: 200 }
  },
  {
    name: '5. 正常访问受保护接口',
    request: {
      url: `${ BASE_URL }/host-list`,
      headers: {
        token: VALID_TOKEN,
        Cookie: `session=${ VALID_SESSION }`
      }
    },
    expected: { status: 200, success: true }
  },
  {
    name: '6. 访问其他受保护接口 - 获取分组',
    request: {
      url: `${ BASE_URL }/group`,
      headers: {
        token: VALID_TOKEN,
        Cookie: `session=${ VALID_SESSION }`
      }
    },
    expected: { status: 200, success: true }
  },
  {
    name: '7. 访问其他受保护接口 - 获取脚本列表',
    request: {
      url: `${ BASE_URL }/script`,
      headers: {
        token: VALID_TOKEN,
        Cookie: `session=${ VALID_SESSION }`
      }
    },
    expected: { status: 200, success: true }
  }
]

// 执行测试
async function runTests() {
  console.log('========================================')
  console.log('RESTful API 权限测试')
  console.log('========================================')
  console.log(`服务器地址: ${ BASE_URL }`)
  console.log(`Session: ${ VALID_SESSION.substring(0, 20) }...`)
  console.log(`Token: ${ VALID_TOKEN.substring(0, 20) }...`)
  console.log('========================================\n')

  let passedCount = 0
  let failedCount = 0

  for (const testCase of testCases) {
    try {
      console.log(`测试: ${ testCase.name }`)

      const response = await axios.get(testCase.request.url, {
        headers: testCase.request.headers || {},
        validateStatus: () => true // 接受所有状态码
      })

      let testPassed = true

      // 检查状态码
      const statusPassed = response.status === testCase.expected.status
      console.log(`  状态码: ${ response.status } (预期: ${ testCase.expected.status }) ${ statusPassed ? '✅' : '❌' }`)
      if (!statusPassed) testPassed = false

      // 检查消息(如果有)
      if (testCase.expected.msg) {
        const msgPassed = response.data.msg === testCase.expected.msg
        console.log(`  消息: "${ response.data.msg }" ${ msgPassed ? '✅' : '❌' }`)
        if (!msgPassed) {
          console.log(`      预期: "${ testCase.expected.msg }"`)
          testPassed = false
        }
      }

      // 检查success字段(如果有)
      // 智能判断:如果响应是数组,或者有data字段,也认为是成功的
      if (testCase.expected.success !== undefined) {
        const isActuallySuccess = response.data.success === true ||
                                 Array.isArray(response.data) ||
                                 (response.data.data !== undefined && response.status === 200)
        const successPassed = isActuallySuccess === testCase.expected.success

        if (response.data.success !== undefined) {
          console.log(`  成功标志: ${ response.data.success } (预期: ${ testCase.expected.success }) ${ successPassed ? '✅' : '❌' }`)
        } else if (Array.isArray(response.data)) {
          console.log(`  响应类型: 数组 (长度: ${ response.data.length }) ${ successPassed ? '✅' : '❌' }`)
        } else if (response.data.data !== undefined) {
          console.log(`  响应类型: 对象包含data字段 ${ successPassed ? '✅' : '❌' }`)
        } else {
          console.log(`  响应类型: 其他 (状态200视为成功) ${ successPassed ? '✅' : '❌' }`)
        }

        if (!successPassed) testPassed = false
      }

      if (testPassed) {
        console.log('  结果: ✅ 通过\n')
        passedCount++
      } else {
        console.log('  结果: ❌ 失败\n')
        failedCount++
      }

    } catch (error) {
      console.log(`  ❌ 测试异常: ${ error.message }\n`)
      failedCount++
    }

    // 添加延迟避免请求过快
    await new Promise(resolve => setTimeout(resolve, 300))
  }

  console.log('========================================')
  console.log('测试总结')
  console.log('========================================')
  console.log(`总计: ${ testCases.length } 个测试`)
  console.log(`通过: ${ passedCount } ✅`)
  console.log(`失败: ${ failedCount } ❌`)
  console.log(`成功率: ${ ((passedCount / testCases.length) * 100).toFixed(2) }%`)
  console.log('========================================')

  process.exit(failedCount > 0 ? 1 : 0)
}

// 检查配置
if (VALID_TOKEN === '你的有效token' || VALID_SESSION === '你的有效session') {
  console.error('❌ 错误:请先配置有效的 TOKEN 和 SESSION')
  console.error('\n获取方法:')
  console.error('1. 登录到系统')
  console.error('2. 打开浏览器开发者工具 (F12)')
  console.error('3. Session: Application → Cookies → session')
  console.error('4. Token: Console 中执行 localStorage.getItem("token")')
  console.error('\n然后修改本文件顶部的 VALID_TOKEN 和 VALID_SESSION')
  process.exit(1)
}

runTests()