Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

3.6 KB
Raw Blame

Permission Test Improvements

We've made significant improvements to the permissions testing framework:

Implemented Solutions

  1. Database Cleaner

    • Added database_cleaner-active_record gem
    • Set up transaction strategy for fast cleaning between tests
    • Properly reset data between tests to avoid state leakage
  2. Tenant Handling

    • Created TenantHelper to properly set/reset tenant between tests
    • Added support for controller tests with proper tenant context
    • Fixed issues with tenancy in authentication tests
  3. Permission Testing Helpers

    • Created PermissionTestHelper with methods for setting up permissions
    • Added support for both local (project) and global (role) permissions
    • Improved test data setup with proper reloading of associations
  4. Job Mocking

    • Created JobsHelper to mock background jobs like OpenaiResponseJob
    • Prevented Redis errors in tests without requiring Redis to be running
    • Stubbed all background job methods for test stability
  5. Flash Messages

    • Added FlashHelper for checking authorization errors consistently
    • Replaced specific message checks with more general error checks
    • Made tests more resilient to UI text changes
  6. Model Factory Fixes

    • Fixed the Chat factory to handle the actual model schema (no title field)
    • Added chatpost creation to Chat factory for valid instances
    • Updated test assertions to match the model structure

Remaining Issues

Some tests are still failing due to the following issues:

  1. Authorization Logic

    • The ProjectAccessService may not be properly respecting read/write permissions
    • Permission checks may need debugging in the controller or model
    • Consider adding extra debugging to the roles & permissions system
  2. Controller Actions

    • Some controller actions are redirecting when they should return success
    • Actions like create and update may not properly handle test data

Next Steps

  1. Debug ProjectAccessService

    # Add to the AccessCheck::ProjectAccessService class
    def debug_permissions(action, area)
      Rails.logger.debug "==== PERMISSION DEBUG ===="
      Rails.logger.debug "User: #{user.inspect}"
      Rails.logger.debug "Project: #{project.inspect}"
      Rails.logger.debug "Action: #{action}, Area: #{area}"
      # Print local permissions
      perm = ProjectPermission.find_by(user: user, project: project)
      Rails.logger.debug "Local permission: #{perm.inspect}" if perm
      # Print role permissions
      role = user.role_in_organisation(project.organisation)
      if role
        read_perm = role.has_permission?('projects', "read_access_#{area}_all_projects")
        write_perm = role.has_permission?('projects', "write_access_#{area}_all_projects")
        Rails.logger.debug "Role: #{role.inspect}"
        Rails.logger.debug "Read permission: #{read_perm}, Write permission: #{write_perm}"
      end
      Rails.logger.debug "====================="
    end
    
  2. Consider Simplified Testing Strategy

    • Use integration tests for permission workflow verification
    • Limit controller tests to basic functionality
    • Add unit tests for the permission service directly
  3. Fix Remaining Specific Issues

    • Missing create functionality for test users with write permission
    • Incorrect authorization checks for admin users
    • Possible issues with the Chat model validation
  4. Run with RSpec's --backtrace

    bundle exec rspec spec/controllers/chats_controller_spec.rb --backtrace
    

    to get detailed error information about where tests are failing.

These solutions should make your permissions testing more reliable and maintainable.