🔧 refactor: Add and use PrincipalType Enum

- Replaced string literals for principal types ('user', 'group', 'public') with the new PrincipalType enum across various models, services, and tests for improved type safety and consistency.
- Updated permission handling in multiple files to utilize the PrincipalType enum, enhancing maintainability and reducing potential errors.
- Ensured all relevant tests reflect these changes to maintain coverage and functionality.
This commit is contained in:
Danny Avila 2025-08-02 16:02:56 -04:00
parent 0262c25989
commit 49d1cefe71
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
23 changed files with 253 additions and 219 deletions

View file

@ -1,9 +1,10 @@
import mongoose from 'mongoose';
import { PrincipalType } from 'librechat-data-provider';
import { MongoMemoryServer } from 'mongodb-memory-server';
import type * as t from '~/types';
import { createUserGroupMethods } from './userGroup';
import groupSchema from '~/schema/group';
import userSchema from '~/schema/user';
import type * as t from '~/types';
/** Mocking logger */
jest.mock('~/config/winston', () => ({
@ -330,9 +331,9 @@ describe('User Group Methods Tests', () => {
expect(principals).toHaveLength(3);
/** Check principal types */
const userPrincipal = principals.find((p) => p.principalType === 'user');
const groupPrincipal = principals.find((p) => p.principalType === 'group');
const publicPrincipal = principals.find((p) => p.principalType === 'public');
const userPrincipal = principals.find((p) => p.principalType === PrincipalType.USER);
const groupPrincipal = principals.find((p) => p.principalType === PrincipalType.GROUP);
const publicPrincipal = principals.find((p) => p.principalType === PrincipalType.PUBLIC);
expect(userPrincipal).toBeDefined();
expect(userPrincipal?.principalId?.toString()).toBe(
@ -352,9 +353,9 @@ describe('User Group Methods Tests', () => {
/** Should still return user and public principals even for non-existent user */
expect(principals).toHaveLength(2);
expect(principals[0].principalType).toBe('user');
expect(principals[0].principalType).toBe(PrincipalType.USER);
expect(principals[0].principalId?.toString()).toBe(nonExistentId.toString());
expect(principals[1].principalType).toBe('public');
expect(principals[1].principalType).toBe(PrincipalType.PUBLIC);
expect(principals[1].principalId).toBeUndefined();
});
});