Files
redefined-designs/backend/tests/integration/schemaLoss.integration.test.ts
T
synAdminandClaude Opus 5 9cc82002b8
Linting / lint (pull_request) Successful in 3m19s
SonarQube Analysis / sonarqube (pull_request) Failing after 27m40s
feat(auth): groundwork for signing in with Google (#340)
Nothing a customer can see. The schema change and the configuration land on their own so the widest-reaching edit in the project can be reviewed for what it is rather than buried inside a feature.

The password hash becomes nullable. That is one line and it is not the work; the work is that every read of the column is now a question rather than a fact. Three places compared against it with bcrypt, and all three now ask first through one shared function.

That function exists because the alternative is worse than a wrong answer. bcrypt.compare throws on a null hash rather than returning false, so any call site that forgot the check would answer a sign-in attempt with a 500 instead of a refusal. On the login route that is also an oracle, because it would happen for exactly the accounts that have no password. One function rather than a null check repeated three times means the question is asked identically everywhere and a fourth site cannot forget to ask it.

Nothing writes a null yet. The first accounts without a password arrive with the sign-up path, which is why this is landed ahead of them.

The identities table is a table rather than columns on customers, because one customer may eventually hold more than one. Columns would make a second provider a migration and a third an embarrassment.

Its important column is the provider subject, and the comment on it is the whole security posture of the feature in one place: never the email. An email is a display value its owner can change and a provider may reassign; a subject is opaque and stable for the life of the account. Matching on the email would strand a customer who changed theirs and, far worse, hand their account to whoever inherited the old address. Unique across the provider and subject together, not the subject alone.

The down migration drops the table and deliberately does not restore the NOT NULL. Re-adding it fails outright once a passwordless customer exists, and a down migration that destroys accounts to satisfy a constraint is far worse than a column that is merely more permissive than it needs to be.

The redirect URI is derived from PUBLIC_URL, the same single source the WebAuthn Relying Party ID uses and for the same reason: Google compares it as an exact string and answers a mismatch with a message that says nothing about which half is wrong. Deriving it means the value is correct by construction anywhere the email links already are. The tests are mostly about what must not end up in it, since a trailing slash on PUBLIC_URL is an easy way to produce a URI that is one character from the registered one.

The config also reports whether it is enabled at all, so a developer without credentials gets a storefront that works and simply does not offer the button, rather than one that offers it and fails. Absent rather than disabled, the same choice made for a browser without WebAuthn.

Environment validation refuses to boot on one credential without the other, matching how the SMTP pair is handled. Half-configured is the case worth catching because the failure otherwise arrives at the moment a customer presses the button.

The QA compose file sets both to empty, and the comment there says why at length rather than leaving it to look like an oversight. Google refuses a redirect URI whose host is not under a domain whose ownership has been proved by DNS, and nobody can prove ownership of anything under bermudalamb.synology.me because Synology owns the registrable domain above it. That is the same wall #285 hit with Cloudflare. So QA cannot run this at all until #313 moves it to a subdomain of the real domain, at which point it is two stack variables and one console entry, with no code change either way.

Also corrects the record in #332, which lists account deletion as confirming with a password. It does not; the route takes none and the confirmation is a modal in the account page. Deletion needed no change here.

Verified: backend tsc clean for src and tests, 550 unit tests pass including new coverage of the config derivation, the null-hash comparison and the environment rules; lint clean apart from warnings that predate this branch. The integration suite needs a database this machine has no Docker for.

Closes #340

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-10 08:18:51 -05:00

77 lines
2.9 KiB
TypeScript

import { pool } from '../../src/db';
import { assertSchemaPresent, migrate, resetDb, testPool, closeDb } from './setup/testDb';
/**
* The diagnostic for #154, exercised against a database that has actually lost
* its schema.
*
* The failure this guards is not hypothetical and not cheap: the integration run
* lost its schema partway through, and it presented as 36 assertion errors about
* categories, price filters and favourite notifications. The real message —
* `relation "items" does not exist` — was further down the same log.
*
* This suite destroys and rebuilds the schema, so it restores in afterAll and
* `migrate()` is what puts it back: dropping the schema takes `pgmigrations`
* with it, so node-pg-migrate re-runs everything rather than believing it has
* already done so.
*/
async function dropEverything(): Promise<void> {
await testPool.query('DROP SCHEMA public CASCADE; CREATE SCHEMA public;');
}
afterAll(async () => {
// Put it back before anything else in the run touches it. Ordering matters:
// jest runs these in band, so a suite after this one would otherwise find
// nothing at all.
await migrate();
await pool.end();
await closeDb();
});
describe('when the database loses its schema', () => {
it('assertSchemaPresent says so, rather than letting the suite guess', async () => {
await dropEverything();
await expect(assertSchemaPresent('in a test')).rejects.toThrow(/has no schema \(in a test\)/);
await migrate();
});
it('names the tables that are missing', async () => {
await dropEverything();
// The count and a few names, not the whole list: the point is that a reader
// can tell at a glance this is a missing schema rather than a logic bug.
await expect(assertSchemaPresent('in a test')).rejects.toThrow(/Missing 22 of 22 tables/);
await expect(assertSchemaPresent('in a test')).rejects.toThrow(/items/);
await migrate();
});
it('points at the cause rather than leaving it to be rediscovered', async () => {
await dropEverything();
await expect(assertSchemaPresent('in a test')).rejects.toThrow(/TRUNCATE does not/);
await expect(assertSchemaPresent('in a test')).rejects.toThrow(/#154/);
await migrate();
});
// The path a real run takes: resetDb's TRUNCATE is what fails first, and it
// has to turn that into the schema message rather than passing on a bare
// "relation does not exist".
it('resetDb turns a failed truncate into the schema message', async () => {
await dropEverything();
await expect(resetDb()).rejects.toThrow(/has no schema \(while resetting between tests\)/);
await migrate();
});
// And it must not swallow unrelated failures behind a schema message.
it('says nothing about the schema when the schema is fine', async () => {
await expect(assertSchemaPresent('in a test')).resolves.toBeUndefined();
await expect(resetDb()).resolves.toBeUndefined();
});
});