Stabilize RBAC admin/users e2e by anchoring forbidden checks to AdminRoute test id - #327
Merged
Merged
Conversation
Agent-Logs-Url: https://github.com/bg-playground/BGSTM/sessions/96c7b666-e2a6-40ba-9d39-1dadf63afe5f Co-authored-by: bg-playground <259109604+bg-playground@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix admin access issue for user management page test
Stabilize RBAC admin/users e2e by anchoring forbidden checks to AdminRoute test id
May 8, 2026
bg-playground
marked this pull request as ready for review
May 8, 2026 22:39
Owner
|
LGTM ✅ — Playwright E2E Tests now passing (8/8 CI checks green), which is the empirical proof we needed. Acceptance check:
One minor follow-up worth filing: the PR description says the broad regex was matching "real admin page content" but doesn't pin down what specifically on The diff is +4/-7 across 2 files. Surgical, correct, green. Merging on green is the right call. |
3 tasks
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.
The RBAC admin e2e failure was deterministic and came from a brittle text-regex assertion, not an auth/role regression. The test could match unrelated page text (e.g. numeric content containing
403) even when/admin/usersloaded correctly for admins.Root cause
rbac.spec.tsused broad text matching (/403|forbidden|access denied/i) across the full page to infer AdminRoute denial.Changes
data-testid="admin-route-forbidden"to the forbidden-state wrapper infrontend/src/components/AdminRoute.tsx.frontend/tests/e2e/rbac.spec.tswith:page.getByTestId('admin-route-forbidden')Why this shape
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
127.0.0.11REDACTED, pid is -1(packet block)dl.google.com/usr/lib/apt/methods/https /usr/lib/apt/methods/https 172.18.0.4 --dport 80 ! -i br-e7c2ac2b3e03 -j DROP(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Goal
Fix the consistently failing frontend e2e test
tests/e2e/rbac.spec.ts:89("admin can access user management page without 403 or login redirect"). The failure is real (not flake — fails on all 3 retries deterministically) and was filed as #325 during the review of #324.Closes: #325
Root cause analysis (confirmed by code reading)
AdminRoute(frontend/src/components/AdminRoute.tsx:11–17) renders this when the authenticated user is not an admin:The test asserts the page does NOT contain text matching
/403|forbidden|access denied/i. So either:admin@test.comis being authenticated butuseAuth().user.role !== 'admin'for some reason (e.g., the seed user's role got changed;/auth/meresponse shape changed; AuthContext doesn't exposerolecorrectly post-some-recent-PR).isLoadingresolves tofalsewhileuseris still partially hydrated. Unlikely given theisLoadingguard but possible./403|forbidden|access denied/iis genuinely too broad for cross-page assertion stability long-term, but in this case it's correctly catching the realAdminRoute403 page.Investigate which one is happening before fixing. The Playwright trace from the failure (
test-results/.../trace.zip) plus a screenshot of the page state at failure should resolve hypothesis 1 vs 2 immediately.Decisions already made — do not re-open
/admin/usersshould NOT see theAdminRoute403 page. Don't weaken the test by removing this assertion./403|forbidden|access denied/iover arbitrary page text with a data-testid-anchored selector, e.g.,page.getByTestId('admin-route-forbidden'). UpdateAdminRoute.tsxto adddata-testid="admin-route-forbidden"to the wrapping<div>(or to the<h1>/<p>). This makes the assertion immune to benign text appearing on legitimate admin pages (which the old regex would falsely catch).AdminRouteto also gate onuserbeing non-null, e.g.if (isLoading || !user) return <LoadingSpinner ... />rather than relying onisAuthenticatedalone.waitForSelectorretry hack to the test as a workaround for a real product issue.Scope
1. Investigate root cause
Step through:
useAuth()returns whenadmin@test.comis logged in:user.role? Is it the string'admin', an enum int, or undefined?userpopulated at the momentAdminRouteevaluatesuser?.role !== 'admin'?/auth/me(or/auth/login) response to confirm the role field is present and a string.frontend/src/context/AuthContext.tsxandfrontend/src/components/AdminRoute.tsxover the past ~2 weeks to identify the regression-introducing commit.2. Fix the underlying bug (depends on root cause)
Most likely candidates, in order of probability:
A. AuthContext race condition —
isLoadingbecomesfalsebeforeuseris populated. Fix: inAdminRoute.tsx, change the loading guard to:B. Backend response shape changed —
/auth/meno longer returnsrole(e.g., renamed toroles: string[]). Fix inAuthContext.tsxto parse the new shape.C. Seed user
admin@test.comhas the wrong role — fix the e2e seed so this user is genuinely an admin. Look atfrontend/tests/e2e/helpers/auth.ts,frontend/tests/e2e/setup.ts, or wherever the seed lives.D.
useAuth()returnsuseras a different shape thanAdminRouteexpects (e.g., snake_case vs camelCase mismatch).Pick whichever is actually broken; do not blindly apply A.
3. Tighten the test assertion (always, regardless of root cause)
Replace the brittle regex in
frontend/tests/e2e/rbac.spec.ts:97:And in
frontend/src/components/AdminRoute.tsx, add adata-testidso the new selector resol...This pull request was created from Copilot chat.