Skip to content

test(rbac): prove hardened agent is blocked on destructive ops (CRM-182) - #282

Merged
gomessguii merged 1 commit into
developfrom
test/CRM-182-agent-destructive-ops-rbac
Aug 17, 2026
Merged

test(rbac): prove hardened agent is blocked on destructive ops (CRM-182)#282
gomessguii merged 1 commit into
developfrom
test/CRM-182-agent-destructive-ops-rbac

Conversation

@pastoriniMatheus

@pastoriniMatheus pastoriniMatheus commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

CRM-182 (lado CRM) — prova negativa que faltava

Achado 🔴 CRITICAL da review do Guilherme: o card é de segurança e não havia teste provando a propriedade de segurança no CRM (o PR do auth só prova que uma string saiu de um array).

Request spec pelo stack completo, concedendo exatamente o set que o agent mantém após o CRM-182:

  • 403 em conversations#destroy, contacts#destroy, pipeline_stages#destroy, teams#create (com o registro-alvo intacto / não criado);
  • 200 em conversations#toggle_status e teams#index.

Um refactor que re-mapeie qualquer desses controllers para uma chave que o agent ainda tem (ou remova o gate) fica vermelho. Modelado sobre partial_actions_rbac_spec.rb / contact_destructive_ops_rbac_spec.rb.

6 examples, 0 failures (rodado localmente contra Postgres + Redis). Sem mudança de código de produção — os gates já existem na develop.

🤖 Generated with Claude Code

Summary by Sourcery

Tests:

  • Add request-level RBAC coverage proving the hardened agent is denied destructive and administrative CRM operations while retaining permitted attendance actions.

Negative proof the card required. Grants exactly the attendance set the default
agent holds AFTER CRM-182 and asserts, through the full request stack:
  - 403 on conversations#destroy, contacts#destroy, pipeline_stages#destroy and
    teams#create (revoked keys), with the target record left intact / uncreated;
  - 200 on conversations#toggle_status and teams#index (kept keys).

A refactor that re-maps any of these controllers to a key the agent still holds
(or drops the gate) turns an example red. Complements the evo-auth seed/migration
change, which stops granting the keys in the first place.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Aug 17, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a new request spec to assert that the hardened default agent role is forbidden from destructive/admin CRM endpoints while still allowed on specific attendance-related actions, without changing production code.

File-Level Changes

Change Details Files
Introduce request specs that simulate a hardened agent permission set and verify RBAC behavior on destructive and attendance endpoints.
  • Define AGENT_KEYS constant modeling the exact post-CRM-182 permissions for the default agent role.
  • Stub authentication and EvoAuthService in the test setup to impersonate an agent with only AGENT_KEYS and global inbox visibility.
  • Add negative tests asserting 403 responses and no state change for conversation destroy, contact destroy, pipeline_stage destroy, and team creation endpoints.
  • Add positive tests asserting 200 responses for conversation toggle_status and teams index endpoints under the same hardened agent context.
spec/requests/api/v1/agent_destructive_ops_rbac_spec.rb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The AGENT_KEYS set is hard-coded in this spec; consider centralizing this permission set in a shared helper or referencing the real role definition so the test doesn’t silently drift from production behavior over time.
  • The use of allow_any_instance_of on Api::BaseController and EvoAuthService tightly couples the spec to internals; if possible, replace this with a shared authentication/authorization test helper or dependency injection to keep the test setup more robust to refactors.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The AGENT_KEYS set is hard-coded in this spec; consider centralizing this permission set in a shared helper or referencing the real role definition so the test doesn’t silently drift from production behavior over time.
- The use of allow_any_instance_of on Api::BaseController and EvoAuthService tightly couples the spec to internals; if possible, replace this with a shared authentication/authorization test helper or dependency injection to keep the test setup more robust to refactors.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@pastoriniMatheus

Copy link
Copy Markdown
Contributor Author

ℹ️ Independente. O request spec stubba a permissão (não lê o seed do auth), então passa standalone na develop — sem ordem obrigatória.

Ordem geral da leva (desmembramento do antigo PR #84):

Os 4 PRs do auth tocam os mesmos arquivos (db/seeds/rbac.rb, rbac_spec.rb, versão do db/schema.rb) → mergeiam em ordem, com rebase: #85 (178) → #86 (181) → #87 (182) → #88 (190). schema.rb resolve para a versão maior.

Par de deploy: #85 (auth, a chave pipeline_items.update) ⟷ #272 (CRM, o gate) — subir juntos.

Independentes (sem ordem): #281 (CRM-181 comentário), #282 (CRM-182 spec), #283 (CRM-190 spec), #310 (CRM-178 front).

🤖 Generated with Claude Code

@gomessguii
gomessguii merged commit 04a389b into develop Aug 17, 2026
11 checks passed
@gomessguii
gomessguii deleted the test/CRM-182-agent-destructive-ops-rbac branch August 17, 2026 23:10
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