Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@cew821

cew821 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!

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

This comment has been minimized.

Show comment
Hide comment
@alagu

alagu May 5, 2014

Thanks, helpful.

alagu commented May 5, 2014

Thanks, helpful.

@kalashnikovisme

This comment has been minimized.

Show comment
Hide comment
@kalashnikovisme

kalashnikovisme May 8, 2014

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?

kalashnikovisme commented May 8, 2014

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

This comment has been minimized.

Show comment
Hide comment
@DazDotOne

DazDotOne Jan 8, 2015

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.

DazDotOne commented Jan 8, 2015

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.

@BKSpurgeon

This comment has been minimized.

Show comment
Hide comment
@BKSpurgeon

BKSpurgeon 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

BKSpurgeon 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