Skip to content

Commit

Permalink
🐛 fix: if enable login and not signed in, return unauthorized error (#…
Browse files Browse the repository at this point in the history
…4571)

* if enable login and not signed in, return unauthorized error

* if enable login and not signed in, return unauthorized error

* if enable login and not signed in, return unauthorized error

* if enable login and not signed in, return unauthorized error, add test method.

* if enable login and not signed in, return unauthorized error, add test method.

* if enable login and not signed in, return unauthorized error, add test method.

* 修正引包。

* 改成mockImplementationOnce
  • Loading branch information
vual authored Nov 4, 2024
1 parent 74f83c1 commit e00c90e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
49 changes: 45 additions & 4 deletions src/services/__tests__/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { LobeChatPluginManifest } from '@lobehub/chat-plugin-sdk';
import { act } from '@testing-library/react';
import { merge } from 'lodash-es';
import OpenAI from 'openai';
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { getAppConfig } from '@/config/app';
import { getServerDBConfig } from '@/config/db';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import {
LobeAnthropicAI,
Expand All @@ -28,7 +26,6 @@ import {
ModelProvider,
} from '@/libs/agent-runtime';
import { AgentRuntime } from '@/libs/agent-runtime';
import { useFileStore } from '@/store/file';
import { useToolStore } from '@/store/tool';
import { UserStore } from '@/store/user';
import { UserSettingsState, initialSettingsState } from '@/store/user/slices/settings/initialState';
Expand All @@ -38,6 +35,8 @@ import { ChatStreamPayload, type OpenAIChatMessage } from '@/types/openai/chat';
import { LobeTool } from '@/types/tool';

import { chatService, initializeWithClientStore } from '../chat';
import { useUserStore } from '@/store/user';
import {modelConfigSelectors} from "@/store/user/selectors";

// Mocking external dependencies
vi.mock('i18next', () => ({
Expand Down Expand Up @@ -524,6 +523,48 @@ describe('ChatService', () => {
});
});

it('should throw InvalidAccessCode error when enableFetchOnClient is true and auth is enabled but user is not signed in', async () => {
// Mock userStore
const mockUserStore = {
enableAuth: () => true,
isSignedIn: false,
};

// Mock modelConfigSelectors
const mockModelConfigSelectors = {
isProviderFetchOnClient: () => () => true,
};

vi.spyOn(useUserStore, 'getState').mockImplementationOnce(() => mockUserStore as any);
vi.spyOn(modelConfigSelectors, 'isProviderFetchOnClient').mockImplementationOnce(mockModelConfigSelectors.isProviderFetchOnClient);

const params: Partial<ChatStreamPayload> = {
model: 'test-model',
messages: [],
};
const options = {};
const expectedPayload = {
model: DEFAULT_AGENT_CONFIG.model,
stream: true,
...DEFAULT_AGENT_CONFIG.params,
...params,
};

const result = await chatService.getChatCompletion(params,options);

expect(global.fetch).toHaveBeenCalledWith(
expect.any(String),
{
body: JSON.stringify(expectedPayload),
headers: expect.objectContaining({
'Content-Type': 'application/json',
}),
method: 'POST',
},
);
expect(result.status).toBe(401);
});

// Add more test cases to cover different scenarios and edge cases
});

Expand Down
10 changes: 9 additions & 1 deletion src/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { INBOX_SESSION_ID } from '@/const/session';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { TracePayload, TraceTagMap } from '@/const/trace';
import { isServerMode } from '@/const/version';
import { AgentRuntime, ChatCompletionErrorPayload, ModelProvider } from '@/libs/agent-runtime';
import {AgentRuntime, AgentRuntimeError, ChatCompletionErrorPayload, ModelProvider} from '@/libs/agent-runtime';
import { filesPrompts } from '@/prompts/files';
import { useSessionStore } from '@/store/session';
import { sessionMetaSelectors } from '@/store/session/selectors';
Expand Down Expand Up @@ -533,6 +533,14 @@ class ChatService {
const agentRuntime = await initializeWithClientStore(params.provider, params.payload);
const data = params.payload as ChatStreamPayload;

/**
* if enable login and not signed in, return unauthorized error
*/
const userStore = useUserStore.getState();
if (userStore.enableAuth() && !userStore.isSignedIn) {
throw AgentRuntimeError.createError(ChatErrorType.InvalidAccessCode);
}

return agentRuntime.chat(data, { signal: params.signal });
};

Expand Down

0 comments on commit e00c90e

Please sign in to comment.