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>
178 lines
6.5 KiB
TypeScript
178 lines
6.5 KiB
TypeScript
import request from 'supertest';
|
|
import app from '../../src/app';
|
|
import { pool } from '../../src/db';
|
|
import { resetDb, closeDb } from './setup/testDb';
|
|
import { renderTemplate } from '../../src/emailTemplates';
|
|
import { loadStoredTemplate } from '../../src/routes/adminEmailTemplates';
|
|
|
|
beforeEach(async () => {
|
|
// resetDb clears stored email templates as well, so a template saved by one
|
|
// test cannot change the mail another asserts on.
|
|
await resetDb();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await pool.end();
|
|
await closeDb();
|
|
});
|
|
|
|
const VALID_RESET_BODY = 'New copy. [Choose a new password]({{resetUrl}}) within the hour.';
|
|
|
|
describe('GET /api/admin/email-templates', () => {
|
|
it('lists every template with its defaults and placeholder rules', async () => {
|
|
const res = await request(app).get('/api/admin/email-templates');
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.map((t: { key: string }) => t.key).sort()).toEqual([
|
|
'cartReminder',
|
|
'favoriteSold',
|
|
'favoriteWithdrawn',
|
|
'passwordReset',
|
|
'verification'
|
|
]);
|
|
|
|
const reset = res.body.find((t: { key: string }) => t.key === 'passwordReset');
|
|
expect(reset.required).toEqual(['resetUrl']);
|
|
expect(reset.defaultBody).toContain('{{resetUrl}}');
|
|
});
|
|
|
|
// Null rather than the default text, so the screen can tell "never edited"
|
|
// from "edited to something identical to the default".
|
|
it('reports an uncustomised template as null rather than as its default', async () => {
|
|
const res = await request(app).get('/api/admin/email-templates');
|
|
const reset = res.body.find((t: { key: string }) => t.key === 'passwordReset');
|
|
|
|
expect(reset.subject).toBeNull();
|
|
expect(reset.body).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('PUT /api/admin/email-templates/:key', () => {
|
|
it('stores a replacement subject and body', async () => {
|
|
const res = await request(app)
|
|
.put('/api/admin/email-templates/passwordReset')
|
|
.send({ subject: 'Your reset link', body: VALID_RESET_BODY });
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
const listed = await request(app).get('/api/admin/email-templates');
|
|
const reset = listed.body.find((t: { key: string }) => t.key === 'passwordReset');
|
|
expect(reset.subject).toBe('Your reset link');
|
|
expect(reset.body).toBe(VALID_RESET_BODY);
|
|
});
|
|
|
|
// The rule that keeps this from being a way to break password resets from a
|
|
// settings screen.
|
|
it('refuses a body that has dropped the required placeholder', async () => {
|
|
const res = await request(app)
|
|
.put('/api/admin/email-templates/passwordReset')
|
|
.send({ subject: 'Your reset link', body: 'Just click the thing.' });
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toContain('{{resetUrl}}');
|
|
});
|
|
|
|
it('names every missing placeholder, not just the first', async () => {
|
|
const res = await request(app)
|
|
.put('/api/admin/email-templates/cartReminder')
|
|
.send({ subject: 'Your cart', body: 'You left things behind.' });
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toContain('{{itemList}}');
|
|
expect(res.body.error).toContain('{{cartUrl}}');
|
|
});
|
|
|
|
it('refuses an empty subject or body', async () => {
|
|
const noSubject = await request(app)
|
|
.put('/api/admin/email-templates/passwordReset')
|
|
.send({ subject: ' ', body: VALID_RESET_BODY });
|
|
expect(noSubject.status).toBe(400);
|
|
|
|
const noBody = await request(app)
|
|
.put('/api/admin/email-templates/passwordReset')
|
|
.send({ subject: 'Something', body: ' ' });
|
|
expect(noBody.status).toBe(400);
|
|
});
|
|
|
|
it('refuses a template key it does not recognise', async () => {
|
|
const res = await request(app)
|
|
.put('/api/admin/email-templates/invoiceOverdue')
|
|
.send({ subject: 'x', body: 'y' });
|
|
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
// Nothing is stored when a save is refused, or a half-applied edit would sit
|
|
// there looking accepted.
|
|
it('stores nothing when it refuses', async () => {
|
|
await request(app)
|
|
.put('/api/admin/email-templates/passwordReset')
|
|
.send({ subject: 'Your reset link', body: 'No link here.' });
|
|
|
|
const { rows } = await pool.query(`SELECT key FROM admin_settings WHERE key LIKE 'email%'`);
|
|
expect(rows).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('DELETE /api/admin/email-templates/:key', () => {
|
|
it('restores the built-in copy by forgetting the stored rows', async () => {
|
|
await request(app)
|
|
.put('/api/admin/email-templates/passwordReset')
|
|
.send({ subject: 'Custom', body: VALID_RESET_BODY });
|
|
|
|
const res = await request(app).delete('/api/admin/email-templates/passwordReset');
|
|
expect(res.status).toBe(200);
|
|
|
|
const listed = await request(app).get('/api/admin/email-templates');
|
|
const reset = listed.body.find((t: { key: string }) => t.key === 'passwordReset');
|
|
expect(reset.subject).toBeNull();
|
|
expect(reset.body).toBeNull();
|
|
});
|
|
});
|
|
|
|
// The point of the whole feature: what is stored is what customers receive.
|
|
describe('what a stored template does to the email that gets sent', () => {
|
|
it('is used in place of the default once saved', async () => {
|
|
await request(app)
|
|
.put('/api/admin/email-templates/passwordReset')
|
|
.send({ subject: 'Reset requested', body: VALID_RESET_BODY });
|
|
|
|
const { subject, html } = renderTemplate(
|
|
'passwordReset',
|
|
await loadStoredTemplate('passwordReset'),
|
|
{ greeting: 'Hi Thom,', resetUrl: 'https://shop.test/r?token=abc' }
|
|
);
|
|
|
|
expect(subject).toBe('Reset requested');
|
|
expect(html).toContain('New copy.');
|
|
expect(html).toContain('https://shop.test/r?token=abc');
|
|
});
|
|
|
|
it('falls back to the built-in copy when nothing is stored', async () => {
|
|
const { subject, html } = renderTemplate(
|
|
'passwordReset',
|
|
await loadStoredTemplate('passwordReset'),
|
|
{ greeting: 'Hi,', resetUrl: 'https://shop.test/r?token=abc' }
|
|
);
|
|
|
|
expect(subject).toBe('Reset your Redefined Designs password');
|
|
expect(html).toContain('Choose a new password');
|
|
});
|
|
|
|
// Editing the copy must not be able to remove the sentence that explains why
|
|
// the email is lawful to send.
|
|
it('keeps the consent footer on a favorite alert whose body was replaced', async () => {
|
|
await request(app)
|
|
.put('/api/admin/email-templates/favoriteSold')
|
|
.send({ subject: 'Gone', body: 'Sorry, {{itemName}} sold.' });
|
|
|
|
const { html } = renderTemplate('favoriteSold', await loadStoredTemplate('favoriteSold'), {
|
|
itemName: 'Oak table',
|
|
siteUrl: 'https://shop.test'
|
|
});
|
|
|
|
expect(html).toContain('Sorry, Oak table sold.');
|
|
expect(html).toContain('account page');
|
|
});
|
|
});
|