98 lines
3.0 KiB
JavaScript
98 lines
3.0 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* Integration tests – health controller (controllers/health.js)
|
||
*
|
||
* partner_sync_service is mocked so tests run without external partner APIs.
|
||
* The DLQ check (RabbitMQ) inside healthCheck_get fails gracefully and is
|
||
* captured in the response's dlq component without breaking the overall result.
|
||
*/
|
||
|
||
// Mock partner_sync_service before the controller is required so the
|
||
// controller module picks up the mock on first load.
|
||
jest.doMock(require.resolve('../../services/partner_sync_service'), () => ({
|
||
healthCheck: jest.fn(async () => ({
|
||
overall: 'healthy',
|
||
services: {},
|
||
})),
|
||
getAvailablePartners: jest.fn(() => []),
|
||
}));
|
||
|
||
const { connectDB, disconnectDB } = require('./jest.setup');
|
||
const { mockReq, mockRes } = require('./mock_data');
|
||
|
||
let PartnerSystemUser;
|
||
|
||
beforeAll(async () => {
|
||
await connectDB();
|
||
({ PartnerSystemUser } = require('../../model/partner'));
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await disconnectDB();
|
||
});
|
||
|
||
// Require after mocks are registered.
|
||
const healthCtl = require('../../controllers/health');
|
||
|
||
// ---------------------------------------------------------------------------
|
||
describe('health controller – healthCheck_get', () => {
|
||
it('returns a response with timestamp, overall, and components fields', async () => {
|
||
const req = mockReq();
|
||
const res = mockRes();
|
||
|
||
await healthCtl.healthCheck_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data).toHaveProperty('timestamp');
|
||
expect(res._data).toHaveProperty('overall');
|
||
expect(res._data).toHaveProperty('components');
|
||
});
|
||
|
||
it('reports database as connected when in-memory MongoDB is running', async () => {
|
||
const req = mockReq();
|
||
const res = mockRes();
|
||
|
||
await healthCtl.healthCheck_get(req, res);
|
||
|
||
expect(res._data.components.database).toBeDefined();
|
||
expect(res._data.components.database.status).toBe('healthy');
|
||
});
|
||
|
||
it('includes partnerUsers component with activeUsers count', async () => {
|
||
const req = mockReq();
|
||
const res = mockRes();
|
||
|
||
await healthCtl.healthCheck_get(req, res);
|
||
|
||
expect(res._data.components.partnerUsers).toBeDefined();
|
||
expect(typeof res._data.components.partnerUsers.activeUsers).toBe('number');
|
||
});
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
describe('health controller – partnerStats_get', () => {
|
||
it('returns a response with timestamp, partners, and summary fields', async () => {
|
||
const req = mockReq();
|
||
const res = mockRes();
|
||
|
||
await healthCtl.partnerStats_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data).toHaveProperty('timestamp');
|
||
expect(res._data).toHaveProperty('partners');
|
||
expect(res._data).toHaveProperty('summary');
|
||
});
|
||
|
||
it('returns zero totals when no partner system users exist', async () => {
|
||
await PartnerSystemUser.deleteMany({});
|
||
const req = mockReq();
|
||
const res = mockRes();
|
||
|
||
await healthCtl.partnerStats_get(req, res);
|
||
|
||
expect(res._data.summary.totalPartners).toBe(0);
|
||
expect(res._data.summary.totalPartnerUsers).toBe(0);
|
||
});
|
||
});
|