+ u = User.find_by(:display_name => user.display_name)
+ assert_equal "google", u.auth_provider
+ assert_equal "987654321", u.auth_uid
+ end
+
+ def test_login_facebook_success
+ user = create(:user, :auth_provider => "facebook", :auth_uid => "1234567890")
+ OmniAuth.config.add_mock(:facebook, :uid => user.auth_uid)
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "facebook")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "changeset/history"
+ assert_select "span.username", user.display_name
+ end
+
+ def test_login_facebook_connection_failed
+ OmniAuth.config.mock_auth[:facebook] = :connection_failed
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "facebook")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Connection to authentication provider failed"
+ assert_select "span.username", false
+ end
+
+ def test_login_facebook_invalid_credentials
+ OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "facebook")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Invalid authentication credentials"
+ assert_select "span.username", false
+ end
+
+ def test_login_facebook_unknown
+ OmniAuth.config.add_mock(:facebook, :uid => "987654321")
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "facebook")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "user/new"
+ assert_select "span.username", false
+ end
+
+ def test_login_windowslive_success
+ user = create(:user, :auth_provider => "windowslive", :auth_uid => "1234567890")
+ OmniAuth.config.add_mock(:windowslive, :uid => user.auth_uid)
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "windowslive")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "changeset/history"
+ assert_select "span.username", user.display_name
+ end
+
+ def test_login_windowslive_connection_failed
+ OmniAuth.config.mock_auth[:windowslive] = :connection_failed
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "windowslive")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "windowslive", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Connection to authentication provider failed"
+ assert_select "span.username", false
+ end
+
+ def test_login_windowslive_invalid_credentials
+ OmniAuth.config.mock_auth[:windowslive] = :invalid_credentials
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "windowslive")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "windowslive", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Invalid authentication credentials"
+ assert_select "span.username", false
+ end
+
+ def test_login_windowslive_unknown
+ OmniAuth.config.add_mock(:windowslive, :uid => "987654321")
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "windowslive")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "user/new"
+ assert_select "span.username", false
+ end
+
+ def test_login_github_success
+ user = create(:user, :auth_provider => "github", :auth_uid => "1234567890")
+ OmniAuth.config.add_mock(:github, :uid => user.auth_uid)
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "github", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "github")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "changeset/history"
+ assert_select "span.username", user.display_name
+ end
+
+ def test_login_github_connection_failed
+ OmniAuth.config.mock_auth[:github] = :connection_failed
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "github", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "github")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "github", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Connection to authentication provider failed"
+ assert_select "span.username", false
+ end
+
+ def test_login_github_invalid_credentials
+ OmniAuth.config.mock_auth[:github] = :invalid_credentials
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "github", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "github")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "github", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Invalid authentication credentials"
+ assert_select "span.username", false
+ end
+
+ def test_login_github_unknown
+ OmniAuth.config.add_mock(:github, :uid => "987654321")
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "github", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "github")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "user/new"
+ assert_select "span.username", false
+ end
+
+ def test_login_wikipedia_success
+ user = create(:user, :auth_provider => "wikipedia", :auth_uid => "1234567890")
+ OmniAuth.config.add_mock(:wikipedia, :uid => user.auth_uid)
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "changeset/history"
+ assert_select "span.username", user.display_name
+ end
+
+ def test_login_wikipedia_connection_failed
+ OmniAuth.config.mock_auth[:wikipedia] = :connection_failed
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "wikipedia", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Connection to authentication provider failed"
+ assert_select "span.username", false
+ end
+
+ def test_login_wikipedia_invalid_credentials
+ OmniAuth.config.mock_auth[:wikipedia] = :invalid_credentials
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ follow_redirect!
+ assert_response :redirect
+ assert_redirected_to auth_failure_path(:strategy => "wikipedia", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "div.flash.error", "Invalid authentication credentials"
+ assert_select "span.username", false
+ end
+
+ def test_login_wikipedia_unknown
+ OmniAuth.config.add_mock(:wikipedia, :uid => "987654321")
+
+ get "/login", :params => { :referer => "/history" }
+ assert_response :redirect
+ assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
+ follow_redirect!
+ assert_response :success
+ assert_template "user/login"
+ get auth_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ assert_response :redirect
+ assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/login?referer=%2Fhistory", :referer => "/history")
+ follow_redirect!
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "user/new"
+ assert_select "span.username", false
+ end
+
+ private
+
+ def try_password_login(username, password, remember_me = nil)
+ get "/login"
+ assert_response :redirect
+ assert_redirected_to :controller => :user, :action => :login, :cookie_test => true
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "input#username", 1 do
+ assert_select "[value]", false
+ end
+ assert_select "input#password", 1 do
+ assert_select "[value=?]", ""
+ end
+ assert_select "input#remember_me", 1 do
+ assert_select "[checked]", false
+ end
+
+ post "/login", :params => { :username => username, :password => "wrong", :remember_me => remember_me, :referer => "/history" }
+ assert_response :redirect
+ follow_redirect!
+ assert_response :success
+ assert_template "login"
+ assert_select "input#username", 1 do
+ assert_select "[value=?]", username
+ end
+ assert_select "input#password", 1 do
+ assert_select "[value=?]", ""
+ end
+ assert_select "input#remember_me", 1 do
+ assert_select "[checked]", remember_me == "yes"
+ end