Files
events-hub/backend/tests/emailLayout.test.js
T
joshuaandClaude Opus 5.5 14dd608e37 Hub 2.10.0: emails laid out as Events' and Accounts'; FastPanel ^~ /api/
- src/utils/emailLayout.js: one layout for every Hub email (Cross Code
  header with the logo, heading, button with the link under it, notes,
  tables, job steps, code blocks, footer), HTML and plain text from the
  same blocks, everything escaped. mail.sendMail attaches the logo.
- Every email moved onto it: job reports, alerts, update requests, the
  SMTP test, notices, and all emails to site admins. Billing keeps its own.
- The FastPanel email and the install page give the backend as nginx config
  with location ^~ /api/ (FastPanel's static-file rule took /api/...png).
- Proxy done checks /api/proxy-check.png doesn't reach the frontend.
- deployment.md: ^~ for the Hub's own /api/ too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 14:40:55 +02:00

68 lines
3.2 KiB
JavaScript

// The Hub's email layout (2.10.0): HTML and plain text from the same blocks, everything escaped.
const { render, logoAttachment } = require('../src/utils/emailLayout');
const { notesBlocks } = require('../src/utils/jobs');
describe('render', () => {
test('makes the HTML and its plain-text twin from the same blocks', () => {
const { html, text } = render({
heading: 'Installed',
sub: 'Now set it up',
blocks: [
{ hello: 'Jane' },
{ p: 'See https://hub.test/jobs/1 for more.' },
{ note: { kind: 'warning', title: 'Careful', text: 'Two lines\nhere' } },
{ rows: [['Site', 'events.hope.co.za'], ['Empty', null]] },
{ steps: [{ outcome: 'ok', name: 'Backup' }, { outcome: 'failed', name: 'Migrate', detail: 'P3009' }] },
{ numbered: [{ text: 'Add the site.' }, { text: 'Add this:', code: 'location ^~ /api/ {\n}' }] },
{ code: 'line 1\nline 2', label: 'Last log lines' },
{ button: { label: 'Open the job', url: 'https://hub.test/jobs/1' } },
null,
],
});
expect(text).toBe([
'Installed', 'Now set it up', 'Hi Jane,', 'See https://hub.test/jobs/1 for more.', 'Careful\nTwo lines\nhere',
'Site: events.hope.co.za', ' ✓ Backup\n ✗ Migrate (P3009)',
'1. Add the site.\n\n2. Add this:\n\n location ^~ /api/ {\n }',
'Last log lines:\nline 1\nline 2', 'Open the job: https://hub.test/jobs/1',
].join('\n\n'));
expect(html).toContain('<a href="https://hub.test/jobs/1" style="color:#1d6a9c;word-break:break-all">https://hub.test/jobs/1</a> for more.');
expect(html).toContain('Two lines<br>here');
expect(html).not.toContain('Empty');
expect(html).toContain('location ^~ /api/ {');
expect(html).toContain('>Open the job</a>');
});
test('escapes everything and only links http(s)', () => {
const { html } = render({
heading: '<script>alert(1)</script>',
blocks: [{ p: 'Tom & "Jerry" <b>' }, { button: { label: 'Go', url: 'javascript:alert(1)' } }, { code: '</pre><img src=x>' }],
});
expect(html).not.toContain('<script>');
expect(html).toContain('&lt;script&gt;alert(1)&lt;/script&gt;');
expect(html).toContain('Tom &amp; &quot;Jerry&quot; &lt;b&gt;');
expect(html).not.toContain('href="javascript:');
expect(html).toContain('href="#"');
expect(html).toContain('&lt;/pre&gt;&lt;img src=x&gt;');
});
test('shows the logo when there is one, through cid:logo', () => {
const { html } = render({ heading: 'Hi', blocks: [] });
if (logoAttachment()) {
expect(html).toContain('src="cid:logo"');
expect(logoAttachment()).toMatchObject({ cid: 'logo', contentType: 'image/png' });
} else {
expect(html).not.toContain('cid:logo');
}
});
});
describe('notesBlocks (What\'s New in the update email)', () => {
test('lists, paragraphs, continuation lines, and no Markdown marks', () => {
expect(notesBlocks('Works with any Accounts.\n\n- **Fix:** logo uploads\n failed with `x`.\n- Faster.\nAfter the list.')).toEqual([
{ p: 'Works with any Accounts.' },
{ bullets: ['Fix: logo uploads failed with x.', 'Faster.'] },
{ p: 'After the list.' },
]);
});
});