API版本化:迁移会话调用入口
This commit is contained in:
1 parent
6ff3499ca9
commit
42cc9ce675
9 files changed
+145
-12
No files matched your search
@@ -39,7 +39,7 @@ import {
|
||||
sendChatMessage,
|
||||
markChatRead,
|
||||
updateAccountTaskSettings,
|
||||
runAccountTask,
|
||||
runAccountTask, login, initializeAdmin, verifySession,
|
||||
} from './api';
|
||||
|
||||
afterEach(() => {
|
||||
@@ -303,7 +303,7 @@ test('logout calls backend session invalidation route', async () => {
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
await logout();
|
||||
expect(fetchMock).toHaveBeenCalledWith('/logout', expect.objectContaining({
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/v1/session/logout', expect.objectContaining({
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
}));
|
||||
@@ -740,3 +740,26 @@ test('updateShippingRule posts review request text action without card requireme
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
// 会话 API 使用版本化兼容入口。
|
||||
const runVersionedSessionAPITest = async () => {
|
||||
// fetchMock 是会话 API 请求的测试替身。
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }))
|
||||
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }))
|
||||
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }))
|
||||
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
await login({ username: 'admin', password: 'pw' });
|
||||
await initializeAdmin('long-password');
|
||||
await verifySession();
|
||||
await logout();
|
||||
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/session/login', expect.objectContaining({ method: 'POST' }));
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/session/initialize', expect.objectContaining({ method: 'POST' }));
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/session', expect.objectContaining({ method: 'GET' }));
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/session/logout', expect.objectContaining({ method: 'POST' }));
|
||||
};
|
||||
|
||||
test('session APIs use versioned compatibility routes', runVersionedSessionAPITest);
|
||||
@@ -19,19 +19,19 @@ const normalizeSettings = (settings: Record<string, any>): SystemSettings => {
|
||||
|
||||
// Auth
|
||||
export const login = async (data: { username?: string; password?: string; email?: string; verification_code?: string }): Promise<LoginResponse> => {
|
||||
return post('/login', data, { skipAuthLogout: true });
|
||||
return post('/api/v1/session/login', data, { skipAuthLogout: true });
|
||||
};
|
||||
|
||||
export const initializeAdmin = async (password: string): Promise<LoginResponse> => {
|
||||
return post('/initialize', { password }, { skipAuthLogout: true });
|
||||
return post('/api/v1/session/initialize', { password }, { skipAuthLogout: true });
|
||||
};
|
||||
|
||||
export const verifySession = async (): Promise<{ authenticated: boolean; initialized?: boolean; user_id?: number; username?: string; is_admin?: boolean }> => {
|
||||
return get('/verify');
|
||||
return get('/api/v1/session');
|
||||
};
|
||||
|
||||
export const logout = async (): Promise<ApiResponse> => {
|
||||
return post('/logout', {});
|
||||
return post('/api/v1/session/logout', {});
|
||||
};
|
||||
|
||||
export const changePassword = async (currentPassword: string, newPassword: string): Promise<ApiResponse> => {
|
||||
|
||||
Reference in new issue
Block a user