开放有效素材库账号授权小壹客户端

This commit is contained in:
WuYongJie 2026-11-30 19:43:36 +08:00
parent 5560b0f46c
commit fcfccb5c4f
2 changed files with 45 additions and 8 deletions

View File

@ -1,8 +1,7 @@
/** @jest-environment node */
import { prisma } from '@/lib/prisma'
import { listDesktopProjects, putDesktopProject } from '@/lib/desktop-client/service'
jest.mock('@/lib/prisma', () => ({ prisma: { $queryRawUnsafe: jest.fn(), $transaction: jest.fn() } }))
jest.mock('@/lib/detail-generator/access', () => ({ assertDetailGeneratorUser: jest.fn() }))
import { desktopUser, listDesktopProjects, putDesktopProject } from '@/lib/desktop-client/service'
jest.mock('@/lib/prisma', () => ({ prisma: { user: { findUnique: jest.fn() }, $queryRawUnsafe: jest.fn(), $transaction: jest.fn() } }))
jest.mock('@/lib/detail-generator/repository', () => ({ findDetailProject: jest.fn(), listDetailAssets: jest.fn(), findDetailAsset: jest.fn() }))
jest.mock('@/lib/detail-generator/storage', () => ({ readDetailFile: jest.fn(), saveDetailFile: jest.fn(), deleteDetailFile: jest.fn() }))
const hash = 'a'.repeat(64), source = 'native:' + hash
@ -21,6 +20,39 @@ beforeEach(() => {
throw new Error(sql)
})
})
describe('desktop account access', () => {
const enabled = process.env.DETAIL_GENERATOR_ENABLED
const regular = { username: 'tester', realName: '试用同事', status: 'active', isActive: true, archivedAt: null }
const findUser = prisma.user.findUnique as jest.Mock
beforeEach(() => {
process.env.DETAIL_GENERATOR_ENABLED = 'true'
findUser.mockResolvedValue(regular)
})
afterEach(() => {
if (enabled === undefined) delete process.env.DETAIL_GENERATOR_ENABLED
else process.env.DETAIL_GENERATOR_ENABLED = enabled
})
test('authorizes an active material-library account', async () => {
await expect(desktopUser('tester')).resolves.toEqual({ id: 'tester', name: '试用同事' })
})
test.each([
null, { ...regular, status: 'pending' }, { ...regular, isActive: false }, { ...regular, archivedAt: new Date() },
])('denies unavailable accounts', async (account) => {
findUser.mockResolvedValue(account)
await expect(desktopUser('tester')).rejects.toMatchObject({ status: 403, code: 'account_inactive' })
})
test('requires login and respects the existing service switch', async () => {
await expect(desktopUser('')).rejects.toMatchObject({ status: 401, code: 'unauthenticated' })
delete process.env.DETAIL_GENERATOR_ENABLED
await expect(desktopUser('tester')).rejects.toMatchObject({ status: 403, code: 'feature_disabled' })
expect(findUser).not.toHaveBeenCalled()
})
})
test('atomically publishes a complete native snapshot only after every same-owner file and preview exists', async () => {
expect(await putDesktopProject('owner','project',body())).toMatchObject({ project: { revision: 3 } })
expect((prisma.$queryRawUnsafe as jest.Mock).mock.calls.filter(([sql]) => sql.startsWith('UPDATE'))).toHaveLength(1)

View File

@ -1,6 +1,5 @@
import { createHash, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto'
import { prisma } from '@/lib/prisma'
import { assertDetailGeneratorUser } from '@/lib/detail-generator/access'
import { findDetailProject, listDetailAssets, findDetailAsset } from '@/lib/detail-generator/repository'
import { readDetailFile, saveDetailFile, deleteDetailFile } from '@/lib/detail-generator/storage'
import { desktopAssert, desktopAuthorization, desktopId, desktopProjectBody, DesktopClientError, DESKTOP_CLIENT_ID, DESKTOP_SCOPES } from './contract.mjs'
@ -11,10 +10,16 @@ const db = prisma
type Row = Record<string, any>
const columns = 'id,owner_id AS "ownerId",source_web_id AS "sourceWebId",document,conversation,composer,status,revision,updated_at AS "updatedAt"'
export async function desktopUser(id: string) {
await assertDetailGeneratorUser(id)
const user = await prisma.user.findUnique({ where: { id }, select: { id: true, username: true, realName: true } })
desktopAssert(user, '账户不存在', 401)
return { id, name: user!.realName || user!.username }
desktopAssert(id, '请先登录', 401, 'unauthenticated')
desktopAssert(process.env.DETAIL_GENERATOR_ENABLED === 'true', '小壹客户端服务尚未启用', 403, 'feature_disabled')
const user = await db.user.findUnique({
where: { id },
select: { username: true, realName: true, status: true, isActive: true, archivedAt: true },
})
const status = user?.status || (user?.isActive ? 'active' : 'pending')
if (!user?.isActive || status !== 'active' || user.archivedAt)
throw new DesktopClientError('账号当前不可用,无法授权小壹客户端', 403, 'account_inactive')
return { id, name: user.realName || user.username }
}
export async function approveDesktop(ownerId: string, input: Record<string, unknown>) {
await desktopUser(ownerId)