Merge pull request #253 from differentreality/final_openid
Authenticate via openid
This commit is contained in:
commit
0c698bac10
24 changed files with 412 additions and 15 deletions
108
spec/features/omniauth_spec.rb
Normal file
108
spec/features/omniauth_spec.rb
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
require 'spec_helper'
|
||||
|
||||
feature Openid do
|
||||
let!(:participant_role) { create(:participant_role) }
|
||||
let!(:admin_role) { create(:admin_role) }
|
||||
|
||||
describe 'sign in with openid' do
|
||||
|
||||
it 'has option to log in with Google account' do
|
||||
visit '/accounts/sign_in'
|
||||
expect(page.has_content?('Or use your openID')).to be true
|
||||
expect(page.has_content?('google')).to be true
|
||||
end
|
||||
|
||||
it 'signs in *new* user with Google account' do
|
||||
expected_count_openid = Openid.count + 1
|
||||
expected_count_user = User.count + 1
|
||||
visit '/accounts/sign_in'
|
||||
|
||||
mock_auth_new_user
|
||||
click_link 'google'
|
||||
expect(flash).to eq('test-1@gmail.com signed in successfully with google')
|
||||
expect(Openid.count).to eq(expected_count_openid)
|
||||
expect(User.count).to eq(expected_count_user)
|
||||
end
|
||||
|
||||
it 'signs in an existing user' do
|
||||
create(:participant, email: 'test-participant-1@google.com')
|
||||
expected_count_openid = Openid.count + 1
|
||||
expected_count_user = User.count
|
||||
visit '/accounts/sign_in'
|
||||
|
||||
mock_auth_existing_user_participant
|
||||
click_link 'google'
|
||||
expect(flash).to eq('test-participant-1@google.com signed in successfully with google')
|
||||
expect(Openid.count).to eq(expected_count_openid)
|
||||
expect(User.count).to eq(expected_count_user)
|
||||
end
|
||||
|
||||
it 'can handle authentication error' do
|
||||
OmniAuth.config.mock_auth[:google] = :invalid_credentials
|
||||
visit '/accounts/sign_in'
|
||||
expect(page.has_content?('Or use your openID')).to be true
|
||||
click_link 'google'
|
||||
expect(flash).to eq("Could not authenticate you from Google because \"Invalid credentials\".")
|
||||
end
|
||||
|
||||
it 'adds openid to existing user' do
|
||||
# Sign in user
|
||||
user = create(:participant, email: 'test-participant-1@google.com')
|
||||
sign_in user
|
||||
|
||||
# Add openID to current user
|
||||
expected_count_openid = Openid.count + 1
|
||||
expected_count_user = User.count
|
||||
visit '/accounts/edit'
|
||||
|
||||
mock_auth_new_user
|
||||
click_link 'google'
|
||||
expect(flash).to eq('test-participant-1@google.com signed in successfully with google')
|
||||
expect(Openid.count).to eq(expected_count_openid)
|
||||
expect(User.count).to eq(expected_count_user)
|
||||
expect(Openid.where(email: 'test-1@gmail.com').first.nil?).to eq(false)
|
||||
end
|
||||
|
||||
it 'signs in with openID using the same email as another associated openid' do
|
||||
# Sign in user
|
||||
create(:participant, email: 'test-participant-1@google.com')
|
||||
expected_count_openid = Openid.count + 1
|
||||
expected_count_user = User.count
|
||||
visit '/accounts/sign_in'
|
||||
|
||||
mock_auth_existing_user_participant
|
||||
click_link 'google'
|
||||
expect(flash).to eq('test-participant-1@google.com signed in successfully with google')
|
||||
expect(Openid.count).to eq(expected_count_openid)
|
||||
expect(User.count).to eq(expected_count_user)
|
||||
|
||||
# Add openID to current user with email test-1@gmail.com
|
||||
expected_count_openid = Openid.count + 1
|
||||
expected_count_user = User.count
|
||||
visit '/accounts/edit'
|
||||
|
||||
mock_auth_new_user
|
||||
click_link 'google'
|
||||
expect(flash).to eq('test-participant-1@google.com signed in successfully with google')
|
||||
expect(Openid.count).to eq(expected_count_openid)
|
||||
expect(User.count).to eq(expected_count_user)
|
||||
expect(Openid.where(email: 'test-participant-1@google.com').first.nil?).to eq(false)
|
||||
expect(Openid.where(email: 'test-1@gmail.com').first.nil?).to eq(false)
|
||||
|
||||
# Sign in with different openID using same email (test-1@gmail.com)
|
||||
sign_out
|
||||
expected_count_openid = Openid.count + 1
|
||||
expected_count_user = User.count
|
||||
|
||||
visit '/accounts/sign_in'
|
||||
mock_auth_new_user_fb
|
||||
click_link 'facebook'
|
||||
expect(flash).to eq('test-participant-1@google.com signed in successfully with facebook')
|
||||
expect(Openid.count).to eq(expected_count_openid)
|
||||
expect(User.count).to eq(expected_count_user)
|
||||
last_openid = Openid.last
|
||||
expect(last_openid.uid).to eq('facebook-test-uid-1')
|
||||
expect(last_openid.email).to eq('test-1@gmail.com')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -53,6 +53,9 @@ RSpec.configure do |config|
|
|||
# Includes support/login_macros for feature tests
|
||||
config.include LoginMacros, type: :feature
|
||||
|
||||
# Includes omniauth macro
|
||||
config.include(OmniauthMacros)
|
||||
|
||||
# Includes support/flash for feature tests
|
||||
config.include Flash, type: :feature
|
||||
|
||||
|
|
@ -63,3 +66,5 @@ RSpec.configure do |config|
|
|||
c.syntax = :expect
|
||||
end
|
||||
end
|
||||
|
||||
OmniAuth.config.test_mode = true
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ module LoginMacros
|
|||
|
||||
fill_in 'user_email', with: user.email
|
||||
fill_in 'user_password', with: user.password
|
||||
find(:xpath, "//div[@id='content']//input[@name='commit']").click
|
||||
find(:xpath, "//div[@id='content']//button[@type='submit']").click
|
||||
|
||||
expect(page.has_content?('Signed in successfully')).to be true
|
||||
end
|
||||
|
|
|
|||
68
spec/support/omniauth_macros.rb
Normal file
68
spec/support/omniauth_macros.rb
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
module OmniauthMacros
|
||||
# The mock_auth configuration allows you to set per-provider (or default)
|
||||
# authentication hashes to return during integration testing.
|
||||
|
||||
def mock_auth_new_user
|
||||
OmniAuth.config.mock_auth[:google] =
|
||||
OmniAuth::AuthHash.new(
|
||||
provider: 'google',
|
||||
uid: 'google-test-uid-1',
|
||||
info: {
|
||||
email: 'test-1@gmail.com'
|
||||
},
|
||||
credentials: {
|
||||
token: 'mock_token',
|
||||
secret: 'mock_secret'
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def mock_auth_new_user_fb
|
||||
OmniAuth.config.mock_auth[:facebook] =
|
||||
OmniAuth::AuthHash.new(
|
||||
provider: 'google',
|
||||
uid: 'facebook-test-uid-1',
|
||||
info: {
|
||||
email: 'test-1@gmail.com'
|
||||
},
|
||||
credentials: {
|
||||
token: 'mock_token',
|
||||
secret: 'mock_secret'
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def mock_auth_existing_user_participant
|
||||
# The mock_auth configuration allows you to set per-provider (or default)
|
||||
# authentication hashes to return during integration testing.
|
||||
OmniAuth.config.mock_auth[:google] =
|
||||
OmniAuth::AuthHash.new(
|
||||
provider: 'google',
|
||||
uid: 'google-test-uid-participant-1',
|
||||
info: {
|
||||
email: 'test-participant-1@google.com'
|
||||
},
|
||||
credentials: {
|
||||
token: 'mock_token',
|
||||
secret: 'mock_secret'
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def mock_auth_existing_user_admin
|
||||
# The mock_auth configuration allows you to set per-provider (or default)
|
||||
# authentication hashes to return during integration testing.
|
||||
OmniAuth.config.mock_auth[:google] =
|
||||
OmniAuth::AuthHash.new(
|
||||
provider: 'google',
|
||||
uid: 'google-test-uid-admin-1',
|
||||
info: {
|
||||
email: 'test-admin-1@google.com'
|
||||
},
|
||||
credentials: {
|
||||
token: 'mock_token',
|
||||
secret: 'mock_secret'
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue