Add Keycloak-backed tests for email-claim precedence and fallback - #97
Open
NovrusShehaj wants to merge 1 commit into
Open
Add Keycloak-backed tests for email-claim precedence and fallback#97NovrusShehaj wants to merge 1 commit into
NovrusShehaj wants to merge 1 commit into
Conversation
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.
Summary
Adds three integration tests that prove
get_current_user's email-claimprecedence/fallback logic against real, Keycloak-issued tokens — not
mocked JWTs or a stubbed identity provider. Two new OIDC clients are added to
the existing
testcontainers-backed Keycloak test realm, each shaped toproduce a specific email-claim configuration on its issued tokens, so the
three failure/precedence modes in
get_current_usercan each be exercisedagainst a token that actually has that shape.
What changed
backend/tests/fixtures/keycloak/realm.json— two new OIDC clientsadded to the
biosim-testrealm import:biosim-test-client-namespaced-email— stamps both a hardcodednamespaced
https://api.biosimulations.org/emailclaim and a plainemailclaim, to prove the namespaced claim wins.biosim-test-client-no-email— stamps neither claim, to prove a missingemail degrades to
Nonerather than crashing.backend/tests/fixtures/keycloak/container.py—NAMESPACED_EMAIL_CLIENT_IDand
NO_EMAIL_CLIENT_IDconstants for the two new clients.backend/tests/fixtures/keycloak/client.py— two new fixtures,alice_token_namespaced_emailandalice_token_no_email_claim, eachfetching a real access token for Alice from one of the new clients via the
existing
fetch_keycloak_token()real direct-grant flow.backend/tests/conftest.py— registers the two new fixtures.backend/tests/common/test_auth0.py(new file) — three tests:test_get_current_user_reads_namespaced_email_claimtest_get_current_user_falls_back_to_plain_email_claimtest_get_current_user_no_email_claim_at_allEach calls
get_current_userdirectly with a real bearer token and assertson
user.email. Markedpytest.mark.integration_local.backend/pyproject.toml— adds a[tool.basedpyright]section(
pythonVersion,venvPath,venv,extraPaths). Editor/type-checkerconfig, unrelated to the Keycloak test work but included in this commit.
Keycloak integration
No mocking of Keycloak, JWKS, or JWT verification is introduced or present.
The existing
keycloak_auth_settingsfixture (unchanged by this PR) pointsthe app's live
Auth0Settingsat the runningtestcontainersKeycloakcontainer via
monkeypatch; the actual verification code incommon/auth/auth0.py(JWKS fetch, RS256 signature check, issuer/audiencecheck) runs unmodified against real tokens fetched from Keycloak's real
/protocol/openid-connect/tokenendpoint. Verified directly: running the newtests shows live HTTP calls to the Keycloak container's token and JWKS
endpoints, e.g.:
Test coverage
3 new tests, all passing. Also re-ran the 8 pre-existing
tests/rbac_demo/test_keycloak_integration.pyend-to-end RBAC tests (sameKeycloak realm, unaffected by this PR) to confirm no regression in the
broader Keycloak test path.
Testing
All commands run against a real Docker-based Keycloak
testcontainersinstance (started for this verification); results observed directly, not
assumed.