fix api config auth and image model compatibility
Some checks failed
Build & Push Docker Image / build-and-push (push) Has been cancelled
Some checks failed
Build & Push Docker Image / build-and-push (push) Has been cancelled
This commit is contained in:
@@ -34,6 +34,9 @@ type SavedProvider = {
|
||||
}
|
||||
|
||||
const prismaMock = vi.hoisted(() => ({
|
||||
user: {
|
||||
findUnique: vi.fn<(...args: unknown[]) => Promise<{ id: string } | null>>(),
|
||||
},
|
||||
userPreference: {
|
||||
findUnique: vi.fn<(...args: unknown[]) => Promise<UserPreferenceSnapshot | null>>(),
|
||||
upsert: vi.fn<(...args: unknown[]) => Promise<unknown>>(),
|
||||
@@ -103,6 +106,7 @@ describe('api specific - user api-config PUT provider uniqueness', () => {
|
||||
vi.clearAllMocks()
|
||||
resetAuthMockState()
|
||||
|
||||
prismaMock.user.findUnique.mockResolvedValue({ id: 'user-1' })
|
||||
prismaMock.userPreference.findUnique.mockResolvedValue({
|
||||
customProviders: null,
|
||||
customModels: null,
|
||||
@@ -111,6 +115,37 @@ describe('api specific - user api-config PUT provider uniqueness', () => {
|
||||
getBillingModeMock.mockResolvedValue('OFF')
|
||||
})
|
||||
|
||||
it('returns unauthorized when session user no longer exists before saving providers', async () => {
|
||||
installAuthMocks()
|
||||
mockAuthenticated('user-missing')
|
||||
prismaMock.user.findUnique.mockResolvedValueOnce(null)
|
||||
const route = await import('@/app/api/user/api-config/route')
|
||||
|
||||
const req = buildMockRequest({
|
||||
path: '/api/user/api-config',
|
||||
method: 'PUT',
|
||||
body: {
|
||||
providers: [
|
||||
{ id: 'openai-compatible:oa-1', name: 'OpenAI A', baseUrl: 'https://oa-a.test', apiKey: 'oa-key-a' },
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const res = await route.PUT(req, routeContext)
|
||||
const body = await res.json() as {
|
||||
error?: {
|
||||
code?: string
|
||||
message?: string
|
||||
}
|
||||
}
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
expect(body.error?.code).toBe('UNAUTHORIZED')
|
||||
expect(body.error?.message).toBe('登录状态已失效,请重新登录')
|
||||
expect(prismaMock.userPreference.findUnique).not.toHaveBeenCalled()
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('allows multiple providers with the same api type when provider ids differ', async () => {
|
||||
installAuthMocks()
|
||||
mockAuthenticated('user-1')
|
||||
|
||||
@@ -9,6 +9,9 @@ const authMock = vi.hoisted(() => ({
|
||||
}))
|
||||
|
||||
const prismaMock = vi.hoisted(() => ({
|
||||
user: {
|
||||
findUnique: vi.fn<(...args: unknown[]) => Promise<{ id: string } | null>>(),
|
||||
},
|
||||
userPreference: {
|
||||
upsert: vi.fn(async () => ({
|
||||
userId: 'user-1',
|
||||
@@ -25,6 +28,7 @@ describe('api specific - user preference art style validation', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
prismaMock.user.findUnique.mockResolvedValue({ id: 'user-1' })
|
||||
})
|
||||
|
||||
it('accepts valid artStyle and persists normalized value', async () => {
|
||||
@@ -58,4 +62,27 @@ describe('api specific - user preference art style validation', () => {
|
||||
expect(body.error.code).toBe('INVALID_PARAMS')
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns unauthorized when session user record is missing', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValueOnce(null)
|
||||
const mod = await import('@/app/api/user-preference/route')
|
||||
const req = buildMockRequest({
|
||||
path: '/api/user-preference',
|
||||
method: 'PATCH',
|
||||
body: { artStyle: 'realistic' },
|
||||
})
|
||||
|
||||
const res = await mod.PATCH(req, routeContext)
|
||||
const body = await res.json() as {
|
||||
error?: {
|
||||
code?: string
|
||||
message?: string
|
||||
}
|
||||
}
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
expect(body.error?.code).toBe('UNAUTHORIZED')
|
||||
expect(body.error?.message).toBe('登录状态已失效,请重新登录')
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
98
tests/regression/user-session-persistence-guard.test.ts
Normal file
98
tests/regression/user-session-persistence-guard.test.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { buildMockRequest } from '../helpers/request'
|
||||
import {
|
||||
installAuthMocks,
|
||||
mockAuthenticated,
|
||||
resetAuthMockState,
|
||||
} from '../helpers/auth'
|
||||
|
||||
const prismaMock = vi.hoisted(() => ({
|
||||
user: {
|
||||
findUnique: vi.fn<(...args: unknown[]) => Promise<{ id: string } | null>>(),
|
||||
},
|
||||
userPreference: {
|
||||
findUnique: vi.fn<(...args: unknown[]) => Promise<{ customProviders: null; customModels: null } | null>>(),
|
||||
upsert: vi.fn<(...args: unknown[]) => Promise<unknown>>(),
|
||||
},
|
||||
}))
|
||||
|
||||
const getBillingModeMock = vi.hoisted(() => vi.fn(async () => 'OFF'))
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
prisma: prismaMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/billing/mode', () => ({
|
||||
getBillingMode: getBillingModeMock,
|
||||
}))
|
||||
|
||||
describe('regression - persisted session user guard', () => {
|
||||
const routeContext = { params: Promise.resolve({}) }
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetModules()
|
||||
vi.clearAllMocks()
|
||||
resetAuthMockState()
|
||||
installAuthMocks()
|
||||
mockAuthenticated('user-missing')
|
||||
prismaMock.user.findUnique.mockResolvedValue(null)
|
||||
prismaMock.userPreference.findUnique.mockResolvedValue({
|
||||
customProviders: null,
|
||||
customModels: null,
|
||||
})
|
||||
prismaMock.userPreference.upsert.mockResolvedValue({ id: 'pref-1' })
|
||||
getBillingModeMock.mockResolvedValue('OFF')
|
||||
})
|
||||
|
||||
it('api-config PUT returns unauthorized before touching userPreference when session user row is missing', async () => {
|
||||
const route = await import('@/app/api/user/api-config/route')
|
||||
const req = buildMockRequest({
|
||||
path: '/api/user/api-config',
|
||||
method: 'PUT',
|
||||
body: {
|
||||
providers: [
|
||||
{
|
||||
id: 'openai-compatible:oa-1',
|
||||
name: 'OpenAI A',
|
||||
baseUrl: 'https://oa-a.test',
|
||||
apiKey: 'oa-key-a',
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const res = await route.PUT(req, routeContext)
|
||||
const body = await res.json() as { error?: { code?: string; message?: string } }
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
expect(body.error?.code).toBe('UNAUTHORIZED')
|
||||
expect(body.error?.message).toBe('登录状态已失效,请重新登录')
|
||||
expect(prismaMock.user.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: 'user-missing' },
|
||||
select: { id: true },
|
||||
})
|
||||
expect(prismaMock.userPreference.findUnique).not.toHaveBeenCalled()
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('user-preference PATCH returns unauthorized before upsert when session user row is missing', async () => {
|
||||
const route = await import('@/app/api/user-preference/route')
|
||||
const req = buildMockRequest({
|
||||
path: '/api/user-preference',
|
||||
method: 'PATCH',
|
||||
body: { artStyle: 'realistic' },
|
||||
})
|
||||
|
||||
const res = await route.PATCH(req, routeContext)
|
||||
const body = await res.json() as { error?: { code?: string; message?: string } }
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
expect(body.error?.code).toBe('UNAUTHORIZED')
|
||||
expect(body.error?.message).toBe('登录状态已失效,请重新登录')
|
||||
expect(prismaMock.user.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: 'user-missing' },
|
||||
select: { id: true },
|
||||
})
|
||||
expect(prismaMock.userPreference.upsert).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -118,6 +118,29 @@ describe('generator-api gateway routing', () => {
|
||||
expect(result).toEqual({ success: true, imageUrl: 'compat-template-image' })
|
||||
})
|
||||
|
||||
it('normalizes legacy openai-compatible gemini image model ids before template gateway call', async () => {
|
||||
resolveModelSelectionMock.mockResolvedValueOnce({
|
||||
provider: 'openai-compatible:oa-1',
|
||||
modelId: 'gemini-3.1-flash-image',
|
||||
modelKey: 'openai-compatible:oa-1::gemini-3.1-flash-image',
|
||||
mediaType: 'image',
|
||||
compatMediaTemplate: {
|
||||
version: 1,
|
||||
mediaType: 'image',
|
||||
mode: 'sync',
|
||||
create: { method: 'POST', path: '/v1/images/generations' },
|
||||
response: { outputUrlPath: '$.data[0].url' },
|
||||
},
|
||||
})
|
||||
resolveModelGatewayRouteMock.mockReturnValueOnce('openai-compat')
|
||||
|
||||
await generateImage('user-1', 'openai-compatible:oa-1::gemini-3.1-flash-image', 'draw hero')
|
||||
|
||||
expect(generateImageViaOpenAICompatTemplateMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
modelId: 'gemini-3.1-flash-image-preview',
|
||||
}))
|
||||
})
|
||||
|
||||
it('routes official image requests to provider generator', async () => {
|
||||
resolveModelSelectionMock.mockResolvedValueOnce({
|
||||
provider: 'google',
|
||||
|
||||
Reference in New Issue
Block a user