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>
179 lines
6.5 KiB
TypeScript
179 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',
|
|
'emailChanged',
|
|
'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');
|
|
});
|
|
});
|