Files
dcrouter/test/test.dcrouter.email.ts

160 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

2025-05-20 11:04:09 +00:00
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as plugins from '../ts/plugins.js';
import * as path from 'path';
import * as fs from 'fs';
import { DcRouter, type IDcRouterOptions } from '../ts/classes.dcrouter.js';
import type { IUnifiedEmailServerOptions } from '@push.rocks/smartmta';
2025-05-20 11:04:09 +00:00
tap.test('DcRouter class - Custom email port configuration', async () => {
// Define custom port mapping
const customPortMapping: Record<number, number> = {
2025-05-20 11:04:09 +00:00
25: 11025, // Custom SMTP port mapping
587: 11587, // Custom submission port mapping
465: 11465, // Custom SMTPS port mapping
2525: 12525 // Additional custom port
};
// Create a custom email configuration using smartmta interfaces
const emailConfig: IUnifiedEmailServerOptions = {
ports: [25, 587, 465, 2525],
2025-05-20 11:04:09 +00:00
hostname: 'mail.example.com',
maxMessageSize: 50 * 1024 * 1024, // 50MB
domains: [
{
domain: 'example.com',
dnsMode: 'external-dns',
},
2025-05-20 11:04:09 +00:00
{
domain: 'example.org',
dnsMode: 'external-dns',
}
],
routes: [
{
name: 'forward-example-com',
match: {
recipients: '*@example.com',
},
action: {
type: 'forward',
forward: {
host: 'mail1.example.com',
port: 25,
}
2025-05-20 11:04:09 +00:00
}
},
{
name: 'deliver-example-org',
match: {
recipients: '*@example.org',
},
action: {
type: 'deliver',
process: {
dkim: true,
}
2025-05-20 11:04:09 +00:00
}
}
]
};
// Create DcRouter options with custom email port configuration
const options: IDcRouterOptions = {
emailConfig,
emailPortConfig: {
portMapping: customPortMapping,
portSettings: {
2525: {
terminateTls: false,
routeName: 'custom-smtp-route'
}
},
},
tls: {
contactEmail: 'test@example.com'
}
};
2025-05-23 19:03:44 +00:00
// Create DcRouter instance
const router = new DcRouter(options);
2025-05-20 11:04:09 +00:00
// Verify the options are correctly set
expect(router.options.emailPortConfig).toBeTruthy();
expect(router.options.emailPortConfig!.portMapping).toEqual(customPortMapping);
2025-05-20 11:04:09 +00:00
// Test the generateEmailRoutes method
if (typeof (router as any)['generateEmailRoutes'] === 'function') {
const routes = (router as any)['generateEmailRoutes'](emailConfig);
2025-05-20 11:04:09 +00:00
// Verify that all ports are configured
expect(routes.length).toBeGreaterThan(0);
2025-05-20 11:04:09 +00:00
// Check the custom port configuration
const customPortRoute = routes.find((r: any) => {
2025-05-24 01:00:30 +00:00
const ports = r.match.ports;
2025-05-24 02:27:50 +00:00
return ports === 2525 || (Array.isArray(ports) && (ports as number[]).includes(2525));
2025-05-24 01:00:30 +00:00
});
2025-05-20 11:04:09 +00:00
expect(customPortRoute).toBeTruthy();
expect(customPortRoute?.name).toEqual('custom-smtp-route');
expect(customPortRoute?.action.targets[0].port).toEqual(12525);
2025-05-20 11:04:09 +00:00
// Check standard port mappings
const smtpRoute = routes.find((r: any) => {
2025-05-24 01:00:30 +00:00
const ports = r.match.ports;
2025-05-24 02:27:50 +00:00
return ports === 25 || (Array.isArray(ports) && (ports as number[]).includes(25));
2025-05-24 01:00:30 +00:00
});
expect(smtpRoute?.action.targets[0].port).toEqual(11025);
const submissionRoute = routes.find((r: any) => {
2025-05-24 01:00:30 +00:00
const ports = r.match.ports;
2025-05-24 02:27:50 +00:00
return ports === 587 || (Array.isArray(ports) && (ports as number[]).includes(587));
2025-05-24 01:00:30 +00:00
});
expect(submissionRoute?.action.targets[0].port).toEqual(11587);
2025-05-20 11:04:09 +00:00
}
});
tap.test('DcRouter class - Email config with domains and routes', async () => {
2025-05-20 11:04:09 +00:00
// Create a basic email configuration
const emailConfig: IUnifiedEmailServerOptions = {
ports: [2525],
2025-05-20 11:04:09 +00:00
hostname: 'mail.example.com',
domains: [],
routes: []
2025-05-20 11:04:09 +00:00
};
// Create DcRouter options
2025-05-20 11:04:09 +00:00
const options: IDcRouterOptions = {
emailConfig,
tls: {
contactEmail: 'test@example.com'
},
cacheConfig: {
enabled: false,
2025-05-20 11:04:09 +00:00
}
};
2025-05-23 19:03:44 +00:00
// Create DcRouter instance
const router = new DcRouter(options);
2025-05-23 19:03:44 +00:00
// Start the router to initialize email services
await router.start();
2025-05-23 19:03:44 +00:00
// Verify unified email server was initialized
expect(router.emailServer).toBeTruthy();
2025-05-23 19:03:44 +00:00
// Stop the router
await router.stop();
2025-05-20 11:04:09 +00:00
});
// Final clean-up test
tap.test('clean up after tests', async () => {
// No-op
2025-05-20 11:04:09 +00:00
});
tap.test('stop', async () => {
await tap.stopForcefully();
});
export default tap.start();