Files
BetterDesk/web-nodejs/tests/helpRequestEmailService.test.js
UNITRONIX 045dadd0b4 feat: add email notification system and SMTP configuration
- Introduced email notifications for help requests, allowing operators assigned to device folders or groups to receive alerts.
- Moved SMTP configuration to **Settings → Email**, including options for host, credentials, and alert email.
- Updated console layout for better usability and removed legacy SMTP automation tab.
- Added `nodemailer` as a dependency for email handling.
2026-06-14 09:04:04 +02:00

95 lines
3.2 KiB
JavaScript

'use strict';
jest.mock('../services/database', () => ({
getSetting: jest.fn(),
hasEmailNotificationSent: jest.fn(),
logEmailNotificationSent: jest.fn(),
}));
jest.mock('../services/emailService', () => ({
getAlertEmail: jest.fn(),
sendHelpRequestEmail: jest.fn(),
}));
jest.mock('../services/deviceGroupService', () => ({
resolveOperatorEmailsForDevice: jest.fn(),
resolveFolderNameForDevice: jest.fn(),
}));
const db = require('../services/database');
const emailService = require('../services/emailService');
const deviceGroupService = require('../services/deviceGroupService');
const {
handleHelpRequestEvent,
parseCommercializationEmailConfig,
} = require('../services/helpRequestEmailService');
describe('helpRequestEmailService', () => {
beforeEach(() => {
jest.clearAllMocks();
db.getSetting.mockResolvedValue(null);
db.hasEmailNotificationSent.mockResolvedValue(false);
db.logEmailNotificationSent.mockResolvedValue(undefined);
deviceGroupService.resolveFolderNameForDevice.mockResolvedValue('Support');
emailService.sendHelpRequestEmail.mockResolvedValue(true);
});
it('uses default config when setting is missing', () => {
const cfg = parseCommercializationEmailConfig(null);
expect(cfg.help_requests_enabled).toBe(true);
expect(cfg.fallback_alert_email).toBe(true);
});
it('sends email to assigned operators', async () => {
deviceGroupService.resolveOperatorEmailsForDevice.mockResolvedValue([
{ username: 'alice', email: 'alice@example.com' },
]);
await handleHelpRequestEvent({
id: '42',
device_id: 'dev-1',
hostname: 'PC-01',
message: 'Need help',
});
expect(emailService.sendHelpRequestEmail).toHaveBeenCalledWith({
to: 'alice@example.com',
helpRequest: expect.objectContaining({ id: '42', device_id: 'dev-1' }),
folderName: 'Support',
});
expect(db.logEmailNotificationSent).toHaveBeenCalledWith('help_request', '42', 'alice@example.com');
});
it('falls back to alert email when no operator addresses exist', async () => {
deviceGroupService.resolveOperatorEmailsForDevice.mockResolvedValue([]);
emailService.getAlertEmail.mockResolvedValue('ops@example.com');
await handleHelpRequestEvent({
id: '43',
device_id: 'dev-2',
hostname: 'PC-02',
message: 'Need help',
});
expect(emailService.sendHelpRequestEmail).toHaveBeenCalledWith(
expect.objectContaining({ to: 'ops@example.com' })
);
});
it('skips duplicate sends for the same recipient', async () => {
deviceGroupService.resolveOperatorEmailsForDevice.mockResolvedValue([
{ username: 'alice', email: 'alice@example.com' },
]);
db.hasEmailNotificationSent.mockResolvedValue(true);
await handleHelpRequestEvent({
id: '44',
device_id: 'dev-3',
hostname: 'PC-03',
message: 'Need help',
});
expect(emailService.sendHelpRequestEmail).not.toHaveBeenCalled();
});
});