Merge pull request #311 from differentreality/openid

Only show openid options if providers are set in User model.
This commit is contained in:
Artem Chernikov 2014-07-22 23:30:12 +02:00
commit 9a3451af4f
6 changed files with 32 additions and 25 deletions

View file

@ -2,7 +2,7 @@ module Users
class OmniauthCallbacksController < Devise::OmniauthCallbacksController class OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_filter :verify_authenticity_token skip_before_filter :verify_authenticity_token
[:novell, :google, :facebook, :twitter].each do |provider| User.omniauth_providers.each do |provider|
define_method(provider) { handle(provider) } define_method(provider) { handle(provider) }
end end

View file

@ -15,15 +15,15 @@
%br %br
= f.inputs name: 'OpenID' do = f.inputs name: 'OpenID' do
%h4 %h4
Currently using the following openIDs: Currently the following openIDs are associated with your account
- @openids.each do |openid| - @openids.each do |openid|
= openid.provider %li= "#{openid.provider}:#{openid.email}"
= openid.email
%br %br
%h4 - if User.omniauth_providers.present?
To add an openID to your account using a different email address, sign in with your %h4
openID while logged in to OSEM To add an openID with a different email address to your account, sign in with your
= render 'devise/shared/openid' openID while logged in to OSEM
= render 'devise/shared/openid'
= f.inputs name: 'Account' do = f.inputs name: 'Account' do
= f.input :email, required: false, input_html: {autocomplete: "off"} = f.input :email, required: false, input_html: {autocomplete: "off"}

View file

@ -13,8 +13,9 @@
%br %br
%h4 Already have an account? %h4 Already have an account?
= render 'devise/shared/links' = render 'devise/shared/links'
.col-md-4 - if resource_class.omniauth_providers.present?
.well .col-md-4
%h4 Or use your openID .well
%h4 Or use your openID
= render 'devise/shared/openid' = render 'devise/shared/openid'

View file

@ -15,7 +15,8 @@
%br %br
= render "devise/shared/links" = render "devise/shared/links"
.col-md-4 - if resource_class.omniauth_providers.present?
.well .col-md-4
%h4 Or use your openID .well
= render 'devise/shared/openid' %h4 Or use your openID
= render 'devise/shared/openid'

View file

@ -8,7 +8,6 @@ Devise.setup do |config|
# ==> openIDs configuration # ==> openIDs configuration
# Define the available openID providers that can be used to log in # Define the available openID providers that can be used to log in
# Pass each provider to User model in :omniauth_providers (for open_id providers use their name) # Pass each provider to User model in :omniauth_providers (for open_id providers use their name)
# Include provider in users/omniauth_callbacks_controller.rb
config.omniauth :open_id, name: 'novell', identifier: 'http://www.opensuse.org/openid/user' config.omniauth :open_id, name: 'novell', identifier: 'http://www.opensuse.org/openid/user'
config.omniauth :google_oauth2, Rails.application.secrets.google_key, Rails.application.secrets.google_secret, { config.omniauth :google_oauth2, Rails.application.secrets.google_key, Rails.application.secrets.google_secret, {

View file

@ -4,15 +4,15 @@ feature Openid do
let!(:participant_role) { create(:participant_role) } let!(:participant_role) { create(:participant_role) }
let!(:admin_role) { create(:admin_role) } let!(:admin_role) { create(:admin_role) }
describe 'sign in with openid' do shared_examples 'sign in with openid' do
it 'has option to log in with Google account' do scenario 'has option to log in with Google account' do
visit '/accounts/sign_in' visit '/accounts/sign_in'
expect(page.has_content?('Or use your openID')).to be true expect(page.has_content?('Or use your openID')).to be true
expect(page.has_content?('google')).to be true expect(page.has_content?('google')).to be true
end end
it 'signs in *new* user with Google account' do scenario 'signs in *new* user with Google account' do
expected_count_openid = Openid.count + 1 expected_count_openid = Openid.count + 1
expected_count_user = User.count + 1 expected_count_user = User.count + 1
visit '/accounts/sign_in' visit '/accounts/sign_in'
@ -24,7 +24,7 @@ feature Openid do
expect(User.count).to eq(expected_count_user) expect(User.count).to eq(expected_count_user)
end end
it 'signs in an existing user' do scenario 'signs in an existing user' do
create(:participant, email: 'test-participant-1@google.com') create(:participant, email: 'test-participant-1@google.com')
expected_count_openid = Openid.count + 1 expected_count_openid = Openid.count + 1
expected_count_user = User.count expected_count_user = User.count
@ -37,7 +37,7 @@ feature Openid do
expect(User.count).to eq(expected_count_user) expect(User.count).to eq(expected_count_user)
end end
it 'can handle authentication error' do scenario 'can handle authentication error' do
OmniAuth.config.mock_auth[:google] = :invalid_credentials OmniAuth.config.mock_auth[:google] = :invalid_credentials
visit '/accounts/sign_in' visit '/accounts/sign_in'
expect(page.has_content?('Or use your openID')).to be true expect(page.has_content?('Or use your openID')).to be true
@ -45,7 +45,7 @@ feature Openid do
expect(flash).to eq("Could not authenticate you from Google because \"Invalid credentials\".") expect(flash).to eq("Could not authenticate you from Google because \"Invalid credentials\".")
end end
it 'adds openid to existing user' do scenario 'adds openid to existing user' do
# Sign in user # Sign in user
user = create(:participant, email: 'test-participant-1@google.com') user = create(:participant, email: 'test-participant-1@google.com')
sign_in user sign_in user
@ -63,7 +63,7 @@ feature Openid do
expect(Openid.where(email: 'test-1@gmail.com').first.nil?).to eq(false) expect(Openid.where(email: 'test-1@gmail.com').first.nil?).to eq(false)
end end
it 'signs in with openID using the same email as another associated openid' do scenario 'signs in with openID using the same email as another associated openid' do
# Sign in user # Sign in user
create(:participant, email: 'test-participant-1@google.com') create(:participant, email: 'test-participant-1@google.com')
expected_count_openid = Openid.count + 1 expected_count_openid = Openid.count + 1
@ -105,4 +105,10 @@ feature Openid do
expect(last_openid.email).to eq('test-1@gmail.com') expect(last_openid.email).to eq('test-1@gmail.com')
end end
end end
describe 'omniauth' do
if User.omniauth_providers.present?
it_behaves_like 'sign in with openid'
end
end
end end