lazy loading email templates
This commit is contained in:
@@ -2,7 +2,22 @@
|
||||
jest.mock('fs', () => ({
|
||||
promises: {
|
||||
readFile: jest.fn(),
|
||||
readdir: jest.fn(),
|
||||
},
|
||||
// Include sync methods needed by logger
|
||||
existsSync: jest.fn().mockReturnValue(true),
|
||||
mkdirSync: jest.fn(),
|
||||
statSync: jest.fn().mockReturnValue({ isDirectory: () => true }),
|
||||
readdirSync: jest.fn().mockReturnValue([]),
|
||||
unlinkSync: jest.fn(),
|
||||
}));
|
||||
|
||||
// Mock the logger to avoid file system issues in tests
|
||||
jest.mock('../../../../utils/logger', () => ({
|
||||
info: jest.fn(),
|
||||
error: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
}));
|
||||
|
||||
// Clear singleton between tests
|
||||
@@ -15,6 +30,12 @@ beforeEach(() => {
|
||||
describe('TemplateManager', () => {
|
||||
const fs = require('fs').promises;
|
||||
|
||||
// Helper to set up common mocks
|
||||
const setupMocks = (templateFiles = []) => {
|
||||
fs.readdir.mockResolvedValue(templateFiles.map((t) => `${t}.html`));
|
||||
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
|
||||
};
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should create a new instance', () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
@@ -22,6 +43,7 @@ describe('TemplateManager', () => {
|
||||
const manager = new TemplateManager();
|
||||
expect(manager).toBeDefined();
|
||||
expect(manager.templates).toBeInstanceOf(Map);
|
||||
expect(manager.templateNames).toBeInstanceOf(Set);
|
||||
expect(manager.initialized).toBe(false);
|
||||
});
|
||||
|
||||
@@ -34,10 +56,89 @@ describe('TemplateManager', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('discoverTemplates', () => {
|
||||
it('should discover all HTML templates in directory', async () => {
|
||||
const templateFiles = [
|
||||
'emailVerificationToUser.html',
|
||||
'passwordResetToUser.html',
|
||||
'rentalConfirmationToUser.html',
|
||||
'README.md', // Should be ignored
|
||||
];
|
||||
fs.readdir.mockResolvedValue(templateFiles);
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.discoverTemplates();
|
||||
|
||||
expect(manager.templateNames.size).toBe(3); // Only .html files
|
||||
expect(manager.templateNames.has('emailVerificationToUser')).toBe(true);
|
||||
expect(manager.templateNames.has('passwordResetToUser')).toBe(true);
|
||||
expect(manager.templateNames.has('rentalConfirmationToUser')).toBe(true);
|
||||
expect(manager.templateNames.has('README')).toBe(false);
|
||||
});
|
||||
|
||||
it('should throw error if directory read fails', async () => {
|
||||
fs.readdir.mockRejectedValue(new Error('Directory not found'));
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await expect(manager.discoverTemplates()).rejects.toThrow('Directory not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadTemplate', () => {
|
||||
it('should load template from disk on first access', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>Template Content</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
const content = await manager.loadTemplate('testTemplate');
|
||||
|
||||
expect(content).toBe('<html>Template Content</html>');
|
||||
expect(fs.readFile).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should return cached template on subsequent access', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>Template Content</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.loadTemplate('testTemplate');
|
||||
await manager.loadTemplate('testTemplate');
|
||||
await manager.loadTemplate('testTemplate');
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalledTimes(1); // Only read once
|
||||
});
|
||||
|
||||
it('should throw error if template file not found', async () => {
|
||||
fs.readFile.mockRejectedValue(new Error('ENOENT: no such file'));
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await expect(manager.loadTemplate('nonExistent')).rejects.toThrow('ENOENT');
|
||||
});
|
||||
});
|
||||
|
||||
describe('initialize', () => {
|
||||
it('should load all templates on initialization', async () => {
|
||||
// Mock fs.readFile to return template content
|
||||
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
|
||||
const criticalTemplates = [
|
||||
'emailVerificationToUser',
|
||||
'passwordResetToUser',
|
||||
'passwordChangedToUser',
|
||||
'personalInfoChangedToUser',
|
||||
];
|
||||
|
||||
it('should discover templates and preload critical ones', async () => {
|
||||
setupMocks(criticalTemplates);
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
@@ -46,26 +147,28 @@ describe('TemplateManager', () => {
|
||||
await manager.initialize();
|
||||
|
||||
expect(manager.initialized).toBe(true);
|
||||
expect(fs.readFile).toHaveBeenCalled();
|
||||
expect(fs.readdir).toHaveBeenCalledTimes(1);
|
||||
// Should have loaded all 4 critical templates
|
||||
expect(fs.readFile).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
|
||||
it('should not re-initialize if already initialized', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
|
||||
setupMocks(criticalTemplates);
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
const callCount = fs.readFile.mock.calls.length;
|
||||
const readCallCount = fs.readFile.mock.calls.length;
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
expect(fs.readFile.mock.calls.length).toBe(callCount);
|
||||
expect(fs.readFile.mock.calls.length).toBe(readCallCount);
|
||||
});
|
||||
|
||||
it('should wait for existing initialization if in progress', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
|
||||
setupMocks(criticalTemplates);
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
@@ -74,50 +177,45 @@ describe('TemplateManager', () => {
|
||||
// Start two initializations concurrently
|
||||
await Promise.all([manager.initialize(), manager.initialize()]);
|
||||
|
||||
// Should only load templates once
|
||||
const uniquePaths = new Set(fs.readFile.mock.calls.map((call) => call[0]));
|
||||
expect(uniquePaths.size).toBeLessThanOrEqual(fs.readFile.mock.calls.length);
|
||||
// Should only discover once
|
||||
expect(fs.readdir).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should throw error if critical templates fail to load', async () => {
|
||||
// All template files fail to load
|
||||
fs.readFile.mockRejectedValue(new Error('File not found'));
|
||||
it('should throw error if critical templates are not found', async () => {
|
||||
// Only discover non-critical templates
|
||||
setupMocks(['rentalConfirmationToUser', 'feedbackToUser']);
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await expect(manager.initialize()).rejects.toThrow('Critical email templates failed to load');
|
||||
await expect(manager.initialize()).rejects.toThrow('Critical email templates not found');
|
||||
});
|
||||
|
||||
it('should succeed if critical templates load but non-critical fail', async () => {
|
||||
const criticalTemplates = [
|
||||
'emailVerificationToUser',
|
||||
'passwordResetToUser',
|
||||
'passwordChangedToUser',
|
||||
'personalInfoChangedToUser',
|
||||
];
|
||||
|
||||
fs.readFile.mockImplementation((path) => {
|
||||
const isCritical = criticalTemplates.some((t) => path.includes(t));
|
||||
if (isCritical) {
|
||||
return Promise.resolve('<html>Template content</html>');
|
||||
}
|
||||
return Promise.reject(new Error('File not found'));
|
||||
});
|
||||
it('should succeed if critical templates exist even if non-critical are missing', async () => {
|
||||
// Discover critical templates plus some others
|
||||
setupMocks([...criticalTemplates, 'rentalConfirmationToUser']);
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
// Should not throw since critical templates loaded
|
||||
await expect(manager.initialize()).resolves.not.toThrow();
|
||||
expect(manager.initialized).toBe(true);
|
||||
expect(manager.templateNames.size).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderTemplate', () => {
|
||||
const criticalTemplates = [
|
||||
'emailVerificationToUser',
|
||||
'passwordResetToUser',
|
||||
'passwordChangedToUser',
|
||||
'personalInfoChangedToUser',
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
fs.readdir.mockResolvedValue([...criticalTemplates, 'testTemplate'].map((t) => `${t}.html`));
|
||||
fs.readFile.mockResolvedValue('<html>Hello {{name}}, your email is {{email}}</html>');
|
||||
});
|
||||
|
||||
@@ -128,9 +226,6 @@ describe('TemplateManager', () => {
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
// Manually set a template for testing
|
||||
manager.templates.set('testTemplate', '<html>Hello {{name}}, your email is {{email}}</html>');
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
name: 'John',
|
||||
email: 'john@example.com',
|
||||
@@ -139,14 +234,32 @@ describe('TemplateManager', () => {
|
||||
expect(result).toBe('<html>Hello John, your email is john@example.com</html>');
|
||||
});
|
||||
|
||||
it('should replace all occurrences of a variable', async () => {
|
||||
it('should lazy load non-critical templates on first render', async () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
manager.templates.set('testTemplate', '<html>{{name}} {{name}} {{name}}</html>');
|
||||
// At this point, only critical templates are loaded
|
||||
const initialLoadCount = fs.readFile.mock.calls.length;
|
||||
expect(initialLoadCount).toBe(4); // Only critical templates
|
||||
|
||||
// Render a non-critical template
|
||||
await manager.renderTemplate('testTemplate', { name: 'John', email: 'test@example.com' });
|
||||
|
||||
// Should have loaded one more template
|
||||
expect(fs.readFile.mock.calls.length).toBe(5);
|
||||
});
|
||||
|
||||
it('should replace all occurrences of a variable', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>{{name}} {{name}} {{name}}</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
name: 'John',
|
||||
@@ -155,15 +268,15 @@ describe('TemplateManager', () => {
|
||||
expect(result).toBe('<html>John John John</html>');
|
||||
});
|
||||
|
||||
it('should replace missing variables with empty string', async () => {
|
||||
it('should not replace unspecified variables', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>Hello {{name}}, {{missing}}</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
manager.templates.set('testTemplate', '<html>Hello {{name}}, {{missing}}</html>');
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
name: 'John',
|
||||
});
|
||||
@@ -171,7 +284,7 @@ describe('TemplateManager', () => {
|
||||
expect(result).toBe('<html>Hello John, {{missing}}</html>');
|
||||
});
|
||||
|
||||
it('should use fallback template when template not found', async () => {
|
||||
it('should use fallback template when template not discovered', async () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
@@ -181,11 +294,13 @@ describe('TemplateManager', () => {
|
||||
const result = await manager.renderTemplate('nonExistentTemplate', {
|
||||
title: 'Test Title',
|
||||
message: 'Test Message',
|
||||
recipientName: 'John',
|
||||
});
|
||||
|
||||
// Should return fallback template content
|
||||
expect(result).toContain('Test Title');
|
||||
expect(result).toContain('Test Message');
|
||||
expect(result).toContain('John');
|
||||
expect(result).toContain('Village Share');
|
||||
});
|
||||
|
||||
@@ -196,86 +311,143 @@ describe('TemplateManager', () => {
|
||||
|
||||
expect(manager.initialized).toBe(false);
|
||||
|
||||
await manager.renderTemplate('someTemplate', {});
|
||||
await manager.renderTemplate('testTemplate', {});
|
||||
|
||||
expect(manager.initialized).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle empty variables object', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>No variables</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
manager.templates.set('testTemplate', '<html>No variables</html>');
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {});
|
||||
|
||||
expect(result).toBe('<html>No variables</html>');
|
||||
});
|
||||
|
||||
it('should handle null or undefined variable values', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>Hello {{name}}</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
manager.templates.set('testTemplate', '<html>Hello {{name}}</html>');
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
name: null,
|
||||
});
|
||||
|
||||
expect(result).toBe('<html>Hello </html>');
|
||||
});
|
||||
|
||||
it('should escape HTML in variables to prevent XSS', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>Hello {{name}}</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
name: '<script>alert("XSS")</script>',
|
||||
});
|
||||
|
||||
expect(result).toContain('<script>');
|
||||
expect(result).toContain('</script>');
|
||||
expect(result).not.toContain('<script>');
|
||||
});
|
||||
|
||||
it('should escape all HTML special characters', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>{{content}}</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
content: '<div class="test" data-attr=\'value\'>©</div>',
|
||||
});
|
||||
|
||||
expect(result).toContain('<div');
|
||||
expect(result).toContain('"test"');
|
||||
expect(result).toContain(''value'');
|
||||
expect(result).toContain('&copy;');
|
||||
expect(result).not.toContain('<div');
|
||||
});
|
||||
|
||||
it('should NOT escape variables ending in Section (trusted HTML)', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>{{refundSection}}</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
refundSection: '<p>Your refund of <strong>$50.00</strong> is processing.</p>',
|
||||
});
|
||||
|
||||
// Should contain actual HTML, not escaped
|
||||
expect(result).toContain('<p>Your refund');
|
||||
expect(result).toContain('<strong>$50.00</strong>');
|
||||
expect(result).not.toContain('<p>');
|
||||
});
|
||||
|
||||
it('should NOT escape variables ending in Html (trusted HTML)', async () => {
|
||||
fs.readFile.mockResolvedValue('<html>{{messageHtml}}</html>');
|
||||
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.renderTemplate('testTemplate', {
|
||||
messageHtml: '<a href="https://example.com">Click here</a>',
|
||||
});
|
||||
|
||||
// Should contain actual HTML link, not escaped
|
||||
expect(result).toContain('<a href="https://example.com">');
|
||||
expect(result).not.toContain('<a');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFallbackTemplate', () => {
|
||||
it('should return specific fallback for known templates', async () => {
|
||||
it('should return generic fallback template', () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
const fallback = manager.getFallbackTemplate('emailVerificationToUser');
|
||||
|
||||
expect(fallback).toContain('Verify Your Email');
|
||||
expect(fallback).toContain('{{verificationUrl}}');
|
||||
});
|
||||
|
||||
it('should return specific fallback for password reset', async () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
const fallback = manager.getFallbackTemplate('passwordResetToUser');
|
||||
|
||||
expect(fallback).toContain('Reset Your Password');
|
||||
expect(fallback).toContain('{{resetUrl}}');
|
||||
});
|
||||
|
||||
it('should return specific fallback for rental request', async () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
const fallback = manager.getFallbackTemplate('rentalRequestToOwner');
|
||||
|
||||
expect(fallback).toContain('New Rental Request');
|
||||
expect(fallback).toContain('{{itemName}}');
|
||||
});
|
||||
|
||||
it('should return generic fallback for unknown templates', async () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
const fallback = manager.getFallbackTemplate('unknownTemplate');
|
||||
const fallback = manager.getFallbackTemplate('anyTemplate');
|
||||
|
||||
expect(fallback).toContain('{{title}}');
|
||||
expect(fallback).toContain('{{message}}');
|
||||
expect(fallback).toContain('{{recipientName}}');
|
||||
expect(fallback).toContain('Village Share');
|
||||
});
|
||||
|
||||
it('should return same fallback for all template names', () => {
|
||||
const TemplateManager = require('../../../../services/email/core/TemplateManager');
|
||||
TemplateManager.instance = null;
|
||||
const manager = new TemplateManager();
|
||||
|
||||
const fallback1 = manager.getFallbackTemplate('emailVerificationToUser');
|
||||
const fallback2 = manager.getFallbackTemplate('rentalRequestToOwner');
|
||||
const fallback3 = manager.getFallbackTemplate('unknownTemplate');
|
||||
|
||||
// All fallbacks should be the same generic template
|
||||
expect(fallback1).toBe(fallback2);
|
||||
expect(fallback2).toBe(fallback3);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user