mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 08:38:20 +00:00
fix: resolve lint and typecheck errors in API and Authz tests
This commit is contained in:
+40
-52
@@ -36,25 +36,21 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
const asset = await assetService.getAsset({ assetId: fileId })
|
||||
return c.json(asset)
|
||||
})
|
||||
.patch(
|
||||
'/files/:fileId/order',
|
||||
zValidator('json', updateAssetOrderRequestSchema),
|
||||
async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
.patch('/files/:fileId/order', zValidator('json', updateAssetOrderRequestSchema), async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
|
||||
const updated = await assetService.updateAssetOrder(fileId, req)
|
||||
return c.json(updated)
|
||||
},
|
||||
)
|
||||
const updated = await assetService.updateAssetOrder(fileId, req)
|
||||
return c.json(updated)
|
||||
})
|
||||
.put('/files/:fileId', zValidator('json', updateFileRequestSchema), async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const user = c.get('user')
|
||||
@@ -165,25 +161,21 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
return c.json(comment, 201)
|
||||
},
|
||||
)
|
||||
.get(
|
||||
'/files/:fileId/comments',
|
||||
zValidator('query', paginationParamsSchema),
|
||||
async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('query')
|
||||
.get('/files/:fileId/comments', zValidator('query', paginationParamsSchema), async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('query')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
|
||||
const comments = await assetService.listComments(fileId, req)
|
||||
return c.json(comments)
|
||||
},
|
||||
)
|
||||
const comments = await assetService.listComments(fileId, req)
|
||||
return c.json(comments)
|
||||
})
|
||||
.get('/comments/:commentId', async (c) => {
|
||||
const commentId = c.req.param('commentId')
|
||||
const user = c.get('user')
|
||||
@@ -198,26 +190,22 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
const comment = await assetService.getComment(commentId)
|
||||
return c.json(comment)
|
||||
})
|
||||
.post(
|
||||
'/files/restore',
|
||||
zValidator('json', restoreFilesRequestSchema),
|
||||
async (c) => {
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
.post('/files/restore', zValidator('json', restoreFilesRequestSchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
for (const id of req.ids) {
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id,
|
||||
})
|
||||
}
|
||||
for (const id of req.ids) {
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id,
|
||||
})
|
||||
}
|
||||
|
||||
await assetService.restoreAssets(req.ids)
|
||||
return c.body(null, 204)
|
||||
},
|
||||
)
|
||||
await assetService.restoreAssets(req.ids)
|
||||
return c.body(null, 204)
|
||||
})
|
||||
.post('/teams/:teamId/files', zValidator('form', uploadFileRequestSchema), async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
const user = c.get('user')
|
||||
|
||||
@@ -11,14 +11,17 @@ vi.mock('@/services/notification/notification')
|
||||
vi.mock('@/services/authz/authz')
|
||||
|
||||
vi.mock('@/api/middleware/auth', () => ({
|
||||
authMiddleware: async (c: Context, next: Next) => {
|
||||
authMiddleware: async (
|
||||
c: Context<{ Variables: { user: { id: string; name: string } } }>,
|
||||
next: Next,
|
||||
) => {
|
||||
c.set('user', { id: 'u1', name: 'Test User' })
|
||||
await next()
|
||||
},
|
||||
}))
|
||||
|
||||
const getApp = () => {
|
||||
const app = new Hono()
|
||||
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
|
||||
.route('/', publicInviteRoute)
|
||||
.use('*', async (c, next) => {
|
||||
c.set('user', { id: 'u1', name: 'Test User' })
|
||||
|
||||
@@ -5,7 +5,10 @@ import { metadataService } from '@/services/metadata/metadata'
|
||||
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
|
||||
|
||||
vi.mock('@/api/middleware/auth', () => ({
|
||||
authMiddleware: async (c: Context, next: Next) => {
|
||||
authMiddleware: async (
|
||||
c: Context<{ Variables: { user: { id: string; name: string } } }>,
|
||||
next: Next,
|
||||
) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
},
|
||||
@@ -15,7 +18,7 @@ vi.mock('@/services/authz/authz')
|
||||
vi.mock('@/services/metadata/metadata')
|
||||
|
||||
describe('metadata api', () => {
|
||||
const app = new Hono()
|
||||
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
|
||||
.use('*', async (c, next) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
|
||||
@@ -5,7 +5,10 @@ import { notificationService } from '@/services/notification/notification'
|
||||
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
|
||||
|
||||
vi.mock('@/api/middleware/auth', () => ({
|
||||
authMiddleware: async (c: Context, next: Next) => {
|
||||
authMiddleware: async (
|
||||
c: Context<{ Variables: { user: { id: string; name: string } } }>,
|
||||
next: Next,
|
||||
) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
},
|
||||
@@ -15,7 +18,7 @@ vi.mock('@/services/authz/authz')
|
||||
vi.mock('@/services/notification/notification')
|
||||
|
||||
describe('notification api', () => {
|
||||
const app = new Hono()
|
||||
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
|
||||
.use('*', async (c, next) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
|
||||
@@ -6,7 +6,10 @@ import { projectService } from '@/services/project/project'
|
||||
import { assetService } from '@/services/asset/asset'
|
||||
|
||||
vi.mock('@/api/middleware/auth', () => ({
|
||||
authMiddleware: async (c: Context, next: Next) => {
|
||||
authMiddleware: async (
|
||||
c: Context<{ Variables: { user: { id: string; name: string } } }>,
|
||||
next: Next,
|
||||
) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
},
|
||||
@@ -17,7 +20,7 @@ vi.mock('@/services/project/project')
|
||||
vi.mock('@/services/asset/asset')
|
||||
|
||||
describe('project api', () => {
|
||||
const app = new Hono()
|
||||
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
|
||||
.use('*', async (c, next) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
|
||||
+27
-29
@@ -5,7 +5,10 @@ import { providerService } from '@/services/provider/provider'
|
||||
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
|
||||
|
||||
vi.mock('@/api/middleware/auth', () => ({
|
||||
authMiddleware: async (c: Context, next: Next) => {
|
||||
authMiddleware: async (
|
||||
c: Context<{ Variables: { user: { id: string; name: string } } }>,
|
||||
next: Next,
|
||||
) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
},
|
||||
@@ -15,7 +18,7 @@ vi.mock('@/services/authz/authz')
|
||||
vi.mock('@/services/provider/provider')
|
||||
|
||||
describe('provider api', () => {
|
||||
const app = new Hono()
|
||||
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
|
||||
.use('*', async (c, next) => {
|
||||
c.set('user', { id: 'user1', name: 'Test User' })
|
||||
await next()
|
||||
@@ -28,11 +31,9 @@ describe('provider api', () => {
|
||||
})
|
||||
|
||||
it('GET /teams/:teamId/providers returns providers', 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.listByTeam).mockResolvedValue([
|
||||
{ id: 'p1', name: 'openai', config: {} },
|
||||
] as any)
|
||||
{ id: 'p1', name: 'openai', config: { api: 'openai' } },
|
||||
] as unknown as Awaited<ReturnType<typeof providerService.listByTeam>>)
|
||||
|
||||
const res = await app.request('/teams/t1/providers')
|
||||
|
||||
@@ -49,14 +50,13 @@ describe('provider api', () => {
|
||||
})
|
||||
|
||||
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
|
||||
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.getById).mockResolvedValue({
|
||||
id: 'p1',
|
||||
teamId: 't1',
|
||||
} as unknown as Awaited<ReturnType<typeof providerService.getById>>)
|
||||
vi.mocked(providerService.listModelsByProvider).mockResolvedValue([
|
||||
{ modelId: 'm1', name: 'model1' },
|
||||
] as any)
|
||||
] as unknown as Awaited<ReturnType<typeof providerService.listModelsByProvider>>)
|
||||
|
||||
const res = await app.request('/providers/p1/models')
|
||||
|
||||
@@ -85,21 +85,19 @@ describe('provider api', () => {
|
||||
config: {
|
||||
api: 'openai-completions',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
input: ['text' as const],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 4096,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
},
|
||||
},
|
||||
]
|
||||
// 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)
|
||||
} as unknown as Awaited<ReturnType<typeof providerService.create>>)
|
||||
|
||||
const res = await app.request('/teams/t1/providers', {
|
||||
method: 'POST',
|
||||
@@ -131,24 +129,23 @@ describe('provider api', () => {
|
||||
config: {
|
||||
api: 'openai-completions',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
input: ['text' as const],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 4096,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
},
|
||||
},
|
||||
]
|
||||
// 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.getById).mockResolvedValue({
|
||||
id: 'p1',
|
||||
teamId: 't1',
|
||||
} as unknown as Awaited<ReturnType<typeof providerService.getById>>)
|
||||
vi.mocked(providerService.update).mockResolvedValue({
|
||||
id: 'p1',
|
||||
name: 'openai',
|
||||
config,
|
||||
models,
|
||||
} as any)
|
||||
} as unknown as Awaited<ReturnType<typeof providerService.update>>)
|
||||
|
||||
const res = await app.request('/providers/p1', {
|
||||
method: 'PUT',
|
||||
@@ -169,12 +166,13 @@ describe('provider api', () => {
|
||||
})
|
||||
|
||||
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)
|
||||
vi.mocked(providerService.getById).mockResolvedValue({
|
||||
id: 'p1',
|
||||
teamId: 't1',
|
||||
} as unknown as Awaited<ReturnType<typeof providerService.getById>>)
|
||||
vi.mocked(providerService.delete).mockResolvedValue({
|
||||
id: 'p1',
|
||||
} as unknown as Awaited<ReturnType<typeof providerService.delete>>)
|
||||
|
||||
const res = await app.request('/providers/p1', {
|
||||
method: 'DELETE',
|
||||
|
||||
@@ -273,7 +273,13 @@ describe('AuthzService', () => {
|
||||
data: { name: 'BotUser2', email: 'bot2@example.com', password: 'p' },
|
||||
})
|
||||
const agent = await prisma.agent.create({
|
||||
data: { id: user.id, teamId: team.id, type: 'chat', enabled: true, config: {} },
|
||||
data: {
|
||||
id: user.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
config: { provider: 'openai', model: 'gpt-4o' },
|
||||
},
|
||||
})
|
||||
const agentSession = await prisma.agentSession.create({
|
||||
data: { agentId: agent.id, cwd: '/tmp' },
|
||||
@@ -286,10 +292,20 @@ describe('AuthzService', () => {
|
||||
|
||||
// Metadata Field
|
||||
const teamField = await prisma.metadataField.create({
|
||||
data: { key: 'field_t', scope: 'TEAM', teamId: team.id, config: { name: 'Field T' } },
|
||||
data: {
|
||||
key: 'field_t',
|
||||
scope: 'TEAM',
|
||||
teamId: team.id,
|
||||
config: { name: 'Field T', type: 'text' },
|
||||
},
|
||||
})
|
||||
const projectField = await prisma.metadataField.create({
|
||||
data: { key: 'field_p', scope: 'PROJECT', projectId: project.id, config: { name: 'Field P' } },
|
||||
data: {
|
||||
key: 'field_p',
|
||||
scope: 'PROJECT',
|
||||
projectId: project.id,
|
||||
config: { name: 'Field P', type: 'text' },
|
||||
},
|
||||
})
|
||||
|
||||
// Skill
|
||||
@@ -299,7 +315,7 @@ describe('AuthzService', () => {
|
||||
|
||||
// Provider
|
||||
const provider = await prisma.provider.create({
|
||||
data: { name: 'Prov 1', teamId: team.id, config: {} },
|
||||
data: { name: 'Prov 1', teamId: team.id, config: { api: 'openai' } },
|
||||
})
|
||||
|
||||
// Invite
|
||||
@@ -322,9 +338,15 @@ describe('AuthzService', () => {
|
||||
})
|
||||
|
||||
// Test execution via cast
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const resolveContext = (type: ResourceType, id: string) =>
|
||||
(authzService as any).resolveContext(type, id)
|
||||
(
|
||||
authzService as unknown as {
|
||||
resolveContext: (
|
||||
type: ResourceType,
|
||||
id: string,
|
||||
) => Promise<{ teamId: string; projectId?: string }>
|
||||
}
|
||||
).resolveContext(type, id)
|
||||
|
||||
// Tests
|
||||
await expect(resolveContext(ResourceType.Team, team.id)).resolves.toEqual({ teamId: team.id })
|
||||
|
||||
Reference in New Issue
Block a user