All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit introduces a new feature for managing user groups within the application, including the creation of a `usergroup.schema.js` file for the Mongoose schema and corresponding route handlers for CRUD operations. Additionally, the permission settings functionality has been refactored to use a singular `permissionSetting` model instead of the previous plural form. Tests have been added to ensure the functionality of both user groups and permission settings services, enhancing the application's capability to manage user roles and permissions effectively.
131 lines
3.5 KiB
JavaScript
131 lines
3.5 KiB
JavaScript
import { jest } from '@jest/globals';
|
|
|
|
jest.unstable_mockModule('../../../database/database.js', () => ({
|
|
searchObjects: jest.fn(),
|
|
getPropertyValues: jest.fn(),
|
|
listObjects: jest.fn(),
|
|
getObject: jest.fn(),
|
|
editObject: jest.fn(),
|
|
newObject: jest.fn(),
|
|
deleteObject: jest.fn(),
|
|
listObjectsByProperties: jest.fn(),
|
|
getModelStats: jest.fn(),
|
|
getModelHistory: jest.fn(),
|
|
getObjectNeighbors: jest.fn(),
|
|
}));
|
|
|
|
jest.unstable_mockModule('../../../database/schemas/management/usergroup.schema.js', () => ({
|
|
userGroupModel: { modelName: 'UserGroup' },
|
|
}));
|
|
|
|
jest.unstable_mockModule('log4js', () => ({
|
|
default: {
|
|
getLogger: () => ({
|
|
level: 'info',
|
|
debug: jest.fn(),
|
|
error: jest.fn(),
|
|
warn: jest.fn(),
|
|
trace: jest.fn(),
|
|
}),
|
|
},
|
|
}));
|
|
|
|
const {
|
|
listUserGroupsRouteHandler,
|
|
getUserGroupRouteHandler,
|
|
newUserGroupRouteHandler,
|
|
editUserGroupRouteHandler,
|
|
} = await import('../usergroups.js');
|
|
|
|
const { listObjects, getObject, editObject, newObject } = await import(
|
|
'../../../database/database.js'
|
|
);
|
|
const { userGroupModel } = await import(
|
|
'../../../database/schemas/management/usergroup.schema.js'
|
|
);
|
|
|
|
describe('User Groups Service Route Handlers', () => {
|
|
let req, res;
|
|
|
|
beforeEach(() => {
|
|
req = {
|
|
params: {},
|
|
query: {},
|
|
body: {},
|
|
user: { id: 'test-user-id' },
|
|
};
|
|
res = {
|
|
send: jest.fn(),
|
|
status: jest.fn().mockReturnThis(),
|
|
};
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
describe('listUserGroupsRouteHandler', () => {
|
|
it('should list user groups', async () => {
|
|
const mockResult = [{ _id: '1', name: 'Admins' }];
|
|
listObjects.mockResolvedValue(mockResult);
|
|
|
|
await listUserGroupsRouteHandler(req, res);
|
|
|
|
expect(listObjects).toHaveBeenCalledWith(
|
|
expect.objectContaining({ model: userGroupModel })
|
|
);
|
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
|
});
|
|
});
|
|
|
|
describe('newUserGroupRouteHandler', () => {
|
|
it('should create a new user group', async () => {
|
|
req.body = { name: 'Editors', permissionSettings: [{ _id: 'pms-1' }] };
|
|
const mockUserGroup = { _id: '456', ...req.body };
|
|
newObject.mockResolvedValue(mockUserGroup);
|
|
|
|
await newUserGroupRouteHandler(req, res);
|
|
|
|
expect(newObject).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
model: userGroupModel,
|
|
newData: expect.objectContaining({
|
|
name: 'Editors',
|
|
permissionSettings: ['pms-1'],
|
|
}),
|
|
})
|
|
);
|
|
expect(res.send).toHaveBeenCalledWith(mockUserGroup);
|
|
});
|
|
});
|
|
|
|
describe('editUserGroupRouteHandler', () => {
|
|
it('should update a user group', async () => {
|
|
req.params.id = '507f1f77bcf86cd799439011';
|
|
req.body = { name: 'Editors', permissionSettings: ['pms-1'] };
|
|
const mockResult = { _id: '507f1f77bcf86cd799439011', ...req.body };
|
|
editObject.mockResolvedValue(mockResult);
|
|
|
|
await editUserGroupRouteHandler(req, res);
|
|
|
|
expect(editObject).toHaveBeenCalled();
|
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
|
});
|
|
});
|
|
|
|
describe('getUserGroupRouteHandler', () => {
|
|
it('should get a user group by id', async () => {
|
|
req.params.id = '123';
|
|
const mockResult = { _id: '123', name: 'Admins' };
|
|
getObject.mockResolvedValue(mockResult);
|
|
|
|
await getUserGroupRouteHandler(req, res);
|
|
|
|
expect(getObject).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
model: userGroupModel,
|
|
id: '123',
|
|
})
|
|
);
|
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
|
});
|
|
});
|
|
});
|