Back to Repositories

Testing OmniAuth URL Helper Implementation in Devise

This test suite validates the OmniAuth URL helper functionality in Devise, focusing on route generation and path mapping for authentication providers. It ensures proper URL construction for various authentication scenarios and parameter handling.

Test Coverage Overview

The test suite provides comprehensive coverage of OmniAuth URL helper methods in Devise.

Key areas tested include:
  • Path generation for OAuth callback routes
  • Authorization path mapping for different providers
  • Parameter handling in authentication URLs
  • Resource and object-based path generation
  • Named OpenID provider support

Implementation Analysis

The testing approach uses ActionController::TestCase to verify URL helper functionality. It implements a custom assert_path helper method to validate multiple path generation scenarios.

Key patterns include:
  • Testing both symbol and object-based resource routing
  • Validation of URL parameter handling
  • Provider-specific path generation verification
  • Error case handling for unsupported providers

Technical Details

Testing infrastructure includes:
  • Minitest framework integration
  • ActionController test helpers
  • Custom assertion methods
  • Mock ApplicationController setup
  • OmniAuth provider configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable elements include:
  • Comprehensive edge case coverage
  • DRY testing through helper methods
  • Clear test naming conventions
  • Explicit assertion messages
  • Systematic validation of URL construction

heartcombo/devise

test/omniauth/url_helpers_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class OmniAuthRoutesTest < ActionController::TestCase
  tests ApplicationController

  def assert_path(action, provider, with_param = true)
    # Resource param
    assert_equal @controller.send(action, :user, provider),
                 @controller.send("user_#{provider}_#{action}")

    # With an object
    assert_equal @controller.send(action, User.new, provider),
                 @controller.send("user_#{provider}_#{action}")

    if with_param
      # Default url params
      assert_equal @controller.send(action, :user, provider, param: 123),
                   @controller.send("user_#{provider}_#{action}", param: 123)
    end
  end

  test 'should alias omniauth_callback to mapped user auth_callback' do
    assert_path :omniauth_callback_path, :facebook
  end

  test 'should alias omniauth_authorize to mapped user auth_authorize' do
    assert_path :omniauth_authorize_path, :facebook, false
  end

  test 'should generate authorization path' do
    assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook)

    assert_raise NoMethodError do
      @controller.omniauth_authorize_path(:user, :github)
    end
  end

  test 'should generate authorization path for named open_id omniauth' do
    assert_match "/users/auth/google", @controller.omniauth_authorize_path(:user, :google)
  end

  test 'should generate authorization path with params' do
    assert_match "/users/auth/openid?openid_url=http%3A%2F%2Fyahoo.com",
                  @controller.omniauth_authorize_path(:user, :openid, openid_url: "http://yahoo.com")
  end

  test 'should not add a "?" if no param was sent' do
    assert_equal "/users/auth/openid",
                  @controller.omniauth_authorize_path(:user, :openid)
  end
end