Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Integration test with Omniauth. This example is using twitter, and assume you've installed rspec and capybara. Official document is here: https://github.com/intridea/omniauth/wiki/Integration-Testing
# in spec/support/omniauth_macros.rb
module OmniauthMacros
def mock_auth_hash
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser',
'image' => 'mock_user_thumbnail_url'
},
'credentials' => {
'token' => 'mock_token',
'secret' => 'mock_secret'
}
}
end
end
# in spec/requests/spec_helper.rb
RSpec.configure do |config|
# ...
# include our macro
config.include(OmniauthMacros)
end
OmniAuth.config.test_mode = true
# in spec/requests/top_spec.rb
describe "access top page" do
it "can sign in user with Twitter account" do
visit '/'
page.should have_content("Sign in with Twitter")
mock_auth_hash
click_link "Sign in"
page.should have_content("mockuser") # user name
page.should have_css('img', :src => 'mock_user_thumbnail_url') # user image
page.should have_content("Sign out")
end
it "can handle authentication error" do
OmniAuth.config.mock_auth[:twitter] = :invalid_credentials
visit '/'
page.should have_content("Sign in with Twitter")
click_link "Sign in"
page.should have_content('Authentication failed.')
end
end
@abitdodgy

This comment has been minimized.

Copy link

abitdodgy commented Feb 25, 2013

Is there anyway to make it so that the user can be made an admin conditionally? I try this, but it does not work for me:

  before do
    login_with_omniauth # maps to visit("auth/#{provider}")
    user = User.last
    user.admin = true
    user.save!
  end

But in some instances I need the user to be an admin, and I'm not sure how to mock an admin user.

@subhashb

This comment has been minimized.

Copy link

subhashb commented Jun 22, 2013

Thank you for the code! :)

@dyatlov

This comment has been minimized.

Copy link

dyatlov commented Mar 16, 2015

There should be OmniAuth::AuthHash.new in omniauth_macros.rb which will contain that hash. This way behavior would be simulated in full. Having raw hash structure there makes it impossible to use things like auth_hash.info.email (pseudo-objects) which can be used throughout application.

# in spec/support/omniauth_macros.rb
module OmniauthMacros
  def mock_auth_hash
    # The mock_auth configuration allows you to set per-provider (or default)
    # authentication hashes to return during integration testing.
    OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
      'provider' => 'twitter',
      'uid' => '123545',
      'user_info' => {
        'name' => 'mockuser',
        'image' => 'mock_user_thumbnail_url'
      },
      'credentials' => {
        'token' => 'mock_token',
        'secret' => 'mock_secret'
      }
    })
  end
end
@indiesquidge

This comment has been minimized.

Copy link

indiesquidge commented Mar 29, 2015

@abitdodgy, try this:

  before do
    login_with_omniauth # maps to visit("auth/#{provider}")
    user = User.find_by_uid_and_provider('123545', '#{provider}')
    user.admin = true
    user.save!
  end

Worked for me. Let me know if you have issues.

@adamjgrant

This comment has been minimized.

Copy link

adamjgrant commented Apr 2, 2015

Is spec/requests still the best place to put spec_helper.rb? It doesn't seem to get picked up.

mock_auth_hash is undefined.

@biglovisa

This comment has been minimized.

Copy link

biglovisa commented Jul 31, 2015

@indiesquidge awesome! thank you.

@vinhnglx

This comment has been minimized.

Copy link

vinhnglx commented Oct 2, 2015

thanks for share :)

@iloveitaly

This comment has been minimized.

Copy link

iloveitaly commented Apr 9, 2016

For google_oauth2 you need to use a info key instead of user_info. Here's the gems I'm using:

omniauth (1.3.1)
omniauth-google-oauth2 (0.3.1)
@jedrekdomanski

This comment has been minimized.

Copy link

jedrekdomanski commented Nov 12, 2017

@dyatlov @kinopyo After deeper investigation I figured out that this code should actualy go to config/environments/test.rb

#in config/environments/test.rb
Rails.application.configure do
  ...
  OmniAuth.config.test_mode = true
  OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new({
      'provider' => 'github',
      'uid' => '123545',
      'info' => {
        'name' => 'mockuser',
        'image' => 'mockuser_avatar_url'
      }
    })
  ...
end
@sanjaygir

This comment has been minimized.

Copy link

sanjaygir commented Sep 5, 2018

I am trying with google and invalid credentials doesnt work. It successfully authenticates from google account. It works on facebook but not google. For google should i do something different? I appreciate any help! Thanks!


 it "can handle authentication error" do
    OmniAuth.config.mock_auth[:google] = :invalid_credentials
    visit '/'
    page.should have_content("Sign in with Twitter")
    click_link "Sign in"
    page.should have_content('Authentication failed.') #this fails
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.