SonarQube Analysis / sonarqube (pull_request) Failing after 12m11s
Two of the three already existed on the backend and had no caller. PUT /api/customers/me updated the name; POST /api/customers/change-password already demanded the current password and enforced the eight-character minimum. Neither was reachable from the frontend, which is why the gap was easy to miss — the API looked finished. The name endpoint accepted empty values and wrote nulls, letting a customer clear fields registration refuses to let them skip. That is the same rule disagreeing with itself, so it now refuses each by name exactly as registration does. Changing a password now ends other sessions and keeps the one making the change. Reset already deleted every session for the customer, on the reasoning that a password is changed precisely when the old one may be known to someone else — change reached the opposite conclusion for no recorded reason, and a session opened with a leaked password outlived the change meant to lock it out. The current session is spared so the change does not eject the person making it. Changing the email address is new. It asks for the current password, because swapping the address a password reset goes to is how an account is taken over and a live session alone is not enough; that also matches what change-password already required. The address is normalised and validated, an address another account holds is refused with the same 409 as registration, and on success the row is marked unverified and any outstanding verification token superseded — one already sitting in the old inbox must not be able to verify the new address. Two emails then go out, to different places. Verification to the new address, and a notice to the old one naming what the address was changed to. The notice is the only thing that tells a real owner their account was taken, and one that does not say where the address went is nearly useless to someone checking whether it was them. Both sends happen after the row is written, never before, so a change that failed cannot produce mail saying it succeeded. That notice is a sixth template in #92's system, which cost a definition and a default body. The unit tests iterate every template, so its defaults were checked against its own required placeholder without writing a new test. Verified: 199 unit and 208 integration passing. The session test signs in on a second agent, changes the password on the first, and asserts the second is refused while the first still works — the property being claimed rather than the code path being executed. One of my own assertions was wrong on the way: /me answers an unauthenticated caller with 401 and an error body, not an empty one, and the frontend is what turns that into null. Refs #111 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
150 lines
5.2 KiB
TypeScript
150 lines
5.2 KiB
TypeScript
import {
|
|
TEMPLATES,
|
|
TemplateKey,
|
|
missingPlaceholders,
|
|
renderTemplate
|
|
} from '../../src/emailTemplates';
|
|
|
|
const KEYS: TemplateKey[] = [
|
|
'verification',
|
|
'passwordReset',
|
|
'favoriteSold',
|
|
'favoriteWithdrawn',
|
|
'cartReminder',
|
|
'emailChanged'
|
|
];
|
|
|
|
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*\}\}/);
|
|
});
|
|
});
|