mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 12:59:48 +00:00
refactor(authz): move to domain-based authz and flatten routes
This commit is contained in:
+91
-55
@@ -1,53 +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 providerRoute from './provider'
|
||||
import { authMiddleware } from '@/api/middleware/auth'
|
||||
import { providerService } from '@/services/provider/provider'
|
||||
import { authzService } from '@/services/authz/authz'
|
||||
|
||||
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()
|
||||
},
|
||||
}))
|
||||
|
||||
describe('provider api', () => {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', providerRoute)
|
||||
vi.mock('@/services/authz/authz')
|
||||
vi.mock('@/services/provider/provider')
|
||||
|
||||
let mockListByTeam: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockCreate: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockUpdate: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
let mockDelete: any // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
describe('provider api', () => {
|
||||
const app = new Hono()
|
||||
.use('*', async (c, next) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
})
|
||||
.route('/', providerRoute)
|
||||
|
||||
beforeEach(() => {
|
||||
mockListByTeam = vi.spyOn(providerService, 'listByTeam')
|
||||
mockCreate = vi.spyOn(providerService, 'create')
|
||||
mockUpdate = vi.spyOn(providerService, 'update')
|
||||
mockDelete = vi.spyOn(providerService, 'delete')
|
||||
|
||||
// Default to admin permission granted
|
||||
vi.restoreAllMocks()
|
||||
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('GET /teams/:teamId/providers returns providers', async () => {
|
||||
mockListByTeam.mockResolvedValue([{ id: 'p1', name: 'openai', config: {} }])
|
||||
// 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(providerService.listByTeam).mockResolvedValue([
|
||||
{ id: 'p1', name: 'openai', config: {} },
|
||||
] as any)
|
||||
|
||||
const res = await app.request('/teams/t1/providers')
|
||||
|
||||
@@ -55,26 +40,37 @@ describe('provider api', () => {
|
||||
const data = await res.json()
|
||||
expect(data).toHaveLength(1)
|
||||
expect(data[0].id).toBe('p1')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
teamId: 't1',
|
||||
permission: 'Admin',
|
||||
}),
|
||||
)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: 't1',
|
||||
})
|
||||
})
|
||||
|
||||
it('GET /teams/:teamId/providers/:id/models returns models', async () => {
|
||||
const mockListModelsByProvider = vi.spyOn(providerService, 'listModelsByProvider')
|
||||
it('GET /providers/:id/models returns models', async () => {
|
||||
// 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
|
||||
mockListModelsByProvider.mockResolvedValue([{ modelId: 'm1', name: 'model1' } as any])
|
||||
vi.mocked(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any)
|
||||
// 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(providerService.listModelsByProvider).mockResolvedValue([
|
||||
{ modelId: 'm1', name: 'model1' },
|
||||
] as any)
|
||||
|
||||
const res = await app.request('/teams/t1/providers/p1/models')
|
||||
const res = await app.request('/providers/p1/models')
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data).toHaveLength(1)
|
||||
expect(data[0].modelId).toBe('m1')
|
||||
expect(mockListModelsByProvider).toHaveBeenCalledWith('t1', 'p1')
|
||||
expect(providerService.listModelsByProvider).toHaveBeenCalledWith('t1', 'p1')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Provider,
|
||||
id: 'p1',
|
||||
})
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/providers creates provider', async () => {
|
||||
@@ -96,7 +92,14 @@ describe('provider api', () => {
|
||||
},
|
||||
},
|
||||
]
|
||||
mockCreate.mockResolvedValue({ id: 'p1', name: 'openai', config, models })
|
||||
// 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(providerService.create).mockResolvedValue({
|
||||
id: 'p1',
|
||||
name: 'openai',
|
||||
config,
|
||||
models,
|
||||
} as any)
|
||||
|
||||
const res = await app.request('/teams/t1/providers', {
|
||||
method: 'POST',
|
||||
@@ -107,10 +110,16 @@ describe('provider api', () => {
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.name).toBe('openai')
|
||||
expect(mockCreate).toHaveBeenCalledWith('t1', 'openai', config, models)
|
||||
expect(providerService.create).toHaveBeenCalledWith('t1', 'openai', config, models)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: 't1',
|
||||
})
|
||||
})
|
||||
|
||||
it('PUT /teams/:teamId/providers/:id updates provider', async () => {
|
||||
it('PUT /providers/:id updates provider', async () => {
|
||||
const config = {
|
||||
baseUrl: 'https://new-url.com',
|
||||
api: 'openai-completions',
|
||||
@@ -129,9 +138,19 @@ describe('provider api', () => {
|
||||
},
|
||||
},
|
||||
]
|
||||
mockUpdate.mockResolvedValue({ id: 'p1', name: 'openai', config, models })
|
||||
// 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(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any)
|
||||
// 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(providerService.update).mockResolvedValue({
|
||||
id: 'p1',
|
||||
name: 'openai',
|
||||
config,
|
||||
models,
|
||||
} as any)
|
||||
|
||||
const res = await app.request('/teams/t1/providers/p1', {
|
||||
const res = await app.request('/providers/p1', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ config, models }),
|
||||
@@ -140,19 +159,36 @@ describe('provider api', () => {
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.config.baseUrl).toBe('https://new-url.com')
|
||||
expect(mockUpdate).toHaveBeenCalledWith('t1', 'p1', config, models)
|
||||
expect(providerService.update).toHaveBeenCalledWith('t1', 'p1', config, models)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Provider,
|
||||
id: 'p1',
|
||||
})
|
||||
})
|
||||
|
||||
it('DELETE /teams/:teamId/providers/:id deletes provider', async () => {
|
||||
mockDelete.mockResolvedValue({ id: 'p1' })
|
||||
it('DELETE /providers/:id deletes provider', async () => {
|
||||
// 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(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any)
|
||||
// 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(providerService.delete).mockResolvedValue({ id: 'p1' } as any)
|
||||
|
||||
const res = await app.request('/teams/t1/providers/p1', {
|
||||
const res = await app.request('/providers/p1', {
|
||||
method: 'DELETE',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data.success).toBe(true)
|
||||
expect(mockDelete).toHaveBeenCalledWith('t1', 'p1')
|
||||
expect(providerService.delete).toHaveBeenCalledWith('t1', 'p1')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: expect.anything(),
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Provider,
|
||||
id: 'p1',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user