A verification email was sent once, at registration. If it was lost, filtered, or arrived after the 24-hour token had expired, the customer had no route back except registering again with a different address. POST /api/customers/resend-verification mints a fresh token and sends the mail, and the account page grows a "Send it again" button beside the warning that was already there. The button only exists while the address is unverified: on a verified account it would be a control whose only outcome is a refusal, and the endpoint refuses that case rather than sending a pointless email. The mint-token-and-send sequence now lives in one place. Registration and the email change already did the same three steps - supersede any outstanding token, mint a new one, send it - and this would have been a third copy. The step most likely to be dropped in a copy is the supersede, and it is the one that matters: without it an older message still sitting in the inbox goes on verifying. Anything that makes the server send mail on request is an abuse vector, so this is rate limited to three an hour, keyed on the customer id. That is tighter than either existing limiter and sidesteps #84's IPv6 problem entirely, since a signed-in caller has an identity better than an address to count against and cannot escape the bucket by moving within a delegated prefix. The refusal says the mail probably did send and to check the spam folder, which is both more useful and more honest than a bare 429. The claim that keying on customer id also solved test isolation was wrong, and the tests caught it. resetDb truncates with RESTART IDENTITY, so every integration test's first customer is id 1: three tests that each sent once left the fourth starting at its limit, and two tests failed on a 429 they never asked for. A "fresh customer per test" is a fresh row with a recycled identity. The limiter now has an explicit exported store the suite clears between tests, and the comment that claimed otherwise has been corrected rather than left to mislead the next reader. Verification: seven integration tests covering the unauthenticated refusal, the send, the new token invalidating the old one - asserted through the endpoint that would honour the stale link rather than by counting rows - the new link actually verifying, the already-verified refusal, the allowance stopping the fourth send rather than merely reporting it, and one customer's exhausted allowance leaving another's intact. Three end-to-end tests for the button, its confirmation and the message on the fourth click. The 33 integration tests across the three suites this touched all pass, as do the 199 backend unit tests. tsc clean on both sides, ESLint no errors. Closes #110 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
151 lines
5.6 KiB
TypeScript
151 lines
5.6 KiB
TypeScript
import request from 'supertest';
|
|
import app from '../../src/app';
|
|
import { pool } from '../../src/db';
|
|
import { resetDb, closeDb } from './setup/testDb';
|
|
import { verificationResendStore } from '../../src/rateLimit';
|
|
|
|
jest.mock('../../src/mailer', () => ({
|
|
sendMail: jest.fn().mockResolvedValue(undefined)
|
|
}));
|
|
import { sendMail } from '../../src/mailer';
|
|
const sentMail = sendMail as jest.MockedFunction<typeof sendMail>;
|
|
|
|
const PASSWORD = 'supersecret123';
|
|
|
|
beforeEach(async () => {
|
|
await resetDb();
|
|
sentMail.mockClear();
|
|
// resetDb truncates with RESTART IDENTITY, so every test's first customer is
|
|
// id 1 and the limiter — keyed on customer id, with a process-wide store —
|
|
// hands them all the same bucket. Without this, three tests that each send
|
|
// once leave the fourth starting at its limit.
|
|
await verificationResendStore.resetAll?.();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await pool.end();
|
|
await closeDb();
|
|
});
|
|
|
|
// Each test registers its own customer. That alone does NOT isolate the
|
|
// allowance, which is what the beforeEach above is for: RESTART IDENTITY hands
|
|
// every test the same customer id, so "a fresh customer" is a fresh row with a
|
|
// recycled identity. This is the same class of leakage #62 and #84 recorded,
|
|
// surviving a key that looked like it had solved it.
|
|
async function register(email: string) {
|
|
const agent = request.agent(app);
|
|
const res = await agent
|
|
.post('/api/customers/register')
|
|
.send({ email, password: PASSWORD, firstName: 'Thom', lastName: 'Lamb' });
|
|
expect(res.status).toBe(200);
|
|
sentMail.mockClear();
|
|
return agent;
|
|
}
|
|
|
|
const tokensFor = async (email: string) => {
|
|
const { rows } = await pool.query(
|
|
`SELECT t.token FROM customer_tokens t
|
|
JOIN customers c ON c.id = t.customer_id
|
|
WHERE c.email = $1 AND t.kind = 'verify_email'`,
|
|
[email]
|
|
);
|
|
return rows.map(r => r.token as string);
|
|
};
|
|
|
|
describe('resending your own verification email', () => {
|
|
it('refuses an unauthenticated caller', async () => {
|
|
const res = await request(app).post('/api/customers/resend-verification');
|
|
expect(res.status).toBe(401);
|
|
expect(sentMail).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('sends to the address on the account', async () => {
|
|
const email = 'resend1@example.com';
|
|
const agent = await register(email);
|
|
|
|
const res = await agent.post('/api/customers/resend-verification');
|
|
|
|
expect(res.status).toBe(204);
|
|
expect(sentMail).toHaveBeenCalledTimes(1);
|
|
expect(String(sentMail.mock.calls[0][0])).toBe(email);
|
|
});
|
|
|
|
// The point of the whole thing. An un-superseded link means a message still
|
|
// sitting in the inbox goes on working, which is the case the supersede in
|
|
// issueVerificationEmail exists to prevent.
|
|
it('mints a new token and invalidates the previous one', async () => {
|
|
const email = 'resend2@example.com';
|
|
const agent = await register(email);
|
|
|
|
const [before] = await tokensFor(email);
|
|
expect(before).toBeDefined();
|
|
|
|
await agent.post('/api/customers/resend-verification');
|
|
|
|
const after = await tokensFor(email);
|
|
expect(after).toHaveLength(1);
|
|
expect(after[0]).not.toBe(before);
|
|
|
|
// And the old link is genuinely dead, asserted through the endpoint that
|
|
// would honour it rather than by counting rows.
|
|
const stale = await request(app).post('/api/customers/verify-email').send({ token: before });
|
|
expect(stale.status).toBe(400);
|
|
});
|
|
|
|
it('the new link verifies the address', async () => {
|
|
const email = 'resend3@example.com';
|
|
const agent = await register(email);
|
|
|
|
await agent.post('/api/customers/resend-verification');
|
|
const [token] = await tokensFor(email);
|
|
|
|
const res = await request(app).post('/api/customers/verify-email').send({ token });
|
|
|
|
expect(res.status).toBe(200);
|
|
const me = await agent.get('/api/customers/me');
|
|
expect(me.body.email_verified).toBe(true);
|
|
});
|
|
|
|
it('refuses once the address is already verified', async () => {
|
|
const email = 'resend4@example.com';
|
|
const agent = await register(email);
|
|
const [token] = await tokensFor(email);
|
|
await request(app).post('/api/customers/verify-email').send({ token });
|
|
sentMail.mockClear();
|
|
|
|
const res = await agent.post('/api/customers/resend-verification');
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toBe('your email address is already verified');
|
|
expect(sentMail).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// Three per hour. The fourth is refused, and the message says what actually
|
|
// happened rather than only that a limit exists.
|
|
it('stops after the allowance, with a message worth reading', async () => {
|
|
const agent = await register('resend5@example.com');
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
expect((await agent.post('/api/customers/resend-verification')).status).toBe(204);
|
|
}
|
|
|
|
const fourth = await agent.post('/api/customers/resend-verification');
|
|
|
|
expect(fourth.status).toBe(429);
|
|
expect(String(fourth.body.error)).toContain('spam folder');
|
|
// Refused rather than merely reported: the fourth send must not have gone.
|
|
expect(sentMail).toHaveBeenCalledTimes(3);
|
|
});
|
|
|
|
// The allowance is per customer, not per address or per caller. Keying it any
|
|
// more coarsely would let one customer spend everybody else's.
|
|
it('one customer exhausting the allowance does not affect another', async () => {
|
|
const first = await register('resend6@example.com');
|
|
for (let i = 0; i < 3; i++) await first.post('/api/customers/resend-verification');
|
|
expect((await first.post('/api/customers/resend-verification')).status).toBe(429);
|
|
|
|
const second = await register('resend7@example.com');
|
|
expect((await second.post('/api/customers/resend-verification')).status).toBe(204);
|
|
});
|
|
});
|