feat(storage): add comprehensive tests for StorageManager with memory, filesystem, and custom function backends
Some checks failed
CI / Type Check & Lint (push) Failing after 3s
CI / Build Test (Current Platform) (push) Failing after 3s
CI / Build All Platforms (push) Failing after 3s

feat(email): implement EmailSendJob class for robust email delivery with retry logic and MX record resolution

feat(mail): restructure mail module exports for simplified access to core and delivery functionalities
This commit is contained in:
2025-10-28 19:46:17 +00:00
parent 6523c55516
commit 17f5661636
271 changed files with 61736 additions and 6222 deletions

View File

@@ -1,21 +1,14 @@
/**
* Test SMTP Server Loader for Deno
* Manages test server lifecycle and configuration
*/
import * as plugins from '../../ts/plugins.ts';
import { UnifiedEmailServer } from '../../ts/mail/routing/classes.unified.email.server.ts';
import { createSmtpServer } from '../../ts/mail/delivery/smtpserver/index.ts';
import type { ISmtpServerOptions } from '../../ts/mail/delivery/smtpserver/interfaces.ts';
import { Email } from '../../ts/mail/core/classes.email.ts';
import { net, crypto } from '../../ts/plugins.ts';
import type { net } from '../../ts/plugins.ts';
export interface ITestServerConfig {
port: number;
hostname?: string;
tlsEnabled?: boolean;
secure?: boolean; // Direct TLS server (like SMTPS on port 465)
authRequired?: boolean;
authMethods?: ('PLAIN' | 'LOGIN' | 'OAUTH2')[];
requireTLS?: boolean; // Whether to require TLS for AUTH (default: true)
timeout?: number;
testCertPath?: string;
testKeyPath?: string;
@@ -34,73 +27,7 @@ export interface ITestServer {
}
/**
* Generate self-signed certificate for testing
* Uses Deno's built-in crypto for key generation
*/
async function generateSelfSignedCert(hostname: string): Promise<{
key: string;
cert: string;
}> {
// For now, return placeholder cert/key that will be replaced with real generation
// In production tests, we should either use pre-generated test certs from fixtures
// or implement proper cert generation using Deno's crypto API
// This is a self-signed test certificate - DO NOT use in production
const key = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAxzYIwlfnr7AK2v6E+c2oYD7nAIXIIvDuvVvZ8R9kyxXIzTXB
j5D1AgntqKS3bFR1XT8hCVeXjuLKPBvXbhVjG15gXlXxpNiFi1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RZWXZRe38lfoLtnpTIlcxlKegbQrDoZCN7jUrm
vRl3OuGsZ+Zv3BINSf2xkZfqKX6gyJjPtSBCQ8TMZRkQnEonDHLxmA91KBu9irhb
A/BsFQmWnDSC3mLoc5LsKmFFqZr7MB/ku99IugtdI/knAgMBAAGjUzBRMB0GA1Ud
DgQWBBQryyWLuN22OqU1r9HIt2tMLBk42DAfBgNVHSMEGDAWgBQryyWLuN22OqU1
r9HIt2tMLBk42DAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAe
CeXQZlXJ2xLnDoOoKY3BpodErNmAwygGYxwDCU0xPbpUMPrQhLI80JlZmfy58gT/
0ZbULS+srShfEsFnBLmzWLGXDvA/IKCQyTmCQwbPeELGXF6h4URMb+lQL7WL9tY0
uUg2dA+7CtYokIrOkGqUitPK3yvVhxugkf51WIgKMACZDibOQSWrV5QO2vHOAaO9
ePzRGGl3+Ebmcs3+5w1fI6OLsIZH10lfEnC83C0lO8tIJlGsXMQkCjAcX22rT0rc
AcxLm07H4EwMwgOAJUkuDjD3y4+KH91jKWF8bhaLZooFB8lccNnaCRiuZRnXlvmf
M7uVlLGwlj5R9iHd+0dP
-----END CERTIFICATE-----`;
return { key, cert };
}
/**
* Start a test SMTP server with the given configuration
* Starts a test SMTP server with the given configuration
*/
export async function startTestServer(config: ITestServerConfig): Promise<ITestServer> {
const serverConfig = {
@@ -111,7 +38,7 @@ export async function startTestServer(config: ITestServerConfig): Promise<ITestS
timeout: config.timeout || 30000,
maxConnections: config.maxConnections || 100,
size: config.size || 10 * 1024 * 1024, // 10MB default
maxRecipients: config.maxRecipients || 100,
maxRecipients: config.maxRecipients || 100
};
// Create a mock email server for testing
@@ -125,43 +52,85 @@ export async function startTestServer(config: ITestServerConfig): Promise<ITestS
return {
recordConnection: (_ip: string) => ({ allowed: true, remaining: 100 }),
checkConnectionLimit: async (_ip: string) => ({ allowed: true, remaining: 100 }),
checkMessageLimit: (
_senderAddress: string,
_ip: string,
_recipientCount?: number,
_pattern?: string,
_domain?: string
) => ({ allowed: true, remaining: 1000 }),
checkMessageLimit: (_senderAddress: string, _ip: string, _recipientCount?: number, _pattern?: string, _domain?: string) => ({ allowed: true, remaining: 1000 }),
checkRecipientLimit: async (_session: any) => ({ allowed: true, remaining: 50 }),
recordAuthenticationFailure: async (_ip: string) => {},
recordSyntaxError: async (_ip: string) => {},
recordCommandError: async (_ip: string) => {},
recordError: (_ip: string) => false, // Returns false = don't block IP in tests
isBlocked: async (_ip: string) => false,
cleanup: async () => {},
cleanup: async () => {}
};
},
}
} as any;
// Load or generate test certificates
// Load test certificates
let key: string;
let cert: string;
if (serverConfig.tlsEnabled && config.testCertPath && config.testKeyPath) {
if (serverConfig.tlsEnabled) {
try {
key = await Deno.readTextFile(config.testKeyPath);
cert = await Deno.readTextFile(config.testCertPath);
const certPath = config.testCertPath || './test/fixtures/test-cert.pem';
const keyPath = config.testKeyPath || './test/fixtures/test-key.pem';
cert = await plugins.fs.promises.readFile(certPath, 'utf8');
key = await plugins.fs.promises.readFile(keyPath, 'utf8');
} catch (error) {
console.warn('⚠️ Failed to load TLS certificates, generating self-signed', error);
const generated = await generateSelfSignedCert(serverConfig.hostname);
key = generated.key;
cert = generated.cert;
console.warn('⚠️ Failed to load TLS certificates, falling back to self-signed');
// Generate self-signed certificate for testing
const forge = await import('node-forge');
const pki = forge.default.pki;
// Generate key pair
const keys = pki.rsa.generateKeyPair(2048);
// Create certificate
const certificate = pki.createCertificate();
certificate.publicKey = keys.publicKey;
certificate.serialNumber = '01';
certificate.validity.notBefore = new Date();
certificate.validity.notAfter = new Date();
certificate.validity.notAfter.setFullYear(certificate.validity.notBefore.getFullYear() + 1);
const attrs = [{
name: 'commonName',
value: serverConfig.hostname
}];
certificate.setSubject(attrs);
certificate.setIssuer(attrs);
certificate.sign(keys.privateKey);
// Convert to PEM
cert = pki.certificateToPem(certificate);
key = pki.privateKeyToPem(keys.privateKey);
}
} else {
// Always generate a certificate (required by the interface)
const generated = await generateSelfSignedCert(serverConfig.hostname);
key = generated.key;
cert = generated.cert;
// Always provide a self-signed certificate for non-TLS servers
// This is required by the interface
const forge = await import('node-forge');
const pki = forge.default.pki;
// Generate key pair
const keys = pki.rsa.generateKeyPair(2048);
// Create certificate
const certificate = pki.createCertificate();
certificate.publicKey = keys.publicKey;
certificate.serialNumber = '01';
certificate.validity.notBefore = new Date();
certificate.validity.notAfter = new Date();
certificate.validity.notAfter.setFullYear(certificate.validity.notBefore.getFullYear() + 1);
const attrs = [{
name: 'commonName',
value: serverConfig.hostname
}];
certificate.setSubject(attrs);
certificate.setIssuer(attrs);
certificate.sign(keys.privateKey);
// Convert to PEM
cert = pki.certificateToPem(certificate);
key = pki.privateKeyToPem(keys.privateKey);
}
// SMTP server options
@@ -176,62 +145,57 @@ export async function startTestServer(config: ITestServerConfig): Promise<ITestS
socketTimeout: serverConfig.timeout,
connectionTimeout: serverConfig.timeout * 2,
cleanupInterval: 300000,
auth: serverConfig.authRequired
? ({
required: true,
methods: (serverConfig.authMethods || ['PLAIN', 'LOGIN']) as ('PLAIN' | 'LOGIN' | 'OAUTH2')[],
requireTLS: serverConfig.requireTLS !== undefined ? serverConfig.requireTLS : true, // Default: require TLS for AUTH
validateUser: async (username: string, password: string) => {
// Test server accepts these credentials
return username === 'testuser' && password === 'testpass';
},
} as any)
: undefined,
auth: serverConfig.authRequired ? ({
required: true,
methods: ['PLAIN', 'LOGIN'] as ('PLAIN' | 'LOGIN' | 'OAUTH2')[],
validateUser: async (username: string, password: string) => {
// Test server accepts these credentials
return username === 'testuser' && password === 'testpass';
}
} as any) : undefined
};
// Create SMTP server
const smtpServer = await createSmtpServer(mockEmailServer, smtpOptions);
// Start the server
await smtpServer.listen();
// Wait for server to be ready
await waitForServerReady(serverConfig.hostname, serverConfig.port);
console.log(
`✅ Test SMTP server started on ${serverConfig.hostname}:${serverConfig.port}`
);
console.log(`✅ Test SMTP server started on ${serverConfig.hostname}:${serverConfig.port}`);
return {
server: mockEmailServer,
smtpServer: smtpServer,
port: serverConfig.port,
hostname: serverConfig.hostname,
config: serverConfig,
startTime: Date.now(),
startTime: Date.now()
};
}
/**
* Stop a test SMTP server
* Stops a test SMTP server
*/
export async function stopTestServer(testServer: ITestServer): Promise<void> {
if (!testServer || !testServer.smtpServer) {
console.warn('⚠️ No test server to stop');
return;
}
try {
console.log(`🛑 Stopping test SMTP server on ${testServer.hostname}:${testServer.port}`);
// Stop the SMTP server
if (testServer.smtpServer.close && typeof testServer.smtpServer.close === 'function') {
await testServer.smtpServer.close();
}
// Wait for port to be free
await waitForPortFree(testServer.port);
console.log(`✅ Test SMTP server stopped`);
} catch (error) {
console.error('❌ Error stopping test server:', error);
@@ -242,24 +206,34 @@ export async function stopTestServer(testServer: ITestServer): Promise<void> {
/**
* Wait for server to be ready to accept connections
*/
async function waitForServerReady(
hostname: string,
port: number,
timeout: number = 10000
): Promise<void> {
async function waitForServerReady(hostname: string, port: number, timeout: number = 10000): Promise<void> {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
const conn = await Deno.connect({ hostname, port, transport: 'tcp' });
conn.close();
await new Promise<void>((resolve, reject) => {
const socket = plugins.net.createConnection({ port, host: hostname });
socket.on('connect', () => {
socket.end();
resolve();
});
socket.on('error', reject);
setTimeout(() => {
socket.destroy();
reject(new Error('Connection timeout'));
}, 1000);
});
return; // Server is ready
} catch {
// Server not ready yet, wait and retry
await new Promise((resolve) => setTimeout(resolve, 100));
await new Promise(resolve => setTimeout(resolve, 100));
}
}
throw new Error(`Server did not become ready within ${timeout}ms`);
}
@@ -268,15 +242,15 @@ async function waitForServerReady(
*/
async function waitForPortFree(port: number, timeout: number = 5000): Promise<void> {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const isFree = await isPortFree(port);
if (isFree) {
return;
}
await new Promise((resolve) => setTimeout(resolve, 100));
await new Promise(resolve => setTimeout(resolve, 100));
}
console.warn(`⚠️ Port ${port} still in use after ${timeout}ms`);
}
@@ -284,13 +258,15 @@ async function waitForPortFree(port: number, timeout: number = 5000): Promise<vo
* Check if a port is free
*/
async function isPortFree(port: number): Promise<boolean> {
try {
const listener = Deno.listen({ port, transport: 'tcp' });
listener.close();
return true;
} catch {
return false;
}
return new Promise((resolve) => {
const server = plugins.net.createServer();
server.listen(port, () => {
server.close(() => resolve(true));
});
server.on('error', () => resolve(false));
});
}
/**
@@ -308,16 +284,14 @@ export async function getAvailablePort(startPort: number = 25000): Promise<numbe
/**
* Create test email data
*/
export function createTestEmail(
options: {
from?: string;
to?: string | string[];
subject?: string;
text?: string;
html?: string;
attachments?: any[];
} = {}
): any {
export function createTestEmail(options: {
from?: string;
to?: string | string[];
subject?: string;
text?: string;
html?: string;
attachments?: any[];
} = {}): any {
return {
from: options.from || 'test@example.com',
to: options.to || 'recipient@example.com',
@@ -326,6 +300,48 @@ export function createTestEmail(
html: options.html || '<p>This is a test email</p>',
attachments: options.attachments || [],
date: new Date(),
messageId: `<${Date.now()}@test.example.com>`,
messageId: `<${Date.now()}@test.example.com>`
};
}
/**
* Simple test server for custom protocol testing
*/
export interface ISimpleTestServer {
server: any;
hostname: string;
port: number;
}
export async function createTestServer(options: {
onConnection?: (socket: any) => void | Promise<void>;
port?: number;
hostname?: string;
}): Promise<ISimpleTestServer> {
const hostname = options.hostname || 'localhost';
const port = options.port || await getAvailablePort();
const server = plugins.net.createServer((socket) => {
if (options.onConnection) {
const result = options.onConnection(socket);
if (result && typeof result.then === 'function') {
result.catch(error => {
console.error('Error in onConnection handler:', error);
socket.destroy();
});
}
}
});
return new Promise((resolve, reject) => {
server.listen(port, hostname, () => {
resolve({
server,
hostname,
port
});
});
server.on('error', reject);
});
}