目录检查不可用时给出 DWS 登录与权限检查说明,避免直接展示机器错误码。 新增真实 Response 回归已先失败复现,修复后完整 make verify 通过。
This commit is contained in:
@@ -2,6 +2,15 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { checkDWSConnection, fetchDWSCredentials, startDWSLogin, validDWSAuthorizationURL } from './dwsLogin'
|
||||
afterEach(() => vi.unstubAllGlobals())
|
||||
describe('DWS transport boundaries', () => {
|
||||
it('explains missing directory authorization with the new login entry', async () => {
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi
|
||||
.fn()
|
||||
.mockResolvedValue(new Response(JSON.stringify({ error: 'dingtalk_discovery_unavailable' }), { status: 503 }))
|
||||
)
|
||||
await expect(checkDWSConnection()).rejects.toThrow('请先登录授权')
|
||||
})
|
||||
it('accepts only the official device verification URL', () => {
|
||||
expect(validDWSAuthorizationURL('https://login.dingtalk.com/oauth2/device/verify.htm?user_code=TEST')).toBe(true)
|
||||
for (const value of [
|
||||
|
||||
+6
-4
@@ -41,10 +41,12 @@ async function request(path: string, signal?: AbortSignal, method = 'GET') {
|
||||
headers: { Accept: 'application/json', ...(method !== 'GET' ? { 'X-Governance-Action': 'dws-login' } : {}) },
|
||||
})
|
||||
const payload: unknown = await response.json().catch(() => null)
|
||||
if (!response.ok)
|
||||
throw new Error(
|
||||
record(payload) && typeof payload.error === 'string' ? payload.error : `DWS 请求失败(HTTP ${response.status})`
|
||||
)
|
||||
if (!response.ok) {
|
||||
const reason = record(payload) && typeof payload.error === 'string' ? payload.error : ''
|
||||
if (reason === 'dingtalk_discovery_unavailable')
|
||||
throw new Error('DWS 未登录、授权已过期或缺少目录权限;请先登录授权,再检查连接。')
|
||||
throw new Error(reason || `DWS 请求失败(HTTP ${response.status})`)
|
||||
}
|
||||
if (!record(payload) || !record(payload.data)) throw new Error('DWS 返回的数据格式不正确,请重试')
|
||||
return payload.data
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user