Back to Repositories

Testing Devise Authentication Integration in Rails Admin

This test suite validates the Devise authentication integration with Rails Admin, focusing on core user authentication workflows. The tests verify login and logout functionality while ensuring proper session management and user state handling.

Test Coverage Overview

The test suite provides comprehensive coverage of essential Devise authentication flows in Rails Admin.

Key areas tested include:
  • User login functionality with credential validation
  • Logout process and session termination
  • Authentication state verification
  • Integration between Devise and Rails Admin configuration

Implementation Analysis

The testing approach utilizes RSpec’s request specs with JavaScript support for realistic user interaction simulation. The implementation leverages FactoryBot for test data generation and Warden for authentication handling, following a behavior-driven development pattern.

Technical patterns include:
  • Subject-based RSpec syntax
  • Before block configuration setup
  • Factory-based test data management
  • Capybara interaction methods

Technical Details

Testing tools and configuration:
  • RSpec for test framework
  • Devise for authentication
  • FactoryBot for test data
  • Warden for authentication middleware
  • Capybara for browser simulation
  • JavaScript-enabled testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Rails authentication testing.

Notable practices include:
  • Isolated test scenarios
  • Clear test case organization
  • Proper setup and teardown management
  • Real-world user flow simulation
  • Configuration encapsulation
  • Explicit expectations and assertions

railsadminteam/rails_admin

spec/integration/authentication/devise_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'RailsAdmin Devise Authentication', type: :request do
  subject { page }
  let!(:user) { FactoryBot.create :user }

  before do
    RailsAdmin.config do |config|
      config.authenticate_with do
        warden.authenticate! scope: :user
      end
      config.current_user_method(&:current_user)
    end
  end

  it 'supports logging-in', js: true do
    visit dashboard_path
    fill_in 'Email', with: user.email
    fill_in 'Password', with: 'password'
    click_button 'Log in'
    is_expected.to have_css 'body.rails_admin'
  end

  it 'supports logging-out', js: true do
    login_as user
    visit dashboard_path
    click_link 'Log out'
    is_expected.to have_content 'Log in'
  end
end