Back to Repositories

Testing URL Helper Path Generation in Devise Authentication Framework

This test suite validates URL helper methods in Devise, ensuring proper route generation and path mapping for authentication-related functionality. It verifies the correct generation of path and URL helpers for various Devise modules including sessions, passwords, confirmations, unlocks, and registrations.

Test Coverage Overview

The test suite provides comprehensive coverage of Devise’s URL helper methods, testing path and URL generation for all major authentication endpoints.

Key areas tested include:
  • Session management routes
  • Password reset functionality
  • Email confirmation paths
  • Account unlock mechanisms
  • Registration workflow paths
Edge cases cover different parameter types including symbols, strings, and model instances.

Implementation Analysis

The testing approach uses a custom assert_path_and_url helper method to systematically verify both path and URL generation across different contexts. The implementation leverages Minitest’s assertion framework while testing against ApplicationController, ensuring proper integration with Devise’s routing system.

The tests validate route generation with various parameter types and URL configurations, including prepended paths and additional URL parameters.

Technical Details

Testing tools and configuration:
  • Minitest framework
  • Devise::ControllerTestCase as base class
  • ApplicationController as test subject
  • Custom assertion helper methods
  • Request path manipulation for context testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices including systematic validation of related functionality, comprehensive parameter testing, and proper test isolation.

Notable practices include:
  • DRY test implementation using helper methods
  • Consistent assertion patterns
  • Thorough coverage of parameter variations
  • Clear test organization by authentication feature

heartcombo/devise

test/controllers/url_helpers_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class RoutesTest < Devise::ControllerTestCase
  tests ApplicationController

  def assert_path_and_url(name, prepend_path = nil)
    @request.path = '/users/session'
    prepend_path = "#{prepend_path}_" if prepend_path

    # Resource param
    assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user),
                 send(:"#{prepend_path}user_#{name}_path")
    assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user),
                 send(:"#{prepend_path}user_#{name}_url")

    # With string
    assert_equal @controller.send(:"#{prepend_path}#{name}_path", "user"),
                 send(:"#{prepend_path}user_#{name}_path")
    assert_equal @controller.send(:"#{prepend_path}#{name}_url", "user"),
                 send(:"#{prepend_path}user_#{name}_url")

    # Default url params
    assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user, param: 123),
                 send(:"#{prepend_path}user_#{name}_path", param: 123)
    assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user, param: 123),
                 send(:"#{prepend_path}user_#{name}_url", param: 123)

    @request.path = nil
    # With an object
    assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
                 send(:"#{prepend_path}user_#{name}_path")
    assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
                 send(:"#{prepend_path}user_#{name}_url")
  end


  test 'should alias session to mapped user session' do
    assert_path_and_url :session
    assert_path_and_url :session, :new
    assert_path_and_url :session, :destroy
  end

  test 'should alias password to mapped user password' do
    assert_path_and_url :password
    assert_path_and_url :password, :new
    assert_path_and_url :password, :edit
  end

  test 'should alias confirmation to mapped user confirmation' do
    assert_path_and_url :confirmation
    assert_path_and_url :confirmation, :new
  end

  test 'should alias unlock to mapped user unlock' do
    assert_path_and_url :unlock
    assert_path_and_url :unlock, :new
  end

  test 'should alias registration to mapped user registration' do
    assert_path_and_url :registration
    assert_path_and_url :registration, :new
    assert_path_and_url :registration, :edit
    assert_path_and_url :registration, :cancel
  end
end