Skip to content

Fix RBAC pyright type errors and add unit tests#553

Open
swinney wants to merge 1 commit into
devfrom
fix/rbac-type-safety-and-tests
Open

Fix RBAC pyright type errors and add unit tests#553
swinney wants to merge 1 commit into
devfrom
fix/rbac-type-safety-and-tests

Conversation

@swinney

@swinney swinney commented Apr 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #552

  • Fix 3 pre-existing pyright type errors in registry.py, jwt_parser.py, and decorators.py
  • Add 46 unit tests across 5 new test files covering the full RBAC subsystem
  • All changes target src/utils/rbac/ and tests/unit/test_rbac_* — no functional behavior changes

Changes

Type Fixes (3 files, 10 line changes)

File Line Fix
registry.py:148 _resolve_permissions Set[str] = NoneOptional[Set[str]] = None
jwt_parser.py:158 assign_default_role List[str] = NoneOptional[List[str]] = None
decorators.py 8 call sites request.endpointrequest.endpoint or '<unknown>'

New Tests (5 files, 655 lines, 46 tests)

File Tests Covers
test_rbac_registry.py 17 Permission resolution, inheritance, wildcards, config validation, filtering, properties
test_rbac_permissions.py 8 has_permission, is_admin, is_expert with explicit roles
test_rbac_audit.py 8 Log levels, message format, structured JSON output
test_rbac_jwt_parser.py 6 extract_roles_from_token (4 scenarios), get_user_roles default fallback
test_rbac_decorators.py 7 require_permission, require_any_permission, check_sso_required via Flask test client

Test plan

  • pyright reports 0 errors on all 3 modified source files
  • All 46 new tests pass (pytest tests/unit/test_rbac_*.py)
  • No changes to RBAC runtime behavior — type annotations and fallback string only

🤖 Generated with Claude Code

Fix 3 pre-existing pyright type errors in the RBAC subsystem:
- registry.py: `visited: Set[str] = None` → `Optional[Set[str]]`
- jwt_parser.py: `original_roles: List[str] = None` → `Optional[List[str]]`
- decorators.py: 8 call sites passing `request.endpoint` (Optional[str])
  to `log_permission_check(endpoint: str)` — use fallback `or '<unknown>'`

Add 46 unit tests across 5 new test files covering:
- RBACRegistry: permission resolution, inheritance, wildcards, config
  validation (circular/empty/undefined), filter_valid_roles, properties
- Permissions: has_permission, is_admin, is_expert with explicit roles
- Audit: log_authentication_event, log_permission_check, log_role_assignment
  levels and structured output
- JWT Parser: extract_roles_from_token (resource_access, fallback, edge
  cases), get_user_roles with default fallback
- Decorators: require_permission, require_any_permission, check_sso_required
  via Flask test client

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
swinney pushed a commit to fasrc/archi that referenced this pull request Apr 14, 2026
Fix 3 pre-existing pyright type errors and add 46 unit tests
for the RBAC subsystem. See archi-physics#552, archi-physics#553.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@swinney swinney requested a review from Copilot June 11, 2026 17:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses pre-existing Pyright typing issues in the RBAC subsystem and adds a focused unit test suite for RBAC behavior (registry, permissions utilities, JWT role extraction, decorators, and audit logging).

Changes:

  • Fix Pyright type errors by making visited / original_roles parameters properly Optional[...] and by providing a non-None fallback for request.endpoint in audit logging call sites.
  • Add 5 new unit test modules (46 tests) covering RBAC permission resolution, config validation, role extraction/fallback, decorator behavior via Flask test client, and audit log formatting/levels.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/utils/rbac/registry.py Fixes a typing issue by making visited optional in _resolve_permissions.
src/utils/rbac/jwt_parser.py Fixes a typing issue by making original_roles optional in assign_default_role.
src/utils/rbac/decorators.py Ensures endpoint passed to audit logger is always a str via request.endpoint or '<unknown>'.
tests/unit/test_rbac_registry.py Adds unit tests for RBACRegistry permission resolution, validation, filtering, and properties.
tests/unit/test_rbac_permissions.py Adds unit tests for permission utility functions with patched registry access.
tests/unit/test_rbac_jwt_parser.py Adds unit tests for role extraction from token shapes and default-role fallback behavior.
tests/unit/test_rbac_decorators.py Adds Flask-based tests for RBAC decorators (401/403/200 behavior).
tests/unit/test_rbac_audit.py Adds tests validating audit logging level usage and message/JSON structure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants