mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* WIP: app.locals refactoring
WIP: appConfig
fix: update memory configuration retrieval to use getAppConfig based on user role
fix: update comment for AppConfig interface to clarify purpose
🏷️ refactor: Update tests to use getAppConfig for endpoint configurations
ci: Update AppService tests to initialize app config instead of app.locals
ci: Integrate getAppConfig into remaining tests
refactor: Update multer storage destination to use promise-based getAppConfig and improve error handling in tests
refactor: Rename initializeAppConfig to setAppConfig and update related tests
ci: Mock getAppConfig in various tests to provide default configurations
refactor: Update convertMCPToolsToPlugins to use mcpManager for server configuration and adjust related tests
chore: rename `Config/getAppConfig` -> `Config/app`
fix: streamline OpenAI image tools configuration by removing direct appConfig dependency and using function parameters
chore: correct parameter documentation for imageOutputType in ToolService.js
refactor: remove `getCustomConfig` dependency in config route
refactor: update domain validation to use appConfig for allowed domains
refactor: use appConfig registration property
chore: remove app parameter from AppService invocation
refactor: update AppConfig interface to correct registration and turnstile configurations
refactor: remove getCustomConfig dependency and use getAppConfig in PluginController, multer, and MCP services
refactor: replace getCustomConfig with getAppConfig in STTService, TTSService, and related files
refactor: replace getCustomConfig with getAppConfig in Conversation and Message models, update tempChatRetention functions to use AppConfig type
refactor: update getAppConfig calls in Conversation and Message models to include user role for temporary chat expiration
ci: update related tests
refactor: update getAppConfig call in getCustomConfigSpeech to include user role
fix: update appConfig usage to access allowedDomains from actions instead of registration
refactor: enhance AppConfig to include fileStrategies and update related file strategy logic
refactor: update imports to use normalizeEndpointName from @librechat/api and remove redundant definitions
chore: remove deprecated unused RunManager
refactor: get balance config primarily from appConfig
refactor: remove customConfig dependency for appConfig and streamline loadConfigModels logic
refactor: remove getCustomConfig usage and use app config in file citations
refactor: consolidate endpoint loading logic into loadEndpoints function
refactor: update appConfig access to use endpoints structure across various services
refactor: implement custom endpoints configuration and streamline endpoint loading logic
refactor: update getAppConfig call to include user role parameter
refactor: streamline endpoint configuration and enhance appConfig usage across services
refactor: replace getMCPAuthMap with getUserMCPAuthMap and remove unused getCustomConfig file
refactor: add type annotation for loadedEndpoints in loadEndpoints function
refactor: move /services/Files/images/parse to TS API
chore: add missing FILE_CITATIONS permission to IRole interface
refactor: restructure toolkits to TS API
refactor: separate manifest logic into its own module
refactor: consolidate tool loading logic into a new tools module for startup logic
refactor: move interface config logic to TS API
refactor: migrate checkEmailConfig to TypeScript and update imports
refactor: add FunctionTool interface and availableTools to AppConfig
refactor: decouple caching and DB operations from AppService, make part of consolidated `getAppConfig`
WIP: fix tests
* fix: rebase conflicts
* refactor: remove app.locals references
* refactor: replace getBalanceConfig with getAppConfig in various strategies and middleware
* refactor: replace appConfig?.balance with getBalanceConfig in various controllers and clients
* test: add balance configuration to titleConvo method in AgentClient tests
* chore: remove unused `openai-chat-tokens` package
* chore: remove unused imports in initializeMCPs.js
* refactor: update balance configuration to use getAppConfig instead of getBalanceConfig
* refactor: integrate configMiddleware for centralized configuration handling
* refactor: optimize email domain validation by removing unnecessary async calls
* refactor: simplify multer storage configuration by removing async calls
* refactor: reorder imports for better readability in user.js
* refactor: replace getAppConfig calls with req.config for improved performance
* chore: replace getAppConfig calls with req.config in tests for centralized configuration handling
* chore: remove unused override config
* refactor: add configMiddleware to endpoint route and replace getAppConfig with req.config
* chore: remove customConfig parameter from TTSService constructor
* refactor: pass appConfig from request to processFileCitations for improved configuration handling
* refactor: remove configMiddleware from endpoint route and retrieve appConfig directly in getEndpointsConfig if not in `req.config`
* test: add mockAppConfig to processFileCitations tests for improved configuration handling
* fix: pass req.config to hasCustomUserVars and call without await after synchronous refactor
* fix: type safety in useExportConversation
* refactor: retrieve appConfig using getAppConfig in PluginController and remove configMiddleware from plugins route, to avoid always retrieving when plugins are cached
* chore: change `MongoUser` typedef to `IUser`
* fix: Add `user` and `config` fields to ServerRequest and update JSDoc type annotations from Express.Request to ServerRequest
* fix: remove unused setAppConfig mock from Server configuration tests
443 lines
15 KiB
JavaScript
443 lines
15 KiB
JavaScript
// --- Mocks ---
|
|
jest.mock('tiktoken');
|
|
jest.mock('fs');
|
|
jest.mock('path');
|
|
jest.mock('node-fetch');
|
|
jest.mock('@node-saml/passport-saml');
|
|
jest.mock('@librechat/data-schemas', () => ({
|
|
logger: {
|
|
info: jest.fn(),
|
|
debug: jest.fn(),
|
|
error: jest.fn(),
|
|
},
|
|
hashToken: jest.fn().mockResolvedValue('hashed-token'),
|
|
}));
|
|
jest.mock('~/models', () => ({
|
|
findUser: jest.fn(),
|
|
createUser: jest.fn(),
|
|
updateUser: jest.fn(),
|
|
}));
|
|
jest.mock('~/server/services/Config', () => ({
|
|
config: {
|
|
registration: {
|
|
socialLogins: ['saml'],
|
|
},
|
|
},
|
|
getAppConfig: jest.fn().mockResolvedValue({}),
|
|
}));
|
|
jest.mock('@librechat/api', () => ({
|
|
getBalanceConfig: jest.fn(() => ({
|
|
tokenCredits: 1000,
|
|
startBalance: 1000,
|
|
})),
|
|
}));
|
|
jest.mock('~/server/services/Config/EndpointService', () => ({
|
|
config: {},
|
|
}));
|
|
jest.mock('~/server/services/Files/strategies', () => ({
|
|
getStrategyFunctions: jest.fn(() => ({
|
|
saveBuffer: jest.fn().mockResolvedValue('/fake/path/to/avatar.png'),
|
|
})),
|
|
}));
|
|
jest.mock('~/config/paths', () => ({
|
|
root: '/fake/root/path',
|
|
}));
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const fetch = require('node-fetch');
|
|
const { Strategy: SamlStrategy } = require('@node-saml/passport-saml');
|
|
const { setupSaml, getCertificateContent } = require('./samlStrategy');
|
|
|
|
// Configure fs mock
|
|
jest.mocked(fs).existsSync = jest.fn();
|
|
jest.mocked(fs).statSync = jest.fn();
|
|
jest.mocked(fs).readFileSync = jest.fn();
|
|
|
|
// To capture the verify callback from the strategy, we grab it from the mock constructor
|
|
let verifyCallback;
|
|
SamlStrategy.mockImplementation((options, verify) => {
|
|
verifyCallback = verify;
|
|
return { name: 'saml', options, verify };
|
|
});
|
|
|
|
describe('getCertificateContent', () => {
|
|
const certWithHeader = `-----BEGIN CERTIFICATE-----
|
|
MIIDazCCAlOgAwIBAgIUKhXaFJGJJPx466rlwYORIsqCq7MwDQYJKoZIhvcNAQEL
|
|
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
|
|
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNTAzMDQwODUxNTJaFw0yNjAz
|
|
MDQwODUxNTJaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
|
|
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
|
|
AQUAA4IBDwAwggEKAoIBAQCWP09NZg0xaRiLpNygCVgV3M+4RFW2S0c5X/fg/uFT
|
|
O5MfaVYzG5GxzhXzWRB8RtNPsxX/nlbPsoUroeHbz+SABkOsNEv6JuKRH4VXRH34
|
|
VzjazVkPAwj+N4WqsC/Wo4EGGpKIGeGi8Zed4yvMqoTyE3mrS19fY0nMHT62wUwS
|
|
GMm2pAQdAQePZ9WY7A5XOA1IoxW2Zh2Oxaf1p59epBkZDhoxSMu8GoSkvK27Km4A
|
|
4UXftzdg/wHNPrNirmcYouioHdmrOtYxPjrhUBQ74AmE1/QK45B6wEgirKH1A1AW
|
|
6C+ApLwpBMvy9+8Gbyvc8G18W3CjdEVKmAeWb9JUedSXAgMBAAGjUzBRMB0GA1Ud
|
|
DgQWBBRxpaqBx8VDLLc8IkHATujj8IOs6jAfBgNVHSMEGDAWgBRxpaqBx8VDLLc8
|
|
IkHATujj8IOs6jAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBc
|
|
Puk6i+yowwGccB3LhfxZ+Fz6s6/Lfx6bP/Hy4NYOxmx2/awGBgyfp1tmotjaS9Cf
|
|
FWd67LuEru4TYtz12RNMDBF5ypcEfibvb3I8O6igOSQX/Jl5D2pMChesZxhmCift
|
|
Qp09T41MA8PmHf1G9oMG0A3ZnjKDG5ebaJNRFImJhMHsgh/TP7V3uZy7YHTgopKX
|
|
Hv63V3Uo3Oihav29Q7urwmf7Ly7X7J2WE86/w3vRHi5dhaWWqEqxmnAXl+H+sG4V
|
|
meeVRI332bg1Nuy8KnnX8v3ZeJzMBkAhzvSr6Ri96R0/Un/oEFwVC5jDTq8sXVn6
|
|
u7wlOSk+oFzDIO/UILIA
|
|
-----END CERTIFICATE-----`;
|
|
|
|
const certWithoutHeader = certWithHeader
|
|
.replace(/-----BEGIN CERTIFICATE-----/g, '')
|
|
.replace(/-----END CERTIFICATE-----/g, '')
|
|
.replace(/\s+/g, '');
|
|
|
|
it('should throw an error if SAML_CERT is not set', () => {
|
|
process.env.SAML_CERT;
|
|
expect(() => getCertificateContent(process.env.SAML_CERT)).toThrow(
|
|
'Invalid input: SAML_CERT must be a string.',
|
|
);
|
|
});
|
|
|
|
it('should throw an error if SAML_CERT is empty', () => {
|
|
process.env.SAML_CERT = '';
|
|
expect(() => getCertificateContent(process.env.SAML_CERT)).toThrow(
|
|
'Invalid cert: SAML_CERT must be a valid file path or certificate string.',
|
|
);
|
|
});
|
|
|
|
it('should load cert from an environment variable if it is a single-line string(with header)', () => {
|
|
process.env.SAML_CERT = certWithHeader;
|
|
|
|
const actual = getCertificateContent(process.env.SAML_CERT);
|
|
expect(actual).toBe(certWithHeader);
|
|
});
|
|
|
|
it('should load cert from an environment variable if it is a single-line string(with no header)', () => {
|
|
process.env.SAML_CERT = certWithoutHeader;
|
|
|
|
const actual = getCertificateContent(process.env.SAML_CERT);
|
|
expect(actual).toBe(certWithoutHeader);
|
|
});
|
|
|
|
it('should throw an error if SAML_CERT is a single-line string (with header, no newline characters)', () => {
|
|
process.env.SAML_CERT = certWithHeader.replace(/\n/g, '');
|
|
expect(() => getCertificateContent(process.env.SAML_CERT)).toThrow(
|
|
'Invalid cert: SAML_CERT must be a valid file path or certificate string.',
|
|
);
|
|
});
|
|
|
|
it('should load cert from a relative file path if SAML_CERT is valid', () => {
|
|
process.env.SAML_CERT = 'test.pem';
|
|
const resolvedPath = '/absolute/path/to/test.pem';
|
|
|
|
path.isAbsolute.mockReturnValue(false);
|
|
path.join.mockReturnValue(resolvedPath);
|
|
path.normalize.mockReturnValue(resolvedPath);
|
|
|
|
fs.existsSync.mockReturnValue(true);
|
|
fs.statSync.mockReturnValue({ isFile: () => true });
|
|
fs.readFileSync.mockReturnValue(certWithHeader);
|
|
|
|
const actual = getCertificateContent(process.env.SAML_CERT);
|
|
expect(actual).toBe(certWithHeader);
|
|
});
|
|
|
|
it('should load cert from an absolute file path if SAML_CERT is valid', () => {
|
|
process.env.SAML_CERT = '/absolute/path/to/test.pem';
|
|
|
|
path.isAbsolute.mockReturnValue(true);
|
|
path.normalize.mockReturnValue(process.env.SAML_CERT);
|
|
|
|
fs.existsSync.mockReturnValue(true);
|
|
fs.statSync.mockReturnValue({ isFile: () => true });
|
|
fs.readFileSync.mockReturnValue(certWithHeader);
|
|
|
|
const actual = getCertificateContent(process.env.SAML_CERT);
|
|
expect(actual).toBe(certWithHeader);
|
|
});
|
|
|
|
it('should throw an error if the file does not exist', () => {
|
|
process.env.SAML_CERT = 'missing.pem';
|
|
const resolvedPath = '/absolute/path/to/missing.pem';
|
|
|
|
path.isAbsolute.mockReturnValue(false);
|
|
path.join.mockReturnValue(resolvedPath);
|
|
path.normalize.mockReturnValue(resolvedPath);
|
|
|
|
fs.existsSync.mockReturnValue(false);
|
|
|
|
expect(() => getCertificateContent(process.env.SAML_CERT)).toThrow(
|
|
'Invalid cert: SAML_CERT must be a valid file path or certificate string.',
|
|
);
|
|
});
|
|
|
|
it('should throw an error if the file is not readable', () => {
|
|
process.env.SAML_CERT = 'unreadable.pem';
|
|
const resolvedPath = '/absolute/path/to/unreadable.pem';
|
|
|
|
path.isAbsolute.mockReturnValue(false);
|
|
path.join.mockReturnValue(resolvedPath);
|
|
path.normalize.mockReturnValue(resolvedPath);
|
|
|
|
fs.existsSync.mockReturnValue(true);
|
|
fs.statSync.mockReturnValue({ isFile: () => true });
|
|
fs.readFileSync.mockImplementation(() => {
|
|
throw new Error('Permission denied');
|
|
});
|
|
|
|
expect(() => getCertificateContent(process.env.SAML_CERT)).toThrow(
|
|
'Error reading certificate file: Permission denied',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('setupSaml', () => {
|
|
// Helper to wrap the verify callback in a promise
|
|
const validate = (profile) =>
|
|
new Promise((resolve, reject) => {
|
|
verifyCallback(profile, (err, user, details) => {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
resolve({ user, details });
|
|
}
|
|
});
|
|
});
|
|
|
|
const baseProfile = {
|
|
nameID: 'saml-1234',
|
|
email: 'test@example.com',
|
|
given_name: 'First',
|
|
family_name: 'Last',
|
|
name: 'My Full Name',
|
|
username: 'flast',
|
|
picture: 'https://example.com/avatar.png',
|
|
custom_name: 'custom',
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
jest.clearAllMocks();
|
|
|
|
// Configure mocks
|
|
const { findUser, createUser, updateUser } = require('~/models');
|
|
findUser.mockResolvedValue(null);
|
|
createUser.mockImplementation(async (userData) => ({
|
|
_id: 'mock-user-id',
|
|
...userData,
|
|
}));
|
|
updateUser.mockImplementation(async (id, userData) => ({
|
|
_id: id,
|
|
...userData,
|
|
}));
|
|
|
|
const cert = `
|
|
-----BEGIN CERTIFICATE-----
|
|
MIIDazCCAlOgAwIBAgIUKhXaFJGJJPx466rlwYORIsqCq7MwDQYJKoZIhvcNAQEL
|
|
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
|
|
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNTAzMDQwODUxNTJaFw0yNjAz
|
|
MDQwODUxNTJaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
|
|
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
|
|
AQUAA4IBDwAwggEKAoIBAQCWP09NZg0xaRiLpNygCVgV3M+4RFW2S0c5X/fg/uFT
|
|
O5MfaVYzG5GxzhXzWRB8RtNPsxX/nlbPsoUroeHbz+SABkOsNEv6JuKRH4VXRH34
|
|
VzjazVkPAwj+N4WqsC/Wo4EGGpKIGeGi8Zed4yvMqoTyE3mrS19fY0nMHT62wUwS
|
|
GMm2pAQdAQePZ9WY7A5XOA1IoxW2Zh2Oxaf1p59epBkZDhoxSMu8GoSkvK27Km4A
|
|
4UXftzdg/wHNPrNirmcYouioHdmrOtYxPjrhUBQ74AmE1/QK45B6wEgirKH1A1AW
|
|
6C+ApLwpBMvy9+8Gbyvc8G18W3CjdEVKmAeWb9JUedSXAgMBAAGjUzBRMB0GA1Ud
|
|
DgQWBBRxpaqBx8VDLLc8IkHATujj8IOs6jAfBgNVHSMEGDAWgBRxpaqBx8VDLLc8
|
|
IkHATujj8IOs6jAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBc
|
|
Puk6i+yowwGccB3LhfxZ+Fz6s6/Lfx6bP/Hy4NYOxmx2/awGBgyfp1tmotjaS9Cf
|
|
FWd67LuEru4TYtz12RNMDBF5ypcEfibvb3I8O6igOSQX/Jl5D2pMChesZxhmCift
|
|
Qp09T41MA8PmHf1G9oMG0A3ZnjKDG5ebaJNRFImJhMHsgh/TP7V3uZy7YHTgopKX
|
|
Hv63V3Uo3Oihav29Q7urwmf7Ly7X7J2WE86/w3vRHi5dhaWWqEqxmnAXl+H+sG4V
|
|
meeVRI332bg1Nuy8KnnX8v3ZeJzMBkAhzvSr6Ri96R0/Un/oEFwVC5jDTq8sXVn6
|
|
u7wlOSk+oFzDIO/UILIA
|
|
-----END CERTIFICATE-----
|
|
`;
|
|
|
|
// Reset environment variables
|
|
process.env.SAML_ENTRY_POINT = 'https://example.com/saml';
|
|
process.env.SAML_ISSUER = 'saml-issuer';
|
|
process.env.SAML_CERT = cert;
|
|
process.env.SAML_CALLBACK_URL = '/oauth/saml/callback';
|
|
delete process.env.SAML_EMAIL_CLAIM;
|
|
delete process.env.SAML_USERNAME_CLAIM;
|
|
delete process.env.SAML_GIVEN_NAME_CLAIM;
|
|
delete process.env.SAML_FAMILY_NAME_CLAIM;
|
|
delete process.env.SAML_PICTURE_CLAIM;
|
|
delete process.env.SAML_NAME_CLAIM;
|
|
|
|
// Simulate image download
|
|
const fakeBuffer = Buffer.from('fake image');
|
|
fetch.mockResolvedValue({
|
|
ok: true,
|
|
buffer: jest.fn().mockResolvedValue(fakeBuffer),
|
|
});
|
|
|
|
await setupSaml();
|
|
});
|
|
|
|
it('should create a new user with correct username when username claim exists', async () => {
|
|
const profile = { ...baseProfile };
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.username).toBe(profile.username);
|
|
expect(user.provider).toBe('saml');
|
|
expect(user.samlId).toBe(profile.nameID);
|
|
expect(user.email).toBe(profile.email);
|
|
expect(user.name).toBe(`${profile.given_name} ${profile.family_name}`);
|
|
});
|
|
|
|
it('should use given_name as username when username claim is missing', async () => {
|
|
const profile = { ...baseProfile };
|
|
delete profile.username;
|
|
const expectUsername = profile.given_name;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.username).toBe(expectUsername);
|
|
expect(user.provider).toBe('saml');
|
|
});
|
|
|
|
it('should use email as username when username and given_name are missing', async () => {
|
|
const profile = { ...baseProfile };
|
|
delete profile.username;
|
|
delete profile.given_name;
|
|
const expectUsername = profile.email;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.username).toBe(expectUsername);
|
|
expect(user.provider).toBe('saml');
|
|
});
|
|
|
|
it('should override username with SAML_USERNAME_CLAIM when set', async () => {
|
|
process.env.SAML_USERNAME_CLAIM = 'nameID';
|
|
const profile = { ...baseProfile };
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.username).toBe(profile.nameID);
|
|
expect(user.provider).toBe('saml');
|
|
});
|
|
|
|
it('should set the full name correctly when given_name and family_name exist', async () => {
|
|
const profile = { ...baseProfile };
|
|
const expectedFullName = `${profile.given_name} ${profile.family_name}`;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.name).toBe(expectedFullName);
|
|
});
|
|
|
|
it('should set the full name correctly when given_name exist', async () => {
|
|
const profile = { ...baseProfile };
|
|
delete profile.family_name;
|
|
const expectedFullName = profile.given_name;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.name).toBe(expectedFullName);
|
|
});
|
|
|
|
it('should set the full name correctly when family_name exist', async () => {
|
|
const profile = { ...baseProfile };
|
|
delete profile.given_name;
|
|
const expectedFullName = profile.family_name;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.name).toBe(expectedFullName);
|
|
});
|
|
|
|
it('should set the full name correctly when username exist', async () => {
|
|
const profile = { ...baseProfile };
|
|
delete profile.family_name;
|
|
delete profile.given_name;
|
|
const expectedFullName = profile.username;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.name).toBe(expectedFullName);
|
|
});
|
|
|
|
it('should set the full name correctly when email only exist', async () => {
|
|
const profile = { ...baseProfile };
|
|
delete profile.family_name;
|
|
delete profile.given_name;
|
|
delete profile.username;
|
|
const expectedFullName = profile.email;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.name).toBe(expectedFullName);
|
|
});
|
|
|
|
it('should set the full name correctly with SAML_NAME_CLAIM when set', async () => {
|
|
process.env.SAML_NAME_CLAIM = 'custom_name';
|
|
const profile = { ...baseProfile };
|
|
const expectedFullName = profile.custom_name;
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.name).toBe(expectedFullName);
|
|
});
|
|
|
|
it('should update an existing user on login', async () => {
|
|
// Set up findUser to return an existing user with saml provider
|
|
const { findUser } = require('~/models');
|
|
const existingUser = {
|
|
_id: 'existing-user-id',
|
|
provider: 'saml',
|
|
email: baseProfile.email,
|
|
samlId: '',
|
|
username: 'oldusername',
|
|
name: 'Old Name',
|
|
};
|
|
findUser.mockResolvedValue(existingUser);
|
|
|
|
const profile = { ...baseProfile };
|
|
const { user } = await validate(profile);
|
|
|
|
expect(user.provider).toBe('saml');
|
|
expect(user.samlId).toBe(baseProfile.nameID);
|
|
expect(user.username).toBe(baseProfile.username);
|
|
expect(user.name).toBe(`${baseProfile.given_name} ${baseProfile.family_name}`);
|
|
expect(user.email).toBe(baseProfile.email);
|
|
});
|
|
|
|
it('should block login when email exists with different provider', async () => {
|
|
// Set up findUser to return a user with different provider
|
|
const { findUser } = require('~/models');
|
|
const existingUser = {
|
|
_id: 'existing-user-id',
|
|
provider: 'google',
|
|
email: baseProfile.email,
|
|
googleId: 'some-google-id',
|
|
username: 'existinguser',
|
|
name: 'Existing User',
|
|
};
|
|
findUser.mockResolvedValue(existingUser);
|
|
|
|
const profile = { ...baseProfile };
|
|
const result = await validate(profile);
|
|
|
|
expect(result.user).toBe(false);
|
|
expect(result.details.message).toBe(require('librechat-data-provider').ErrorTypes.AUTH_FAILED);
|
|
});
|
|
|
|
it('should attempt to download and save the avatar if picture is provided', async () => {
|
|
const profile = { ...baseProfile };
|
|
|
|
const { user } = await validate(profile);
|
|
|
|
expect(fetch).toHaveBeenCalled();
|
|
expect(user.avatar).toBe('/fake/path/to/avatar.png');
|
|
});
|
|
|
|
it('should not attempt to download avatar if picture is not provided', async () => {
|
|
const profile = { ...baseProfile };
|
|
delete profile.picture;
|
|
|
|
await validate(profile);
|
|
|
|
expect(fetch).not.toHaveBeenCalled();
|
|
});
|
|
});
|