OpenCRE icon indicating copy to clipboard operation
OpenCRE copied to clipboard

feat(api): filter /rest/v1/standards by saved resource selection (#586)

Open skypank-coder opened this issue 4 months ago • 1 comments

What & why

Part of #586. This makes a user's saved resource selection (from PR #980 + #981) actually take effect: a logged-in user with a non-empty selection now sees only their selected standards when calling GET /rest/v1/standards. Builds directly on the merged User persistence (#980) and resource-selection API (#981).

Behaviour

  • Logged-in + non-empty selection + MyOpenCRE enabled → /rest/v1/standards returns only the selected standards. OPENCRE is always kept (it's the core graph, already special-cased).
  • Anonymous / empty selection / login disabled / MyOpenCRE disabled → full, unfiltered list (same no-op discipline as #981 — never narrow results silently for users who didn't opt in).
  • ?all=true bypasses the filter for a single request, so a user can see everything without clearing their saved selection. Documented in OpenAPI.

Design

  • One reusable helper apply_user_resource_filter(database, names) — no per-endpoint duplication.
  • Reuses _resolve_current_user (#981) and get_user_resource_selection (#980); gate matches #981 (is_login_enabled() + is_myopencre_enabled()).
  • No DB/model/migration changes. No auth-route changes.

Scope (deliberately tight)

  • In scope: /rest/v1/standards only.
  • Deferred to a follow-up: /rest/v1/ga_standards (filtering it server-side without the paired frontend ?all=true wiring would silently narrow gap-analysis inputs), and graph-node/link pruning for root_cres / text_search / find_cre (a distinct semantic decision; the client applyFilters already handles link-level filtering). Single-resource lookups (/standard/<name>, map_analysis) are intentionally never filtered — the user asked for that resource explicitly.

Testing

  • New application/tests/resource_filter_test.py, verified on real Postgres: selection → {selected}+OPENCRE; empty/anonymous/login-off/myopencre-off → full; OPENCRE always kept; ?all=true bypass.
  • Mutation-checked: removing the filter call makes the "returns only selected" tests fail, so they genuinely detect an unapplied filter.
  • OpenAPI ?all param added + spec regenerated; guardrail green (documented-views / freshness / validity / route-coverage).
  • black clean; no new mypy errors.

skypank-coder avatar Aug 02 '26 17:08 skypank-coder

Review Change Stack

Summary by CodeRabbit

  • New Features

    • Standards results can now be personalized based on a logged-in user’s saved resource selections.
    • The OpenCRE standard is always included in filtered results.
    • Added an all=true option to return the complete standards list.
  • Documentation

    • Updated API documentation to describe standards filtering and the new query parameter.
  • Tests

    • Added coverage for authenticated, anonymous, feature-disabled, empty-selection, and full-list scenarios.

Walkthrough

Changes

Standards filtering

Layer / File(s) Summary
Filtering logic and coverage
application/web/web_main.py, application/tests/resource_filter_test.py
The standards endpoint filters results by saved user selections when the relevant features and user state permit it. OPENCRE remains included. Tests cover fallback cases and all=true.
OpenAPI parameter and contract wiring
application/web/openapi_registry.py, docs/api/openapi.yaml
PathSpec supports custom parameters. The standards endpoint documents the optional all boolean parameter and its filtering behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • OWASP/OpenCRE#981: Implements the per-user resource selections used by this filtering logic.

Suggested reviewers: pa04rth, northdpole

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.53% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes filtering the standards endpoint by saved resource selection.
Description check ✅ Passed The description accurately explains the endpoint behavior, feature flags, bypass parameter, scope, implementation, and tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Aug 02 '26 17:08 coderabbitai[bot]