Back to Repositories

Testing Base Action Authorization and Widget Behavior in Rails Admin

This integration test suite examines the base action functionality in Rails Admin, focusing on authorization and widget behavior. It verifies action enablement, visibility controls, and form widget interactions across different model configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of Rails Admin’s base action functionality, particularly focusing on authorization and UI element visibility.

  • Tests unauthorized action prevention
  • Validates form widget behavior for filtering-select and multiselect components
  • Verifies interaction between enabled and visible states
  • Covers modal link visibility based on action configuration

Implementation Analysis

The testing approach utilizes RSpec’s request specs to validate both backend authorization and frontend rendering.

Key patterns include:
  • Dynamic configuration blocks for testing different action settings
  • Factory-based test data generation
  • CSS selector-based UI element verification
  • Behavioral testing of conditional UI elements

Technical Details

Testing infrastructure includes:
  • RSpec as the primary testing framework
  • FactoryBot for test data generation
  • Capybara for interface interaction
  • Page object pattern for test organization
  • Request spec type for integration testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test contexts using RSpec describe blocks
  • Clear test case organization and naming
  • Proper setup and teardown management
  • Comprehensive edge case coverage
  • Effective use of subject and expectation syntax

railsadminteam/rails_admin

spec/integration/actions/base_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Base action', type: :request do
  subject { page }

  describe '#enabled?' do
    it 'prevents the access to unauthorized actions' do
      RailsAdmin.config do |config|
        config.actions do
          index do
            except %w[FieldTest]
          end
        end
      end
      expect { visit index_path(model_name: 'field_test') }.to raise_error 'RailsAdmin::ActionNotAllowed'
    end

    describe 'in form action' do
      before do
        RailsAdmin.config do |config|
          config.actions do
            index
            new
            edit do
              except %w[Player Team]
            end
          end
        end
      end

      describe 'for filterling-select widget' do
        it 'hides modal links to disabled actions' do
          visit new_path(model_name: 'player')
          expect(page).to have_link 'Add a new Team'
          expect(page).not_to have_link 'Edit this Team'
        end
      end

      describe 'for filterling-multiselect widget' do
        it 'hides edit link to another model' do
          visit new_path(model_name: 'team')
          expect(page).to have_link 'Add a new Player'
          expect(page).not_to have_link 'Edit this Player'
        end
      end
    end

    context 'when used with #visible?' do
      let!(:player) { FactoryBot.create(:player) }
      before do
        RailsAdmin.config do |config|
          config.actions do
            index
            show
            edit do
              enabled false
              visible true
            end
          end
        end
      end

      it 'allows disabled links to be shown' do
        visit index_path(model_name: 'player')
        is_expected.to have_css('.edit_member_link.disabled span', text: /Edit/, visible: false)
        visit show_path(model_name: 'player', id: player.id)
        is_expected.to have_css('.edit_member_link.disabled a[href*="void(0)"]')
      end
    end
  end
end