Skip to content

Instantly share code, notes, and snippets.

@nicalpi
Created September 7, 2011 11:48
Show Gist options
  • Save nicalpi/1200365 to your computer and use it in GitHub Desktop.
Save nicalpi/1200365 to your computer and use it in GitHub Desktop.
Testing Omniauth with Devise, Rspec and Capybara
background do
set_omniauth()
click_link_or_button 'Sign up with Facebook'
end
# You can read about this gist at: http://wealsodocookies.com/posts/how-to-test-facebook-login-using-devise-omniauth-rspec-and-capybara
def set_omniauth(opts = {})
default = {:provider => :facebook,
:uuid => "1234",
:facebook => {
:email => "foobar@example.com",
:gender => "Male",
:first_name => "foo",
:last_name => "bar"
}
}
credentials = default.merge(opts)
provider = credentials[:provider]
user_hash = credentials[provider]
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[provider] = {
'uid' => credentials[:uuid],
"extra" => {
"user_hash" => {
"email" => user_hash[:email],
"first_name" => user_hash[:first_name],
"last_name" => user_hash[:last_name],
"gender" => user_hash[:gender]
}
}
}
end
def set_invalid_omniauth(opts = {})
credentials = { :provider => :facebook,
:invalid => :invalid_crendentials
}.merge(opts)
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[credentials[:provider]] = credentials[:invalid]
end
@cew821
Copy link

cew821 commented Mar 4, 2014

The link referenced above no longer works, but you can find the post at the new URL: http://cookieshq.co.uk/posts/how-to-test-facebook-login-using-devise-omniauth-rspec-and-capybara/

Thanks for posting it!

@alagu
Copy link

alagu commented May 5, 2014

Thanks, helpful.

@kalashnikovisme
Copy link

have the problem with testing devise

class ArticlesControllerTest < ActionController::TestCase
  setup do
    default = {:provider => :facebook,
               :uuid     => "1234",
               :facebook => {
                 :email => "foobar@example.com",
                 :gender => "Male",
                 :first_name => "foo",
                 :last_name => "bar"
               }
    }
    opts = {}
    credentials = default.merge(opts)
    provider = credentials[:provider]
    user_hash = credentials[provider]

    OmniAuth.config.test_mode = true

    OmniAuth.config.mock_auth[provider] = {
      'uid' => credentials[:uuid],
      "extra" => {
        "user_hash" => {
          "email" => user_hash[:email],
          "first_name" => user_hash[:first_name],
          "last_name" => user_hash[:last_name],
          "gender" => user_hash[:gender]
        }
      }
    }
  end
  test "should get index" do
    get :index
    assert_response :success
  end
end

but I have an error ActionView::Template::Error: No route matches {:controller=>"users/omniauth_callbacks", :action=>"passthru", :provider=>:facebook}

What should I do?

@DazDotOne
Copy link

Thanks for this, I was having an issue with invalid credentials when running a test after calling OmniAuth.config.mock_auth[:facebook] = :invalid_credentials on a previous test.

This approach lets me control the credentials that are being supplied with greater accuracy.

@benkoshy
Copy link

benkoshy commented Sep 6, 2018

  OmniAuth.config.mock_auth[provider] = {
    'uid' => credentials[:uuid],
    "extra" => {
    "user_hash" => {
      "email" => user_hash[:email],
     etc. etc. etc.

After supplying the above, it does not seem to be saving all the pertinent details in the request.env["omniauth.auth"] hash. Am having problems with the integration test. advice much appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment