修复前端登录权限边界问题

This commit is contained in:
andy committed 2026-07-10 09:35:47 +08:00
1 parent e49b2ccc1d
commit 25efcccf72
11 files changed
+282 -15

No files matched your search

+33
View File
@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { DebugEmlUploadError, uploadDebugEmlSuperAgentRun } from '@/services/debugEmlService'
import { setUnauthorizedHandler } from '@/services/httpClient'
const jsonHeaders = {
headers: {
@@ -21,6 +22,7 @@ function mockJsonResponse(payload: unknown, status = 201): Response {
describe('debugEmlService', () => {
beforeEach(() => {
vi.restoreAllMocks()
setUnauthorizedHandler(null)
})
it('uploads an eml file with FormData and debug key header', async () => {
@@ -76,6 +78,8 @@ describe('debugEmlService', () => {
})
it('throws a typed safe error for backend error_code responses', async () => {
const unauthorizedHandler = vi.fn()
setUnauthorizedHandler(unauthorizedHandler)
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
mockJsonResponse(
{
@@ -98,5 +102,34 @@ describe('debugEmlService', () => {
errorCode: 'DEBUG_UPLOAD_KEY_INVALID',
message: 'Debug 上传访问口令缺失或错误。',
} satisfies Partial<DebugEmlUploadError>)
expect(unauthorizedHandler).not.toHaveBeenCalled()
})
it('runs the shared unauthorized handler for auth session errors', async () => {
const unauthorizedHandler = vi.fn()
setUnauthorizedHandler(unauthorizedHandler)
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
mockJsonResponse(
{
error_code: 'AUTH_SESSION_INVALID',
message: '登录已失效。',
},
401,
),
)
const file = new File(['From: guest@example.test'], 'booking.eml', { type: 'message/rfc822' })
await expect(
uploadDebugEmlSuperAgentRun({
hotelId: 'HOTEL-TEST',
debugUploadKey: 'manual-debug-key',
file,
}),
).rejects.toMatchObject({
status: 401,
errorCode: 'AUTH_SESSION_INVALID',
} satisfies Partial<DebugEmlUploadError>)
expect(unauthorizedHandler).toHaveBeenCalledOnce()
})
})