Repository navigation
fix: correct stale premises in passwordless live tests - #1317
Merged
Merged
Conversation
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.
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The
passwordless-auth-testjob (.github/workflows/test.yml:653-655) onlyruns when
needs.changes.outputs.email == 'true'AND(github.ref == 'refs/heads/main' || github.base_ref == 'main')-- i.e. onlyon a direct push to
mainor a PR whose base ismain. A feature-branch PRinto
devnever trips it, however much ofbackend/src/routes/auth-web.tsit touches. Epics #1250 and #1272 landed on
devthrough such PRs, so thisjob 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 aschedule, 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?.statuswas asserted"pending". Since ADR 0040 phase 2(epic #1250),
POST /auth/code/verifypromotes a first-timependingrow toverifiedin 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 wirevalue is also not the raw DB status:
userStatusForDashboard(
backend/src/routes/auth-web.ts:146-150) folds bothverifiedandapprovedto"active". Verdict: test wrong, on two counts at once.Fixed to expect
"active". Mutation: changed theverified/approvedbranch of
userStatusForDashboardto return"verified"instead of"active"-- red (RESULT status: verified), reverted, green.2. Hardcoded ORCID collided with migration 0077's live-row unique index
seedWebUser500'd with{"error":"Failed to seed user"}. The profilefixture hardcoded
orcid: "0000-0002-1825-0097"; migration 0077 addedidx_users_orcid_live_unique, so the literal is only good for the first runever -- 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 toauth-device-cli-live.test.tsfor the identical reason (its own commentnames this file as one of the leftover-row sources). Audited the rest of
test/andbackend/test/for the same literal: ~25 files reference it, butonly
auth-device-cli-live.test.ts(already fixed) and this file ever POSTit through
/admin/test-fixtures/seed-web-user'sprofile.orcidwrite path,which is the only path that reaches the unique index. The others use it in
local fixture objects or in
/auth/signupbodies that 409 on an earliercheck (username/email) before the ORCID gate is ever reached --
api.test.tseven has a comment (
test/api.test.ts:139) documenting that orderingdeliberately. 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=truewith a cookie for apendingaccount;got 200. The test seeded
pendingand signed in through/code/verify--but per finding 1, that promotes the row to
verifiedbefore the cookie isever 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_STATUSESinbackend/src/services/account-tier.ts:26excludespending) still holds.Fixed by minting the cookie while
approved(never re-tiered by thepromotion), then demoting the same row to
pendingvia the fixture endpoint-- a plain UPDATE that never touches the already-issued session -- so the
test exercises
resolveCookieUser/isActiveAccountStatusdirectly insteadof relying on a state
/code/verifycan no longer produce. Mutation: widenedACTIVE_ACCOUNT_STATUSESto["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_patchonPATCH /auth/profilewith onlygiven_name/family_name; got 200. Commitfa0b1b33("feat: username andname 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 iDis present (
backend/src/routes/auth-web.ts:943-955, 409name_is_orcid_canonical) -- ADR 0042 states this rule by name. The fixedPROFILE_EMAILfixture this test used has no ORCID, so the old assertiondisagreed 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 theno-ORCID case; (b)
orcid_verified === 1changed 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_incorrectwhen a different signed-in user tries to redeemsomeone else's email-change code; got
code_expired. Unrelated to eitherepic: introduced in
da436dea("Bind email-change codes to requestingsession", 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
!rowbranch(
backend/src/routes/auth-web.ts:1525-1526) answersCODE_EXPIRED_BODY,the same body a truly expired/used code gets.
code_incorrectis reservedfor a row that was found with a non-matching hash (the
wrong-code casejust 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
!rowbranch to returncode_incorrect-- red,reverted, green.
6. Cascading 429 on the final test -- no separate fix needed
requestCode(originalEmail)expected 200dev_skip: "unregistered"; got429. Pure cascade of #5: the hijack assertion threw before the test reached
the line that actually calls the legitimate
/auth/email/change/verify, sothe account's email was never moved off
originalEmail. The next test thenrequested a code for a still-registered, still-rate-limited address (the
beforeAllhad already used its one-per-minute slot on it) instead of theintended 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 andbackend/) clean.https://api-test.nemar.org: 29/29 (28original + 1 net-new from splitting Remove automatic collaborator addition on user approval #4).
PROFILE_EMAILfixture(
passwordless email-code auth (#569), covering findings 1-3, andemail change flow (#911), covering findings 5-6) each reran clean,back-to-back, twice.
PATCH /auth/profile (#912)block (findings 4) could not get a secondlive rerun in this session: its untouched
beforeAllsigns in through theshared, fixed
[email protected]fixture (deliberately notfreshEmail()-- see its own comment -- so the github-handle-uniquenesstest 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 isa 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
passwordless-auth-testtrigger condition (see Context above) --flagged, not changed.
PROFILE_EMAILfixed-fixture design in thePATCH /auth/profiledescribe block.
0000-0002-1825-0097literal(audited in finding 2; none reach the live unique index).