* Allow -'s in #normalize_url [Rick]
[gitorious:openid_auth.git] / test / open_id_authentication_test.rb
1 require 'test/unit'
2
3 require 'rubygems'
4 gem 'mocha'
5 require 'mocha'
6
7 gem 'ruby-openid'
8 require 'openid'
9
10 RAILS_ROOT = File.dirname(__FILE__)
11 require File.dirname(__FILE__) + "/../lib/open_id_authentication"
12
13 class OpenIdAuthenticationTest < Test::Unit::TestCase
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, "Identity Server took too long." }))
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")
43   end
44 end