-
-
Save gencer/f85d837e80ef6b381e391eab1aa3065d to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in spec/requests/spec_helper.rb | |
RSpec.configure do |config| | |
# ... | |
# include our macro | |
config.include(OmniauthMacros) | |
end | |
OmniAuth.config.test_mode = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment