Add test for OpenID login and signup
authorKai Krueger <kakrueger@gmail.com>
Sun, 2 May 2010 10:04:51 +0000 (11:04 +0100)
committerKai Krueger <kakrueger@gmail.com>
Sun, 2 May 2010 10:04:51 +0000 (11:04 +0100)
The tests need the ROTS (Ruby OpenID Test Server) running.
ROTS can be found at http://github.com/roman/rots
If ROTS is not running, there will be 4 failed test cases, but otherwise
things should work

test/fixtures/users.yml
test/integration/user_creation_test.rb
test/integration/user_login_test.rb [new file with mode: 0644]
test/test_helper.rb

index 1849fd4d99aae8ddeb603e51f1d58786aa1e4be1..8c8577520d71a5fd116123858d619a8a6b7e6168 100644 (file)
@@ -68,3 +68,13 @@ administrator_user:
   creation_time: "2008-05-01 01:23:45"
   display_name: administrator
   data_public: true
+
+openid_user:
+  id: 7
+  email: openid-user@example.com
+  active: true
+  pass_crypt: <%= Digest::MD5.hexdigest('test') %>
+  creation_time: "2008-05-01 01:23:45"
+  display_name: openIDuser
+  data_public: true
+  openid_url: http://localhost:1123/john.doe?openid.success=true
index 77d73552a82ef100d4e04eeb4746af540e5a910c..da0ffd76872c3a8a5afdcc750b76f3bae2b5f02e 100644 (file)
@@ -127,4 +127,28 @@ class UserCreationTest < ActionController::IntegrationTest
     assert_response :success
     assert_template "trace/list.html.erb"
   end
+
+  def test_user_create_openid_success
+    new_email = "newtester-openid@osm.org"
+    display_name = "new_tester-openid"
+    assert_difference('User.count') do
+      assert_difference('ActionMailer::Base.deliveries.size', 1) do
+        post "/user/save",
+          {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :openid_url => "http://localhost:1123/john.doe?openid.success=newuser"}}
+        assert_response :redirect
+        res = openid_request(@response.redirected_to)
+        post '/user/save', res
+        assert_response :redirect
+        follow_redirect!
+      end
+    end
+
+    # Check the page
+    assert_response :success
+    assert_template 'login'
+
+    ActionMailer::Base.deliveries.clear
+  end
+
+
 end
diff --git a/test/integration/user_login_test.rb b/test/integration/user_login_test.rb
new file mode 100644 (file)
index 0000000..0fe5b65
--- /dev/null
@@ -0,0 +1,81 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class UserLoginTest < ActionController::IntegrationTest
+  fixtures :users
+
+  def test_login_openid_success
+    get '/login'
+    assert_response :redirect
+    assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true"
+    follow_redirect!
+    assert_response :success
+    post '/login', {'user[openid_url]' => "http://localhost:1123/john.doe?openid.success=true", :referer => "/browse"}
+    assert_response :redirect
+
+       res = openid_request(@response.redirected_to)
+       res2 = post '/login', res
+
+    assert_response :redirect
+    follow_redirect!
+       assert_response :success
+       assert_template 'changeset/list'
+  end
+
+  def test_login_openid_cancel
+    get '/login'
+    assert_response :redirect
+    assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true"
+    follow_redirect!
+    assert_response :success
+    post '/login', {'user[openid_url]' => "http://localhost:1123/john.doe", :referer => "/diary"}
+    assert_response :redirect
+
+       res = openid_request(@response.redirected_to)
+       post '/login', res
+
+       assert_response :success
+       assert_template 'login'
+  end
+
+  def test_login_openid_invalid_provider
+    get '/login'
+    assert_response :redirect
+    assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true"
+    follow_redirect!
+    assert_response :success
+       #Use a different port that doesn't have the OpenID provider running on to test an invalid openID
+    post '/login', {'user[openid_url]' => "http://localhost:1124/john.doe", :referer => "/diary"}
+    assert_response :success
+       assert_template 'login'
+  end
+
+  def test_login_openid_invalid_url
+    get '/login'
+    assert_response :redirect
+    assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true"
+    follow_redirect!
+    assert_response :success
+       #Use a url with an invalid protocol to make sure it handles that correctly too
+    post '/login', {'user[openid_url]' => "htt://localhost:1123/john.doe", :referer => "/diary"}
+    assert_response :success
+       assert_template 'login'
+  end
+
+  def test_login_openid_unknown
+    get '/login'
+    assert_response :redirect
+    assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true"
+    follow_redirect!
+    assert_response :success
+    post '/login', {'user[openid_url]' => "http://localhost:1123/john.doe?openid.success=true_somethingelse", :referer => "/diary"}
+    assert_response :redirect
+
+       res = openid_request(@response.redirected_to)
+       res2 = post '/login', res
+
+    assert_response :redirect
+    follow_redirect!
+       assert_response :success
+       assert_template 'user/new'
+  end
+end
index 4972ee6d6e6b90b1d7fb14c8f2e54ed687fdf9e0..a83a89f2ab8f56979c3db9c2d573fbfff2d3cad2 100644 (file)
@@ -141,6 +141,15 @@ class ActiveSupport::TestCase
   def assert_no_missing_translations(msg="")
     assert_select "span[class=translation_missing]", false, "Missing translation #{msg}"
   end
+
+  def openid_request(openid_request_uri)
+    openid_response = Net::HTTP.get_response(URI.parse(openid_request_uri))
+    openid_response_uri = URI(openid_response['Location'])
+    openid_response_qs = Rack::Utils.parse_query(openid_response_uri.query)
+
+    return openid_response_qs
+  end
+
   
   # Add more helper methods to be used by all tests here...
 end