fix: add roleId parameter to grantPermission and update tests for GraphApiService

This commit is contained in:
Atef Bellaaj 2025-06-13 11:54:32 +02:00 committed by Danny Avila
parent 1016a33b89
commit 6bbefcd16e
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
3 changed files with 17 additions and 1 deletions

View file

@ -52,9 +52,17 @@ describe('GraphApiService', () => {
await mongoServer.stop(); await mongoServer.stop();
}); });
afterEach(() => {
// Clean up environment variables
delete process.env.OPENID_GRAPH_SCOPES;
});
beforeEach(async () => { beforeEach(async () => {
jest.clearAllMocks(); jest.clearAllMocks();
await mongoose.connection.dropDatabase(); await mongoose.connection.dropDatabase();
// Set up environment variable for People.Read scope
process.env.OPENID_GRAPH_SCOPES = 'User.Read,People.Read,Group.Read.All';
// Mock Graph client // Mock Graph client
mockGraphClient = { mockGraphClient = {
@ -341,6 +349,7 @@ describe('GraphApiService', () => {
// Should call contacts first with user filter // Should call contacts first with user filter
expect(mockGraphClient.api).toHaveBeenCalledWith('/me/people'); expect(mockGraphClient.api).toHaveBeenCalledWith('/me/people');
expect(mockGraphClient.search).toHaveBeenCalledWith('"john"');
expect(mockGraphClient.filter).toHaveBeenCalledWith( expect(mockGraphClient.filter).toHaveBeenCalledWith(
"personType/subclass eq 'OrganizationUser'", "personType/subclass eq 'OrganizationUser'",
); );
@ -404,7 +413,9 @@ describe('GraphApiService', () => {
10, 10,
); );
// Should call contacts with user filter only // Should call contacts first with user filter
expect(mockGraphClient.api).toHaveBeenCalledWith('/me/people');
expect(mockGraphClient.search).toHaveBeenCalledWith('"test"');
expect(mockGraphClient.filter).toHaveBeenCalledWith( expect(mockGraphClient.filter).toHaveBeenCalledWith(
"personType/subclass eq 'OrganizationUser'", "personType/subclass eq 'OrganizationUser'",
); );
@ -440,6 +451,7 @@ describe('GraphApiService', () => {
// Should call contacts first // Should call contacts first
expect(mockGraphClient.api).toHaveBeenCalledWith('/me/people'); expect(mockGraphClient.api).toHaveBeenCalledWith('/me/people');
expect(mockGraphClient.search).toHaveBeenCalledWith('"test"');
// Should not call users endpoint since limit was reached // Should not call users endpoint since limit was reached
expect(mockGraphClient.api).not.toHaveBeenCalledWith('/users'); expect(mockGraphClient.api).not.toHaveBeenCalledWith('/users');

View file

@ -85,6 +85,7 @@ const grantPermission = async ({
role.permBits, role.permBits,
grantedBy, grantedBy,
session, session,
role._id,
); );
} catch (error) { } catch (error) {
logger.error(`[PermissionService.grantPermission] Error: ${error.message}`); logger.error(`[PermissionService.grantPermission] Error: ${error.message}`);

View file

@ -125,6 +125,7 @@ export function createAclEntryMethods(mongoose: typeof import('mongoose')) {
* @param permBits - The permission bits to grant * @param permBits - The permission bits to grant
* @param grantedBy - The ID of the user granting the permission * @param grantedBy - The ID of the user granting the permission
* @param session - Optional MongoDB session for transactions * @param session - Optional MongoDB session for transactions
* @param roleId - Optional role ID to associate with this permission
* @returns The created or updated ACL entry * @returns The created or updated ACL entry
*/ */
async function grantPermission( async function grantPermission(
@ -135,6 +136,7 @@ export function createAclEntryMethods(mongoose: typeof import('mongoose')) {
permBits: number, permBits: number,
grantedBy: string | Types.ObjectId, grantedBy: string | Types.ObjectId,
session?: ClientSession, session?: ClientSession,
roleId?: string | Types.ObjectId,
): Promise<IAclEntry | null> { ): Promise<IAclEntry | null> {
const AclEntry = mongoose.models.AclEntry as Model<IAclEntry>; const AclEntry = mongoose.models.AclEntry as Model<IAclEntry>;
const query: Record<string, unknown> = { const query: Record<string, unknown> = {
@ -153,6 +155,7 @@ export function createAclEntryMethods(mongoose: typeof import('mongoose')) {
permBits, permBits,
grantedBy, grantedBy,
grantedAt: new Date(), grantedAt: new Date(),
...(roleId && { roleId }),
}, },
}; };