136 lines
3.9 KiB
TypeScript
136 lines
3.9 KiB
TypeScript
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
|
import * as plugins from '../ts/plugins.js';
|
|
import { DcRouterDb, EmailServerSettingsDoc } from '../ts/db/index.js';
|
|
import { EmailSettingsManager } from '../ts/email/index.js';
|
|
import type { IDcRouterOptions } from '../ts/classes.dcrouter.js';
|
|
|
|
const createTestDb = async () => {
|
|
const storagePath = plugins.path.join(
|
|
plugins.os.tmpdir(),
|
|
`dcrouter-email-settings-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
|
);
|
|
|
|
DcRouterDb.resetInstance();
|
|
const db = DcRouterDb.getInstance({
|
|
storagePath,
|
|
dbName: `dcrouter-email-settings-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
|
});
|
|
await db.start();
|
|
await db.getDb().mongoDb.createCollection('__test_init');
|
|
|
|
return {
|
|
async cleanup() {
|
|
await db.stop();
|
|
DcRouterDb.resetInstance();
|
|
await plugins.fs.promises.rm(storagePath, { recursive: true, force: true });
|
|
},
|
|
};
|
|
};
|
|
|
|
const testDbPromise = createTestDb();
|
|
|
|
const clearSettings = async () => {
|
|
for (const doc of await EmailServerSettingsDoc.findAll()) {
|
|
await doc.delete();
|
|
}
|
|
};
|
|
|
|
tap.test('EmailSettingsManager does not backfill from legacy constructor options', async () => {
|
|
await testDbPromise;
|
|
await clearSettings();
|
|
|
|
const options: IDcRouterOptions = {
|
|
emailConfig: {
|
|
hostname: 'mail.example.com',
|
|
ports: [25, 587],
|
|
domains: [],
|
|
routes: [],
|
|
maxMessageSize: 1024,
|
|
},
|
|
emailPortConfig: {
|
|
portMapping: { 25: 10025, 587: 10587 },
|
|
},
|
|
};
|
|
|
|
const manager = new EmailSettingsManager(options);
|
|
await manager.start();
|
|
|
|
expect(manager.getPublicSettings().enabled).toEqual(false);
|
|
expect(manager.getPublicSettings().hostname).toEqual(null);
|
|
expect(options.emailConfig).toBeUndefined();
|
|
expect(options.emailPortConfig).toBeUndefined();
|
|
|
|
await clearSettings();
|
|
const migratedDoc = new EmailServerSettingsDoc();
|
|
migratedDoc.settingsId = 'email-server-settings';
|
|
migratedDoc.enabled = true;
|
|
migratedDoc.emailConfig = {
|
|
hostname: 'mail.example.com',
|
|
ports: [25, 587],
|
|
domains: [],
|
|
routes: [],
|
|
maxMessageSize: 1024,
|
|
};
|
|
migratedDoc.emailPortConfig = {
|
|
portMapping: { 25: 10025, 587: 10587 },
|
|
};
|
|
migratedDoc.updatedAt = Date.now();
|
|
migratedDoc.updatedBy = 'migration';
|
|
await migratedDoc.save();
|
|
|
|
const secondOptions: IDcRouterOptions = {
|
|
emailConfig: {
|
|
hostname: 'ignored.example.com',
|
|
ports: [2525],
|
|
domains: [],
|
|
routes: [],
|
|
},
|
|
};
|
|
const secondManager = new EmailSettingsManager(secondOptions);
|
|
await secondManager.start();
|
|
|
|
expect(secondManager.getPublicSettings().hostname).toEqual('mail.example.com');
|
|
expect(secondOptions.emailConfig?.hostname).toEqual('mail.example.com');
|
|
});
|
|
|
|
tap.test('EmailSettingsManager updates redacted mutable server settings', async () => {
|
|
await testDbPromise;
|
|
await clearSettings();
|
|
|
|
const options: IDcRouterOptions = {};
|
|
const manager = new EmailSettingsManager(options);
|
|
await manager.start();
|
|
expect(manager.getPublicSettings().enabled).toEqual(false);
|
|
expect(options.emailConfig).toBeUndefined();
|
|
|
|
const settings = await manager.updateSettings(
|
|
{
|
|
enabled: true,
|
|
hostname: 'smtp.example.com',
|
|
ports: [587, 25, 587],
|
|
portMapping: { 25: 10025, 587: 10587 },
|
|
maxMessageSize: 2048,
|
|
},
|
|
'tester',
|
|
);
|
|
|
|
expect(settings.enabled).toEqual(true);
|
|
expect(settings.ports).toEqual([25, 587]);
|
|
expect(settings.portMapping?.[587]).toEqual(10587);
|
|
expect(options.emailConfig?.hostname).toEqual('smtp.example.com');
|
|
expect(options.emailConfig?.maxMessageSize).toEqual(2048);
|
|
|
|
await manager.updateSettings({ enabled: false }, 'tester');
|
|
expect(manager.getPublicSettings().enabled).toEqual(false);
|
|
expect(options.emailConfig).toBeUndefined();
|
|
});
|
|
|
|
tap.test('cleanup', async () => {
|
|
const testDb = await testDbPromise;
|
|
await clearSettings();
|
|
await testDb.cleanup();
|
|
await tap.stopForcefully();
|
|
});
|
|
|
|
export default tap.start();
|