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
+33 -34
View File
@@ -1,52 +1,38 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import { Hono } from 'hono'
import { describe, expect, it, vi, beforeEach } from 'vitest'
import { Hono, type Context, type Next } from 'hono'
import notificationRoute from './notification'
import { notificationService } from '@/services/notification/notification'
import { authMiddleware } from '@/api/middleware/auth'
vi.mock('@/services/authz/authz', () => ({
authzService: {
hasPermission: vi.fn(),
},
Permission: {
Read: 'Read',
Edit: 'Edit',
Admin: 'Admin',
},
}))
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
vi.mock('@/api/middleware/auth', () => ({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
authMiddleware: async (c: any, next: any) => {
authMiddleware: async (c: Context, next: Next) => {
c.set('user', { id: 'user1', name: 'Test User' })
await next()
},
}))
vi.mock('@/services/authz/authz')
vi.mock('@/services/notification/notification')
describe('notification api', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let mockList: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let mockMarkRead: any
const app = new Hono()
.use('*', async (c, next) => {
c.set('user', { id: 'user1', name: 'Test User' })
await next()
})
.route('/', notificationRoute)
beforeEach(() => {
mockList = vi.fn()
mockMarkRead = vi.fn()
vi.spyOn(notificationService, 'list').mockImplementation(mockList)
vi.spyOn(notificationService, 'markRead').mockImplementation(mockMarkRead)
})
afterEach(() => {
vi.restoreAllMocks()
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
})
it('GET /teams/:teamId/notifications', async () => {
mockList.mockResolvedValue({
data: [{ id: 'n1', type: 'comment_created' }],
vi.mocked(notificationService.list).mockResolvedValue({
data: [{ id: 'n1', type: 'comment_created' }] as any, // eslint-disable-line @typescript-eslint/no-explicit-any
pageInfo: { total: 1, cursor: 'abc' },
})
const app = new Hono().use('*', authMiddleware).route('/', notificationRoute)
const res = await app.request('/teams/t1/notifications?unreadOnly=true&pageSize=10')
expect(res.status).toBe(200)
@@ -55,17 +41,24 @@ describe('notification api', () => {
expect(json.pageInfo.total).toBe(1)
expect(json.pageInfo.cursor).toBe('abc')
expect(mockList).toHaveBeenCalledWith('t1', 'user1', {
expect(notificationService.list).toHaveBeenCalledWith('t1', 'user1', {
unreadOnly: true,
after: undefined,
pageSize: 10,
})
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Read,
type: ResourceType.Team,
id: 't1',
})
})
it('POST /teams/:teamId/notifications/read', async () => {
mockMarkRead.mockResolvedValue(undefined)
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(notificationService.markRead).mockResolvedValue(undefined as any)
const app = new Hono().use('*', authMiddleware).route('/', notificationRoute)
const res = await app.request('/teams/t1/notifications/read', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -73,6 +66,12 @@ describe('notification api', () => {
})
expect(res.status).toBe(204)
expect(mockMarkRead).toHaveBeenCalledWith('t1', 'user1', 'n1')
expect(notificationService.markRead).toHaveBeenCalledWith('t1', 'user1', 'n1')
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: expect.anything(),
permission: Permission.Read,
type: ResourceType.Team,
id: 't1',
})
})
})