Files
redefined-designs/backend/tests/integration/disableCustomer.integration.test.ts
T
bermudalambandClaude Opus 5 ecc2219fa5
SonarQube Analysis / sonarqube (pull_request) Failing after 38m41s
Tests / lint (pull_request) Successful in 8m37s
Tests / backend-unit (pull_request) Successful in 1m22s
Tests / frontend-e2e (pull_request) Failing after 30m44s
feat: stage new items as pending until an admin publishes them (#90)
An item used to be live on the storefront the instant it was created. Now it starts pending, and a customer sees it only once it is published.

The migration changes the column default and nothing else. Backfilling would un-publish the entire live catalogue, which is the one thing it must not do.

Hiding a pending item took four separate changes, not one, and that is the part worth knowing. The storefront's item routes had no status filter at all — sold items are listed and rendered with a Sold badge deliberately — so pending could not be expressed as one more optional filter. GET /api/items now carries an exclusion the caller cannot opt out of; GET /api/items/:id carries the same, because hiding an item from the list while still serving it by id would leave it reachable to anyone who kept a link; and GET /api/filters excludes pending from both aggregates it computes. That last one is the least obvious: a pending item would have inflated its tags' counts, so a customer would read "Rare (1)", filter by it, and be told nothing matches — and its price would have stretched the slider to a range no visible item occupies.

The tag count is computed over the joined items rather than filtered with a WHERE. A WHERE would have dropped the row for a tag whose only item is pending, and the tag would have vanished from the drawer instead of showing zero. There is a test for exactly that, because the first version of this query had that bug.

parseItemFilters is shared by the storefront and admin routes, so 'pending' parses on both. The public route refuses it explicitly rather than answering with an empty list, which would read as "no items match" instead of "you may not ask that". The storefront's URL reader is deliberately left not accepting it either, with a comment saying so, since a request guaranteed to fail is not worth constructing.

Publishing is the existing mark-available: same transition, same UPDATE, so the admin UI labels that button "Publish" when the item is pending rather than adding a second endpoint that does the same thing. Unpublish is new and is not symmetrical — it is refused for a reserved item, which someone is holding in their cart right now, and for a sold one, which is a record of something that happened rather than a draft. Both refusals name their reason, and the buttons are hidden in those states so the refusal is not how you find out.

Changing a column default has reach, and it surfaced eight test fixtures that silently depended on it. Each is now explicit about the status it wants rather than inheriting one — better practice regardless, and immune to the next default change. Two tests also used 'pending' as their example of an *unknown* status; both would have quietly become tautologies, so they now use one that is genuinely unknown.

Verified: 98 unit, 160 integration and 94 end-to-end passing, the last on a freshly created container. One earlier run showed a single failure in favorites.spec.ts; it passes in isolation and on a clean container, and is the cross-spec interference already recorded against the suite rather than anything from this change.

Refs #90
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 12:40:08 -05:00

206 lines
8.1 KiB
TypeScript

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();
});
const PASSWORD = 'supersecret123';
async function register(email: string) {
const agent = request.agent(app);
const res = await agent.post('/api/customers/register').send({ email, password: PASSWORD });
expect(res.status).toBe(200);
const { rows } = await pool.query(`SELECT id FROM customers WHERE email = $1`, [email]);
return { agent, id: rows[0].id as number };
}
async function createItem(name: string) {
const { rows } = await pool.query(
`INSERT INTO items (name, price_cents, status) VALUES ($1, 1000, 'available') RETURNING id`,
[name]
);
return rows[0].id as number;
}
describe('disabling a customer', () => {
it('is reported in the admin customer list', async () => {
const { id } = await register('flag@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
const res = await request(app).get('/api/admin/customers');
const customer = res.body.find((c: { id: number }) => c.id === id);
expect(customer.disabled_at).toBeTruthy();
});
it('refuses the sign-in with an explicit reason, not a credential failure', async () => {
const { id } = await register('told@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
const res = await request(app)
.post('/api/customers/login')
.send({ email: 'told@example.com', password: PASSWORD });
// A generic 401 would send them round the password-reset loop forever.
expect(res.status).toBe(403);
expect(res.body.error).toMatch(/disabled/i);
});
it('still refuses a sign-in with the wrong password, without revealing the disable', async () => {
const { id } = await register('wrongpw@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
const res = await request(app)
.post('/api/customers/login')
.send({ email: 'wrongpw@example.com', password: 'not-the-password' });
expect(res.status).toBe(401);
});
it('kills sessions that already existed', async () => {
const { agent, id } = await register('evicted@example.com');
expect((await agent.get('/api/customers/me')).status).toBe(200);
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
// The cookie is still held by the agent; it must stop working immediately
// rather than at its 30-day expiry.
expect((await agent.get('/api/customers/me')).status).toBe(401);
});
it('releases items the customer was holding', async () => {
const itemId = await createItem('Held item');
const { agent, id } = await register('holder@example.com');
expect((await agent.post(`/api/cart/items/${itemId}`)).status).toBe(201);
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
const item = await request(app).get(`/api/items/${itemId}`);
expect(item.body.status).toBe('available');
const { rows } = await pool.query(`SELECT COUNT(*)::int AS n FROM cart_items WHERE item_id = $1`, [itemId]);
expect(rows[0].n).toBe(0);
});
it('leaves a released item purchasable by someone else', async () => {
const itemId = await createItem('Freed item');
const { agent: first, id } = await register('first@example.com');
await first.post(`/api/cart/items/${itemId}`);
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
const { agent: second } = await register('second@example.com');
expect((await second.post(`/api/cart/items/${itemId}`)).status).toBe(201);
});
it('does not resurrect an item that was already sold', async () => {
const itemId = await createItem('Sold item');
const { agent, id } = await register('sold@example.com');
await agent.post(`/api/cart/items/${itemId}`);
await pool.query(`UPDATE items SET status = 'sold' WHERE id = $1`, [itemId]);
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
const { rows } = await pool.query(`SELECT status FROM items WHERE id = $1`, [itemId]);
expect(rows[0].status).toBe('sold');
});
it('blocks the self-service data export and account deletion', async () => {
const { agent, id } = await register('gdpr@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
expect((await agent.get('/api/customers/me/export')).status).toBe(401);
expect((await agent.delete('/api/customers/me')).status).toBe(401);
});
it('sends no reset mail and issues no token for a disabled account', async () => {
const { id } = await register('noreset@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
// Still 200, so the endpoint stays non-enumerating.
const res = await request(app)
.post('/api/customers/request-password-reset')
.send({ email: 'noreset@example.com' });
expect(res.status).toBe(200);
const { rows } = await pool.query(
`SELECT COUNT(*)::int AS n FROM customer_tokens WHERE customer_id = $1 AND kind = 'password_reset'`,
[id]
);
expect(rows[0].n).toBe(0);
});
it('refuses to complete a reset whose token predates the disable', async () => {
const { id } = await register('midreset@example.com');
await request(app).post('/api/customers/request-password-reset').send({ email: 'midreset@example.com' });
const { rows } = await pool.query(
`SELECT token FROM customer_tokens WHERE customer_id = $1 AND kind = 'password_reset'`,
[id]
);
const token = rows[0].token;
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
const res = await request(app)
.post('/api/customers/reset-password')
.send({ token, password: 'a-brand-new-password' });
expect(res.status).toBe(403);
});
it('rejects registering again with the same address', async () => {
const { id } = await register('reregister@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
// Otherwise disabling is trivially undone by signing up again.
const res = await request(app)
.post('/api/customers/register')
.send({ email: 'reregister@example.com', password: PASSWORD });
expect(res.status).toBe(409);
});
});
describe('re-enabling a customer', () => {
it('restores sign-in', async () => {
const { id } = await register('back@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
await request(app).post(`/api/admin/customers/${id}/enable`).expect(204);
const res = await request(app)
.post('/api/customers/login')
.send({ email: 'back@example.com', password: PASSWORD });
expect(res.status).toBe(200);
});
it('clears the disabled timestamp', async () => {
const { id } = await register('cleared@example.com');
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
await request(app).post(`/api/admin/customers/${id}/enable`).expect(204);
const res = await request(app).get('/api/admin/customers');
const customer = res.body.find((c: { id: number }) => c.id === id);
expect(customer.disabled_at).toBeNull();
});
it('does not give back the items that were released', async () => {
const itemId = await createItem('Not returned');
const { agent, id } = await register('norestore@example.com');
await agent.post(`/api/cart/items/${itemId}`);
await request(app).post(`/api/admin/customers/${id}/disable`).expect(204);
await request(app).post(`/api/admin/customers/${id}/enable`).expect(204);
const item = await request(app).get(`/api/items/${itemId}`);
expect(item.body.status).toBe('available');
});
it('returns 404 for a customer that does not exist', async () => {
expect((await request(app).post('/api/admin/customers/999999/disable')).status).toBe(404);
expect((await request(app).post('/api/admin/customers/999999/enable')).status).toBe(404);
});
});