Skip to content

test: sign test requests in with real credentials and persona clients - #1018

Draft
bencap wants to merge 5 commits into
integration/rls-baselinefrom
test/bencap/834/real-auth
Draft

bencap wants to merge 5 commits into
integration/rls-baselinefrom
test/bencap/834/real-auth

Conversation

@bencap

@bencap bencap commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

Router tests replaced get_current_user and require_current_user with stubs, so bind_principal never ran in a test. Once row-level security reads the principal, a permission test written that way passes without proving anything. This PR makes every test request sign in through the real dependency, and names each request's actor through the client that sends it.

  • Real credentials: a request carries a JWT signed with an RSA key made for the test run, or an access key stored for the user. The trusted_signing_key fixture makes the app trust that key for one test by patching ORCID_JWT_SIGNING_PUBLIC_KEY and ORCID_JWT_AUDIENCE. It's the only place the tests reach into mavedb, and it becomes a settings value once settings are read at call time.

  • Persona clients: the request under test goes through a client named for its actor:

    • owner_client, outsider_client, admin_client and anonymous_client
    • make_contributor(urn, Model), which adds the contributor and returns their client
    • client_for(user, via=...) for anyone else

    client, signed in as TEST_USER, stays for setup. DependencyOverrider and the three *_app_overrides fixtures are deleted.

  • Credentials and anonymity are explicit: Jwt() acts with every role the user holds, and Jwt(active_roles=[...]) sends X-Active-Roles. ApiKey(role=...) creates a key with at most one role. ANONYMOUS replaces None, so a persona that was never set fails rather than quietly running anonymous.

  • Shared session: the test get_db override clears the bound principal after each request. close() keeps session.info, so the next query would otherwise run as the last caller.

  • tests/db/test_principal.py: signs real tokens instead of patching decode_jwt.

  • Rule: .claude/rules/tests.md asks permission tests to make the request under test through one of these clients.

Part of #834

How personas are defined (for #810)

A persona is a user row plus a credential. Constant personas (TEST_USER, EXTRA_USER, ADMIN_USER, OUTSIDER_USER, CONTRIBUTOR_USER) live in tests/helpers/constants.py. client_for adds a constant persona's user if it's missing, so a JWT never sends the app to ORCID for an unknown user. A relationship to data, such as contributor, comes from the fixture that creates it, so the client's name is given where it becomes true. #810's canary adds its own personas (mapper, calibrator, collection members) the same way.

Real sign-in has one side effect to know about. The first JWT request in a test writes last_login, clears is_first_login and commits, which expires loaded ORM objects. Tests that assert on login state use ApiKey.

Tests

  • New tests/routers/test_authentication.py covers the admin-only user list by credential (admin by JWT with every role, acting as admin or acting with no role; admin key with and without the admin role; ordinary user; anonymous). It also checks that /users/me resolves a JWT and an access key to their user, and that a token signed by an untrusted key gets 403.
  • The other router tests were converted to persona clients. No assertion was loosened, skipped or deleted.
  • test_permissions.py and test_access_keys.py now act as real owners and contributors, instead of reassigning ownership or keys by hand. The calibration permission tests also assert investigatorProvided.
  • These tests changed only their setup:
    • Job-run tests read the id before the request.
    • Two lean-variant streaming tests pass the client's credential headers in the ASGI scope.
    • Two has-logged-in tests use an access key, so sign-in doesn't clear the flag they check.
  • Ran bin/check and the PR set (bin/t tests/ -n 6 -m "not network and not slow"), rebased on the current release-2026.3.4: 5792 passed, 22 skipped.

Checklist

  • Targets the open patch branch if this is a small, non-breaking change, or the cycle's release-<year>.<n>.0 branch otherwise; never main
  • API schema change? A matching mavedb-ui PR regenerates src/schema/openapi.d.ts (no schema change)
  • Docs and instruction files touched by this change are updated

Router tests replaced get_current_user and require_current_user with
stubs, so the code that binds the request's principal never ran in a
test. Once row-level security reads that principal, every permission
test would have passed while proving nothing.

Requests now carry a JWT signed with a key made for the test run, or
an access key stored for the user, and the real dependency checks
either one. Each test makes its request through a client named for
the actor, such as owner_client, outsider_client, admin_client,
anonymous_client, make_contributor or client_for(user, via=...),
instead of switching one shared client. ANONYMOUS replaces None, so
an unset persona fails loudly instead of running anonymous, and
Jwt(active_roles=...) and ApiKey(role=...) say which credential and
roles a request uses.

The shared test session now drops its bound principal after each
request, because close() keeps session.info and the next query would
otherwise run as the last caller. The trusted_signing_key fixture
holds the one patch of mavedb internals left, the ORCID public key
and audience; it becomes a settings value when settings are read at
call time.
Expect: new permission tests make the request under test through a
persona fixture, a relation fixture such as make_contributor, or
client_for, not through the default client or a dependency override.
@bencap bencap added the app: backend Task implementation touches the backend label Nov 25, 2026
@coveralls

coveralls commented Nov 25, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 46492352014

Warning

No base build found for commit 942937f on integration/rls-baseline.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 91.382%

Details

  • Patch coverage: No coverable lines changed in this PR.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 20050
Covered Lines: 18322
Line Coverage: 91.38%
Coverage Strength: 0.91 hits per line

💛 - Coveralls

Base automatically changed from release-2026.3.4 to main November 30, 2026 18:02
@bencap
bencap changed the base branch from main to release-2026.3.5 November 30, 2026 18:03
# Conflicts:
#	tests/routers/test_score_set.py
…ient

Three CSV streaming tests arrived with 2026.3.4 and passed the app to
_get_streamed, which now takes a client so the request carries real
credentials.
@bencap
bencap changed the base branch from release-2026.3.5 to integration/rls-baseline November 30, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: backend Task implementation touches the backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants