Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6324

Show
Ignore:
Timestamp:
03/04/07 22:02:36 (2 years ago)
Author:
david
Message:

Added OpenIdAuthentication::Result to make it easier to deal with default situations where you don't care to do something particular for each error state [DHH] Started on Mocha-based testing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/open_id_authentication/CHANGELOG

    r6318 r6324  
     1* Added OpenIdAuthentication::Result to make it easier to deal with default situations where you don't care to do something particular for each error state [DHH] 
     2 
    13* Stop relying on root_url being defined, we can just grab the current url instead [DHH] 
  • plugins/open_id_authentication/lib/open_id_authentication.rb

    r6318 r6324  
    11module OpenIdAuthentication 
    22  OPEN_ID_AUTHENTICATION_DIR = RAILS_ROOT + "/tmp/openids" 
     3 
     4  class Result 
     5    MESSAGES = { 
     6      :missing    => "Sorry, the OpenID server couldn't be found", 
     7      :canceled   => "OpenID verification was canceled", 
     8      :failed     => "Sorry, the OpenID verification failed", 
     9      :successful => "OpenID authentication successful" 
     10    } 
     11     
     12    ERROR_STATES = [ :missing, :canceled, :failed ] 
     13     
     14    def self.[](code) 
     15      new(code) 
     16    end 
     17     
     18    def initialize(code) 
     19      @code = code 
     20    end 
     21     
     22    def ===(code) 
     23      if code == :unsuccessful && unsuccessful? 
     24        true 
     25      else 
     26        @code == code 
     27      end 
     28    end 
     29     
     30    MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } } 
     31 
     32    def unsuccessful? 
     33      ERROR_STATES.include?(@code) 
     34    end 
     35     
     36    def message 
     37      MESSAGES[@code] 
     38    end 
     39  end 
     40 
    341 
    442  protected 
     
    1856  private 
    1957    def begin_open_id_authentication(identity_url, fields = {}) 
    20       open_id_response = open_id_consumer.begin(identity_url) 
     58      open_id_response = timeout_protection_from_identity_server { open_id_consumer.begin(identity_url) } 
    2159 
    2260      case open_id_response.status 
    2361      when OpenID::FAILURE 
    24         yield :missing, identity_url, nil 
     62        yield Result[:missing], identity_url, nil 
    2563      when OpenID::SUCCESS 
    2664        add_simple_registration_fields(open_id_response, fields) 
     
    3068   
    3169    def complete_open_id_authentication 
    32       open_id_response = open_id_consumer.complete(params) 
     70      open_id_response = timeout_protection_from_identity_server { open_id_consumer.complete(params) } 
    3371 
    3472      case open_id_response.status 
    3573      when OpenID::CANCEL 
    36         yield :canceled, open_id_response.identity_url, nil 
     74        yield Result[:canceled], open_id_response.identity_url, nil 
    3775      when OpenID::FAILURE 
    3876        logger.info "OpenID authentication failed: #{open_id_response.msg}" 
    39         yield :failed, open_id_response.identity_url, nil 
     77        yield Result[:failed], open_id_response.identity_url, nil 
    4078      when OpenID::SUCCESS 
    41         yield :successful, open_id_response.identity_url, open_id_response.extension_response('sreg') 
     79        yield Result[:successful], open_id_response.identity_url, open_id_response.extension_response('sreg') 
    4280      end       
    4381    end 
     
    5997      )      
    6098    end 
     99 
     100 
     101    def timeout_protection_from_identity_server 
     102      yield 
     103    rescue Timeout::Error 
     104      Class.new do 
     105        def status 
     106          OpenID::FAILURE 
     107        end 
     108         
     109        def msg 
     110          "Identity server timed out" 
     111        end 
     112      end.new 
     113    end 
    61114end 
  • plugins/open_id_authentication/README

    r6318 r6324  
    5757 
    5858      def open_id_authentication(identity_url) 
    59         authenticate_with_open_id(identity_url) do |status, identity_url| 
    60           case status 
     59        authenticate_with_open_id(identity_url) do |result, identity_url| 
     60          case result 
    6161          when :missing 
    6262            failed_login "Sorry, the OpenID server couldn't be found" 
     
    8787      end 
    8888  end 
     89 
     90 
     91 
     92If you're fine with the result messages above and don't need individual logic on a per-failure basis, 
     93you can collapse the case into a mere boolean: 
     94 
     95    def open_id_authentication(identity_url) 
     96      authenticate_with_open_id(identity_url) do |result, identity_url| 
     97        if result.successful? 
     98          if @current_user = @account.users.find_by_identity_url(identity_url) 
     99            successful_login 
     100          else 
     101            failed_login "Sorry, no user by that identity URL exists" 
     102          end 
     103        else 
     104          failed_login(result.message) 
     105        end 
     106      end 
     107    end 
    89108 
    90109 
  • plugins/open_id_authentication/test/open_id_authentication_test.rb

    r6245 r6324  
    11require 'test/unit' 
    22 
     3require 'rubygems' 
     4gem 'mocha' 
     5require 'mocha' 
     6 
     7gem 'ruby-openid' 
     8require 'openid' 
     9 
     10RAILS_ROOT = File.dirname(__FILE__) 
     11require File.dirname(__FILE__) + "/../lib/open_id_authentication" 
     12 
    313class OpenIdAuthenticationTest < Test::Unit::TestCase 
    4   # Replace this with your real tests. 
    5   def test_this_plugin 
    6     flunk 
     14  def setup 
     15    @controller = Class.new do 
     16      include OpenIdAuthentication 
     17      def params() {} end 
     18    end.new 
     19  end 
     20 
     21  def test_authentication_should_fail_when_the_identity_server_is_missing 
     22    @controller.stubs(:open_id_consumer).returns(stub(:begin => stub(:status => OpenID::FAILURE))) 
     23     
     24    @controller.send(:authenticate_with_open_id, "http://someone.example.com") do |result, identity_url| 
     25      assert result.missing? 
     26      assert_equal "Sorry, the OpenID server couldn't be found", result.message 
     27    end 
     28  end 
     29 
     30  def test_authentication_should_fail_when_the_identity_server_times_out 
     31    @controller.stubs(:open_id_consumer).returns(stub(:begin => Proc.new { raise Timeout::Error })) 
     32 
     33    @controller.send(:authenticate_with_open_id, "http://someone.example.com") do |result, identity_url| 
     34      assert result.missing? 
     35      assert_equal "Sorry, the OpenID server couldn't be found", result.message 
     36    end 
     37  end 
     38 
     39  def test_authentication_should_begin_when_the_identity_server_is_present 
     40    @controller.stubs(:open_id_consumer).returns(stub(:begin => stub(:status => OpenID::SUCCESS))) 
     41    @controller.expects(:begin_open_id_authentication)  
     42    @controller.send(:authenticate_with_open_id, "http://someone.example.com") 
    743  end 
    844end