Every customer email was a template literal in the route that sent it, so changing a word meant a code change, a review and a deploy. All five now render from markdown that an admin can edit: verification, password reset, favorite sold, favorite withdrawn, and the cart reminder. Five, not the four the issue counted — the favorite alerts have separate copy for sold and withdrawn.
markdown-it runs with html disabled, which is its default and the reason for choosing it over marked. Raw HTML in a stored body is escaped rather than passed through, so editing copy cannot put script into a customer's inbox. That is a stronger guarantee than sanitising output, because there is no output to sanitise.
Values are substituted into the markdown before it renders, which means a value that should become a list has to arrive as markdown. The cart reminder previously built li elements by hand; those would now be escaped and shown to the customer as literal angle brackets, so it emits a markdown list instead. The greeting is one placeholder rather than a bare name, so a template author writes {{greeting}} instead of "Hi {{firstName}}," — which reads as "Hi ," for anyone who registered before first names were required.
Saving is refused when a body has dropped a placeholder it needs, naming all of them rather than the first. This is the rule that separates a convenience from a way to break password resets from a settings screen: a reset email with no link still sends, still looks correct in the log, and is useless to everyone who receives it.
The favorite alerts' consent sentence is appended by the server and is not editable. It explains why the customer is receiving the mail, which is a compliance artifact rather than copy, and editing wording should not be able to delete it.
Unset templates fall back to the built-in defaults, so an install that never touches the settings screen behaves exactly as it did. The API reports an uncustomised template as null rather than as its default text, so "never edited" stays distinguishable from "edited to something identical", and DELETE restores the default by forgetting the row rather than writing the default into it.
Two problems surfaced during verification, both worth recording.
Five favorite-alert tests failed with no error and no mail. The cause was not this code: resetDb does not truncate admin_settings, so a subject of "Gone" stored by the new template tests survived into a later suite and changed the mail it was asserting on. Cleaning up inside the template tests would have fixed only that pairing, so resetDb now clears stored templates for every suite — template rows are test data like any other, and one outliving the suite that wrote it makes a failure appear somewhere unrelated.
The withdrawal notification then failed on timing. Loading copy from the database made the sender async, and the removal path was fire-and-forget, so the response could beat the mail out of the door. Dispatch was previously synchronous even though the sends themselves were not awaited; that is now restored by awaiting it.
Verified: 197 unit and 195 integration passing, lint unchanged at 4 warnings. The admin screen for editing these follows in the next commit.
Refs #92
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
149 lines
5.2 KiB
TypeScript
149 lines
5.2 KiB
TypeScript
import {
|
|
TEMPLATES,
|
|
TemplateKey,
|
|
missingPlaceholders,
|
|
renderTemplate
|
|
} from '../../src/emailTemplates';
|
|
|
|
const KEYS: TemplateKey[] = [
|
|
'verification',
|
|
'passwordReset',
|
|
'favoriteSold',
|
|
'favoriteWithdrawn',
|
|
'cartReminder'
|
|
];
|
|
|
|
describe('the built-in templates', () => {
|
|
it.each(KEYS)('%s has a default subject and body', (key) => {
|
|
expect(TEMPLATES[key].defaultSubject.trim()).not.toBe('');
|
|
expect(TEMPLATES[key].defaultBody.trim()).not.toBe('');
|
|
});
|
|
|
|
// A default that would be refused on save is a default nobody can edit and
|
|
// put back.
|
|
it.each(KEYS)('%s default body satisfies its own required placeholders', (key) => {
|
|
expect(missingPlaceholders(key, TEMPLATES[key].defaultBody)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('missingPlaceholders', () => {
|
|
it('names the placeholder a body has dropped', () => {
|
|
expect(missingPlaceholders('passwordReset', 'Hello, no link here.')).toEqual(['resetUrl']);
|
|
});
|
|
|
|
it('is satisfied once the placeholder is present', () => {
|
|
expect(missingPlaceholders('passwordReset', 'Reset it [here]({{resetUrl}}).')).toEqual([]);
|
|
});
|
|
|
|
it('reports every missing placeholder rather than the first', () => {
|
|
const missing = missingPlaceholders('cartReminder', 'You have things.');
|
|
expect(missing).toContain('itemList');
|
|
expect(missing).toContain('cartUrl');
|
|
});
|
|
|
|
it('tolerates whitespace inside the braces', () => {
|
|
expect(missingPlaceholders('passwordReset', 'Go [here]({{ resetUrl }}).')).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('renderTemplate', () => {
|
|
const resetValues = { resetUrl: 'https://shop.test/reset-password?token=abc' };
|
|
|
|
it('substitutes a placeholder into the rendered body', () => {
|
|
const { html } = renderTemplate('passwordReset', {}, resetValues);
|
|
expect(html).toContain('https://shop.test/reset-password?token=abc');
|
|
expect(html).not.toContain('{{resetUrl}}');
|
|
});
|
|
|
|
it('renders markdown as HTML', () => {
|
|
const { html } = renderTemplate(
|
|
'passwordReset',
|
|
{ body: 'Use **this** [link]({{resetUrl}}).' },
|
|
resetValues
|
|
);
|
|
expect(html).toContain('<strong>this</strong>');
|
|
expect(html).toContain('<a href="https://shop.test/reset-password?token=abc"');
|
|
});
|
|
|
|
// The reason markdown-it runs with html disabled. An admin editing copy must
|
|
// not be able to put script into a customer's inbox, and sanitising after the
|
|
// fact is a weaker guarantee than never emitting it.
|
|
it('escapes raw HTML in a stored body rather than passing it through', () => {
|
|
const { html } = renderTemplate(
|
|
'passwordReset',
|
|
{ body: '<script>alert(1)</script> [link]({{resetUrl}})' },
|
|
resetValues
|
|
);
|
|
expect(html).not.toContain('<script>');
|
|
expect(html).toContain('<script>');
|
|
});
|
|
|
|
it('falls back to the built-in body when nothing is stored', () => {
|
|
const stored = renderTemplate('passwordReset', {}, resetValues);
|
|
const explicit = renderTemplate(
|
|
'passwordReset',
|
|
{ body: TEMPLATES.passwordReset.defaultBody },
|
|
resetValues
|
|
);
|
|
expect(stored.html).toBe(explicit.html);
|
|
});
|
|
|
|
it('uses a stored subject over the default, and substitutes into it', () => {
|
|
const { subject } = renderTemplate(
|
|
'favoriteSold',
|
|
{ subject: '{{itemName}} is gone' },
|
|
{ itemName: 'Oak table', siteUrl: 'https://shop.test' }
|
|
);
|
|
expect(subject).toBe('Oak table is gone');
|
|
});
|
|
|
|
// Values are substituted into the markdown source, so a value that needs to
|
|
// become a list has to arrive as markdown. Emitting HTML here would be
|
|
// escaped and shown to the customer as literal tags.
|
|
it('renders a markdown list supplied as a placeholder value', () => {
|
|
const { html } = renderTemplate(
|
|
'cartReminder',
|
|
{},
|
|
{
|
|
greeting: 'Hi Thom,',
|
|
itemList: '- Oak table\n- Brass lamp',
|
|
cartUrl: 'https://shop.test/cart'
|
|
}
|
|
);
|
|
expect(html).toContain('<ul>');
|
|
expect(html).toContain('<li>Oak table</li>');
|
|
});
|
|
|
|
// The consent sentence explains why the email is lawful to send. It is
|
|
// appended by the server precisely so that editing the copy cannot remove it.
|
|
it.each(['favoriteSold', 'favoriteWithdrawn'] as TemplateKey[])(
|
|
'appends the unremovable consent footer to %s',
|
|
(key) => {
|
|
const { html } = renderTemplate(
|
|
key,
|
|
{ body: 'Short replacement copy about {{itemName}}.' },
|
|
{ itemName: 'Oak table', siteUrl: 'https://shop.test' }
|
|
);
|
|
expect(html).toContain('favorited');
|
|
expect(html).toContain('account page');
|
|
}
|
|
);
|
|
|
|
it('does not append that footer to templates it does not belong to', () => {
|
|
const { html } = renderTemplate('passwordReset', {}, resetValues);
|
|
expect(html).not.toContain('account page');
|
|
});
|
|
|
|
// An unsubstituted placeholder in the output means a caller forgot a value,
|
|
// and shipping "{{resetUrl}}" to a customer is worse than failing.
|
|
it('leaves no unsubstituted placeholders when every value is supplied', () => {
|
|
const { html, subject } = renderTemplate(
|
|
'cartReminder',
|
|
{},
|
|
{ greeting: 'Hi Thom,', itemList: '- One thing', cartUrl: 'https://shop.test/cart' }
|
|
);
|
|
expect(html).not.toMatch(/\{\{\s*\w+\s*\}\}/);
|
|
expect(subject).not.toMatch(/\{\{\s*\w+\s*\}\}/);
|
|
});
|
|
});
|