All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit introduces mock functions for the `permissions` module in the user API test file, enhancing test isolation and allowing for comprehensive testing of permission-related functionalities. The new mocks include methods for checking permissions, managing user permissions cache, and resolving permission settings, improving the overall test coverage and reliability of the user API tests.
87 lines
3.0 KiB
JavaScript
87 lines
3.0 KiB
JavaScript
import { jest } from '@jest/globals';
|
|
import request from 'supertest';
|
|
|
|
// Mock Keycloak and Auth
|
|
jest.unstable_mockModule('../keycloak.js', () => ({
|
|
keycloak: {
|
|
middleware: () => (req, res, next) => next(),
|
|
protect: () => (req, res, next) => next(),
|
|
},
|
|
isAuthenticated: (req, res, next) => next(),
|
|
isAppAuthenticated: (req, res, next) => next(),
|
|
isAppQueryAuthenticated: (req, res, next) => next(),
|
|
expressSession: (req, res, next) => next(),
|
|
}));
|
|
|
|
jest.unstable_mockModule('../database/permissions.js', () => ({
|
|
checkPermissions: () => (req, res, next) => next(),
|
|
hasPermission: jest.fn(async () => true),
|
|
getUserPermissionsCacheKey: jest.fn((userId) => `permissions:${userId}`),
|
|
saveUserPermissionsToRedis: jest.fn(),
|
|
applyPermissionSettingsList: jest.fn(() => ({})),
|
|
getPermissionSettingsId: jest.fn(),
|
|
excludeIdFromList: jest.fn((items = []) => items),
|
|
resolveReferencedDocs: jest.fn(async () => []),
|
|
resolvePermissionSettings: jest.fn(async () => []),
|
|
}));
|
|
|
|
// Mock database connections and initializations in index.js
|
|
jest.unstable_mockModule('../database/mongo.js', () => ({
|
|
dbConnect: jest.fn(),
|
|
}));
|
|
jest.unstable_mockModule('../database/nats.js', () => ({
|
|
natsServer: { connect: jest.fn() },
|
|
}));
|
|
jest.unstable_mockModule('../database/ceph.js', () => ({
|
|
initializeBuckets: jest.fn(),
|
|
uploadFile: jest.fn(),
|
|
downloadFile: jest.fn(),
|
|
deleteFile: jest.fn(),
|
|
fileExists: jest.fn(),
|
|
listFiles: jest.fn(),
|
|
getFileMetadata: jest.fn(),
|
|
getPresignedUrl: jest.fn(),
|
|
BUCKETS: { FILES: 'test-bucket' },
|
|
}));
|
|
|
|
// Mock the service handlers to avoid database calls
|
|
jest.unstable_mockModule('../services/management/users.js', () => ({
|
|
listUsersRouteHandler: jest.fn((req, res) => res.send([{ id: '1', name: 'Mock User' }])),
|
|
listUsersByPropertiesRouteHandler: jest.fn(),
|
|
searchUsersRouteHandler: jest.fn(),
|
|
getUserRouteHandler: jest.fn((req, res) => res.send({ id: req.params.id, name: 'Mock User' })),
|
|
editUserRouteHandler: jest.fn(),
|
|
getUserStatsRouteHandler: jest.fn(),
|
|
getUserHistoryRouteHandler: jest.fn(),
|
|
setAppPasswordRouteHandler: jest.fn(),
|
|
getUserPropertyValuesRouteHandler: jest.fn(),
|
|
getUserNeighborsRouteHandler: jest.fn(),
|
|
}));
|
|
|
|
const { default: app } = await import('../index.js');
|
|
const { listUsersRouteHandler, getUserRouteHandler } = await import(
|
|
'../services/management/users.js'
|
|
);
|
|
|
|
describe('Users API Endpoints', () => {
|
|
describe('GET /users', () => {
|
|
it('should return a list of users', async () => {
|
|
const response = await request(app).get('/users');
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toEqual([{ id: '1', name: 'Mock User' }]);
|
|
expect(listUsersRouteHandler).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('GET /users/:id', () => {
|
|
it('should return a single user', async () => {
|
|
const response = await request(app).get('/users/123');
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toEqual({ id: '123', name: 'Mock User' });
|
|
expect(getUserRouteHandler).toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|