refactor(authz): move to domain-based authz and flatten routes

This commit is contained in:
yiling
2026-05-25 23:51:37 +08:00
parent 35fd5c78ce
commit 9f9549255d
61 changed files with 1646 additions and 1147 deletions
+28 -1
View File
@@ -5,6 +5,7 @@ import { authMiddleware } from '@/api/middleware/auth'
import { uploadService } from '@/services/upload/upload'
import type { CreateUploadTaskResponse, TaskInfo } from '@/dtos/upload'
import type { PaginatedData } from '@/services/pagination'
import { authzService, ResourceType, Permission } from '@/services/authz/authz'
vi.mock('@/api/middleware/auth', () => ({
// We use any here because mocking Hono context and middleware
@@ -19,13 +20,17 @@ vi.mock('@/api/middleware/auth', () => ({
vi.mock('@/services/authz/authz', () => ({
authzService: {
hasPermission: vi.fn(),
hasPermission: vi.fn().mockResolvedValue(undefined),
},
Permission: {
Read: 'Read',
Edit: 'Edit',
Admin: 'Admin',
},
ResourceType: {
Team: 'team',
Asset: 'asset',
},
}))
vi.mock('@/services/notification/notification', () => ({
@@ -39,6 +44,7 @@ describe('Upload API', () => {
beforeEach(() => {
vi.restoreAllMocks()
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
})
it('GET /teams/:teamId/upload/tasks', async () => {
@@ -53,6 +59,13 @@ describe('Upload API', () => {
})
expect(res.status).toBe(200)
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
type: ResourceType.Team,
id: 'team1',
permission: Permission.Read,
}),
)
expect(uploadService.listUploadTasks).toHaveBeenCalled()
})
@@ -73,6 +86,13 @@ describe('Upload API', () => {
})
expect(res.status).toBe(200)
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
type: ResourceType.Asset,
id: 'parent1',
permission: Permission.Edit,
}),
)
expect(uploadService.createUploadTask).toHaveBeenCalled()
})
@@ -88,6 +108,13 @@ describe('Upload API', () => {
})
expect(res.status).toBe(200)
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
type: ResourceType.Team,
id: 'team1',
permission: Permission.Edit,
}),
)
expect(uploadService.confirmFileUpload).toHaveBeenCalledWith('user1', 'task1', {
fileId: 'file1',
})