mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-08-31 17:03:24 +00:00
test: remove unrelated suite stabilization
This commit is contained in:
parent
affd0194b2
commit
16d5fa63b3
10 changed files with 140 additions and 197 deletions
|
|
@ -27,27 +27,23 @@ describe('Convos Routes', () => {
|
|||
deleteConvoSharedLinksWithCleanup,
|
||||
} = require('@librechat/api');
|
||||
|
||||
const createApp = (userId = 'test-user-123') => {
|
||||
const testApp = express();
|
||||
testApp.use(express.json());
|
||||
beforeAll(() => {
|
||||
convosRouter = require('../convos');
|
||||
|
||||
app = express();
|
||||
app.use(express.json());
|
||||
|
||||
/** Mock authenticated user */
|
||||
testApp.use((req, res, next) => {
|
||||
req.user = { id: userId };
|
||||
app.use((req, res, next) => {
|
||||
req.user = { id: 'test-user-123' };
|
||||
next();
|
||||
});
|
||||
|
||||
testApp.use('/api/convos', convosRouter);
|
||||
return testApp;
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
convosRouter = require('../convos');
|
||||
app.use('/api/convos', convosRouter);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
app = createApp();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('DELETE /all', () => {
|
||||
|
|
@ -150,7 +146,13 @@ describe('Convos Routes', () => {
|
|||
jest.clearAllMocks();
|
||||
|
||||
/** Second user (simulate different user by modifying middleware) */
|
||||
const app2 = createApp('test-user-456');
|
||||
const app2 = express();
|
||||
app2.use(express.json());
|
||||
app2.use((req, res, next) => {
|
||||
req.user = { id: 'test-user-456' };
|
||||
next();
|
||||
});
|
||||
app2.use('/api/convos', require('../convos'));
|
||||
|
||||
deleteConvos.mockResolvedValue({ deletedCount: 7 });
|
||||
deleteToolCalls.mockResolvedValue({ deletedCount: 12 });
|
||||
|
|
|
|||
|
|
@ -25,16 +25,6 @@ jest.mock('@librechat/api', () => ({
|
|||
GenerationJobManager: mockGenerationJobManager,
|
||||
}));
|
||||
|
||||
jest.mock('@librechat/api/telemetry', () => ({
|
||||
createSseStreamTelemetry: jest.fn(() => ({
|
||||
recordHeadersFlushed: jest.fn(),
|
||||
recordWrite: jest.fn(),
|
||||
recordFinalEventEmitted: jest.fn(),
|
||||
recordErrorEventEmitted: jest.fn(),
|
||||
recordSubscribeFailed: jest.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
jest.mock('~/models', () => ({
|
||||
saveMessage: jest.fn(),
|
||||
}));
|
||||
|
|
@ -77,8 +67,7 @@ function mockSubscribeSuccess() {
|
|||
|
||||
describe('SSE stream tenant isolation', () => {
|
||||
beforeEach(() => {
|
||||
Object.values(mockGenerationJobManager).forEach((mock) => mock.mockReset());
|
||||
mockGenerationJobManager.getActiveJobIdsForUser.mockResolvedValue([]);
|
||||
jest.clearAllMocks();
|
||||
mockUserId = 'user-123';
|
||||
mockTenantId = undefined;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -11,15 +11,14 @@ const mockLogger = {
|
|||
debug: jest.fn(),
|
||||
};
|
||||
|
||||
let mockOpenIDCallbackAuthenticatorOptions;
|
||||
const mockOAuthHandler = jest.fn((_req, res) => res.status(204).end());
|
||||
const mockOpenIDCallbackMiddleware = jest.fn((_req, _res, next) => next());
|
||||
const setMockOpenIDCallbackAuthenticator = (options) => {
|
||||
let mockOpenIDCallbackAuthenticatorOptions;
|
||||
const mockCreateOpenIDCallbackAuthenticator = jest.fn((options) => {
|
||||
mockOpenIDCallbackAuthenticatorOptions = options;
|
||||
return mockOpenIDCallbackMiddleware;
|
||||
};
|
||||
const mockCreateOpenIDCallbackAuthenticator = jest.fn(setMockOpenIDCallbackAuthenticator);
|
||||
const buildMockOAuthFailureLog = ({ provider, req, err, info, defaultMessage }) => ({
|
||||
});
|
||||
const mockBuildOAuthFailureLog = jest.fn(({ provider, req, err, info, defaultMessage }) => ({
|
||||
provider,
|
||||
code: err?.code ?? info?.code ?? info?.error ?? req.query?.error,
|
||||
name: err?.name ?? info?.name,
|
||||
|
|
@ -38,17 +37,17 @@ const buildMockOAuthFailureLog = ({ provider, req, err, info, defaultMessage })
|
|||
path: req.path,
|
||||
forwarded_for: req.headers?.['x-forwarded-for'],
|
||||
user_agent: req.headers?.['user-agent'],
|
||||
});
|
||||
const mockBuildOAuthFailureLog = jest.fn(buildMockOAuthFailureLog);
|
||||
const getMockOAuthFailureMessage = (req) =>
|
||||
req.session?.messages?.pop() ??
|
||||
req.query?.error_description ??
|
||||
req.query?.error ??
|
||||
'OAuth authentication failed';
|
||||
const mockGetOAuthFailureMessage = jest.fn(getMockOAuthFailureMessage);
|
||||
const redirectMockToAuthFailure = (res, { clientDomain, authFailedError }) =>
|
||||
res.redirect(`${clientDomain}/login?redirect=false&error=${authFailedError}`);
|
||||
const mockRedirectToAuthFailure = jest.fn(redirectMockToAuthFailure);
|
||||
}));
|
||||
const mockGetOAuthFailureMessage = jest.fn(
|
||||
(req) =>
|
||||
req.session?.messages?.pop() ??
|
||||
req.query?.error_description ??
|
||||
req.query?.error ??
|
||||
'OAuth authentication failed',
|
||||
);
|
||||
const mockRedirectToAuthFailure = jest.fn((res, { clientDomain, authFailedError }) =>
|
||||
res.redirect(`${clientDomain}/login?redirect=false&error=${authFailedError}`),
|
||||
);
|
||||
const mockPassportAuthenticate = jest.fn(() => (_req, _res, next) => next());
|
||||
|
||||
jest.mock('passport', () => ({
|
||||
|
|
@ -106,11 +105,8 @@ afterAll(() => {
|
|||
});
|
||||
|
||||
function getOAuthRouter() {
|
||||
let router;
|
||||
jest.isolateModules(() => {
|
||||
router = require('./oauth');
|
||||
});
|
||||
return router;
|
||||
jest.resetModules();
|
||||
return require('./oauth');
|
||||
}
|
||||
|
||||
function createApp(sessionMessages) {
|
||||
|
|
@ -130,17 +126,19 @@ function createApp(sessionMessages) {
|
|||
|
||||
describe('OAuth route failure logging', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
jest.clearAllMocks();
|
||||
process.env.DOMAIN_CLIENT = 'http://client.test';
|
||||
mockLogger.warn.mockClear();
|
||||
mockLogger.error.mockClear();
|
||||
mockLogger.info.mockClear();
|
||||
mockLogger.debug.mockClear();
|
||||
mockOAuthHandler.mockClear();
|
||||
mockOpenIDCallbackMiddleware.mockClear();
|
||||
mockBuildOAuthFailureLog.mockClear();
|
||||
mockGetOAuthFailureMessage.mockClear();
|
||||
mockRedirectToAuthFailure.mockClear();
|
||||
mockPassportAuthenticate.mockClear();
|
||||
mockOpenIDCallbackAuthenticatorOptions = undefined;
|
||||
mockPassportAuthenticate.mockImplementation(() => (_req, _res, next) => next());
|
||||
mockCreateOpenIDCallbackAuthenticator.mockImplementation(setMockOpenIDCallbackAuthenticator);
|
||||
mockOpenIDCallbackMiddleware.mockImplementation((_req, _res, next) => next());
|
||||
mockOAuthHandler.mockImplementation((_req, res) => res.status(204).end());
|
||||
mockBuildOAuthFailureLog.mockImplementation(buildMockOAuthFailureLog);
|
||||
mockGetOAuthFailureMessage.mockImplementation(getMockOAuthFailureMessage);
|
||||
mockRedirectToAuthFailure.mockImplementation(redirectMockToAuthFailure);
|
||||
});
|
||||
|
||||
it('wires the package OpenID callback middleware into the route', async () => {
|
||||
|
|
|
|||
|
|
@ -56,10 +56,6 @@ function setTestUser(app, user) {
|
|||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
if (mongoose.connection.readyState !== 0) {
|
||||
await mongoose.disconnect();
|
||||
}
|
||||
|
||||
mongoServer = await MongoMemoryServer.create();
|
||||
const mongoUri = mongoServer.getUri();
|
||||
await mongoose.connect(mongoUri);
|
||||
|
|
@ -101,10 +97,8 @@ beforeAll(async () => {
|
|||
// Set default user
|
||||
currentTestUser = testUsers.owner;
|
||||
|
||||
// Import routes after middleware is set up and test-local mocks are registered.
|
||||
jest.isolateModules(() => {
|
||||
promptRoutes = require('./prompts');
|
||||
});
|
||||
// Import routes after middleware is set up
|
||||
promptRoutes = require('./prompts');
|
||||
app.use('/api/prompts', promptRoutes);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,33 +1,45 @@
|
|||
const dataSchemasMock = {
|
||||
logger: { info: jest.fn(), warn: jest.fn(), debug: jest.fn(), error: jest.fn() },
|
||||
getTenantId: jest.fn(() => undefined),
|
||||
DEFAULT_SESSION_EXPIRY: 900000,
|
||||
DEFAULT_REFRESH_TOKEN_EXPIRY: 604800000,
|
||||
};
|
||||
const dataProviderMock = {
|
||||
ErrorTypes: {},
|
||||
SystemRoles: { USER: 'USER', ADMIN: 'ADMIN' },
|
||||
errorsToString: jest.fn(),
|
||||
};
|
||||
const apiMock = {
|
||||
isEnabled: jest.fn((val) => val === 'true' || val === true),
|
||||
checkEmailConfig: jest.fn(),
|
||||
isEmailDomainAllowed: jest.fn(),
|
||||
math: jest.fn((val, fallback) => (val ? Number(val) : fallback)),
|
||||
shouldUseSecureCookie: jest.fn(() => false),
|
||||
resolveAppConfigForUser: jest.fn(async (_getAppConfig, _user) => ({})),
|
||||
setCloudFrontCookies: jest.fn(() => true),
|
||||
getCloudFrontConfig: jest.fn(() => ({
|
||||
domain: 'https://cdn.example.com',
|
||||
imageSigning: 'cookies',
|
||||
cookieDomain: '.example.com',
|
||||
privateKey: 'test-private-key',
|
||||
keyPairId: 'K123ABC',
|
||||
})),
|
||||
parseCloudFrontCookieScope: jest.fn(() => null),
|
||||
CLOUDFRONT_SCOPE_COOKIE: 'LibreChat-CloudFront-Scope',
|
||||
};
|
||||
const modelsMock = {
|
||||
jest.mock(
|
||||
'@librechat/data-schemas',
|
||||
() => ({
|
||||
logger: { info: jest.fn(), warn: jest.fn(), debug: jest.fn(), error: jest.fn() },
|
||||
getTenantId: jest.fn(() => undefined),
|
||||
DEFAULT_SESSION_EXPIRY: 900000,
|
||||
DEFAULT_REFRESH_TOKEN_EXPIRY: 604800000,
|
||||
}),
|
||||
{ virtual: true },
|
||||
);
|
||||
jest.mock(
|
||||
'librechat-data-provider',
|
||||
() => ({
|
||||
ErrorTypes: {},
|
||||
SystemRoles: { USER: 'USER', ADMIN: 'ADMIN' },
|
||||
errorsToString: jest.fn(),
|
||||
}),
|
||||
{ virtual: true },
|
||||
);
|
||||
jest.mock(
|
||||
'@librechat/api',
|
||||
() => ({
|
||||
isEnabled: jest.fn((val) => val === 'true' || val === true),
|
||||
checkEmailConfig: jest.fn(),
|
||||
isEmailDomainAllowed: jest.fn(),
|
||||
math: jest.fn((val, fallback) => (val ? Number(val) : fallback)),
|
||||
shouldUseSecureCookie: jest.fn(() => false),
|
||||
resolveAppConfigForUser: jest.fn(async (_getAppConfig, _user) => ({})),
|
||||
setCloudFrontCookies: jest.fn(() => true),
|
||||
getCloudFrontConfig: jest.fn(() => ({
|
||||
domain: 'https://cdn.example.com',
|
||||
imageSigning: 'cookies',
|
||||
cookieDomain: '.example.com',
|
||||
privateKey: 'test-private-key',
|
||||
keyPairId: 'K123ABC',
|
||||
})),
|
||||
parseCloudFrontCookieScope: jest.fn(() => null),
|
||||
CLOUDFRONT_SCOPE_COOKIE: 'LibreChat-CloudFront-Scope',
|
||||
}),
|
||||
{ virtual: true },
|
||||
);
|
||||
jest.mock('~/models', () => ({
|
||||
findUser: jest.fn(),
|
||||
findToken: jest.fn(),
|
||||
createUser: jest.fn(),
|
||||
|
|
@ -42,8 +54,8 @@ const modelsMock = {
|
|||
generateToken: jest.fn(),
|
||||
deleteUserById: jest.fn(),
|
||||
generateRefreshToken: jest.fn(),
|
||||
};
|
||||
const validatorsMock = {
|
||||
}));
|
||||
jest.mock('~/strategies/validators', () => ({
|
||||
registerSchema: {
|
||||
safeParse: jest.fn((user) => ({
|
||||
success: true,
|
||||
|
|
@ -56,18 +68,9 @@ const validatorsMock = {
|
|||
},
|
||||
})),
|
||||
},
|
||||
};
|
||||
const configMock = { getAppConfig: jest.fn() };
|
||||
const serverUtilsMock = { sendEmail: jest.fn() };
|
||||
|
||||
jest.resetModules();
|
||||
jest.doMock('@librechat/data-schemas', () => dataSchemasMock);
|
||||
jest.doMock('librechat-data-provider', () => dataProviderMock);
|
||||
jest.doMock('@librechat/api', () => apiMock);
|
||||
jest.doMock('~/models', () => modelsMock);
|
||||
jest.doMock('~/strategies/validators', () => validatorsMock);
|
||||
jest.doMock('~/server/services/Config', () => configMock);
|
||||
jest.doMock('~/server/utils', () => serverUtilsMock);
|
||||
}));
|
||||
jest.mock('~/server/services/Config', () => ({ getAppConfig: jest.fn() }));
|
||||
jest.mock('~/server/utils', () => ({ sendEmail: jest.fn() }));
|
||||
|
||||
const {
|
||||
checkEmailConfig,
|
||||
|
|
@ -77,9 +80,9 @@ const {
|
|||
setCloudFrontCookies,
|
||||
getCloudFrontConfig,
|
||||
parseCloudFrontCookieScope,
|
||||
} = apiMock;
|
||||
} = require('@librechat/api');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { logger, getTenantId } = dataSchemasMock;
|
||||
const { logger, getTenantId } = require('@librechat/data-schemas');
|
||||
const {
|
||||
findUser,
|
||||
findToken,
|
||||
|
|
@ -92,9 +95,9 @@ const {
|
|||
createSession,
|
||||
createToken,
|
||||
deleteTokens,
|
||||
} = modelsMock;
|
||||
const { getAppConfig } = configMock;
|
||||
const { sendEmail } = serverUtilsMock;
|
||||
} = require('~/models');
|
||||
const { getAppConfig } = require('~/server/services/Config');
|
||||
const { sendEmail } = require('~/server/utils');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const {
|
||||
setOpenIDAuthTokens,
|
||||
|
|
@ -127,36 +130,6 @@ function mockRequest(sessionData = {}, cookies = {}) {
|
|||
};
|
||||
}
|
||||
|
||||
const originalEnv = { ...process.env };
|
||||
const defaultCloudFrontCookieConfig = {
|
||||
domain: 'https://cdn.example.com',
|
||||
imageSigning: 'cookies',
|
||||
cookieDomain: '.example.com',
|
||||
privateKey: 'test-private-key',
|
||||
keyPairId: 'K123ABC',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
JWT_REFRESH_SECRET: 'test-refresh-secret',
|
||||
OPENID_REUSE_TOKENS: 'true',
|
||||
};
|
||||
checkEmailConfig.mockReturnValue(false);
|
||||
shouldUseSecureCookie.mockReturnValue(false);
|
||||
isEmailDomainAllowed.mockReturnValue(true);
|
||||
resolveAppConfigForUser.mockResolvedValue({});
|
||||
setCloudFrontCookies.mockReturnValue(true);
|
||||
getCloudFrontConfig.mockReturnValue(defaultCloudFrontCookieConfig);
|
||||
parseCloudFrontCookieScope.mockReturnValue(null);
|
||||
getTenantId.mockReturnValue(undefined);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
describe('setOpenIDAuthTokens', () => {
|
||||
const env = process.env;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const express = require('express');
|
||||
const request = require('supertest');
|
||||
const zlib = require('zlib');
|
||||
const staticCache = require('../staticCache');
|
||||
|
||||
const binaryParser = (res, callback) => {
|
||||
const chunks = [];
|
||||
|
|
@ -18,9 +18,15 @@ describe('staticCache', () => {
|
|||
let indexFile;
|
||||
let manifestFile;
|
||||
let swFile;
|
||||
let staticCache;
|
||||
|
||||
const createTestFiles = () => {
|
||||
beforeAll(() => {
|
||||
// Create a test directory and files
|
||||
testDir = path.join(__dirname, 'test-static');
|
||||
if (!fs.existsSync(testDir)) {
|
||||
fs.mkdirSync(testDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Create test files
|
||||
testFile = path.join(testDir, 'test.js');
|
||||
indexFile = path.join(testDir, 'index.html');
|
||||
manifestFile = path.join(testDir, 'manifest.json');
|
||||
|
|
@ -56,10 +62,6 @@ describe('staticCache', () => {
|
|||
const distImagesDir = path.join(testDir, 'dist', 'images');
|
||||
fs.mkdirSync(distImagesDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(distImagesDir, 'logo.png'), 'fake-png-data');
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'librechat-static-cache-'));
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
|
@ -77,15 +79,6 @@ describe('staticCache', () => {
|
|||
delete process.env.STATIC_CACHE_S_MAX_AGE;
|
||||
delete process.env.STATIC_CACHE_MAX_AGE;
|
||||
delete process.env.ENABLE_STATIC_ASSET_BROTLI;
|
||||
|
||||
if (fs.existsSync(testDir)) {
|
||||
fs.rmSync(testDir, { recursive: true, force: true });
|
||||
}
|
||||
fs.mkdirSync(testDir, { recursive: true });
|
||||
createTestFiles();
|
||||
|
||||
jest.resetModules();
|
||||
staticCache = require('../staticCache');
|
||||
});
|
||||
describe('cache headers in production', () => {
|
||||
beforeEach(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue