2010-10-14 18:44:21 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
2011-10-14 16:33:56 -04:00
|
|
|
|
2010-10-14 18:44:21 -04:00
|
|
|
class OmniauthableIntegrationTest < ActionController::IntegrationTest
|
|
|
|
FACEBOOK_INFO = {
|
2011-02-24 14:50:22 -05:00
|
|
|
"id" => '12345',
|
|
|
|
"link" => 'http://facebook.com/josevalim',
|
|
|
|
"email" => 'user@example.com',
|
|
|
|
"first_name" => 'Jose',
|
|
|
|
"last_name" => 'Valim',
|
|
|
|
"website" => 'http://blog.plataformatec.com.br'
|
2010-10-14 18:44:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setup do
|
2011-02-24 14:50:22 -05:00
|
|
|
OmniAuth.config.test_mode = true
|
|
|
|
OmniAuth.config.mock_auth[:facebook] = {
|
|
|
|
"uid" => '12345',
|
|
|
|
"provider" => 'facebook',
|
|
|
|
"user_info" => {"nickname" => 'josevalim'},
|
|
|
|
"credentials" => {"token" => 'plataformatec'},
|
|
|
|
"extra" => {"user_hash" => FACEBOOK_INFO}
|
|
|
|
}
|
2010-10-14 18:44:21 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 16:33:56 -04:00
|
|
|
teardown do
|
|
|
|
OmniAuth.config.test_mode = false
|
|
|
|
end
|
|
|
|
|
2011-02-24 16:24:21 -05:00
|
|
|
def stub_action!(name)
|
|
|
|
Users::OmniauthCallbacksController.class_eval do
|
|
|
|
alias_method :__old_facebook, :facebook
|
|
|
|
alias_method :facebook, name
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Users::OmniauthCallbacksController.class_eval do
|
|
|
|
alias_method :facebook, :__old_facebook
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-14 18:44:21 -04:00
|
|
|
test "can access omniauth.auth in the env hash" do
|
|
|
|
visit "/users/sign_in"
|
|
|
|
click_link "Sign in with Facebook"
|
|
|
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
|
|
|
assert_equal "12345", json["uid"]
|
|
|
|
assert_equal "facebook", json["provider"]
|
|
|
|
assert_equal "josevalim", json["user_info"]["nickname"]
|
2011-02-24 14:50:22 -05:00
|
|
|
assert_equal FACEBOOK_INFO, json["extra"]["user_hash"]
|
2010-10-14 18:44:21 -04:00
|
|
|
assert_equal "plataformatec", json["credentials"]["token"]
|
|
|
|
end
|
|
|
|
|
2011-02-24 14:50:22 -05:00
|
|
|
test "cleans up session on sign up" do
|
2010-10-14 18:44:21 -04:00
|
|
|
assert_no_difference "User.count" do
|
|
|
|
visit "/users/sign_in"
|
|
|
|
click_link "Sign in with Facebook"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert session["devise.facebook_data"]
|
|
|
|
|
|
|
|
assert_difference "User.count" do
|
|
|
|
visit "/users/sign_up"
|
|
|
|
fill_in "Password", :with => "123456"
|
|
|
|
fill_in "Password confirmation", :with => "123456"
|
|
|
|
click_button "Sign up"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_current_url "/"
|
|
|
|
assert_contain "You have signed up successfully."
|
|
|
|
assert_contain "Hello User user@example.com"
|
|
|
|
assert_not session["devise.facebook_data"]
|
|
|
|
end
|
|
|
|
|
2011-02-24 14:50:22 -05:00
|
|
|
test "cleans up session on cancel" do
|
2010-10-14 18:44:21 -04:00
|
|
|
assert_no_difference "User.count" do
|
|
|
|
visit "/users/sign_in"
|
|
|
|
click_link "Sign in with Facebook"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert session["devise.facebook_data"]
|
|
|
|
visit "/users/cancel"
|
|
|
|
assert !session["devise.facebook_data"]
|
|
|
|
end
|
|
|
|
|
2011-02-24 14:50:22 -05:00
|
|
|
test "cleans up session on sign in" do
|
2010-10-14 18:44:21 -04:00
|
|
|
assert_no_difference "User.count" do
|
|
|
|
visit "/users/sign_in"
|
|
|
|
click_link "Sign in with Facebook"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert session["devise.facebook_data"]
|
|
|
|
user = sign_in_as_user
|
|
|
|
assert !session["devise.facebook_data"]
|
|
|
|
end
|
|
|
|
|
2011-02-24 16:24:21 -05:00
|
|
|
test "sign in and send remember token if configured" do
|
|
|
|
visit "/users/sign_in"
|
|
|
|
click_link "Sign in with Facebook"
|
|
|
|
assert_nil warden.cookies["remember_user_token"]
|
|
|
|
|
|
|
|
stub_action!(:sign_in_facebook) do
|
|
|
|
create_user
|
|
|
|
visit "/users/sign_in"
|
|
|
|
click_link "Sign in with Facebook"
|
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
assert warden.cookies["remember_user_token"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-25 12:20:12 -05:00
|
|
|
test "generates a proper link when SCRIPT_NAME is set" do
|
|
|
|
header 'SCRIPT_NAME', '/q'
|
|
|
|
visit "/users/sign_in"
|
2011-05-04 13:23:40 -04:00
|
|
|
assert_select "a", :href => "/q/users/auth/facebook"
|
2011-02-25 12:20:12 -05:00
|
|
|
end
|
|
|
|
|
2011-03-12 11:58:19 -05:00
|
|
|
test "handles callback error parameter according to the specification" do
|
|
|
|
OmniAuth.config.mock_auth[:facebook] = :access_denied
|
|
|
|
visit "/users/auth/facebook/callback?error=access_denied"
|
|
|
|
assert_current_url "/users/sign_in"
|
2012-03-02 23:25:17 -05:00
|
|
|
assert_contain 'Could not authenticate you from Facebook because "Access denied".'
|
2011-03-12 11:58:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "handles other exceptions from omniauth" do
|
|
|
|
OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
|
|
|
|
|
|
|
|
visit "/users/sign_in"
|
2011-10-14 16:33:56 -04:00
|
|
|
click_link "Sign in with Facebook"
|
2011-03-12 11:58:19 -05:00
|
|
|
|
|
|
|
assert_current_url "/users/sign_in"
|
2012-03-02 23:25:17 -05:00
|
|
|
assert_contain 'Could not authenticate you from Facebook because "Invalid credentials".'
|
2011-03-12 11:58:19 -05:00
|
|
|
end
|
2011-02-24 14:50:22 -05:00
|
|
|
end
|