Registration collected one optional Name, so every greeting had only a whole name to use: "Hi Thom Lamb," rather than "Hi Thom,". Both parts are now captured, and the cart reminder greets by first name. Both are required of anyone new, refused individually rather than as a single "name is required" so a form that filled one and not the other is told which. The columns are nullable even so, and that is deliberate. Marking them NOT NULL would mean backfilling legacy rows with empty strings, which asserts that every customer has a name — untrue of anyone who registered while the field was optional. The table records what is actually the case; the rule that new registrations must supply both lives in the route, where a missing field can produce a message naming it. The backfill splits on the first space, and it is lossy in a way no version of this avoids. "Thom Lamb" becomes Thom and Lamb; "Mary Jane Smith" gets a last name of "Jane Smith"; names that are not two parts fare worse. It was chosen over leaving the columns empty because nothing currently lets a customer correct their own name — PUT /api/customers/me exists but no frontend calls it — so empty would have meant permanently unpersonalised for every existing customer. The migration says so, so nobody later reads backfilled values as data the customer supplied in that shape. Verified against a seeded database rather than reasoned about, because this is the part that cannot be covered by the suite: migrations run in globalSetup before any test, and the old column is gone afterwards. Six representative rows through the real migration gave Thom/Lamb, Mary/"Jane Smith", Cher/null, " Padded Name " trimmed to Padded/Name, and null and whitespace-only names left as null on both. The down migration rejoins the parts and returns all six to their original strings. The old column is dropped rather than kept alongside, so there is one source of truth instead of two that drift. The admin keeps receiving a single composed display name. It only ever shows one — the list cell and the drawer title — and never edits one, so giving it both parts plus the joining logic would be work for no reader. Churn was the bulk of this: 14 backend registrations and 10 end-to-end registration forms. A first attempt at the backend fixtures also added names to login and password-reset payloads, which would still have passed since the server ignores unknown fields, but a login test implying login takes a name is a small lie; that was reverted and redone against register calls only. Verified: 172 unit, 183 integration and 95 end-to-end passing, lint unchanged at 4 backend and 27 frontend warnings. Not covered: the cart reminder itself, which runs from a cron and had no test before this either. The greeting change is a one-line substitution in that query. Refs #106 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
167 lines
5.7 KiB
TypeScript
Executable File
167 lines
5.7 KiB
TypeScript
Executable File
import request from 'supertest';
|
|
import app from '../../src/app';
|
|
import { pool } from '../../src/db';
|
|
import { resetDb, closeDb } from './setup/testDb';
|
|
|
|
beforeEach(async () => {
|
|
await resetDb();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await pool.end();
|
|
await closeDb();
|
|
});
|
|
|
|
describe('POST /api/customers/register', () => {
|
|
// Both names are required from anyone new, so the greeting in every email has
|
|
// something to use. Refused individually rather than as one "name required",
|
|
// so a form that filled one and not the other is told which. See #106.
|
|
it('refuses a registration with no first name', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({
|
|
email: 'nofirst@example.com',
|
|
password: 'supersecret123',
|
|
lastName: 'Customer'
|
|
});
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toBe('first name is required');
|
|
});
|
|
|
|
it('refuses a registration with no last name', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({
|
|
email: 'nolast@example.com',
|
|
password: 'supersecret123',
|
|
firstName: 'Test'
|
|
});
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toBe('last name is required');
|
|
});
|
|
|
|
it('refuses names that are only whitespace', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({
|
|
email: 'blank@example.com',
|
|
password: 'supersecret123',
|
|
firstName: ' ',
|
|
lastName: 'Customer'
|
|
});
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toBe('first name is required');
|
|
});
|
|
|
|
it('stores both names, trimmed, and returns them', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({
|
|
email: 'named@example.com',
|
|
password: 'supersecret123',
|
|
firstName: ' Thom ',
|
|
lastName: ' Lamb '
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.first_name).toBe('Thom');
|
|
expect(res.body.last_name).toBe('Lamb');
|
|
|
|
const { rows } = await pool.query(
|
|
`SELECT first_name, last_name FROM customers WHERE email = $1`,
|
|
['named@example.com']
|
|
);
|
|
expect(rows[0]).toEqual({ first_name: 'Thom', last_name: 'Lamb' });
|
|
});
|
|
|
|
// The account is the customer's own, but the shape returned to them should
|
|
// not quietly grow — a password hash or a token appearing here is the kind of
|
|
// thing that goes unnoticed.
|
|
it('does not return anything beyond the public customer shape', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({
|
|
email: 'shape@example.com',
|
|
password: 'supersecret123',
|
|
firstName: 'Test',
|
|
lastName: 'Customer'
|
|
});
|
|
|
|
expect(Object.keys(res.body).sort()).toEqual([
|
|
'created_at', 'email', 'email_verified', 'favorite_alerts',
|
|
'first_name', 'id', 'last_name', 'marketing_consent'
|
|
]);
|
|
});
|
|
|
|
it('creates an account with marketing consent unchecked by default', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'jane@example.com',
|
|
password: 'supersecret123'
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.email).toBe('jane@example.com');
|
|
expect(res.body.marketing_consent).toBe(false);
|
|
expect(res.headers['set-cookie']).toBeDefined();
|
|
});
|
|
|
|
it('respects an explicit marketing opt-in', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'opt-in@example.com',
|
|
password: 'supersecret123',
|
|
marketingConsent: true
|
|
});
|
|
expect(res.body.marketing_consent).toBe(true);
|
|
});
|
|
|
|
it('rejects a duplicate email', async () => {
|
|
await request(app).post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'dupe@example.com',
|
|
password: 'supersecret123'
|
|
});
|
|
const res = await request(app).post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'dupe@example.com',
|
|
password: 'anotherpassword'
|
|
});
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('rejects a password under 8 characters', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'short@example.com',
|
|
password: '123'
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a malformed email', async () => {
|
|
const res = await request(app).post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'not-an-email',
|
|
password: 'supersecret123'
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|
|
|
|
describe('session-gated routes', () => {
|
|
it('logs in and can access /me with the returned session cookie', async () => {
|
|
const agent = request.agent(app);
|
|
await agent.post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'login-test@example.com',
|
|
password: 'supersecret123'
|
|
});
|
|
const me = await agent.get('/api/customers/me');
|
|
expect(me.status).toBe(200);
|
|
expect(me.body.email).toBe('login-test@example.com');
|
|
});
|
|
|
|
it('rejects /me with no session', async () => {
|
|
const res = await request(app).get('/api/customers/me');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('rejects login with the wrong password', async () => {
|
|
await request(app).post('/api/customers/register').send({ firstName: 'Test', lastName: 'Customer',
|
|
email: 'wrongpass@example.com',
|
|
password: 'supersecret123'
|
|
});
|
|
const res = await request(app).post('/api/customers/login').send({
|
|
email: 'wrongpass@example.com',
|
|
password: 'incorrect-password'
|
|
});
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|