Files
website/server/test/authMe.test.js
wtclaude fbb4b0bd91
All checks were successful
PR Checks / bot-tests (pull_request) Successful in 29s
PR Checks / client-build (pull_request) Successful in 31s
PR Checks / server-tests (pull_request) Successful in 10m34s
feat(auth): unique, changeable, verifiable email addresses (engagement Phase 1b)
Makes `users.email` unique, de-duplicates the addresses an upgrade will find,
and builds the self-service change-and-verify flow that did not exist.

The uniqueness index is on a generated `email_norm AS (LOWER(email)) STORED`
column under `utf8mb4_bin`, NOT on `email` under a `_ci` collation as the plan
specified. Every case-insensitive collation this server offers is also
accent-insensitive: `josé@x.com` and `jose@x.com` compare equal, and those are
two different mailboxes. The plan's index would have refused the second address
forever and the de-duplication would have nulled a legitimate account's.

A requested address is STAGED in `email_pending` and only a tokened link
installs it, so a typo cannot silently redirect account-recovery mail.

`isDuplicateUsername()` now distinguishes the two indexes. All five call sites
branch on it; each answers differently on purpose, because a public form, an
IdP callback, a half-completed invite and an admin screen do not owe the same
person the same amount of truth.

SSO reads the IdP's actual `email_verified`/`verified` claim instead of
inferring verification from an address merely being present.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-29 01:53:50 -05:00

56 lines
2.6 KiB
JavaScript

// Point the DB at a closed port BEFORE requiring anything that builds the pool,
// so any stray DB path fails fast instead of holding the process open. The cases
// here reject at requireAuth (no session token) before any query runs.
process.env.DB_HOST = '127.0.0.1'
process.env.DB_PORT = '59999'
const { test, after } = require('node:test')
const assert = require('node:assert/strict')
const { startApp } = require('./_helper')
const authRouter = require('../src/router/v1/auth')
const db = require('../src/utils/db')
after(() => db.close())
// The role-agnostic /auth/me/* self surface must be gated: every route sits behind
// requireAuth (any role), so an unauthenticated caller gets 401 — never a 404
// (which would mean the route isn't mounted) and never a 200.
test('/auth/me/account* rejects unauthenticated callers with 401', async () => {
const app = await startApp((a) => a.use('/api/v1/auth', authRouter))
try {
const calls = [
['GET', '/api/v1/auth/me/account'],
['GET', '/api/v1/auth/me/account/identities'],
['PATCH', '/api/v1/auth/me/account/username', { username: 'someone' }],
['PATCH', '/api/v1/auth/me/account/password', { newPassword: 'abcd1234' }],
// Engagement Phase 1b — the email change/verify request half is self-service
// and must be gated exactly like the rest. (The CONFIRM half is public by
// design and lives at /auth/email/verify/:token, tested separately.)
['PATCH', '/api/v1/auth/me/account/email', { email: 'new@example.com' }],
['POST', '/api/v1/auth/me/account/email/resend'],
['DELETE', '/api/v1/auth/me/account/email/pending'],
['POST', '/api/v1/auth/me/account/totp/setup'],
['POST', '/api/v1/auth/me/account/totp/enable', { code: '123456' }],
['DELETE', '/api/v1/auth/me/account/identities/google'],
// Trusted devices + recovery codes are self-service too — same gate.
['GET', '/api/v1/auth/me/trusted-devices'],
['POST', '/api/v1/auth/me/trusted-devices', { deviceName: 'X' }],
['DELETE', '/api/v1/auth/me/trusted-devices'],
['DELETE', '/api/v1/auth/me/trusted-devices/1'],
['GET', '/api/v1/auth/me/account/recovery-codes/status'],
['POST', '/api/v1/auth/me/account/recovery-codes/generate', { currentPassword: 'x' }],
]
for (const [method, path, body] of calls) {
const res = await fetch(app.url + path, {
method,
headers: body ? { 'Content-Type': 'application/json' } : {},
body: body ? JSON.stringify(body) : undefined,
})
assert.equal(res.status, 401, `${method} ${path} should be 401, got ${res.status}`)
}
} finally {
await app.close()
}
})