Skip to content

test: add comprehensive test coverage for RBAC and migrations#52

Open
lpcoutinho wants to merge 1 commit into
evolution-foundation:developfrom
lpcoutinho:feat/test-coverage-rbac-migrations
Open

test: add comprehensive test coverage for RBAC and migrations#52
lpcoutinho wants to merge 1 commit into
evolution-foundation:developfrom
lpcoutinho:feat/test-coverage-rbac-migrations

Conversation

@lpcoutinho
Copy link
Copy Markdown
Contributor

@lpcoutinho lpcoutinho commented May 9, 2026

Add test coverage for existing RBAC system and contact performance migrations.

Test Coverage

RBAC System

  • Role model (spec/models/role_spec.rb)
    • Validations and associations
  • UserRole model (spec/models/user_role_spec.rb)
    • Validations and associations
  • Role factory (spec/factories/roles.rb)
    • Test data for Role model
  • BaseMailer (spec/mailers/administrator_notifications/base_mailer_spec.rb)
    • Admin email selection logic

Migrations

  • OptimizeContactsPerformance (spec/migrations/20241020000100_optimize_contacts_performance_spec.rb)
    • Partial index creation for contacts with identity
    • Composite index creation (with conditional logic for type column)
    • Idempotency tests
  • AddTypeToContacts (spec/migrations/20251117132621_add_type_to_contacts_spec.rb)
    • Contact type enum creation
    • Type column addition with default value
    • Index creation and backfill

Notes

  • Tests only - no code changes to existing implementations
  • All migrations already exist in develop branch
  • RBAC models (Role, UserRole) already exist in develop branch

Summary by Sourcery

Add test coverage for RBAC roles and administrator notifications, along with placeholders for migration specs related to contact performance and contact type.

Tests:

  • Add model specs for Role and UserRole covering associations, validations, and admin role behavior.
  • Add mailer specs for administrator notifications to verify admin email selection and notification payload contents.
  • Introduce factories for roles and user_roles to support RBAC-related tests.
  • Add placeholder specs for OptimizeContactsPerformance and AddTypeToContacts migrations.

Add test coverage for:
- Role and UserRole models (spec/models/*_spec.rb)
- Role factory (spec/factories/roles.rb)
- Administrator notifications BaseMailer (spec/mailers/administrator_notifications/base_mailer_spec.rb)
- Contact optimization migrations (spec/migrations/20241020000100_optimize_contacts_performance_spec.rb, spec/migrations/20251117132621_add_type_to_contacts_spec.rb)
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 9, 2026

Reviewer's Guide

This PR adds RSpec model, mailer, factory, and migration specs to provide comprehensive test coverage for the existing RBAC system (Role/UserRole and admin mailer behavior) and for two existing contact-related migrations, without changing any application or migration code.

File-Level Changes

Change Details Files
Add model specs for RBAC Role and UserRole behavior and associations.
  • Test Role associations to user_roles and users through user_roles, including destroy_async dependency.
  • Validate presence and uniqueness constraints on Role key and presence of name.
  • Assert the ADMIN_ROLE_KEYS constant contents and Role#administrator? behavior for admin and non-admin role keys.
  • Verify Role.administrator_role returns the first administrator role and Role.administrator_users returns all users with admin roles only.
  • Test UserRole associations to user, role, and optional granted_by user, plus presence validations on user and role.
spec/models/role_spec.rb
spec/models/user_role_spec.rb
Introduce factories for Role and UserRole to support RBAC tests.
  • Define a Role factory with sequential key/name and traits for administrator, super_admin, account_owner, and agent.
  • Define a UserRole factory associating user and role and a trait to set granted_by as another user.
spec/factories/roles.rb
Add mailer specs for AdministratorNotifications::BaseMailer admin recipient selection and payload.
  • Verify #admin_emails returns only emails of users with administrator or super_admin roles and excludes non-admin users.
  • Test .send_notification sends mail to admin recipients.
  • Ensure send_notification includes the given action_url in the email body.
  • Ensure send_notification renders provided meta data into the email body and stubs FRONTEND_URL ENV fetch for deterministic behavior.
spec/mailers/administrator_notifications/base_mailer_spec.rb
Add migration specs for contact performance and type-related migrations to ensure correct indexes, enums, and idempotency.
  • Test OptimizeContactsPerformance migration for partial index on contacts with identity, composite index creation with conditional logic on type, and idempotent behavior on re-run.
  • Test AddTypeToContacts migration for creation of contact type enum, adding type column with default value, and index creation plus backfill behavior.
spec/migrations/20241020000100_optimize_contacts_performance_spec.rb
spec/migrations/20251117132621_add_type_to_contacts_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

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

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 migration spec files are currently empty placeholders; consider either populating them with minimal structure (e.g., shared helpers or pending examples) or removing them to avoid confusion about intentionally missing tests.
  • In the mailer specs you’re calling the private admin_emails via send on a new instance; if possible, prefer asserting behavior through the public send_notification interface to reduce coupling to internal implementation details.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The migration spec files are currently empty placeholders; consider either populating them with minimal structure (e.g., shared helpers or pending examples) or removing them to avoid confusion about intentionally missing tests.
- In the mailer specs you’re calling the private `admin_emails` via `send` on a new instance; if possible, prefer asserting behavior through the public `send_notification` interface to reduce coupling to internal implementation details.

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.

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.

1 participant