All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
75 lines
2.5 KiB
JavaScript
75 lines
2.5 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(),
|
|
}));
|
|
|
|
// 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();
|
|
});
|
|
});
|
|
});
|