Skip to content

fix: correct stale premises in passwordless live tests - #1317

Merged
neuromechanist merged 1 commit into
devfrom
fix/passwordless-live-tests
Sep 8, 2026
Merged

neuromechanist merged 1 commit into
devfrom
fix/passwordless-live-tests

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Context

The passwordless-auth-test job (.github/workflows/test.yml:653-655) only
runs when needs.changes.outputs.email == 'true' AND
(github.ref == 'refs/heads/main' || github.base_ref == 'main') -- i.e. only
on a direct push to main or a PR whose base is main. A feature-branch PR
into dev never trips it, however much of backend/src/routes/auth-web.ts
it touches. Epics #1250 and #1272 landed on dev through such PRs, so this
job did not run once across either epic; it first ran again on the dev->main
release PR (#1307), by which point two epics' worth of behavior had drifted
out from under six of this file's assertions at once. Recommendation (not
implemented here): also gate this job on PRs targeting dev, or run it on a
schedule, so a premise drift is caught within one epic instead of stacking.

Run 34244951458 failed six assertions. Root-caused each below; all are
test-side (stale premises or one pre-existing bug), no backend regression.

1. Status assertion expected the DB value, not the wire value (line 170)

v.body.user?.status was asserted "pending". Since ADR 0040 phase 2
(epic #1250), POST /auth/code/verify promotes a first-time pending row to
verified in the same transaction that mints the session
(applyEmailVerification, backend/src/services/email-verification.ts:200)
-- a session can never observe status='pending' through this path. The wire
value is also not the raw DB status: userStatusForDashboard
(backend/src/routes/auth-web.ts:146-150) folds both verified and
approved to "active". Verdict: test wrong, on two counts at once.
Fixed to expect "active". Mutation: changed the verified/approved
branch of userStatusForDashboard to return "verified" instead of
"active" -- red (RESULT status: verified), reverted, green.

2. Hardcoded ORCID collided with migration 0077's live-row unique index

seedWebUser 500'd with {"error":"Failed to seed user"}. The profile
fixture hardcoded orcid: "0000-0002-1825-0097"; migration 0077 added
idx_users_orcid_live_unique, so the literal is only good for the first run
ever -- every later run collides with the row a previous run left behind.
Verdict: test wrong (stale fixture data, not an assertion). Fixed by
reusing the freshOrcid() pattern PR #1301 already added to
auth-device-cli-live.test.ts for the identical reason (its own comment
names this file as one of the leftover-row sources). Audited the rest of
test/ and backend/test/ for the same literal: ~25 files reference it, but
only auth-device-cli-live.test.ts (already fixed) and this file ever POST
it through /admin/test-fixtures/seed-web-user's profile.orcid write path,
which is the only path that reaches the unique index. The others use it in
local fixture objects or in /auth/signup bodies that 409 on an earlier
check (username/email) before the ORCID gate is ever reached -- api.test.ts
even has a comment (test/api.test.ts:139) documenting that ordering
deliberately. No other file needs a change.

3. Pending-cookie test tried to reach a state ADR 0040 made unreachable (line 531)

Expected 401 on /datasets?mine=true with a cookie for a pending account;
got 200. The test seeded pending and signed in through /code/verify --
but per finding 1, that promotes the row to verified before the cookie is
ever handed back, so the session it captured was never actually pending.
Verdict: test wrong (unreachable premise), not a backend regression --
the security property itself (ACTIVE_ACCOUNT_STATUSES in
backend/src/services/account-tier.ts:26 excludes pending) still holds.
Fixed by minting the cookie while approved (never re-tiered by the
promotion), then demoting the same row to pending via the fixture endpoint
-- a plain UPDATE that never touches the already-issued session -- so the
test exercises resolveCookieUser/isActiveAccountStatus directly instead
of relying on a state /code/verify can no longer produce. Mutation: widened
ACTIVE_ACCOUNT_STATUSES to ["verified", "approved", "pending"] -- red
(protected route returned 200 instead of 401/403), reverted, green. This is
the one place I'd flag as worth a maintainer's second look: the mutation
proves the gate is sound, but it's also proof that nothing besides this
test currently exercises "a session whose account status changed after the
cookie was issued" -- a live re-check design, working as intended, just
previously unverified this directly.

4. "Name fields are not editable" predated ADR 0042 phase #1253 (lines 785-805)

Expected 400 empty_patch on PATCH /auth/profile with only
given_name/family_name; got 200. Commit fa0b1b33 ("feat: username and
name self-service", epic #1250 phase #1253) made those fields editable for an
account with no verified ORCID linked (normalizeProfilePatch,
backend/src/services/profile.ts:177-199), refusing only when a verified iD
is present (backend/src/routes/auth-web.ts:943-955, 409
name_is_orcid_canonical) -- ADR 0042 states this rule by name. The fixed
PROFILE_EMAIL fixture this test used has no ORCID, so the old assertion
disagreed with the current, intentional contract. Verdict: test wrong.
Split into two tests against fresh accounts (not the shared fixture, since
unlike affiliation/github_username these fields have no "clear to null"
input to leave the row as it was): a no-ORCID account gets 200 and the name
lands; a verified-ORCID account gets 409 name_is_orcid_canonical.
Mutations: (a) always-refuse regardless of orcid_verified -- red on the
no-ORCID case; (b) orcid_verified === 1 changed to === 2 (never matches)
-- red on the verified-ORCID case. Both reverted, both green.

5. Hijack case expected the wrong error code (line 911) -- pre-existing bug

Expected code_incorrect when a different signed-in user tries to redeem
someone else's email-change code; got code_expired. Unrelated to either
epic: introduced in da436dea ("Bind email-change codes to requesting
session", Aug 2), the same commit that added the test itself. The session-
bound lookup (USER_BOUND_CODE_LOOKUP_SQL, bound to the bystander's own id)
finds no row at all for the bystander -- the code exists but is bound to
the legitimate requester's id -- and the route's !row branch
(backend/src/routes/auth-web.ts:1525-1526) answers CODE_EXPIRED_BODY,
the same body a truly expired/used code gets. code_incorrect is reserved
for a row that was found with a non-matching hash (the wrong-code case
just above it in the same test). The test's own comment already described
this mechanism correctly ("the per-user lookup finds no row for them") but
asserted the wrong code anyway. Verdict: test wrong (assertion
contradicted its own adjacent comment). Fixed to expect code_expired.
Mutation: changed that !row branch to return code_incorrect -- red,
reverted, green.

6. Cascading 429 on the final test -- no separate fix needed

requestCode(originalEmail) expected 200 dev_skip: "unregistered"; got
429. Pure cascade of #5: the hijack assertion threw before the test reached
the line that actually calls the legitimate /auth/email/change/verify, so
the account's email was never moved off originalEmail. The next test then
requested a code for a still-registered, still-rate-limited address (the
beforeAll had already used its one-per-minute slot on it) instead of the
intended now-unregistered one. Fixing #5 lets the email-move complete, and
this test passes with no change of its own.

Verification

  • bun run lint / bun run typecheck (both root and backend/) clean.
  • Full suite green against https://api-test.nemar.org: 29/29 (28
    original + 1 net-new from splitting Remove automatic collaborator addition on user approval #4).
  • The two describe blocks independent of the fixed PROFILE_EMAIL fixture
    (passwordless email-code auth (#569), covering findings 1-3, and
    email change flow (#911), covering findings 5-6) each reran clean,
    back-to-back, twice.
  • The PATCH /auth/profile (#912) block (findings 4) could not get a second
    live rerun in this session: its untouched beforeAll signs in through the
    shared, fixed [email protected] fixture (deliberately not
    freshEmail() -- see its own comment -- so the github-handle-uniqueness
    test stays rerun-safe without teardown), which is subject to the inline
    per-email cap in backend/src/services/auth-code.ts
    (PER_HOUR_LIMIT = 5); this session's own iteration exhausted it. This is
    a pre-existing property of that fixture, not something introduced here,
    and I left it alone rather than weaken it to suit my own verification
    cadence. Substituted a local, real-engine (bun:sqlite + actual Hono
    dispatch through the unmodified production route/middleware modules, no
    mocks) mutation-test harness for every corrected assertion instead --
    each of the five mutations above was run against it, confirmed red, then
    reverted with a clean git status --porcelain.

Deliberately left alone

  • The passwordless-auth-test trigger condition (see Context above) --
    flagged, not changed.
  • The PROFILE_EMAIL fixed-fixture design in the PATCH /auth/profile
    describe block.
  • The other ~23 files that reference the 0000-0002-1825-0097 literal
    (audited in finding 2; none reach the live unique index).

Six of auth-passwordless.test.ts's assertions were pinned to
pre-epic-#1250/#1272 behavior and had never run since (the
passwordless-auth-test CI job only fires on a PR into main):

- status assertion expected the raw DB "pending"; /code/verify has
  promoted a first-time pending row to verified since ADR 0040
  phase 2, and the wire value is userStatusForDashboard's "active"
  mapping, not the DB status.
- the populated-profile fixture hardcoded ORCID
  "0000-0002-1825-0097", now UNIQUE on live rows (migration 0077)
  and long claimed by earlier runs; switched to a freshOrcid()
  helper mirroring auth-device-cli-live.test.ts's (#1301).
- the pending-cookie-rejected test tried to reach a pending account
  through /code/verify, which no longer leaves one pending; it now
  mints the cookie while approved and demotes the row afterward to
  exercise the isActiveAccountStatus gate directly.
- "name fields are not editable" predated ADR 0042 phase #1253,
  which made given_name/family_name editable for an account with no
  verified ORCID; split into a no-ORCID success case and a
  verified-ORCID 409 case.
- the email-change hijack case expected code_incorrect; the
  session-bound lookup finds no row at all for a different user's
  session and answers code_expired (a pre-existing test bug from the
  same commit that added session binding, unrelated to the epics
  above).
- the final unregistered-address test was failing as a cascade of
  the code_incorrect bug above (the assertion failure aborted the
  test before the email change actually landed); resolved by the
  code_expired fix with no separate change needed.

Every corrected assertion verified against a mutated production
line via a local bun:sqlite + real Hono harness (no mocks): each
mutation reproduced the wrong-premise failure, each revert restored
green.
@neuromechanist
neuromechanist merged commit 1878f33 into dev Sep 8, 2026
12 checks passed
@neuromechanist
neuromechanist deleted the fix/passwordless-live-tests branch September 8, 2026 16:16

This branch was previously deployed

1 inactive deployment
npm — 926d2c71 Deployed Sep 8, 2026 by neuromechanist via publish #2383
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant